diff options
author | Dmitry Donskoy <donskdmitry@mail.ru> | 2018-08-27 18:17:39 +0300 |
---|---|---|
committer | Aleksandr Zatsepin <alexzatsepin@users.noreply.github.com> | 2018-08-29 14:01:26 +0300 |
commit | 4a676bb2d91d59a779244aa2a8f0bd57d24103e3 (patch) | |
tree | 91ab55a4b310e51ed71a066434dcbf9f8ca56ebf /android/src/com/mapswithme | |
parent | ab5639d6fc0afc039596cb95d5b3cd403e19c47d (diff) |
[android] Modified implicit receivers for lollipop or later
Diffstat (limited to 'android/src/com/mapswithme')
4 files changed, 210 insertions, 1 deletions
diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 6a8e7ec79d..5cc77f7ae4 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -546,7 +546,6 @@ public class MwmActivity extends BaseMwmFragmentActivity mSearchController.setVisibilityListener(this); SharingHelper.INSTANCE.initialize(); - //TODO: uncomment after correct visible rect calculation. //mVisibleRectMeasurer = new VisibleRectMeasurer(new VisibleRectListener() { // @Override diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 57b84b8f08..017790ae4b 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -31,6 +31,10 @@ import com.mapswithme.maps.routing.RoutingController; import com.mapswithme.maps.scheduling.ConnectivityListener; import com.mapswithme.maps.scheduling.ConnectivityJobScheduler; import com.mapswithme.maps.sound.TtsPlayer; +import com.mapswithme.maps.routing.RoutingController; +import com.mapswithme.maps.scheduling.JobDispatcher; +import com.mapswithme.maps.scheduling.JobDispatcherComposite; +import com.mapswithme.maps.sound.TtsPlayer; import com.mapswithme.maps.ugc.UGC; import com.mapswithme.util.Config; import com.mapswithme.util.Counters; diff --git a/android/src/com/mapswithme/maps/scheduling/JobDispatcher.java b/android/src/com/mapswithme/maps/scheduling/JobDispatcher.java new file mode 100644 index 0000000000..68ffd2d5d7 --- /dev/null +++ b/android/src/com/mapswithme/maps/scheduling/JobDispatcher.java @@ -0,0 +1,7 @@ +package com.mapswithme.maps.scheduling; + +public interface JobDispatcher +{ + void dispatch(); + void cancelAll(); +} diff --git a/android/src/com/mapswithme/maps/scheduling/JobDispatcherComposite.java b/android/src/com/mapswithme/maps/scheduling/JobDispatcherComposite.java new file mode 100644 index 0000000000..4a28a58f28 --- /dev/null +++ b/android/src/com/mapswithme/maps/scheduling/JobDispatcherComposite.java @@ -0,0 +1,199 @@ +package com.mapswithme.maps.scheduling; + +import android.annotation.TargetApi; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +import android.content.ComponentName; +import android.content.Context; +import android.content.IntentFilter; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Build; +import android.support.annotation.NonNull; + +import com.mapswithme.maps.MwmApplication; +import com.mapswithme.maps.background.ConnectivityChangedReceiver; + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicInteger; + +public class JobDispatcherComposite implements JobDispatcher +{ + @NonNull + private final JobDispatcher mMasterJobDispatcher; + + @NonNull + private final MwmApplication mContext; + + @NonNull + private final AtomicInteger mCurrentNetworkType; + + public JobDispatcherComposite(@NonNull MwmApplication context) + { + mContext = context; + mMasterJobDispatcher = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP + ? new NativeJobDispatcher(mContext) + : new LegacyJobDispatcher(mContext); + + mCurrentNetworkType = new AtomicInteger(getCurrentNetworkType().ordinal()); + } + + @Override + public void dispatch() + { + mMasterJobDispatcher.dispatch(); + } + + @Override + public void cancelAll() + { + mMasterJobDispatcher.cancelAll(); + } + + @NonNull + public NetworkStatus getNetworkStatus() + { + NetworkType currentNetworkType = getCurrentNetworkType(); + int prevTypeIndex = mCurrentNetworkType.getAndSet(currentNetworkType.mType); + NetworkType prevNetworkType = NetworkType.getInstance(prevTypeIndex); + boolean isNetworkChanged = prevNetworkType != currentNetworkType; + return new NetworkStatus(isNetworkChanged, currentNetworkType); + } + + private NetworkType getCurrentNetworkType() + { + ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetwork; + if (cm == null || (activeNetwork = cm.getActiveNetworkInfo()) == null) + return NetworkType.UNDEFINED; + + return NetworkType.getInstance(activeNetwork.getType()); + } + + public enum NetworkType + { + WIFI(ConnectivityManager.TYPE_WIFI), + MOBILE(ConnectivityManager.TYPE_MOBILE), + UNDEFINED; + + private final int mType; + + NetworkType(int type) + { + + mType = type; + } + + NetworkType() + { + this(-1); + } + + @NonNull + public static NetworkType getInstance(int type) + { + for (NetworkType each : values()) + { + if (each.mType == type) + return each; + } + + return NetworkType.UNDEFINED; + } + } + + public static JobDispatcherComposite from(@NonNull Context context) + { + MwmApplication application = (MwmApplication) context.getApplicationContext(); + return (JobDispatcherComposite) application.getJobDispatcher(); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private static class NativeJobDispatcher implements JobDispatcher + { + private static final int PERIODIC_IN_MILLIS = 4000; + @NonNull + private final JobScheduler mJobScheduler; + @NonNull + private final Context mContext; + + NativeJobDispatcher(@NonNull Context context) + { + mContext = context; + JobScheduler jobScheduler = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE); + Objects.requireNonNull(jobScheduler); + mJobScheduler = jobScheduler; + } + + @Override + public void dispatch() + { + ComponentName component = new ComponentName(mContext, NativeJobService.class); + int jobId = NativeJobService.class.hashCode(); + JobInfo jobInfo = new JobInfo + .Builder(jobId, component) + .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) + .setPeriodic(PERIODIC_IN_MILLIS) + .build(); + mJobScheduler.schedule(jobInfo); + } + + @Override + public void cancelAll() + { + mJobScheduler.cancelAll(); + } + } + + public static class NetworkStatus + { + private final boolean mNetworkStateChanged; + @NonNull + private final NetworkType mCurrentNetworkType; + + NetworkStatus(boolean networkStateChanged, + @NonNull NetworkType currentNetworkType) + { + mNetworkStateChanged = networkStateChanged; + mCurrentNetworkType = currentNetworkType; + } + + public boolean isNetworkStateChanged() + { + return mNetworkStateChanged; + } + + @NonNull + public NetworkType getCurrentNetworkType() + { + return mCurrentNetworkType; + } + } + + private static class LegacyJobDispatcher implements JobDispatcher + { + @NonNull + private final MwmApplication mContext; + @NonNull + private final ConnectivityChangedReceiver mReceiver; + + LegacyJobDispatcher(@NonNull MwmApplication context) + { + mContext = context; + mReceiver = new ConnectivityChangedReceiver(); + } + + @Override + public void dispatch() + { + IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); + mContext.registerReceiver(mReceiver, filter); + } + + @Override + public void cancelAll() + { + mContext.unregisterReceiver(mReceiver); + } + } +} |