[android] getting rid of MwmApplication.get() method

This commit is contained in:
Alexey Osminin 2020-12-02 12:10:55 +03:00 committed by Maria Velichko
parent a8dfa2893e
commit aa7ef4a189
14 changed files with 47 additions and 158 deletions

View file

@ -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);
}
}

View file

@ -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()

View file

@ -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);

View file

@ -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<LocationListener> 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));
}

View file

@ -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;

View file

@ -175,8 +175,7 @@ public enum LocationHelper implements Initializable<Context>
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<Context>
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<Context>
if (mSavedLocation != null)
return mSavedLocation;
return AndroidNativeProvider.findBestLocation();
return AndroidNativeProvider.findBestLocation(mContext);
}
@Nullable

View file

@ -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());

View file

@ -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;

View file

@ -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);
}
}
}

View file

@ -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);

View file

@ -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<Item> 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();
}

View file

@ -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;
}
}
}

View file

@ -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)

View file

@ -1268,9 +1268,9 @@ public enum Statistics implements Initializable<Context>
.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())
{