Skip to content

Commit

Permalink
Merge pull request #4945 from alexzatsepin/fix-traffic-toast-shows
Browse files Browse the repository at this point in the history
[android] Adjusted behaviour of traffic toasts and dialogs
  • Loading branch information
dobriy-eeh authored Dec 9, 2016
2 parents 70c756b + f8cc9ca commit d21ab27
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
8 changes: 7 additions & 1 deletion android/src/com/mapswithme/maps/MwmActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public class MwmActivity extends BaseMwmFragmentActivity
private TrafficButton mTraffic;
@Nullable
private NavigationButtonsAnimationController mNavAnimationController;
@Nullable
private TrafficButtonController mTrafficButtonController;

private View mPositionChooser;

Expand Down Expand Up @@ -538,6 +540,7 @@ private void initNavigationButtons()
mNavMyPosition = new MyPositionButton(myPosition);
ImageButton traffic = (ImageButton) frame.findViewById(R.id.traffic);
mTraffic = new TrafficButton(this, traffic);
mTrafficButtonController = new TrafficButtonController(mTraffic, this);
mNavAnimationController = new NavigationButtonsAnimationController(zoomIn, zoomOut, myPosition);
}

Expand Down Expand Up @@ -929,7 +932,8 @@ protected void onStart()

if (MapFragment.nativeIsEngineCreated())
LocationHelper.INSTANCE.attach(this);
TrafficManager.INSTANCE.attach(new TrafficButtonController(mTraffic, this));
if (mTrafficButtonController != null)
TrafficManager.INSTANCE.attach(mTrafficButtonController);
if (mNavigationController != null)
TrafficManager.INSTANCE.attach(mNavigationController);
}
Expand All @@ -941,6 +945,8 @@ protected void onStop()
LocationHelper.INSTANCE.detach(!isFinishing());
RoutingController.get().detach();
TrafficManager.INSTANCE.detachAll();
if (mTrafficButtonController != null)
mTrafficButtonController.destroy();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public void onOutdated()
}

@Override
public void onNoData()
public void onNoData(boolean notify)
{
// no op
}
Expand All @@ -361,13 +361,13 @@ public void onNetworkError()
}

@Override
public void onExpiredData()
public void onExpiredData(boolean notify)
{
// no op
}

@Override
public void onExpiredApp()
public void onExpiredApp(boolean notify)
{
// no op
}
Expand Down
16 changes: 9 additions & 7 deletions android/src/com/mapswithme/maps/traffic/TrafficManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.mapswithme.util.Utils;
Expand All @@ -23,6 +22,8 @@ public enum TrafficManager
private final TrafficState.StateChangeListener mStateChangeListener = new TrafficStateListener();
@TrafficState.Value
private int mState = TrafficState.DISABLED;
@TrafficState.Value
private int mLastPostedState = mState;
@NonNull
private final List<TrafficCallback> mCallbacks = new ArrayList<>();
private boolean mInitialized = false;
Expand Down Expand Up @@ -137,7 +138,7 @@ public void invoke(@NonNull TrafficCallback callback)
break;

case TrafficState.NO_DATA:
callback.onNoData();
callback.onNoData(mLastPostedState != mState);
break;

case TrafficState.OUTDATED:
Expand All @@ -149,16 +150,17 @@ public void invoke(@NonNull TrafficCallback callback)
break;

case TrafficState.EXPIRED_DATA:
callback.onExpiredData();
callback.onExpiredData(mLastPostedState != mState);
break;

case TrafficState.EXPIRED_APP:
callback.onExpiredApp();
callback.onExpiredApp(mLastPostedState != mState);
break;

default:
throw new IllegalArgumentException("Unsupported traffic state: " + mState);
}
mLastPostedState = mState;
}
});
}
Expand All @@ -176,9 +178,9 @@ public interface TrafficCallback
void onDisabled();
void onWaitingData();
void onOutdated();
void onNoData();
void onNetworkError();
void onExpiredData();
void onExpiredApp();
void onNoData(boolean notify);
void onExpiredData(boolean notify);
void onExpiredApp(boolean notify);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

Expand All @@ -16,7 +18,8 @@ public class TrafficButtonController implements TrafficManager.TrafficCallback
private final TrafficButton mButton;
@NonNull
private final Activity mActivity;
private boolean mErrorDlgShown;
@Nullable
private Dialog mDialog;

public TrafficButtonController(@NonNull TrafficButton button, @NonNull Activity activity)
{
Expand Down Expand Up @@ -50,16 +53,17 @@ public void onOutdated()
}

@Override
public void onNoData()
public void onNoData(boolean notify)
{
mButton.turnOn();
Toast.makeText(mActivity, R.string.traffic_data_unavailable, Toast.LENGTH_SHORT).show();
if (notify)
Toast.makeText(mActivity, R.string.traffic_data_unavailable, Toast.LENGTH_SHORT).show();
}

@Override
public void onNetworkError()
{
if (mErrorDlgShown)
if (mDialog != null && mDialog.isShowing())
return;

AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
Expand All @@ -70,7 +74,6 @@ public void onNetworkError()
public void onClick(DialogInterface dialog, int which)
{
TrafficManager.INSTANCE.disable();
mErrorDlgShown = false;
}
})
.setCancelable(true)
Expand All @@ -80,26 +83,31 @@ public void onClick(DialogInterface dialog, int which)
public void onCancel(DialogInterface dialog)
{
TrafficManager.INSTANCE.disable();
mErrorDlgShown = false;

}
});
builder.show();
mErrorDlgShown = true;
mDialog = builder.show();
}

public void destroy()
{
if (mDialog != null && mDialog.isShowing())
mDialog.cancel();
}

@Override
public void onExpiredData()
public void onExpiredData(boolean notify)
{
mButton.turnOn();
Toast.makeText(mActivity, R.string.traffic_update_maps_text, Toast.LENGTH_SHORT).show();
if (notify)
Toast.makeText(mActivity, R.string.traffic_update_maps_text, Toast.LENGTH_SHORT).show();
}

@Override
public void onExpiredApp()
public void onExpiredApp(boolean notify)
{
mButton.turnOn();
Toast.makeText(mActivity, R.string.traffic_update_app, Toast.LENGTH_SHORT).show();
if (notify)
Toast.makeText(mActivity, R.string.traffic_update_app, Toast.LENGTH_SHORT).show();
}

private class OnTrafficClickListener implements View.OnClickListener
Expand Down

0 comments on commit d21ab27

Please sign in to comment.