diff --git a/common/src/main/java/ch/liip/timeforcoffee/common/FontFitTextView.java b/common/src/main/java/ch/liip/timeforcoffee/common/FontFitTextView.java deleted file mode 100644 index 0af8e0e..0000000 --- a/common/src/main/java/ch/liip/timeforcoffee/common/FontFitTextView.java +++ /dev/null @@ -1,88 +0,0 @@ -package ch.liip.timeforcoffee.common; - -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Paint; -import android.graphics.Rect; -import android.support.v7.widget.AppCompatTextView; -import android.util.AttributeSet; -import android.util.TypedValue; -import android.view.ViewGroup; -import android.widget.RelativeLayout; - -public class FontFitTextView extends AppCompatTextView { - - private int mMaxFontSize; - private Paint mTestPaint; - - public FontFitTextView(Context context) { - super(context); - initialise(); - } - - public FontFitTextView(Context context, AttributeSet attrs) { - super(context, attrs); - TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FontFitTextView, 0, 0); - try { - mMaxFontSize = a.getInteger(R.styleable.FontFitTextView_maxFontSize, 40); - } - finally { - a.recycle(); - } - - initialise(); - } - - private void initialise() { - mTestPaint = new Paint(); - mTestPaint.set(this.getPaint()); - } - - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - int parentWidth = MeasureSpec.getSize(widthMeasureSpec); - int parentHeight = MeasureSpec.getSize(heightMeasureSpec); - - refitText(this.getText().toString(), parentHeight, parentWidth); - } - - @Override - protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) { - refitText(text.toString(), this.getHeight(), this.getWidth()); - } - - - /* Re size the font so the specified text fits in the text box - * assuming the text box is the specified width. - */ - private void refitText(String text, int textHeight, int textWidth) { - if (textWidth <= 0) return; - - ViewGroup.LayoutParams vlp = this.getLayoutParams(); - int marginTop = ((RelativeLayout.LayoutParams) vlp).topMargin; - int marginBottom = ((RelativeLayout.LayoutParams) vlp).bottomMargin; - int marginLeft = ((RelativeLayout.LayoutParams) vlp).leftMargin; - int marginRight = ((RelativeLayout.LayoutParams) vlp).rightMargin; - int targetHeight = textHeight - marginTop - marginBottom; - int targetWidth = textWidth - marginLeft - marginRight; - - float testSize = 5; - Rect testTextSize = new Rect(); - mTestPaint.set(this.getPaint()); - - while (testSize <= mMaxFontSize) { - testSize += 1; - - mTestPaint.setTextSize(testSize); - mTestPaint.getTextBounds(text, 0, text.length(), testTextSize); - - if (testTextSize.width() >= targetWidth || testTextSize.height() >= targetHeight) { - break; // Big enough - } - } - - this.setTextSize(TypedValue.COMPLEX_UNIT_PX, testSize); - } -} - diff --git a/common/src/main/res/values/attrs.xml b/common/src/main/res/values/attrs.xml deleted file mode 100644 index fefa63b..0000000 --- a/common/src/main/res/values/attrs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/mobile/src/main/java/ch/liip/timeforcoffee/adapter/ConnectionListAdapter.java b/mobile/src/main/java/ch/liip/timeforcoffee/adapter/ConnectionListAdapter.java index 8767ba6..92257d8 100644 --- a/mobile/src/main/java/ch/liip/timeforcoffee/adapter/ConnectionListAdapter.java +++ b/mobile/src/main/java/ch/liip/timeforcoffee/adapter/ConnectionListAdapter.java @@ -56,10 +56,16 @@ public View getView(int position, View convertView, ViewGroup parent) { Connection connection = this.mConnexions.get(position); viewHolder.stationTextView.setText(connection.getStationName()); - viewHolder.timeLabelTextView.setText(mContext.getResources().getString(R.string.connection_departure)); viewHolder.timeTextView.setText(connection.getScheduledDepartureStr()); viewHolder.departureTextView.setText(connection.getDepartureInMinutes()); + if(position != mConnexions.size() - 1) { + viewHolder.timeLabelTextView.setText(mContext.getResources().getString(R.string.connection_departure)); + } + else { + viewHolder.timeLabelTextView.setText(mContext.getResources().getString(R.string.connection_arrival)); + } + if (connection.isLate()) { viewHolder.realtimeDepartureTextView.setVisibility(View.VISIBLE); viewHolder.realtimeDepartureTextView.setText(connection.getRealtimeDepartureStr()); diff --git a/mobile/src/main/java/ch/liip/timeforcoffee/adapter/DepartureListAdapter.java b/mobile/src/main/java/ch/liip/timeforcoffee/adapter/DepartureListAdapter.java index 81fb87c..a6c478b 100644 --- a/mobile/src/main/java/ch/liip/timeforcoffee/adapter/DepartureListAdapter.java +++ b/mobile/src/main/java/ch/liip/timeforcoffee/adapter/DepartureListAdapter.java @@ -16,7 +16,6 @@ import ch.liip.timeforcoffee.R; import ch.liip.timeforcoffee.api.models.Departure; -import ch.liip.timeforcoffee.common.FontFitTextView; import ch.liip.timeforcoffee.common.Typefaces; public class DepartureListAdapter extends ArrayAdapter { @@ -139,7 +138,7 @@ public void setDepartures(List departures) { } private static class DepartureViewHolder { - FontFitTextView lineNameTextView; + TextView lineNameTextView; TextView destinationTextView; TextView departureTextView; TextView scheduledTimeTextView; diff --git a/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java b/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java index 815fb28..56f90b4 100644 --- a/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java +++ b/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java @@ -82,7 +82,7 @@ public void onPauseView() { @Override public void onRefreshView() { - loadStationsWithLastPosition(); + loadStationsWithLastPositionForce(); } public void onDestroy() { @@ -90,57 +90,12 @@ public void onDestroy() { mEventBus.unregister(this); } - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { - if (requestCode == PERMISSION_REQUEST_CODE) { - for (int i = 0; i < permissions.length; i++) { - String permission = permissions[i]; - int grantResult = grantResults[i]; - - if (!(permission.equals(locationPermission))) { - continue; - } - - if (grantResult == PackageManager.PERMISSION_GRANTED) { - startLocation(); - } else { - mActivity.setIsPositionLoading(false); - SnackBars.showLocalisationSettings(mActivity); - } - } - } - } - - @Override - public void onLocationUpdated(Location location) { - Log.i(LOG_TAG, "onLocationUpdated : lat = " + location.getLatitude() + " , long = " + location.getLongitude()); - mLastLocation = location; - - loadStations(location); - } - - @Subscribe - public void onStationsFetched(StationsLocationFetchedEvent event) { - mActivity.setIsPositionLoading(false); - - mStations = event.getStations(); - mActivity.updateStations(mStations); - - updateFavorites(); - } - - @Subscribe - public void onFetchErrorEvent(FetchStationsLocationErrorEvent event) { - mActivity.setIsPositionLoading(false); - SnackBars.showNetworkError(mActivity, new View.OnClickListener() { - @Override - public void onClick(View view) { - loadStationsWithLastPosition(); - } - }); - } - public void updateStations() { if (Build.FINGERPRINT.contains("generic")) { //emulator + mLastLocation = new Location("emulator"); + mLastLocation.setLatitude(46.8017); + mLastLocation.setLongitude(7.1456); + loadStationsWithLastPosition(); } else if (!mIsCapturingLocation) { @@ -182,14 +137,16 @@ private void updateFavoritesOnFavoriteList() { private void startLocation() { if (!permissionsChecker.LacksPermission(locationPermission)) { - mActivity.updateStations(new ArrayList()); - mActivity.setIsPositionLoading(true); + if(mStations == null || mStations.size() == 0) { + mActivity.setIsPositionLoading(true); + } if (!SmartLocation.with(mActivity).location().state().locationServicesEnabled()) { SnackBars.showLocalisationServiceOff(mActivity); mActivity.setIsPositionLoading(false); return; - } else if(SmartLocation.with(mActivity).location().state().isGpsAvailable() && !SmartLocation.with(mActivity).location().state().isNetworkAvailable()) { + } + else if(SmartLocation.with(mActivity).location().state().isGpsAvailable() && !SmartLocation.with(mActivity).location().state().isNetworkAvailable()) { SnackBars.showLocalisationServiceSetToDeviceOnly(mActivity); mActivity.setIsPositionLoading(false); return; @@ -201,7 +158,8 @@ private void startLocation() { .config(locationParams) .oneFix() .start(this); - } else { + } + else { permissionsChecker.RequestPermission(mActivity, locationPermission, PERMISSION_REQUEST_CODE, mActivity.getResources().getString(R.string.permission_message)); } } @@ -211,27 +169,84 @@ private void stopLocation() { SmartLocation.with(mActivity).location().stop(); } - private void loadStations(Location location) { - if (location != null) { - Log.i(LOG_TAG, "get stations for lat = " + location.getLatitude() + " and long = " + location.getLongitude()); - mEventBus.post(new FetchStationsLocationEvent(location.getLatitude(), location.getLongitude())); + private void loadStationsWithLastPosition() { + if(mStations == null || mStations.size() == 0) { + mActivity.setIsPositionLoading(true); + } + + if (mLastLocation != null) { + loadStations(mLastLocation); + } + else { + startLocation(); } } - private void loadStationsWithLastPosition() { + private void loadStationsWithLastPositionForce() { mActivity.updateStations(new ArrayList()); mActivity.setIsPositionLoading(true); - if (Build.FINGERPRINT.contains("generic")) { //emulator - mLastLocation = new Location("emulator"); - mLastLocation.setLatitude(46.8017); - mLastLocation.setLongitude(7.1456); - } - if (mLastLocation != null) { loadStations(mLastLocation); - } else { + } + else { startLocation(); } } + + private void loadStations(Location location) { + if (location != null) { + Log.i(LOG_TAG, "get stations for lat = " + location.getLatitude() + " and long = " + location.getLongitude()); + mEventBus.post(new FetchStationsLocationEvent(location.getLatitude(), location.getLongitude())); + } + } + + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + if (requestCode == PERMISSION_REQUEST_CODE) { + for (int i = 0; i < permissions.length; i++) { + String permission = permissions[i]; + int grantResult = grantResults[i]; + + if (!(permission.equals(locationPermission))) { + continue; + } + + if (grantResult == PackageManager.PERMISSION_GRANTED) { + startLocation(); + } else { + mActivity.setIsPositionLoading(false); + SnackBars.showLocalisationSettings(mActivity); + } + } + } + } + + @Override + public void onLocationUpdated(Location location) { + Log.i(LOG_TAG, "onLocationUpdated : lat = " + location.getLatitude() + " , long = " + location.getLongitude()); + mLastLocation = location; + + loadStations(location); + } + + @Subscribe + public void onStationsFetched(StationsLocationFetchedEvent event) { + mActivity.setIsPositionLoading(false); + + mStations = event.getStations(); + mActivity.updateStations(mStations); + + updateFavorites(); + } + + @Subscribe + public void onFetchErrorEvent(FetchStationsLocationErrorEvent event) { + mActivity.setIsPositionLoading(false); + SnackBars.showNetworkError(mActivity, new View.OnClickListener() { + @Override + public void onClick(View view) { + loadStationsWithLastPosition(); + } + }); + } } diff --git a/mobile/src/main/res/layout/fragment_departure_list_row.xml b/mobile/src/main/res/layout/fragment_departure_list_row.xml index af769f8..9b34110 100644 --- a/mobile/src/main/res/layout/fragment_departure_list_row.xml +++ b/mobile/src/main/res/layout/fragment_departure_list_row.xml @@ -1,20 +1,20 @@ - { private static final String[] linesWithSymbol = {"ICN", "EN", "ICN", "TGV", "RX", "EC", "IC", "SC", "CNL", "ICE", "IR"}; private static class DepartureViewHolder { - FontFitTextView lineNameTextView; + TextView lineNameTextView; TextView toTextView; TextView departureTextView; TextView scheduledTimeTextView; diff --git a/wear/src/main/java/ch/liip/timeforcoffee/presenter/WearPresenter.java b/wear/src/main/java/ch/liip/timeforcoffee/presenter/WearPresenter.java index 5348d41..1d091e8 100644 --- a/wear/src/main/java/ch/liip/timeforcoffee/presenter/WearPresenter.java +++ b/wear/src/main/java/ch/liip/timeforcoffee/presenter/WearPresenter.java @@ -1,7 +1,6 @@ package ch.liip.timeforcoffee.presenter; import android.Manifest; -import android.content.Context; import android.content.IntentSender; import android.content.pm.PackageManager; import android.location.Location; @@ -9,14 +8,7 @@ import android.support.v4.content.ContextCompat; import android.util.Log; import android.widget.Toast; -import ch.liip.timeforcoffee.R; -import ch.liip.timeforcoffee.activity.WearActivity; -import ch.liip.timeforcoffee.api.models.Departure; -import ch.liip.timeforcoffee.api.models.Station; -import ch.liip.timeforcoffee.common.SerialisationUtilsGSON; -import ch.liip.timeforcoffee.common.SerializableLocation; -import ch.liip.timeforcoffee.common.presenter.Presenter; -import ch.liip.timeforcoffee.helper.PermissionsChecker; + import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.common.api.GoogleApiClient; @@ -25,9 +17,26 @@ import com.google.android.gms.common.api.Status; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; -import com.google.android.gms.wearable.*; +import com.google.android.gms.wearable.MessageApi; +import com.google.android.gms.wearable.MessageEvent; +import com.google.android.gms.wearable.Node; +import com.google.android.gms.wearable.NodeApi; +import com.google.android.gms.wearable.Wearable; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; -import java.util.*; +import ch.liip.timeforcoffee.R; +import ch.liip.timeforcoffee.activity.WearActivity; +import ch.liip.timeforcoffee.api.models.Departure; +import ch.liip.timeforcoffee.api.models.Station; +import ch.liip.timeforcoffee.common.SerialisationUtilsGSON; +import ch.liip.timeforcoffee.common.SerializableLocation; +import ch.liip.timeforcoffee.common.presenter.Presenter; +import ch.liip.timeforcoffee.helper.PermissionsChecker; public class WearPresenter implements Presenter, MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, diff --git a/wear/src/main/res/layout/departure_list_row.xml b/wear/src/main/res/layout/departure_list_row.xml index c816937..3a3b5cc 100644 --- a/wear/src/main/res/layout/departure_list_row.xml +++ b/wear/src/main/res/layout/departure_list_row.xml @@ -1,20 +1,22 @@ - + app:autoSizeTextType="uniform" + app:autoSizeMinTextSize="10sp" + app:autoSizeMaxTextSize="20sp" + tools:text="3"/> +