[android] RoutingPlanController refactoring.

This commit is contained in:
Roman Romanov 2017-06-14 08:22:34 +04:00
parent 79fb4ba2b3
commit 46828f074e
3 changed files with 285 additions and 225 deletions

View file

@ -0,0 +1,263 @@
package com.mapswithme.maps.routing;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.uber.UberAdapter;
import com.mapswithme.maps.uber.UberInfo;
import com.mapswithme.maps.uber.UberLinks;
import com.mapswithme.maps.widget.DotPager;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.statistics.AlohaHelper;
import com.mapswithme.util.statistics.Statistics;
import java.util.List;
import java.util.Locale;
final class RoutingBottomMenuController
{
private static final String STATE_ALTITUDE_CHART_SHOWN = "altitude_chart_shown";
private static final String STATE_TAXI_INFO = "taxi_info";
@NonNull
private final Activity mContext;
@NonNull
private final View mAltitudeChartFrame;
@NonNull
private final View mUberFrame;
@NonNull
private final TextView mError;
@NonNull
private final Button mStart;
@NonNull
private final ImageView mAltitudeChart;
@NonNull
private final TextView mAltitudeDifference;
@NonNull
private final View mNumbersFrame;
@Nullable
private UberInfo mUberInfo;
@Nullable
private UberInfo.Product mUberProduct;
RoutingBottomMenuController(@NonNull Activity context,
@NonNull View altitudeChartFrame,
@NonNull View uberFrame,
@NonNull TextView error,
@NonNull Button start,
@NonNull ImageView altitudeChart,
@NonNull TextView altitudeDifference,
@NonNull View numbersFrame)
{
mContext = context;
mAltitudeChartFrame = altitudeChartFrame;
mUberFrame = uberFrame;
mError = error;
mStart = start;
mAltitudeChart = altitudeChart;
mAltitudeDifference = altitudeDifference;
mNumbersFrame = numbersFrame;
UiUtils.hide(mAltitudeChartFrame, mUberFrame);
}
void showAltitudeChartAndRoutingDetails()
{
UiUtils.hide(mError, mUberFrame);
showRouteAltitudeChart();
showRoutingDetails();
UiUtils.show(mAltitudeChartFrame);
}
void hideAltitudeChartAndRoutingDetails()
{
if (UiUtils.isHidden(mAltitudeChartFrame))
return;
UiUtils.hide(mAltitudeChartFrame);
}
void showUberInfo(@NonNull UberInfo info)
{
UiUtils.hide(mError, mAltitudeChartFrame);
final List<UberInfo.Product> products = info.getProducts();
mUberInfo = info;
mUberProduct = products.get(0);
final PagerAdapter adapter = new UberAdapter(mContext, products);
DotPager pager = new DotPager.Builder(mContext, (ViewPager) mUberFrame.findViewById(R.id.pager),
adapter)
.setIndicatorContainer((ViewGroup) mUberFrame.findViewById(R.id.indicator))
.setPageChangedListener(new DotPager.OnPageChangedListener()
{
@Override
public void onPageChanged(int position)
{
mUberProduct = products.get(position);
}
}).build();
pager.show();
setStartButton();
UiUtils.show(mUberFrame);
}
void setStartButton()
{
if (RoutingController.get().isTaxiRouterType())
{
final boolean isUberInstalled = Utils.isUberInstalled(mContext);
mStart.setText(isUberInstalled ? R.string.taxi_order : R.string.install_app);
mStart.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mUberProduct != null)
{
UberLinks links = RoutingController.get().getUberLink(mUberProduct.getProductId());
if (links != null)
{
Utils.launchUber(mContext, links);
trackUberStatistics(isUberInstalled);
}
}
}
});
} else
{
mStart.setText(mContext.getText(R.string.p2p_start));
mStart.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
((MwmActivity)mContext).closeMenu(Statistics.EventName.ROUTING_START, AlohaHelper.ROUTING_START, new Runnable()
{
@Override
public void run()
{
RoutingController.get().start();
}
});
}
});
}
UiUtils.updateAccentButton(mStart);
showStartButton(true);
}
void showError(@StringRes int message)
{
UiUtils.hide(mUberFrame, mAltitudeChartFrame);
mError.setText(message);
mError.setVisibility(View.VISIBLE);
showStartButton(false);
}
void showStartButton(boolean show)
{
UiUtils.showIf(show, mStart);
}
void saveRoutingPanelState(@NonNull Bundle outState)
{
outState.putBoolean(STATE_ALTITUDE_CHART_SHOWN, UiUtils.isVisible(mAltitudeChartFrame));
outState.putParcelable(STATE_TAXI_INFO, mUberInfo);
}
void restoreRoutingPanelState(@NonNull Bundle state)
{
if (state.getBoolean(STATE_ALTITUDE_CHART_SHOWN))
showAltitudeChartAndRoutingDetails();
UberInfo info = state.getParcelable(STATE_TAXI_INFO);
if (info != null)
showUberInfo(info);
}
private void showRouteAltitudeChart()
{
if (RoutingController.get().isVehicleRouterType())
{
UiUtils.hide(mAltitudeChart, mAltitudeDifference);
return;
}
int chartWidth = UiUtils.dimen(mContext, R.dimen.altitude_chart_image_width);
int chartHeight = UiUtils.dimen(mContext, R.dimen.altitude_chart_image_height);
Framework.RouteAltitudeLimits limits = new Framework.RouteAltitudeLimits();
Bitmap bm = Framework.generateRouteAltitudeChart(chartWidth, chartHeight, limits);
if (bm != null)
{
mAltitudeChart.setImageBitmap(bm);
UiUtils.show(mAltitudeChart);
String meter = mAltitudeDifference.getResources().getString(R.string.meter);
String foot = mAltitudeDifference.getResources().getString(R.string.foot);
mAltitudeDifference.setText(String.format(Locale.getDefault(), "%d %s",
limits.maxRouteAltitude - limits.minRouteAltitude,
limits.isMetricUnits ? meter : foot));
Drawable icon = ContextCompat.getDrawable(mAltitudeDifference.getContext(),
R.drawable.ic_altitude_difference);
int colorAccent = ContextCompat.getColor(mAltitudeDifference.getContext(),
UiUtils.getStyledResourceId(mAltitudeDifference.getContext(), R.attr.colorAccent));
mAltitudeDifference.setCompoundDrawablesWithIntrinsicBounds(Graphics.tint(icon, colorAccent),
null, null, null);
UiUtils.show(mAltitudeDifference);
}
}
private void showRoutingDetails()
{
final RoutingInfo rinfo = RoutingController.get().getCachedRoutingInfo();
if (rinfo == null)
{
UiUtils.hide(mNumbersFrame);
return;
}
TextView numbersTime = (TextView) mNumbersFrame.findViewById(R.id.time);
TextView numbersDistance = (TextView) mNumbersFrame.findViewById(R.id.distance);
TextView numbersArrival = (TextView) mNumbersFrame.findViewById(R.id.arrival);
numbersTime.setText(RoutingController.formatRoutingTime(mContext, rinfo.totalTimeInSeconds,
R.dimen.text_size_routing_number));
numbersDistance.setText(rinfo.distToTarget + " " + rinfo.targetUnits);
if (numbersArrival != null)
{
String arrivalTime = RoutingController.formatArrivalTime(rinfo.totalTimeInSeconds);
numbersArrival.setText(arrivalTime);
}
}
private static void trackUberStatistics(boolean isUberInstalled)
{
MapObject from = RoutingController.get().getStartPoint();
MapObject to = RoutingController.get().getEndPoint();
Location location = LocationHelper.INSTANCE.getLastKnownLocation();
Statistics.INSTANCE.trackUber(from, to, location, isUberInstalled);
}
}

View file

@ -3,19 +3,13 @@ package com.mapswithme.maps.routing;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@ -26,35 +20,21 @@ import android.widget.RadioGroup;
import android.widget.TextView;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.uber.Uber;
import com.mapswithme.maps.uber.UberAdapter;
import com.mapswithme.maps.uber.UberInfo;
import com.mapswithme.maps.uber.UberLinks;
import com.mapswithme.maps.widget.DotPager;
import com.mapswithme.maps.widget.RotateDrawable;
import com.mapswithme.maps.widget.RoutingToolbarButton;
import com.mapswithme.maps.widget.ToolbarController;
import com.mapswithme.maps.widget.WheelProgressView;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.statistics.AlohaHelper;
import com.mapswithme.util.statistics.Statistics;
import java.util.List;
import java.util.Locale;
public class RoutingPlanController extends ToolbarController implements SlotFrame.SlotClickListener
{
static final int ANIM_TOGGLE = MwmApplication.get().getResources().getInteger(R.integer.anim_slots_toggle);
private static final String STATE_ALTITUDE_CHART_SHOWN = "altitude_chart_shown";
private static final String STATE_TAXI_INFO = "taxi_info";
protected final View mFrame;
private final ImageView mToggle;
@ -65,18 +45,13 @@ public class RoutingPlanController extends ToolbarController implements SlotFram
private final WheelProgressView mProgressBicycle;
private final WheelProgressView mProgressTaxi;
private final View mAltitudeChartFrame;
private final View mUberFrame;
@NonNull
private final RoutingBottomMenuController mRoutingBottomMenuController;
private final RotateDrawable mToggleImage = new RotateDrawable(R.drawable.ic_down);
int mFrameHeight;
private int mToolbarHeight;
private boolean mOpen;
@Nullable
private UberInfo mUberInfo;
@Nullable
private UberInfo.Product mUberProduct;
@Nullable
private OnToggleListener mToggleListener;
@ -178,11 +153,19 @@ public class RoutingPlanController extends ToolbarController implements SlotFram
mProgressBicycle = (WheelProgressView) progressFrame.findViewById(R.id.progress_bicycle);
mProgressTaxi = (WheelProgressView) progressFrame.findViewById(R.id.progress_taxi);
mAltitudeChartFrame = getViewById(R.id.altitude_chart_panel);
UiUtils.hide(mAltitudeChartFrame);
mUberFrame = getViewById(R.id.uber_panel);
UiUtils.hide(mUberFrame);
View altitudeChartFrame = getViewById(R.id.altitude_chart_panel);
View uberFrame = getViewById(R.id.uber_panel);
TextView error = (TextView) getViewById(R.id.error);
Button start = (Button) getViewById(R.id.start);
ImageView altitudeChart = (ImageView) getViewById(R.id.altitude_chart);
TextView altitudeDifference = (TextView) getViewById(R.id.altitude_difference);
View numbersFrame = getViewById(R.id.numbers);
mRoutingBottomMenuController = new RoutingBottomMenuController(activity, altitudeChartFrame,
uberFrame, error, start,
altitudeChart,
altitudeDifference,
numbersFrame);
mToggle.setImageDrawable(mToggleImage);
mToggle.setOnClickListener(new View.OnClickListener()
@ -258,58 +241,15 @@ public class RoutingPlanController extends ToolbarController implements SlotFram
if (!ready)
{
hideAltitudeChartAndRoutingDetails();
mRoutingBottomMenuController.hideAltitudeChartAndRoutingDetails();
return;
}
if (!isTaxiRouterType())
setStartButton();
showAltitudeChartAndRoutingDetails();
}
private void showAltitudeChartAndRoutingDetails()
{
if (isTaxiRouterType())
return;
UiUtils.hide(getViewById(R.id.error));
UiUtils.hide(mUberFrame);
showRouteAltitudeChart();
showRoutingDetails();
UiUtils.show(mAltitudeChartFrame);
}
private void showRoutingDetails()
{
final View numbersFrame = mAltitudeChartFrame.findViewById(R.id.numbers);
final RoutingInfo rinfo = RoutingController.get().getCachedRoutingInfo();
if (rinfo == null)
{
UiUtils.hide(numbersFrame);
return;
mRoutingBottomMenuController.setStartButton();
mRoutingBottomMenuController.showAltitudeChartAndRoutingDetails();
}
TextView numbersTime = (TextView) numbersFrame.findViewById(R.id.time);
TextView numbersDistance = (TextView) numbersFrame.findViewById(R.id.distance);
TextView numbersArrival = (TextView) numbersFrame.findViewById(R.id.arrival);
numbersTime.setText(RoutingController.formatRoutingTime(mFrame.getContext(), rinfo.totalTimeInSeconds,
R.dimen.text_size_routing_number));
numbersDistance.setText(rinfo.distToTarget + " " + rinfo.targetUnits);
if (numbersArrival != null)
{
String arrivalTime = RoutingController.formatArrivalTime(rinfo.totalTimeInSeconds);
numbersArrival.setText(arrivalTime);
}
}
private void hideAltitudeChartAndRoutingDetails()
{
if (UiUtils.isHidden(mAltitudeChartFrame))
return;
UiUtils.hide(mAltitudeChartFrame);
}
public void updateBuildProgress(int progress, @Framework.RouterType int router)
@ -433,11 +373,6 @@ public class RoutingPlanController extends ToolbarController implements SlotFram
}
}
private boolean isVehicleRouterType()
{
return RoutingController.get().isVehicleRouterType();
}
private boolean isTaxiRouterType()
{
return RoutingController.get().isTaxiRouterType();
@ -454,68 +389,9 @@ public class RoutingPlanController extends ToolbarController implements SlotFram
return mOpen;
}
public void showRouteAltitudeChart()
{
ImageView altitudeChart = (ImageView) mFrame.findViewById(R.id.altitude_chart);
TextView altitudeDifference = (TextView) mAltitudeChartFrame.findViewById(R.id.altitude_difference);
showRouteAltitudeChartInternal(altitudeChart, altitudeDifference);
}
void showRouteAltitudeChartInternal(@NonNull ImageView altitudeChart,
@NonNull TextView altitudeDifference)
{
if (isVehicleRouterType())
{
UiUtils.hide(altitudeChart);
UiUtils.hide(altitudeDifference);
return;
}
int chartWidth = UiUtils.dimen(mActivity, R.dimen.altitude_chart_image_width);
int chartHeight = UiUtils.dimen(mActivity, R.dimen.altitude_chart_image_height);
Framework.RouteAltitudeLimits limits = new Framework.RouteAltitudeLimits();
Bitmap bm = Framework.generateRouteAltitudeChart(chartWidth, chartHeight, limits);
if (bm != null)
{
altitudeChart.setImageBitmap(bm);
UiUtils.show(altitudeChart);
String meter = altitudeDifference.getResources().getString(R.string.meter);
String foot = altitudeDifference.getResources().getString(R.string.foot);
altitudeDifference.setText(String.format(Locale.getDefault(), "%d %s",
limits.maxRouteAltitude - limits.minRouteAltitude,
limits.isMetricUnits ? meter : foot));
Drawable icon = ContextCompat.getDrawable(altitudeDifference.getContext(),
R.drawable.ic_altitude_difference);
int colorAccent = ContextCompat.getColor(altitudeDifference.getContext(),
UiUtils.getStyledResourceId(altitudeDifference.getContext(), R.attr.colorAccent));
altitudeDifference.setCompoundDrawablesWithIntrinsicBounds(Graphics.tint(icon,colorAccent),
null, null, null);
UiUtils.show(altitudeDifference);
}
}
public void showUberInfo(@NonNull UberInfo info)
{
UiUtils.hide(getViewById(R.id.error), mAltitudeChartFrame);
final List<UberInfo.Product> products = info.getProducts();
mUberInfo = info;
mUberProduct = products.get(0);
final PagerAdapter adapter = new UberAdapter(mActivity, products);
DotPager pager = new DotPager.Builder(mActivity, (ViewPager) mUberFrame.findViewById(R.id.pager), adapter)
.setIndicatorContainer((ViewGroup) mUberFrame.findViewById(R.id.indicator))
.setPageChangedListener(new DotPager.OnPageChangedListener()
{
@Override
public void onPageChanged(int position)
{
mUberProduct = products.get(position);
}
}).build();
pager.show();
setStartButton();
UiUtils.show(mUberFrame);
mRoutingBottomMenuController.showUberInfo(info);
}
public void showUberError(@NonNull Uber.ErrorCode code)
@ -544,19 +420,12 @@ public class RoutingPlanController extends ToolbarController implements SlotFram
private void showError(@StringRes int message)
{
UiUtils.hide(mUberFrame, mAltitudeChartFrame);
TextView error = (TextView) getViewById(R.id.error);
error.setText(message);
error.setVisibility(View.VISIBLE);
showStartButton(false);
mRoutingBottomMenuController.showError(message);
}
void showStartButton(boolean show)
{
if (show)
UiUtils.show(getViewById(R.id.start));
else
UiUtils.hide(getViewById(R.id.start));
mRoutingBottomMenuController.showStartButton(show);
}
@NonNull
@ -570,74 +439,12 @@ public class RoutingPlanController extends ToolbarController implements SlotFram
void saveRoutingPanelState(@NonNull Bundle outState)
{
outState.putBoolean(STATE_ALTITUDE_CHART_SHOWN, UiUtils.isVisible(mAltitudeChartFrame));
outState.putParcelable(STATE_TAXI_INFO, mUberInfo);
mRoutingBottomMenuController.saveRoutingPanelState(outState);
}
void restoreRoutingPanelState(@NonNull Bundle state)
{
if (state.getBoolean(STATE_ALTITUDE_CHART_SHOWN))
showRouteAltitudeChart();
UberInfo info = state.getParcelable(STATE_TAXI_INFO);
if (info != null)
showUberInfo(info);
}
private void setStartButton()
{
Button start = (Button) getViewById(R.id.start);
if (isTaxiRouterType())
{
final boolean isUberInstalled = Utils.isUberInstalled(mActivity);
start.setText(isUberInstalled ? R.string.taxi_order : R.string.install_app);
start.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mUberProduct != null)
{
UberLinks links = RoutingController.get().getUberLink(mUberProduct.getProductId());
if (links != null)
{
Utils.launchUber(mActivity, links);
trackUberStatistics(isUberInstalled);
}
}
}
});
} else
{
start.setText(mActivity.getText(R.string.p2p_start));
start.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
((MwmActivity)mActivity).closeMenu(Statistics.EventName.ROUTING_START, AlohaHelper.ROUTING_START, new Runnable()
{
@Override
public void run()
{
RoutingController.get().start();
}
});
}
});
}
UiUtils.updateAccentButton(start);
showStartButton(true);
}
private static void trackUberStatistics(boolean isUberInstalled)
{
MapObject from = RoutingController.get().getStartPoint();
MapObject to = RoutingController.get().getEndPoint();
Location location = LocationHelper.INSTANCE.getLastKnownLocation();
Statistics.INSTANCE.trackUber(from, to, location, isUberInstalled);
mRoutingBottomMenuController.restoreRoutingPanelState(state);
}
public int getHeight()

View file

@ -5,8 +5,6 @@ import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.TextView;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.R;
@ -71,14 +69,6 @@ public class RoutingPlanInplaceController extends RoutingPlanController
restoreRoutingPanelState(state);
}
@Override
public void showRouteAltitudeChart()
{
ImageView altitudeChart = (ImageView) mActivity.findViewById(R.id.altitude_chart);
TextView altitudeDifference = (TextView) mActivity.findViewById(R.id.altitude_difference);
showRouteAltitudeChartInternal(altitudeChart, altitudeDifference);
}
private void animateFrame(final boolean show, final @Nullable Runnable completion)
{
if (!checkFrameHeight())