From aa7ef4a189ea81892f5c1caca90b28bef6236020 Mon Sep 17 00:00:00 2001 From: Alexey Osminin Date: Wed, 2 Dec 2020 12:10:55 +0300 Subject: [PATCH] [android] getting rid of MwmApplication.get() method --- .../com/mapswithme/maps/MwmApplication.java | 6 +- .../maps/background/AppBackgroundTracker.java | 6 +- .../maps/base/BaseMwmFragmentActivity.java | 2 +- .../maps/location/AndroidNativeProvider.java | 12 ++- .../location/GoogleFusedLocationProvider.java | 2 +- .../maps/location/LocationHelper.java | 7 +- .../maps/onboarding/BaseNewsFragment.java | 2 +- .../mapswithme/maps/widget/menu/BaseMenu.java | 13 +-- .../mapswithme/maps/widget/menu/MainMenu.java | 15 --- .../maps/widget/menu/MenuToggle.java | 11 ++- .../mapswithme/maps/widget/menu/NavMenu.java | 12 ++- .../src/com/mapswithme/util/Animations.java | 98 ------------------- .../src/com/mapswithme/util/BatteryState.java | 15 +-- .../util/statistics/Statistics.java | 4 +- 14 files changed, 47 insertions(+), 158 deletions(-) delete mode 100644 android/src/com/mapswithme/util/Animations.java diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 8374333b0a..481cb088bb 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -170,7 +170,7 @@ public class MwmApplication extends Application implements AppBackgroundTracker. initNotificationChannels(); - mBackgroundTracker = new AppBackgroundTracker(); + mBackgroundTracker = new AppBackgroundTracker(this); mBackgroundTracker.addListener(mVisibleAppLaunchListener); mSubwayManager = new SubwayManager(this); mIsolinesManager = new IsolinesManager(this); @@ -410,12 +410,12 @@ public class MwmApplication extends Application implements AppBackgroundTracker. return mGuidesManager; } - private static class VisibleAppLaunchListener implements AppBackgroundTracker.OnVisibleAppLaunchListener + private class VisibleAppLaunchListener implements AppBackgroundTracker.OnVisibleAppLaunchListener { @Override public void onVisibleAppLaunch() { - Statistics.INSTANCE.trackColdStartupInfo(); + Statistics.INSTANCE.trackColdStartupInfo(MwmApplication.this); } } diff --git a/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java b/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java index 85148a1609..6620e710f8 100644 --- a/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java +++ b/android/src/com/mapswithme/maps/background/AppBackgroundTracker.java @@ -2,11 +2,13 @@ package com.mapswithme.maps.background; import android.app.Activity; import android.app.Application; +import android.content.Context; import android.os.Bundle; import android.util.SparseArray; import java.lang.ref.WeakReference; +import androidx.annotation.NonNull; import com.mapswithme.maps.MwmApplication; import com.mapswithme.util.Listeners; import com.mapswithme.util.concurrency.UiThread; @@ -120,9 +122,9 @@ public final class AppBackgroundTracker void onVisibleAppLaunch(); } - public AppBackgroundTracker() + public AppBackgroundTracker(@NonNull Context context) { - MwmApplication.get().registerActivityLifecycleCallbacks(mAppLifecycleCallbacks); + MwmApplication.from(context).registerActivityLifecycleCallbacks(mAppLifecycleCallbacks); } public boolean isForeground() diff --git a/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java b/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java index 210442c0a8..0df9c34781 100644 --- a/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java +++ b/android/src/com/mapswithme/maps/base/BaseMwmFragmentActivity.java @@ -73,7 +73,7 @@ public abstract class BaseMwmFragmentActivity extends AppCompatActivity if (initialIntent != null) setIntent(initialIntent); - if (!MwmApplication.get().arePlatformAndCoreInitialized() + if (!MwmApplication.from(this).arePlatformAndCoreInitialized() || !PermissionsUtils.isExternalStorageGranted()) { super.onCreate(savedInstanceState); diff --git a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java index 6f03248e0d..1a32f2ad59 100644 --- a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java +++ b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java @@ -4,14 +4,15 @@ import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; - import com.mapswithme.maps.MwmApplication; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; +import java.util.Objects; class AndroidNativeProvider extends BaseLocationProvider { @@ -24,10 +25,11 @@ class AndroidNativeProvider extends BaseLocationProvider @NonNull private final List mListeners = new ArrayList<>(); - AndroidNativeProvider(@NonNull LocationFixChecker locationFixChecker) + AndroidNativeProvider(@NonNull LocationFixChecker locationFixChecker, @NonNull Context context) { super(locationFixChecker); - mLocationManager = (LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE); + Objects.requireNonNull(context, "Context should be passed!"); + mLocationManager = (LocationManager) MwmApplication.from(context).getSystemService(Context.LOCATION_SERVICE); } @SuppressWarnings("MissingPermission") @@ -92,9 +94,9 @@ class AndroidNativeProvider extends BaseLocationProvider } @Nullable - static Location findBestLocation() + static Location findBestLocation(@NonNull Context context) { - final LocationManager manager = (LocationManager) MwmApplication.get().getSystemService(Context.LOCATION_SERVICE); + final LocationManager manager = (LocationManager) MwmApplication.from(context).getSystemService(Context.LOCATION_SERVICE); return findBestLocation(manager, getAvailableProviders(manager)); } diff --git a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java b/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java index d47af6a315..c752c3077a 100644 --- a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java +++ b/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java @@ -2,8 +2,8 @@ package com.mapswithme.maps.location; import android.location.Location; import android.os.Bundle; -import androidx.annotation.NonNull; +import androidx.annotation.NonNull; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.PendingResult; diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index d31864ee2b..956706a4ee 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -175,8 +175,7 @@ public enum LocationHelper implements Initializable private void initProvider() { mLogger.d(TAG, "initProvider", new Throwable()); - final MwmApplication application = MwmApplication.get(); - final boolean containsGoogleServices = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(application) == ConnectionResult.SUCCESS; + final boolean containsGoogleServices = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS; final boolean googleServicesTurnedInSettings = Config.useGoogleServices(); if (containsGoogleServices && googleServicesTurnedInSettings) { @@ -192,7 +191,7 @@ public enum LocationHelper implements Initializable void initNativeProvider() { mLogger.d(TAG, "Use native provider"); - mLocationProvider = new AndroidNativeProvider(new DefaultLocationFixChecker()); + mLocationProvider = new AndroidNativeProvider(new DefaultLocationFixChecker(), mContext); } public void onLocationUpdated(@NonNull Location location) @@ -652,7 +651,7 @@ public enum LocationHelper implements Initializable if (mSavedLocation != null) return mSavedLocation; - return AndroidNativeProvider.findBestLocation(); + return AndroidNativeProvider.findBestLocation(mContext); } @Nullable diff --git a/android/src/com/mapswithme/maps/onboarding/BaseNewsFragment.java b/android/src/com/mapswithme/maps/onboarding/BaseNewsFragment.java index c705f79c54..544c7a0b10 100644 --- a/android/src/com/mapswithme/maps/onboarding/BaseNewsFragment.java +++ b/android/src/com/mapswithme/maps/onboarding/BaseNewsFragment.java @@ -70,7 +70,7 @@ public abstract class BaseNewsFragment extends BaseMwmDialogFragment Adapter() { - Resources res = MwmApplication.get().getResources(); + Resources res = MwmApplication.from(requireContext()).getResources(); mTitles = getTitles(res); mSubtitles = res.getStringArray(getSubtitles1()); diff --git a/android/src/com/mapswithme/maps/widget/menu/BaseMenu.java b/android/src/com/mapswithme/maps/widget/menu/BaseMenu.java index 1e1ef6faff..8300566a7a 100644 --- a/android/src/com/mapswithme/maps/widget/menu/BaseMenu.java +++ b/android/src/com/mapswithme/maps/widget/menu/BaseMenu.java @@ -13,8 +13,6 @@ import com.mapswithme.util.UiUtils; public abstract class BaseMenu { - public static final int ANIMATION_DURATION = MwmApplication.get().getResources().getInteger(R.integer.anim_menu); - final View mFrame; final View mLineFrame; @@ -35,14 +33,9 @@ public abstract class BaseMenu View res = frame.findViewById(item.getViewId()); if (res != null) { - res.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - //noinspection unchecked - mItemClickListener.onItemClick(item); - } + res.setOnClickListener(v -> { + //noinspection unchecked + mItemClickListener.onItemClick(item); }); } return res; diff --git a/android/src/com/mapswithme/maps/widget/menu/MainMenu.java b/android/src/com/mapswithme/maps/widget/menu/MainMenu.java index b34167844f..6a625424a7 100644 --- a/android/src/com/mapswithme/maps/widget/menu/MainMenu.java +++ b/android/src/com/mapswithme/maps/widget/menu/MainMenu.java @@ -11,7 +11,6 @@ import com.mapswithme.maps.downloader.MapManager; import com.mapswithme.maps.downloader.UpdateInfo; import com.mapswithme.maps.maplayer.Mode; import com.mapswithme.maps.routing.RoutingController; -import com.mapswithme.util.Animations; import com.mapswithme.util.SharedPropertiesUtils; import com.mapswithme.util.UiUtils; import com.mapswithme.util.statistics.StatisticValueConverter; @@ -345,18 +344,4 @@ public class MainMenu extends BaseMenu if (itemInButtonsFrame != null) UiUtils.showIf(show, itemInButtonsFrame); } - - public void showLineFrame(boolean show) - { - if (show) - { - UiUtils.hide(mFrame); - Animations.appearSliding(mFrame, Animations.BOTTOM, null); - } - else - { - UiUtils.show(mFrame); - Animations.disappearSliding(mFrame, Animations.BOTTOM, null); - } - } } diff --git a/android/src/com/mapswithme/maps/widget/menu/MenuToggle.java b/android/src/com/mapswithme/maps/widget/menu/MenuToggle.java index b3dfe10efd..7f8b38d09b 100644 --- a/android/src/com/mapswithme/maps/widget/menu/MenuToggle.java +++ b/android/src/com/mapswithme/maps/widget/menu/MenuToggle.java @@ -9,6 +9,8 @@ import androidx.annotation.DrawableRes; import android.view.View; import android.widget.ImageView; +import androidx.annotation.IntegerRes; +import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.R; import com.mapswithme.maps.widget.RotateByAlphaDrawable; import com.mapswithme.maps.widget.TrackedTransitionDrawable; @@ -16,6 +18,8 @@ import com.mapswithme.util.UiUtils; class MenuToggle { + @IntegerRes + private final int mAnimationDuration; private final ImageView mButton; private final TransitionDrawable mOpenImage; private final TransitionDrawable mCollapseImage; @@ -27,8 +31,9 @@ class MenuToggle private MenuToggle(View frame, @DimenRes int heightRes, @DrawableRes int src, @DrawableRes int dst) { - mButton = (ImageView) frame.findViewById(R.id.toggle); + mButton = frame.findViewById(R.id.toggle); Context context = frame.getContext(); + mAnimationDuration = context.getResources().getInteger(R.integer.anim_menu); int sz = UiUtils.dimen(context, heightRes); Rect bounds = new Rect(0, 0, sz, sz); @@ -55,9 +60,9 @@ class MenuToggle mButton.setImageDrawable(image); if (forward) - image.startTransition(animate ? BaseMenu.ANIMATION_DURATION : 0); + image.startTransition(animate ? mAnimationDuration : 0); else - image.reverseTransition(animate ? BaseMenu.ANIMATION_DURATION : 0); + image.reverseTransition(animate ? mAnimationDuration : 0); if (!animate) image.getDrawable(forward ? 1 : 0).setAlpha(0xFF); diff --git a/android/src/com/mapswithme/maps/widget/menu/NavMenu.java b/android/src/com/mapswithme/maps/widget/menu/NavMenu.java index c9ed85569a..566fffc02f 100644 --- a/android/src/com/mapswithme/maps/widget/menu/NavMenu.java +++ b/android/src/com/mapswithme/maps/widget/menu/NavMenu.java @@ -7,9 +7,11 @@ import android.view.View; import android.widget.Button; import android.widget.ImageView; +import androidx.annotation.IntegerRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.mapswithme.maps.Framework; +import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.R; import com.mapswithme.maps.maplayer.traffic.TrafficManager; import com.mapswithme.maps.sound.TtsPlayer; @@ -19,6 +21,8 @@ import com.mapswithme.util.UiUtils; public class NavMenu extends BaseMenu { + @IntegerRes + private final int mAnimationDuration; private final RotateDrawable mToggleImage; @NonNull private final ImageView mTts; @@ -75,6 +79,8 @@ public class NavMenu extends BaseMenu public NavMenu(View frame, ItemClickListener listener) { super(frame, listener); + mAnimationDuration = MwmApplication.from(frame.getContext()) + .getResources().getInteger(R.integer.anim_menu); mContentFrame = mFrame.findViewById(R.id.content_frame); mToggleImage = new RotateDrawable(Graphics.tint(mFrame.getContext(), R.drawable.ic_menu_close, R.attr.iconTintLight)); ImageView toggle = (ImageView) mLineFrame.findViewById(R.id.toggle); @@ -126,7 +132,7 @@ public class NavMenu extends BaseMenu mFrame.setTranslationY(mContentHeight); mFrame.animate() - .setDuration(ANIMATION_DURATION) + .setDuration(mAnimationDuration) .translationY(0.0f) .setListener(new AnimationListener()) .start(); @@ -160,7 +166,7 @@ public class NavMenu extends BaseMenu } mFrame.animate() - .setDuration(ANIMATION_DURATION) + .setDuration(mAnimationDuration) .translationY(mContentHeight) .setListener(new AnimationListener() { @@ -259,7 +265,7 @@ public class NavMenu extends BaseMenu } }); - animator.setDuration(ANIMATION_DURATION); + animator.setDuration(mAnimationDuration); animator.start(); } diff --git a/android/src/com/mapswithme/util/Animations.java b/android/src/com/mapswithme/util/Animations.java deleted file mode 100644 index 9937664280..0000000000 --- a/android/src/com/mapswithme/util/Animations.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.mapswithme.util; - -import android.animation.Animator; -import androidx.annotation.IntDef; -import androidx.annotation.Nullable; -import android.view.View; -import android.view.ViewPropertyAnimator; - -import com.mapswithme.maps.MwmApplication; -import com.mapswithme.maps.R; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -public final class Animations -{ - private Animations() {} - - @IntDef({LEFT, RIGHT, TOP, BOTTOM}) - @Retention(RetentionPolicy.SOURCE) - @interface AnimationDirection {} - static final int LEFT = 0; - static final int RIGHT = 1; - static final int TOP = 2; - public static final int BOTTOM = 3; - - private static final int DURATION_DEFAULT = MwmApplication.get().getResources().getInteger(R.integer.anim_default); - - public static void appearSliding(final View view, @AnimationDirection int appearFrom, @Nullable final Runnable completionListener) - { - if (UiUtils.isVisible(view)) - { - if (completionListener != null) - completionListener.run(); - return; - } - final ViewPropertyAnimator animator = view.animate().setDuration(DURATION_DEFAULT).alpha(1).setListener(new UiUtils.SimpleAnimatorListener() - { - @Override - public void onAnimationEnd(Animator animation) - { - if (completionListener != null) - completionListener.run(); - } - }); - - switch (appearFrom) - { - case LEFT: - case RIGHT: - animator.translationX(0); - break; - case TOP: - case BOTTOM: - animator.translationY(0); - break; - } - - UiUtils.show(view); - } - - public static void disappearSliding(final View view, @AnimationDirection int disappearTo, @Nullable final Runnable completionListener) - { - if (!UiUtils.isVisible(view)) - { - if (completionListener != null) - completionListener.run(); - return; - } - - final ViewPropertyAnimator animator = view.animate().setDuration(DURATION_DEFAULT).alpha(0).setListener(new UiUtils.SimpleAnimatorListener() - { - @Override - public void onAnimationEnd(Animator animation) - { - UiUtils.hide(view); - if (completionListener != null) - completionListener.run(); - } - }); - - switch (disappearTo) - { - case RIGHT: - animator.translationX(view.getWidth()); - break; - case LEFT: - animator.translationX(-view.getWidth()); - break; - case BOTTOM: - animator.translationY(view.getHeight()); - break; - case TOP: - animator.translationY(-view.getHeight()); - break; - } - } -} diff --git a/android/src/com/mapswithme/util/BatteryState.java b/android/src/com/mapswithme/util/BatteryState.java index 218a56706e..ca39ebc50e 100644 --- a/android/src/com/mapswithme/util/BatteryState.java +++ b/android/src/com/mapswithme/util/BatteryState.java @@ -1,5 +1,6 @@ package com.mapswithme.util; +import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; @@ -25,12 +26,12 @@ public final class BatteryState private BatteryState() {} @NonNull - public static State getState() + public static State getState(@NonNull Context context) { IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); // Because it's a sticky intent, you don't need to register a BroadcastReceiver // by simply calling registerReceiver passing in null - Intent batteryStatus = MwmApplication.get().registerReceiver(null, filter); + Intent batteryStatus = MwmApplication.from(context).registerReceiver(null, filter); if (batteryStatus == null) return new State(0, CHARGING_STATUS_UNKNOWN); @@ -38,15 +39,9 @@ public final class BatteryState } @IntRange(from=0, to=100) - public static int getLevel() + public static int getLevel(@NonNull Context context) { - return getState().getLevel(); - } - - @ChargingStatus - public static int getChargingStatus() - { - return getState().getChargingStatus(); + return getState(context).getLevel(); } @IntRange(from=0, to=100) diff --git a/android/src/com/mapswithme/util/statistics/Statistics.java b/android/src/com/mapswithme/util/statistics/Statistics.java index 259c7d7c35..db110e35ca 100644 --- a/android/src/com/mapswithme/util/statistics/Statistics.java +++ b/android/src/com/mapswithme/util/statistics/Statistics.java @@ -1268,9 +1268,9 @@ public enum Statistics implements Initializable .get()); } - public void trackColdStartupInfo() + public void trackColdStartupInfo(@NonNull Context context) { - BatteryState.State state = BatteryState.getState(); + BatteryState.State state = BatteryState.getState(context); final String charging; switch (state.getChargingStatus()) {