forked from organicmaps/organicmaps
[android] refactoring
This commit is contained in:
parent
04b47f6b62
commit
a0641b2c39
2 changed files with 49 additions and 7 deletions
|
@ -2,6 +2,8 @@ package com.mapswithme.maps;
|
|||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.os.Environment;
|
||||
|
@ -101,6 +103,20 @@ public class MwmApplication extends Application
|
|||
}
|
||||
};
|
||||
|
||||
@NonNull
|
||||
private final AppBackgroundTracker.OnFirstLaunchListener mFirstLaunchListener =
|
||||
new AppBackgroundTracker.OnFirstLaunchListener()
|
||||
{
|
||||
@Override
|
||||
public void onFirstLaunch()
|
||||
{
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||||
Intent batteryStatus = registerReceiver(null, filter);
|
||||
if (batteryStatus != null)
|
||||
Statistics.INSTANCE.trackColdStartupInfo(batteryStatus);
|
||||
}
|
||||
};
|
||||
|
||||
public MwmApplication()
|
||||
{
|
||||
super();
|
||||
|
@ -182,6 +198,7 @@ public class MwmApplication extends Application
|
|||
|
||||
mBackgroundTracker = new AppBackgroundTracker();
|
||||
mBackgroundTracker.addListener(mBackgroundListener);
|
||||
mBackgroundTracker.addListener(mFirstLaunchListener);
|
||||
TrackRecorder.init();
|
||||
Editor.init();
|
||||
mIsPlatformInitialized = true;
|
||||
|
|
|
@ -23,7 +23,8 @@ public final class AppBackgroundTracker
|
|||
private static final String TAG = AppBackgroundTracker.class.getSimpleName();
|
||||
private static final int TRANSITION_DELAY_MS = 1000;
|
||||
|
||||
private final Listeners<OnTransitionListener> mListeners = new Listeners<>();
|
||||
private final Listeners<OnTransitionListener> mTransitionListeners = new Listeners<>();
|
||||
private final Listeners<OnFirstLaunchListener> mFirstLaunchListeners = new Listeners<>();
|
||||
private SparseArray<WeakReference<Activity>> mActivities = new SparseArray<>();
|
||||
private boolean mForeground;
|
||||
|
||||
|
@ -47,7 +48,7 @@ public final class AppBackgroundTracker
|
|||
mForeground = (mActivities.size() > 0);
|
||||
|
||||
if (mForeground != old)
|
||||
notifyListeners();
|
||||
notifyTransitionListeners();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -64,6 +65,8 @@ public final class AppBackgroundTracker
|
|||
public void onActivityStarted(Activity activity)
|
||||
{
|
||||
LOGGER.d(TAG, "onActivityStarted activity = " + activity);
|
||||
if (mActivities.size() == 0)
|
||||
notifyFirstLaunchListeners();
|
||||
mActivities.put(activity.hashCode(), new WeakReference<>(activity));
|
||||
onActivityChanged();
|
||||
}
|
||||
|
@ -112,6 +115,11 @@ public final class AppBackgroundTracker
|
|||
void onTransit(boolean foreground);
|
||||
}
|
||||
|
||||
public interface OnFirstLaunchListener
|
||||
{
|
||||
void onFirstLaunch();
|
||||
}
|
||||
|
||||
public AppBackgroundTracker()
|
||||
{
|
||||
MwmApplication.get().registerActivityLifecycleCallbacks(mAppLifecycleCallbacks);
|
||||
|
@ -122,21 +130,38 @@ public final class AppBackgroundTracker
|
|||
return mForeground;
|
||||
}
|
||||
|
||||
private void notifyListeners()
|
||||
private void notifyTransitionListeners()
|
||||
{
|
||||
for (OnTransitionListener listener : mListeners)
|
||||
for (OnTransitionListener listener : mTransitionListeners)
|
||||
listener.onTransit(mForeground);
|
||||
mListeners.finishIterate();
|
||||
mTransitionListeners.finishIterate();
|
||||
}
|
||||
|
||||
private void notifyFirstLaunchListeners()
|
||||
{
|
||||
for (OnFirstLaunchListener listener : mFirstLaunchListeners)
|
||||
listener.onFirstLaunch();
|
||||
mTransitionListeners.finishIterate();
|
||||
}
|
||||
|
||||
public void addListener(OnTransitionListener listener)
|
||||
{
|
||||
mListeners.register(listener);
|
||||
mTransitionListeners.register(listener);
|
||||
}
|
||||
|
||||
public void removeListener(OnTransitionListener listener)
|
||||
{
|
||||
mListeners.unregister(listener);
|
||||
mTransitionListeners.unregister(listener);
|
||||
}
|
||||
|
||||
public void addListener(OnFirstLaunchListener listener)
|
||||
{
|
||||
mFirstLaunchListeners.register(listener);
|
||||
}
|
||||
|
||||
public void removeListener(OnFirstLaunchListener listener)
|
||||
{
|
||||
mFirstLaunchListeners.unregister(listener);
|
||||
}
|
||||
|
||||
@android.support.annotation.UiThread
|
||||
|
|
Loading…
Add table
Reference in a new issue