From dc7d46fc111a44364932f67275619fc27c1258e1 Mon Sep 17 00:00:00 2001 From: Roman Romanov Date: Wed, 5 Apr 2017 11:54:39 +0400 Subject: [PATCH] [android] Review fixes. --- .../src/com/mapswithme/maps/MwmActivity.java | 7 +++++++ .../com/mapswithme/maps/SplashActivity.java | 10 +++++----- .../maps/background/WorkerService.java | 20 +++++-------------- .../maps/base/BaseActivityDelegate.java | 19 +++++++++--------- .../mapswithme/maps/base/BaseMwmFragment.java | 9 ++------- .../maps/base/BaseMwmFragmentActivity.java | 9 --------- .../maps/base/BaseMwmListFragment.java | 8 +------- .../maps/base/BaseMwmRecyclerFragment.java | 8 +------- .../bookmarks/BookmarkCategoriesFragment.java | 2 +- .../maps/downloader/OnmapDownloader.java | 1 - .../maps/editor/EditorFragment.java | 7 +------ .../maps/location/AndroidNativeProvider.java | 14 ++++++++++--- .../maps/location/LocationHelper.java | 6 ------ .../maps/routing/RoutingPlanFragment.java | 4 ---- android/src/com/mapswithme/util/Utils.java | 14 +++++++++++++ 15 files changed, 58 insertions(+), 80 deletions(-) diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 470fb31fbb..0e2dd7eef4 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -950,6 +950,13 @@ public class MwmActivity extends BaseMwmFragmentActivity }); } + if (mIsFragmentContainer) + { + RoutingPlanFragment fragment = (RoutingPlanFragment) getFragment(RoutingPlanFragment.class); + if (fragment != null) + fragment.restoreRoutingPanelState(savedInstanceState); + } + if (!mIsFragmentContainer && RoutingController.get().isPlanning()) mRoutingPlanInplaceController.restoreState(savedInstanceState); diff --git a/android/src/com/mapswithme/maps/SplashActivity.java b/android/src/com/mapswithme/maps/SplashActivity.java index e0026d36d5..2354d92eea 100644 --- a/android/src/com/mapswithme/maps/SplashActivity.java +++ b/android/src/com/mapswithme/maps/SplashActivity.java @@ -177,18 +177,19 @@ public class SplashActivity extends AppCompatActivity if (grantResults.length == 0) return; - boolean isWriteGranted = false; for (int i = 0; i < permissions.length; i++) { int result = grantResults[i]; String permission = permissions[i]; if (permission.equals(WRITE_EXTERNAL_STORAGE) && result == PERMISSION_GRANTED) - isWriteGranted = true; + { + mPermissionsGranted = true; + break; + } } - if (isWriteGranted) + if (mPermissionsGranted) { - mPermissionsGranted = true; init(); resumeDialogs(); } @@ -215,7 +216,6 @@ public class SplashActivity extends AppCompatActivity { MwmApplication.get().initNativePlatform(); MwmApplication.get().initNativeCore(); - LocationHelper.INSTANCE.init(); } @SuppressWarnings("unchecked") diff --git a/android/src/com/mapswithme/maps/background/WorkerService.java b/android/src/com/mapswithme/maps/background/WorkerService.java index d87b7c5ec9..a337dec39c 100644 --- a/android/src/com/mapswithme/maps/background/WorkerService.java +++ b/android/src/com/mapswithme/maps/background/WorkerService.java @@ -5,7 +5,6 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.location.Location; -import android.location.LocationManager; import android.text.TextUtils; import java.util.concurrent.CountDownLatch; @@ -15,6 +14,7 @@ import com.mapswithme.maps.R; import com.mapswithme.maps.downloader.CountryItem; import com.mapswithme.maps.downloader.MapManager; import com.mapswithme.maps.editor.Editor; +import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.util.LocationUtils; import com.mapswithme.util.concurrency.UiThread; @@ -124,23 +124,13 @@ public class WorkerService extends IntentService @android.support.annotation.UiThread private static boolean processLocation() { - final LocationManager manager = (LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE); - Location l = null; - try - { - l = manager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); - } - catch (SecurityException e) - { - e.printStackTrace(); - } - - if (l == null || LocationUtils.isExpired(l, l.getTime(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_LONG)) - return false; - MwmApplication.get().initNativePlatform(); MwmApplication.get().initNativeCore(); + Location l = LocationHelper.INSTANCE.getLastKnownLocation(); + if (l == null) + return false; + String country = MapManager.nativeFindCountry(l.getLatitude(), l.getLongitude()); if (TextUtils.isEmpty(country)) return false; diff --git a/android/src/com/mapswithme/maps/base/BaseActivityDelegate.java b/android/src/com/mapswithme/maps/base/BaseActivityDelegate.java index 3431c4e3f8..6a7592f56a 100644 --- a/android/src/com/mapswithme/maps/base/BaseActivityDelegate.java +++ b/android/src/com/mapswithme/maps/base/BaseActivityDelegate.java @@ -2,6 +2,7 @@ package com.mapswithme.maps.base; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.text.TextUtils; import com.mapswithme.util.Config; import com.mapswithme.util.UiUtils; @@ -10,14 +11,14 @@ import com.mapswithme.util.concurrency.UiThread; import com.mapswithme.util.statistics.Statistics; import com.my.tracker.MyTracker; -public class BaseActivityDelegate +class BaseActivityDelegate { @NonNull private final BaseActivity mActivity; @Nullable private String mThemeName; - public BaseActivityDelegate(@NonNull BaseActivity activity) + BaseActivityDelegate(@NonNull BaseActivity activity) { mActivity = activity; } @@ -25,27 +26,27 @@ public class BaseActivityDelegate public void onCreate() { mThemeName = Config.getCurrentUiTheme(); - if (mThemeName != null) + if (!TextUtils.isEmpty(mThemeName)) mActivity.get().setTheme(mActivity.getThemeResourceId(mThemeName)); } - public void onDestroy() + void onDestroy() { ViewServer.get(mActivity.get()).removeWindow(mActivity.get()); } - public void onPostCreate() + void onPostCreate() { ViewServer.get(mActivity.get()).addWindow(mActivity.get()); } - public void onStart() + void onStart() { Statistics.INSTANCE.startActivity(mActivity.get()); MyTracker.onStartActivity(mActivity.get()); } - public void onStop() + void onStop() { Statistics.INSTANCE.stopActivity(mActivity.get()); MyTracker.onStopActivity(mActivity.get()); @@ -63,9 +64,9 @@ public class BaseActivityDelegate org.alohalytics.Statistics.logEvent("$onPause", mActivity.getClass().getSimpleName()); } - public void onPostResume() + void onPostResume() { - if (mThemeName != null && mThemeName.equals(Config.getCurrentUiTheme())) + if (!TextUtils.isEmpty(mThemeName) && mThemeName.equals(Config.getCurrentUiTheme())) return; // Workaround described in https://code.google.com/p/android/issues/detail?id=93731 diff --git a/android/src/com/mapswithme/maps/base/BaseMwmFragment.java b/android/src/com/mapswithme/maps/base/BaseMwmFragment.java index 60d6ba8952..e06cfc6b35 100644 --- a/android/src/com/mapswithme/maps/base/BaseMwmFragment.java +++ b/android/src/com/mapswithme/maps/base/BaseMwmFragment.java @@ -5,6 +5,7 @@ import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import com.mapswithme.maps.MwmApplication; +import com.mapswithme.util.Utils; public class BaseMwmFragment extends Fragment { @@ -13,13 +14,7 @@ public class BaseMwmFragment extends Fragment public void onAttach(Context context) { super.onAttach(context); - if (context instanceof AppCompatActivity && !MwmApplication.get().isPlatformInitialized()) - { - ((AppCompatActivity)context).getSupportFragmentManager() - .beginTransaction() - .detach(this) - .commit(); - } + Utils.detachFragmentIfInitializing(context, this); } @Override diff --git a/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java b/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java index c66ecd68f5..4ce718c2ec 100644 --- a/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java +++ b/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java @@ -27,8 +27,6 @@ public class BaseMwmFragmentActivity extends AppCompatActivity { private final BaseActivityDelegate mBaseDelegate = new BaseActivityDelegate(this); - @Nullable - private Bundle mSavedState; private boolean mInitializationComplete = false; @Override @@ -54,7 +52,6 @@ public class BaseMwmFragmentActivity extends AppCompatActivity @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - mSavedState = savedInstanceState; if (!MwmApplication.get().isPlatformInitialized() || !Utils.checkPermissions(this, SplashActivity.PERMISSIONS)) { @@ -261,10 +258,4 @@ public class BaseMwmFragmentActivity extends AppCompatActivity SplashActivity.start(this, type); finish(); } - - @Nullable - public Bundle getSavedInstanceState() - { - return mSavedState; - } } diff --git a/android/src/com/mapswithme/maps/base/BaseMwmListFragment.java b/android/src/com/mapswithme/maps/base/BaseMwmListFragment.java index 92fd81bfda..91fcf4f00b 100644 --- a/android/src/com/mapswithme/maps/base/BaseMwmListFragment.java +++ b/android/src/com/mapswithme/maps/base/BaseMwmListFragment.java @@ -22,13 +22,7 @@ public abstract class BaseMwmListFragment extends ListFragment public void onAttach(Context context) { super.onAttach(context); - if (context instanceof AppCompatActivity && !MwmApplication.get().isPlatformInitialized()) - { - ((AppCompatActivity)context).getSupportFragmentManager() - .beginTransaction() - .detach(this) - .commit(); - } + Utils.detachFragmentIfInitializing(context, this); } @Override diff --git a/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java b/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java index 818255c3e2..b32a80a710 100644 --- a/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java +++ b/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java @@ -45,13 +45,7 @@ public abstract class BaseMwmRecyclerFragment extends Fragment public void onAttach(Context context) { super.onAttach(context); - if (context instanceof AppCompatActivity && !MwmApplication.get().isPlatformInitialized()) - { - ((AppCompatActivity)context).getSupportFragmentManager() - .beginTransaction() - .detach(this) - .commit(); - } + Utils.detachFragmentIfInitializing(context, this); } @Override diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java b/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java index 7aec8a9803..f8a861150b 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java @@ -46,7 +46,7 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment protected BookmarkCategoriesAdapter getAdapter() { RecyclerView.Adapter adapter = super.getAdapter(); - return adapter != null ? (BookmarkCategoriesAdapter)adapter : null; + return adapter != null ? (BookmarkCategoriesAdapter) adapter : null; } @CallSuper diff --git a/android/src/com/mapswithme/maps/downloader/OnmapDownloader.java b/android/src/com/mapswithme/maps/downloader/OnmapDownloader.java index 4cf0617bc4..952bc40b39 100644 --- a/android/src/com/mapswithme/maps/downloader/OnmapDownloader.java +++ b/android/src/com/mapswithme/maps/downloader/OnmapDownloader.java @@ -253,7 +253,6 @@ public class OnmapDownloader implements MwmActivity.LeftAnimationTrackListener { MapManager.nativeUnsubscribe(mStorageSubscriptionSlot); mStorageSubscriptionSlot = 0; - MapManager.nativeUnsubscribeOnCountryChanged(); } } diff --git a/android/src/com/mapswithme/maps/editor/EditorFragment.java b/android/src/com/mapswithme/maps/editor/EditorFragment.java index 3e9d737b0c..c30dc341ab 100644 --- a/android/src/com/mapswithme/maps/editor/EditorFragment.java +++ b/android/src/com/mapswithme/maps/editor/EditorFragment.java @@ -115,8 +115,6 @@ public class EditorFragment extends BaseMwmFragment implements View.OnClickListe private EditorHostFragment mParent; - private boolean mIsViewCreated = false; - @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) @@ -128,8 +126,6 @@ public class EditorFragment extends BaseMwmFragment implements View.OnClickListe @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - mIsViewCreated = true; - mParent = (EditorHostFragment) getParentFragment(); initViews(view); @@ -210,8 +206,7 @@ public class EditorFragment extends BaseMwmFragment implements View.OnClickListe public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - if (mIsViewCreated) - setEdits(); + setEdits(); } boolean setEdits() diff --git a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java index 495f831994..e668c89ec8 100644 --- a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java +++ b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java @@ -9,6 +9,8 @@ import android.support.annotation.Nullable; import com.mapswithme.maps.MwmApplication; import com.mapswithme.util.LocationUtils; +import com.mapswithme.util.log.Logger; +import com.mapswithme.util.log.LoggerFactory; import java.util.ArrayList; import java.util.List; @@ -19,6 +21,8 @@ class AndroidNativeProvider extends BaseLocationProvider private final static String TAG = AndroidNativeProvider.class.getSimpleName(); private final static String[] TRUSTED_PROVIDERS = { LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER }; + private final static Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.LOCATION); + @NonNull private final LocationManager mLocationManager; @NonNull @@ -51,15 +55,18 @@ class AndroidNativeProvider extends BaseLocationProvider long interval = LocationHelper.INSTANCE.getInterval(); LOGGER.d(TAG, "Request Android native provider '" + provider + "' to get locations at this interval = " + interval + " ms"); + mListeners.add(listener); try { mLocationManager.requestLocationUpdates(provider, interval, 0, listener); } catch (SecurityException e) { - e.printStackTrace(); + LOGGER.e(TAG, "Dynamic permission ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION is not granted", + e); + setActive(false); + return; } - mListeners.add(listener); } LocationHelper.INSTANCE.startSensors(); @@ -131,7 +138,8 @@ class AndroidNativeProvider extends BaseLocationProvider } catch (SecurityException e) { - e.printStackTrace(); + LOGGER.e(TAG, "Dynamic permission ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION is not granted", + e); } return res; } diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index 63c676823a..9490d433d1 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -158,12 +158,6 @@ public enum LocationHelper } }; - - public void init() - { - mLogger.d(LocationHelper.class.getSimpleName(), "ctor()"); - } - @UiThread public void initialize() { diff --git a/android/src/com/mapswithme/maps/routing/RoutingPlanFragment.java b/android/src/com/mapswithme/maps/routing/RoutingPlanFragment.java index 841b6e7be0..d04188f548 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingPlanFragment.java +++ b/android/src/com/mapswithme/maps/routing/RoutingPlanFragment.java @@ -28,10 +28,6 @@ public class RoutingPlanFragment extends BaseMwmFragment mPlanController = new RoutingPlanController(res, getActivity()); updatePoints(); - Bundle activityState = getMwmActivity().getSavedInstanceState(); - if (activityState != null) - restoreRoutingPanelState(activityState); - return res; } diff --git a/android/src/com/mapswithme/util/Utils.java b/android/src/com/mapswithme/util/Utils.java index 737761dbc4..c1b908355a 100644 --- a/android/src/com/mapswithme/util/Utils.java +++ b/android/src/com/mapswithme/util/Utils.java @@ -15,8 +15,10 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.v4.app.ActivityCompat; +import android.support.v4.app.Fragment; import android.support.v4.app.NavUtils; import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextUtils; @@ -438,4 +440,16 @@ public class Utils return true; } + + public static void detachFragmentIfInitializing(@NonNull Context context, + @NonNull Fragment fragment) + { + if (context instanceof AppCompatActivity && !MwmApplication.get().isPlatformInitialized()) + { + ((AppCompatActivity)context).getSupportFragmentManager() + .beginTransaction() + .detach(fragment) + .commit(); + } + } }