From 680379d98a6c9b00deb95986ef283ec3689de496 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Tue, 25 Jul 2017 15:31:26 +0300 Subject: [PATCH] Fix splash screen --- .../osmand/SecondSplashScreenFragment.java | 268 +++++++++--------- .../osmand/plus/activities/MapActivity.java | 60 +++- .../osmand/plus/views/OsmandMapTileView.java | 17 +- 3 files changed, 187 insertions(+), 158 deletions(-) diff --git a/OsmAnd/src/net/osmand/SecondSplashScreenFragment.java b/OsmAnd/src/net/osmand/SecondSplashScreenFragment.java index 0323c2ae2a..e028e0c5c5 100644 --- a/OsmAnd/src/net/osmand/SecondSplashScreenFragment.java +++ b/OsmAnd/src/net/osmand/SecondSplashScreenFragment.java @@ -3,7 +3,6 @@ package net.osmand; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.os.Bundle; -import android.os.Handler; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; @@ -14,161 +13,150 @@ import android.widget.ImageView; import android.widget.RelativeLayout; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.inapp.InAppHelper; public class SecondSplashScreenFragment extends Fragment { - public static final String TAG = "SecondSplashScreenFragment"; - public static boolean SHOW = true; - private static final int SECOND_SPLASH_TIME_OUT = 8000; - private boolean started = false; - private OsmandApplication app; + public static final String TAG = "SecondSplashScreenFragment"; + public static boolean SHOW = true; + public static boolean VISIBLE = false; - public OsmandApplication getMyApplication() { - return ((OsmandApplication) getActivity().getApplication()); - } + public OsmandApplication getMyApplication() { + return ((OsmandApplication) getActivity().getApplication()); + } - private boolean hasNavBar() { - int id = getResources().getIdentifier("config_showNavigationBar", "bool", "android"); - if (id > 0) - return getResources().getBoolean(id); - else - return false; - } + public MapActivity getMapActivity() { + return (MapActivity) getActivity(); + } - private int getStatusBarHeight() { - int statusBarHeight = 0; - int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); - if (resourceId > 0) { - statusBarHeight = getResources().getDimensionPixelSize(resourceId); - } - return statusBarHeight; - } + private boolean hasNavBar() { + int id = getResources().getIdentifier("config_showNavigationBar", "bool", "android"); + if (id > 0) { + return getResources().getBoolean(id); + } else { + return false; + } + } - private int getNavigationBarHeight() { - if (!hasNavBar()) - return 0; - int orientation = getResources().getConfiguration().orientation; - boolean isSmartphone = getResources().getConfiguration().smallestScreenWidthDp < 600; - if (isSmartphone && Configuration.ORIENTATION_LANDSCAPE == orientation) - return 0; - int id = getResources().getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android"); - if (id > 0) - return getResources().getDimensionPixelSize(id); - return 0; - } + private int getStatusBarHeight() { + int statusBarHeight = 0; + int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (resourceId > 0) { + statusBarHeight = getResources().getDimensionPixelSize(resourceId); + } + return statusBarHeight; + } - private int getNavigationBarWidth() { - if (!hasNavBar()) - return 0; - int orientation = getResources().getConfiguration().orientation; - boolean isSmartphone = getResources().getConfiguration().smallestScreenWidthDp < 600; - if (orientation == Configuration.ORIENTATION_LANDSCAPE && isSmartphone) { - int id = getResources().getIdentifier("navigation_bar_width", "dimen", "android"); - if (id > 0) - return getResources().getDimensionPixelSize(id); - } - return 0; - } + private int getNavigationBarHeight() { + if (!hasNavBar()) + return 0; + int orientation = getResources().getConfiguration().orientation; + boolean isSmartphone = getResources().getConfiguration().smallestScreenWidthDp < 600; + if (isSmartphone && Configuration.ORIENTATION_LANDSCAPE == orientation) + return 0; + int id = getResources().getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android"); + if (id > 0) + return getResources().getDimensionPixelSize(id); + return 0; + } - @Nullable - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - app = getMyApplication(); + private int getNavigationBarWidth() { + if (!hasNavBar()) + return 0; + int orientation = getResources().getConfiguration().orientation; + boolean isSmartphone = getResources().getConfiguration().smallestScreenWidthDp < 600; + if (orientation == Configuration.ORIENTATION_LANDSCAPE && isSmartphone) { + int id = getResources().getIdentifier("navigation_bar_width", "dimen", "android"); + if (id > 0) + return getResources().getDimensionPixelSize(id); + } + return 0; + } - RelativeLayout view = new RelativeLayout(getActivity()); - view.setOnClickListener(null); - view.setBackgroundColor(getResources().getColor(R.color.map_background_color_light)); + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + OsmandApplication app = getMyApplication(); + OsmandSettings settings = app.getSettings(); + FragmentActivity activity = getActivity(); - ImageView logo = new ImageView(getContext()); - if (Version.isFreeVersion(app)) { - logo.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_splash_osmand)); - } else if (Version.isPaidVersion(app) || Version.isDeveloperVersion(app)) { - logo.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_splash_osmand_plus)); - } - RelativeLayout.LayoutParams logoLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); - logoLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); - logoLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); - ImageView text = new ImageView(getActivity()); - if (Version.isFreeVersion(app)) { - if ((InAppHelper.isSubscribedToLiveUpdates() && InAppHelper.isFullVersionPurchased()) || InAppHelper.isSubscribedToLiveUpdates()) { - text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_osmlive)); - } else if (InAppHelper.isFullVersionPurchased()) { - text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_inapp)); - } else { - text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand)); - } - } else if (Version.isPaidVersion(app) || Version.isDeveloperVersion(app)) { - if (InAppHelper.isSubscribedToLiveUpdates()) { - text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_plus_osmlive)); - } else { - text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_plus)); - } - } - RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); - textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); - textLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); - int defaultLogoMarginTop = (int) getResources().getDimension(R.dimen.splash_screen_logo_top); - int logoMarginTop = defaultLogoMarginTop - getStatusBarHeight(); - int logoPaddingLeft = 0; - int logoPaddingRight = 0; - int defaultTextMarginBottom = (int) getResources().getDimension(R.dimen.splash_screen_text_bottom); - int textMarginBottom = defaultTextMarginBottom - getNavigationBarHeight(); - int textPaddingLeft = 0; - int textPaddingRight = 0; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) { - if (AndroidUiHelper.getScreenOrientation(getActivity()) == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { - logoPaddingLeft = getNavigationBarWidth(); - textPaddingLeft = getNavigationBarWidth(); - } else if (AndroidUiHelper.getScreenOrientation(getActivity()) == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) { - logoPaddingRight = getNavigationBarWidth(); - textPaddingRight = getNavigationBarWidth(); - } - } else { - logoPaddingLeft = getNavigationBarWidth(); - textPaddingLeft = getNavigationBarWidth(); - } - logoLayoutParams.setMargins(0, logoMarginTop, 0, 0); - logo.setPadding(logoPaddingLeft, 0, logoPaddingRight, 0); - logo.setLayoutParams(logoLayoutParams); - view.addView(logo); - textLayoutParams.setMargins(0, 0, 0, textMarginBottom); - text.setPadding(textPaddingLeft, 0, textPaddingRight, 0); - text.setLayoutParams(textLayoutParams); - view.addView(text); + RelativeLayout view = new RelativeLayout(activity); + view.setOnClickListener(null); + view.setBackgroundColor(getResources().getColor(R.color.map_background_color_light)); - return view; - } + ImageView logo = new ImageView(getContext()); + if (Version.isFreeVersion(app)) { + logo.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_splash_osmand)); + } else if (Version.isPaidVersion(app) || Version.isDeveloperVersion(app)) { + logo.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_splash_osmand_plus)); + } + RelativeLayout.LayoutParams logoLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); + logoLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); + logoLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); + ImageView text = new ImageView(activity); + if (Version.isFreeVersion(app)) { + if (settings.LIVE_UPDATES_PURCHASED.get()) { + text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_osmlive)); + } else if (settings.FULL_VERSION_PURCHASED.get()) { + text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_inapp)); + } else { + text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand)); + } + } else if (Version.isPaidVersion(app) || Version.isDeveloperVersion(app)) { + if (settings.LIVE_UPDATES_PURCHASED.get()) { + text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_plus_osmlive)); + } else { + text.setImageDrawable(getResources().getDrawable(R.drawable.image_text_osmand_plus)); + } + } + RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); + textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); + textLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); + int defaultLogoMarginTop = (int) getResources().getDimension(R.dimen.splash_screen_logo_top); + int logoMarginTop = defaultLogoMarginTop - getStatusBarHeight(); + int logoPaddingLeft = 0; + int logoPaddingRight = 0; + int defaultTextMarginBottom = (int) getResources().getDimension(R.dimen.splash_screen_text_bottom); + int textMarginBottom = defaultTextMarginBottom - getNavigationBarHeight(); + int textPaddingLeft = 0; + int textPaddingRight = 0; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) { + int screenOrientation = AndroidUiHelper.getScreenOrientation(activity); + if (screenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { + logoPaddingLeft = getNavigationBarWidth(); + textPaddingLeft = getNavigationBarWidth(); + } else if (screenOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) { + logoPaddingRight = getNavigationBarWidth(); + textPaddingRight = getNavigationBarWidth(); + } + } else { + logoPaddingLeft = getNavigationBarWidth(); + textPaddingLeft = getNavigationBarWidth(); + } + logoLayoutParams.setMargins(0, logoMarginTop, 0, 0); + logo.setPadding(logoPaddingLeft, 0, logoPaddingRight, 0); + logo.setLayoutParams(logoLayoutParams); + view.addView(logo); + textLayoutParams.setMargins(0, 0, 0, textMarginBottom); + text.setPadding(textPaddingLeft, 0, textPaddingRight, 0); + text.setLayoutParams(textLayoutParams); + view.addView(text); - @Override - public void onResume() { - super.onResume(); - if (getActivity() instanceof MapActivity) { - ((MapActivity) getActivity()).disableDrawer(); - } - if (!started) { - started = true; - new Handler().postDelayed(new Runnable() { + return view; + } - @Override - public void run() { - if (getActivity() instanceof MapActivity - && (getActivity().getSupportFragmentManager().findFragmentByTag(SecondSplashScreenFragment.TAG) != null)) { - ((MapActivity)getActivity()).dismissSecondSplashScreen(); - } - } - }, SECOND_SPLASH_TIME_OUT); - } - } + @Override + public void onResume() { + super.onResume(); + getMapActivity().disableDrawer(); + } - @Override - public void onPause() { - super.onPause(); - if (getActivity() instanceof MapActivity) { - ((MapActivity) getActivity()).enableDrawer(); - } - } + @Override + public void onPause() { + super.onPause(); + getMapActivity().enableDrawer(); + } } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index a72cf15036..6831a4efb4 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -10,7 +10,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.media.AudioManager; import android.net.Uri; @@ -110,6 +109,7 @@ import net.osmand.plus.views.OsmAndMapLayersView; import net.osmand.plus.views.OsmAndMapSurfaceView; import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.plus.views.OsmandMapTileView.OnDrawMapListener; import net.osmand.plus.views.corenative.NativeCoreContext; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType; @@ -132,7 +132,7 @@ import java.util.regex.Pattern; public class MapActivity extends OsmandActionBarActivity implements DownloadEvents, OnRequestPermissionsResultCallback, IRouteInformationListener, - MapMarkerChangedListener, OnDismissDialogFragmentListener { + MapMarkerChangedListener, OnDismissDialogFragmentListener, OnDrawMapListener { public static final String INTENT_KEY_PARENT_MAP_ACTIVITY = "intent_parent_map_activity_key"; private static final int SHOW_POSITION_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 1; @@ -140,6 +140,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven private static final int LONG_KEYPRESS_DELAY = 500; private static final int ZOOM_LABEL_DISPLAY = 16; private static final int MIN_ZOOM_LABEL_DISPLAY = 12; + private static final int SECOND_SPLASH_TIME_OUT = 8000; private static final Log LOG = PlatformUtil.getLog(MapActivity.class); @@ -188,6 +189,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven private boolean mIsDestroyed = false; private InAppHelper inAppHelper; + private Timer splashScreenTimer; @Override public void onCreate(Bundle savedInstanceState) { @@ -691,31 +693,59 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven new XMasDialogFragment().show(getSupportFragmentManager(), XMasDialogFragment.TAG); } FirstUsageWelcomeFragment.SHOW = false; + if (SecondSplashScreenFragment.SHOW) { SecondSplashScreenFragment.SHOW = false; + SecondSplashScreenFragment.VISIBLE = true; getSupportFragmentManager() .beginTransaction() .add(R.id.fragmentContainer, new SecondSplashScreenFragment(), SecondSplashScreenFragment.TAG) .commitAllowingStateLoss(); + mapView.setOnDrawMapListener(this); + splashScreenTimer = new Timer(); + splashScreenTimer.schedule(new TimerTask() { + @Override + public void run() { + dismissSecondSplashScreen(); + } + }, SECOND_SPLASH_TIME_OUT); } else { - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + if (SecondSplashScreenFragment.VISIBLE) { + dismissSecondSplashScreen(); + } + //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); if (settings.MAP_SCREEN_ORIENTATION.get() != getRequestedOrientation()) { setRequestedOrientation(settings.MAP_SCREEN_ORIENTATION.get()); - // can't return from this method we are not sure if activity will be recreated or not } } } - public void dismissSecondSplashScreen() { - SecondSplashScreenFragment.SHOW = false; - getSupportFragmentManager() - .beginTransaction() - .remove(getSupportFragmentManager().findFragmentByTag(SecondSplashScreenFragment.TAG)) - .commitAllowingStateLoss(); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); - if (app.getSettings().MAP_SCREEN_ORIENTATION.get() != getRequestedOrientation()) { - setRequestedOrientation(app.getSettings().MAP_SCREEN_ORIENTATION.get()); - // can't return from this method we are not sure if activity will be recreated or not + private void dismissSecondSplashScreen() { + if (SecondSplashScreenFragment.VISIBLE) { + SecondSplashScreenFragment.VISIBLE = false; + SecondSplashScreenFragment.SHOW = false; + Fragment fragment = getSupportFragmentManager().findFragmentByTag(SecondSplashScreenFragment.TAG); + if (fragment != null) { + getSupportFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss(); + } + //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + if (app.getSettings().MAP_SCREEN_ORIENTATION.get() != getRequestedOrientation()) { + setRequestedOrientation(app.getSettings().MAP_SCREEN_ORIENTATION.get()); + } + } + } + + @Override + public void onDrawOverMap() { + mapView.setOnDrawMapListener(null); + cancelSplashScreenTimer(); + dismissSecondSplashScreen(); + } + + private void cancelSplashScreenTimer() { + if (splashScreenTimer != null) { + splashScreenTimer.cancel(); + splashScreenTimer = null; } } @@ -1041,6 +1071,8 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven @Override protected void onPause() { + mapView.setOnDrawMapListener(null); + cancelSplashScreenTimer(); app.getMapMarkersHelper().removeListener(this); app.getRoutingHelper().removeListener(this); app.getDownloadThread().resetUiActivity(this); diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java index 5dd5750aaf..2553ffaaad 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java @@ -29,7 +29,6 @@ import android.view.WindowManager; import android.widget.Toast; import net.osmand.PlatformUtil; -import net.osmand.SecondSplashScreenFragment; import net.osmand.access.AccessibilityActionsProvider; import net.osmand.core.android.MapRendererView; import net.osmand.data.LatLon; @@ -109,6 +108,10 @@ public class OsmandMapTileView implements IMapDownloaderCallback { public boolean onPressEvent(PointF point); } + public interface OnDrawMapListener { + public void onDrawOverMap(); + } + public int getDefaultColor() { return defaultColor; } @@ -142,6 +145,8 @@ public class OsmandMapTileView implements IMapDownloaderCallback { private Map zOrders = new HashMap(); + private OnDrawMapListener onDrawMapListener; + // UI Part // handler to refresh map (in ui thread - ui thread is not necessary, but msg queue is required). protected Handler handler; @@ -448,6 +453,10 @@ public class OsmandMapTileView implements IMapDownloaderCallback { return locationListener; } + public void setOnDrawMapListener(OnDrawMapListener onDrawMapListener) { + this.onDrawMapListener = onDrawMapListener; + } + // ////////////////////////////// DRAWING MAP PART ///////////////////////////////////////////// public BaseMapLayer getMainLayer() { return mainLayer; @@ -663,10 +672,10 @@ public class OsmandMapTileView implements IMapDownloaderCallback { } } - if (activity instanceof MapActivity && !((MapActivity) activity).isActivityDestroyed() && - ((MapActivity) activity).getSupportFragmentManager().findFragmentByTag(SecondSplashScreenFragment.TAG) != null) { - ((MapActivity) activity).dismissSecondSplashScreen(); + if (onDrawMapListener != null) { + onDrawMapListener.onDrawOverMap(); } + for (int i = 0; i < layers.size(); i++) { try { OsmandMapLayer layer = layers.get(i);