Create base class for fragments with colored status bar

This commit is contained in:
Alexander Sytnyk 2017-11-03 12:57:19 +02:00
parent d316f854e5
commit c14fd73393
4 changed files with 56 additions and 37 deletions

View file

@ -640,7 +640,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
changeKeyguardFlags();
applicationModeListener = new StateChangedListener<ApplicationMode>() {
@Override
public void stateChanged(ApplicationMode change) {

View file

@ -0,0 +1,35 @@
package net.osmand.plus.base;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.Window;
public class ColoredStatusBarFragment extends Fragment {
private int statusBarColor = -1;
@Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= 21 && getStatusBarColor() != -1) {
Window window = getActivity().getWindow();
statusBarColor = window.getStatusBarColor();
window.setStatusBarColor(ContextCompat.getColor(getActivity(), getStatusBarColor()));
}
}
@Override
public void onPause() {
super.onPause();
if (Build.VERSION.SDK_INT >= 21 && statusBarColor != -1) {
getActivity().getWindow().setStatusBarColor(statusBarColor);
}
}
@ColorRes
protected int getStatusBarColor() {
return -1;
}
}

View file

@ -1,10 +1,7 @@
package net.osmand.plus.mapcontextmenu.builders.cards.dialogs;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
@ -12,7 +9,6 @@ import android.support.v7.widget.PopupMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -21,19 +17,18 @@ import net.osmand.AndroidUtils;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.ColoredStatusBarFragment;
import net.osmand.plus.dialogs.DirectionsDialogs;
import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.util.Algorithms;
public class ContextMenuCardDialogFragment extends Fragment {
public class ContextMenuCardDialogFragment extends ColoredStatusBarFragment {
public static final String TAG = "ContextMenuCardDialogFragment";
private ContextMenuCardDialog dialog;
private LinearLayout contentLayout;
private View contentView;
private int statusBarColor = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -50,11 +45,6 @@ public class ContextMenuCardDialogFragment extends Fragment {
if (dialog.getType() == ContextMenuCardDialog.CardDialogType.MAPILLARY) {
view.findViewById(R.id.dialog_layout)
.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.mapillary_action_bar));
if (Build.VERSION.SDK_INT >= 21) {
Window window = getActivity().getWindow();
statusBarColor = window.getStatusBarColor();
window.setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.status_bar_mapillary));
}
}
contentLayout = (LinearLayout) view.findViewById(R.id.content);
contentView = dialog.getContentView();
@ -116,12 +106,6 @@ public class ContextMenuCardDialogFragment extends Fragment {
((WebView) contentView).loadUrl("about:blank");
}
}
if (Build.VERSION.SDK_INT >= 21 && statusBarColor != -1) {
Activity activity = getActivity();
if (activity != null) {
activity.getWindow().setStatusBarColor(statusBarColor);
}
}
}
@Override
@ -129,6 +113,14 @@ public class ContextMenuCardDialogFragment extends Fragment {
dialog.saveMenu(outState);
}
@Override
protected int getStatusBarColor() {
if (dialog != null && dialog.getType() == ContextMenuCardDialog.CardDialogType.MAPILLARY) {
return R.color.status_bar_mapillary;
}
return -1;
}
public static void showInstance(ContextMenuCardDialog menu) {
ContextMenuCardDialogFragment fragment = new ContextMenuCardDialogFragment();
fragment.dialog = menu;

View file

@ -1,6 +1,5 @@
package net.osmand.plus.mapmarkers;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
@ -21,7 +20,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -48,6 +46,7 @@ import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.TargetPointsHelper.TargetPoint;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.ColoredStatusBarFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapmarkers.PlanRouteOptionsBottomSheetDialogFragment.PlanRouteOptionsFragmentListener;
import net.osmand.plus.mapmarkers.adapters.MapMarkersItemTouchHelperCallback;
@ -67,7 +66,7 @@ import java.util.List;
import static net.osmand.plus.OsmandSettings.LANDSCAPE_MIDDLE_RIGHT_CONSTANT;
import static net.osmand.plus.OsmandSettings.MIDDLE_TOP_CONSTANT;
public class PlanRouteFragment extends Fragment implements OsmAndLocationListener {
public class PlanRouteFragment extends ColoredStatusBarFragment implements OsmAndLocationListener {
public static final String TAG = "PlanRouteFragment";
private static final int MIN_DISTANCE_FOR_RECALCULATE = 50; // in meters
@ -86,6 +85,7 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
private boolean nightMode;
private boolean portrait;
private boolean fullScreen;
private boolean wasCollapseButtonVisible;
private boolean cancelSnapToRoad = true;
@ -94,8 +94,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
private View mainView;
private RecyclerView markersRv;
private int statusBarColor = -1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -167,7 +165,7 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
final int backgroundColor = ContextCompat.getColor(mapActivity,
nightMode ? R.color.ctx_menu_info_view_bg_dark : R.color.ctx_menu_info_view_bg_light);
portrait = AndroidUiHelper.isOrientationPortrait(mapActivity);
boolean fullScreen = portrait && planRouteContext.isMarkersListOpened();
fullScreen = portrait && planRouteContext.isMarkersListOpened();
int layoutRes = fullScreen ? R.layout.fragment_plan_route_full_screen : R.layout.fragment_plan_route_half_screen;
View view = View.inflate(new ContextThemeWrapper(getContext(), themeRes), layoutRes, null);
@ -175,13 +173,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
if (fullScreen || !portrait) {
AndroidUtils.addStatusBarPadding21v(getActivity(), view);
}
if (fullScreen) {
if (Build.VERSION.SDK_INT >= 21) {
Window window = getActivity().getWindow();
statusBarColor = window.getStatusBarColor();
window.setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.status_bar_route_light));
}
}
mainView = fullScreen ? view : view.findViewById(R.id.main_view);
@ -394,12 +385,14 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
public void onDestroyView() {
super.onDestroyView();
exitPlanRouteMode();
if (Build.VERSION.SDK_INT >= 21 && statusBarColor != -1) {
Activity activity = getActivity();
if (activity != null) {
activity.getWindow().setStatusBarColor(statusBarColor);
}
}
@Override
protected int getStatusBarColor() {
if (fullScreen) {
return nightMode ? R.color.status_bar_dark : R.color.status_bar_route_light;
}
return -1;
}
@Override