Back to project page MrParkingNavigator.
The source code is released under:
MIT License
If you think the Android project MrParkingNavigator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package ru.mail.parking.utils; /*from w w w. j ava2 s .co m*/ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public final class NetworkAwaiter { private static final NetworkAwaiter sInstance = new NetworkAwaiter(); public static NetworkAwaiter getInstance() { return sInstance; } private NetworkAwaiter() {} private boolean mNetworkAvailable = NetworkStateReceiver.isNetworkAvailable(); private final Map<String, Runnable> mTasks = new HashMap<>(); private final Set<String> mPending = new HashSet<>(); public synchronized void start(String key, Runnable task) { mTasks.put(key, task); if (!mPending.contains(key)) if (mNetworkAvailable) Utils.runUi(mTasks.remove(key)); else mPending.add(key); } public synchronized void onNetworkChanged(boolean hasNetwork) { boolean old = mNetworkAvailable; mNetworkAvailable = hasNetwork; if (mNetworkAvailable && !old) { for (String key: mPending) Utils.runUi(mTasks.remove(key)); mPending.clear(); } } public synchronized void cancelAll() { mTasks.clear(); mPending.clear(); } }