[android] Fixed review notes

This commit is contained in:
Dmitry Donskoy 2018-07-05 18:43:38 +03:00 committed by Vlad Mihaylenko
parent 7a72e3c66c
commit 3535738819
9 changed files with 150 additions and 247 deletions

View file

@ -1,31 +1,23 @@
package com.mapswithme.maps.content;
import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapswithme.maps.MwmApplication;
import java.lang.ref.WeakReference;
public class AbstractContextualListener
{
@NonNull
private final WeakReference<MwmApplication> mApp;
private final Application mApp;
public AbstractContextualListener(@NonNull MwmApplication app)
public AbstractContextualListener(@NonNull Application app)
{
mApp = new WeakReference<>(app);
mApp = app;
}
@NonNull
private WeakReference<MwmApplication> getAppReference()
public Context getContext()
{
return mApp;
}
@Nullable
public MwmApplication getApp()
{
return getAppReference().get();
}
}

View file

@ -1,12 +1,13 @@
package com.mapswithme.maps.maplayer.subway;
import android.app.Application;
import android.content.Context;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.content.AbstractContextualListener;
public interface OnTransitSchemeChangedListener
interface OnTransitSchemeChangedListener
{
@SuppressWarnings("unused")
@MainThread
@ -14,20 +15,17 @@ public interface OnTransitSchemeChangedListener
class Default extends AbstractContextualListener implements OnTransitSchemeChangedListener
{
public Default(@NonNull MwmApplication app)
public Default(@NonNull Application context)
{
super(app);
super(context);
}
@Override
public void onTransitStateChanged(int index)
{
MwmApplication app = getApp();
if (app == null)
return;
TransitSchemeState state = TransitSchemeState.makeInstance(index);
state.onReceived(app);
Context app = getContext();
TransitSchemeState state = TransitSchemeState.values()[index];
state.activate(app);
}
}
}

View file

@ -1,5 +1,6 @@
package com.mapswithme.maps.maplayer.subway;
import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
@ -11,7 +12,7 @@ public class SubwayManager
@NonNull
private final OnTransitSchemeChangedListener mSchemeChangedListener;
public SubwayManager(@NonNull MwmApplication application) {
public SubwayManager(@NonNull Application application) {
mSchemeChangedListener = new OnTransitSchemeChangedListener.Default(application);
}
@ -36,10 +37,10 @@ public class SubwayManager
public void initialize()
{
registryListener();
registerListener();
}
private void registryListener()
private void registerListener()
{
nativeAddListener(mSchemeChangedListener);
}

View file

@ -1,19 +1,19 @@
package com.mapswithme.maps.maplayer.subway;
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.util.statistics.Statistics;
public enum TransitSchemeState
enum TransitSchemeState
{
DISABLED,
ENABLED
{
@Override
public void onReceived(@NonNull MwmApplication app)
public void activate(@NonNull Context context)
{
Statistics.INSTANCE.trackSubwayEvent(Statistics.ParamValue.SUCCESS);
}
@ -21,22 +21,14 @@ public enum TransitSchemeState
NO_DATA
{
@Override
public void onReceived(@NonNull MwmApplication app)
public void activate(@NonNull Context context)
{
Toast.makeText(app, R.string.subway_data_unavailable, Toast.LENGTH_SHORT).show();
Toast.makeText(context, R.string.subway_data_unavailable, Toast.LENGTH_SHORT).show();
Statistics.INSTANCE.trackSubwayEvent(Statistics.ParamValue.UNAVAILABLE);
}
};
@NonNull
public static TransitSchemeState makeInstance(int index)
{
if (index < 0 || index >= TransitSchemeState.values().length)
throw new IllegalArgumentException("No value for index = " + index);
return TransitSchemeState.values()[index];
}
public void onReceived(@NonNull MwmApplication app)
void activate(@NonNull Context context)
{
/* Do nothing by default */
}

View file

@ -21,7 +21,7 @@ public enum TrafficManager
private final TrafficState.StateChangeListener mStateChangeListener = new TrafficStateListener();
@NonNull
private TrafficState.Type mState = TrafficState.Type.DISABLED;
private TrafficState mState = TrafficState.DISABLED;
@NonNull
private final List<TrafficCallback> mCallbacks = new ArrayList<>();
@ -123,13 +123,13 @@ public enum TrafficManager
{
@Override
@MainThread
public void onTrafficStateChanged(@TrafficState.Value int state)
public void onTrafficStateChanged(int index)
{
TrafficState.Type newTrafficState = TrafficState.getType(state);
TrafficState newTrafficState = TrafficState.values()[index];
mLogger.d(mTag, "onTrafficStateChanged current state = " + mState
+ " new value = " + newTrafficState);
newTrafficState.onReceived(mCallbacks, mState);
newTrafficState.activate(mCallbacks, mState);
mState = newTrafficState;
}
}

View file

@ -1,164 +1,142 @@
package com.mapswithme.maps.maplayer.traffic;
import android.support.annotation.IntDef;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import com.mapswithme.util.statistics.Statistics;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
final class TrafficState
enum TrafficState
{
DISABLED
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onDisabled();
}
},
ENABLED(Statistics.ParamValue.SUCCESS)
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onEnabled();
}
},
WAITING_DATA
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onWaitingData();
}
},
OUTDATED
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onOutdated();
}
},
NO_DATA(Statistics.ParamValue.UNAVAILABLE)
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onNoData(lastPostedState != NO_DATA);
}
},
NETWORK_ERROR(Statistics.EventParam.ERROR)
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onNetworkError();
}
},
EXPIRED_DATA
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onExpiredData(lastPostedState != EXPIRED_DATA);
}
},
EXPIRED_APP
{
@Override
protected void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState)
{
callback.onExpiredApp(lastPostedState != EXPIRED_APP);
}
};
@NonNull
private final String mAnalyticsParamName;
TrafficState()
{
mAnalyticsParamName = name();
}
TrafficState(@NonNull String analyticsParamName)
{
mAnalyticsParamName = analyticsParamName;
}
@NonNull
private String getAnalyticsParamName()
{
return mAnalyticsParamName;
}
public void activate(@NonNull List<TrafficManager.TrafficCallback> trafficCallbacks,
@NonNull TrafficState lastPostedState)
{
for (TrafficManager.TrafficCallback callback : trafficCallbacks)
{
activateInternal(callback, lastPostedState);
Statistics.INSTANCE.trackTrafficEvent(getAnalyticsParamName());
}
}
protected abstract void activateInternal(@NonNull TrafficManager.TrafficCallback callback,
@NonNull TrafficState lastPostedState);
interface StateChangeListener
{
// This method is called from JNI layer.
@SuppressWarnings("unused")
@MainThread
void onTrafficStateChanged(@Value int state);
void onTrafficStateChanged(int state);
}
@Retention(RetentionPolicy.SOURCE)
@IntDef({ DISABLED, ENABLED, WAITING_DATA, OUTDATED, NO_DATA, NETWORK_ERROR, EXPIRED_DATA, EXPIRED_APP})
@interface Value {}
// These values should correspond to
// TrafficManager::TrafficState enum (from map/traffic_manager.hpp)
private static final int DISABLED = 0;
private static final int ENABLED = 1;
private static final int WAITING_DATA = 2;
private static final int OUTDATED = 3;
private static final int NO_DATA = 4;
private static final int NETWORK_ERROR = 5;
private static final int EXPIRED_DATA = 6;
private static final int EXPIRED_APP = 7;
private TrafficState() {}
@MainThread
static native void nativeSetListener(@NonNull StateChangeListener listener);
static native void nativeRemoveListener();
static native void nativeEnable();
static native void nativeDisable();
static native boolean nativeIsEnabled();
public enum Type
{
DISABLED
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onDisabled();
}
},
ENABLED(Statistics.ParamValue.SUCCESS)
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onEnabled();
}
},
WAITING_DATA
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onWaitingData();
}
},
OUTDATED
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onOutdated();
}
},
NO_DATA(Statistics.ParamValue.UNAVAILABLE)
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onNoData(lastPostedState != NO_DATA);
}
},
NETWORK_ERROR(Statistics.ParamValue.ERROR)
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onNetworkError();
}
},
EXPIRED_DATA
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onExpiredData(lastPostedState != EXPIRED_DATA);
}
},
EXPIRED_APP
{
@Override
protected void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState)
{
param.onExpiredApp(lastPostedState != EXPIRED_APP);
}
};
@NonNull
private final String mAnalyticsParamName;
Type()
{
mAnalyticsParamName = name();
}
Type(@NonNull String analyticsParamName)
{
mAnalyticsParamName = analyticsParamName;
}
@NonNull
private String getAnalyticsParamName()
{
return mAnalyticsParamName;
}
public void onReceived(@NonNull List<TrafficManager.TrafficCallback> trafficCallbacks,
@NonNull Type lastPostedState)
{
for (TrafficManager.TrafficCallback callback : trafficCallbacks)
{
onReceivedInternal(callback, lastPostedState);
Statistics.INSTANCE.trackTrafficEvent(getAnalyticsParamName());
}
}
protected abstract void onReceivedInternal(@NonNull TrafficManager.TrafficCallback param,
@NonNull Type lastPostedState);
}
public static Type getType(int index)
{
if (index < 0 || index >= Type.values().length)
throw new IllegalArgumentException("Not found value for index = " + index);
return Type.values()[index];
}
}

View file

@ -1,38 +0,0 @@
package com.mapswithme.maps.subway;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.widget.Toast;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.content.AbstractContextualListener;
public interface OnSubwaySchemeChangedListener
{
@SuppressWarnings("unused")
@MainThread
void onTransitStateChanged(int type);
class Default extends AbstractContextualListener implements OnSubwaySchemeChangedListener
{
public Default(@NonNull MwmApplication app)
{
super(app);
}
@Override
public void onTransitStateChanged(int index)
{
MwmApplication app = getApp();
if (app == null)
return;
SubwaySchemeState state = SubwaySchemeState.makeInstance(index);
if (state != SubwaySchemeState.NO_DATA)
return;
Toast.makeText(app, R.string.subway_data_unavailable, Toast.LENGTH_SHORT).show();
}
}
}

View file

@ -1,18 +0,0 @@
package com.mapswithme.maps.subway;
import android.support.annotation.NonNull;
public enum SubwaySchemeState
{
DISABLED,
ENABLED,
NO_DATA;
@NonNull
public static SubwaySchemeState makeInstance(int index)
{
if (index < 0 || index >= SubwaySchemeState.values().length)
throw new IllegalArgumentException("No value for index = " + index);
return SubwaySchemeState.values()[index];
}
}

View file

@ -306,7 +306,7 @@ public enum Statistics
public static final String UGC_AUTH_DECLINED = "UGC_Auth_declined";
public static final String UGC_AUTH_EXTERNAL_REQUEST_SUCCESS = "UGC_Auth_external_request_success";
public static final String UGC_AUTH_ERROR = "UGC_Auth_error";
public static final String MAP_LAYER = "Map_Layers_activate";
public static final String MAP_LAYERS_ACTIVATE = "Map_Layers_activate";
public static class Settings
{
@ -393,7 +393,6 @@ public enum Statistics
public static final String PRICE_CATEGORY = "price_category";
public static final String DATE = "date";
static final String HAS_AUTH = "has_auth";
public static final String NAME_LOWER_CASE = "name";
public static final String STATUS = "status";
private EventParam() {}
@ -443,7 +442,6 @@ public enum Statistics
static final String TRAFFIC = "traffic";
public static final String SUCCESS = "success";
public static final String UNAVAILABLE = "unavailable";
public static final String ERROR = "error";
}
// Initialized once in constructor and does not change until the process restarts.
@ -651,9 +649,9 @@ public enum Statistics
private void trackMapLayerEvent(@NonNull String eventName, @NonNull String status)
{
ParameterBuilder builder = new ParameterBuilder().add(EventParam.NAME_LOWER_CASE, eventName)
.add(EventParam.STATUS, status);
trackEvent(EventName.MAP_LAYER, builder);
ParameterBuilder builder = params().add(EventParam.NAME, eventName)
.add(EventParam.STATUS, status);
trackEvent(EventName.MAP_LAYERS_ACTIVATE, builder);
}
public void trackEditorSuccess(boolean newObject)