move swap repo polling to SwapService
SwapService is the long lived background service, and it was already doing the core work anyway.
This commit is contained in:
parent
6c1375bf3a
commit
11e0c1926c
@ -77,6 +77,7 @@ public class SwapService extends Service {
|
||||
private static SharedPreferences swapPreferences;
|
||||
private static BluetoothAdapter bluetoothAdapter;
|
||||
private static WifiManager wifiManager;
|
||||
private static Timer pollConnectedSwapRepoTimer;
|
||||
|
||||
public static void start(Context context) {
|
||||
Intent intent = new Intent(context, SwapService.class);
|
||||
@ -128,12 +129,6 @@ public class SwapService extends Service {
|
||||
return appsToSwap;
|
||||
}
|
||||
|
||||
public void refreshSwap() {
|
||||
if (peer != null) {
|
||||
connectTo(peer, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void connectToPeer() {
|
||||
if (getPeer() == null) {
|
||||
throw new IllegalStateException("Cannot connect to peer, no peer has been selected.");
|
||||
@ -444,8 +439,12 @@ public class SwapService extends Service {
|
||||
|
||||
Preferences.get().registerLocalRepoHttpsListeners(httpsEnabledListener);
|
||||
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(onWifiChange,
|
||||
localBroadcastManager.registerReceiver(onWifiChange,
|
||||
new IntentFilter(WifiStateChangeService.BROADCAST));
|
||||
localBroadcastManager.registerReceiver(onBluetoothSwapStateChange,
|
||||
new IntentFilter(SwapService.BLUETOOTH_STATE_CHANGE));
|
||||
localBroadcastManager.registerReceiver(onWifiSwapStateChange,
|
||||
new IntentFilter(SwapService.WIFI_STATE_CHANGE));
|
||||
|
||||
if (getBluetoothVisibleUserPreference()) {
|
||||
Utils.debugLog(TAG, "Previously the user enabled Bluetooth swap, so enabling again automatically.");
|
||||
@ -485,7 +484,9 @@ public class SwapService extends Service {
|
||||
public void onDestroy() {
|
||||
Utils.debugLog(TAG, "Destroying service, will disable swapping if required, and unregister listeners.");
|
||||
Preferences.get().unregisterLocalRepoHttpsListeners(httpsEnabledListener);
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(onWifiChange);
|
||||
localBroadcastManager.unregisterReceiver(onWifiChange);
|
||||
localBroadcastManager.unregisterReceiver(onBluetoothSwapStateChange);
|
||||
localBroadcastManager.unregisterReceiver(onWifiSwapStateChange);
|
||||
|
||||
if (bluetoothAdapter != null && !wasBluetoothEnabledBeforeSwap()) {
|
||||
bluetoothAdapter.disable();
|
||||
@ -495,6 +496,8 @@ public class SwapService extends Service {
|
||||
wifiManager.setWifiEnabled(false);
|
||||
}
|
||||
|
||||
stopPollingConnectedSwapRepo();
|
||||
|
||||
//TODO getBluetoothSwap().stopInBackground();
|
||||
getWifiSwap().stopInBackground();
|
||||
|
||||
@ -538,6 +541,27 @@ public class SwapService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
private void startPollingConnectedSwapRepo() {
|
||||
stopPollingConnectedSwapRepo();
|
||||
pollConnectedSwapRepoTimer = new Timer("pollConnectedSwapRepoTimer", true);
|
||||
TimerTask timerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (peer != null) {
|
||||
connectTo(peer, false);
|
||||
}
|
||||
}
|
||||
};
|
||||
pollConnectedSwapRepoTimer.schedule(timerTask, 5000);
|
||||
}
|
||||
|
||||
public void stopPollingConnectedSwapRepo() {
|
||||
if (pollConnectedSwapRepoTimer != null) {
|
||||
pollConnectedSwapRepoTimer.cancel();
|
||||
pollConnectedSwapRepoTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void initTimer() {
|
||||
if (timer != null) {
|
||||
Utils.debugLog(TAG, "Cancelling existing timeout timer so timeout can be reset.");
|
||||
@ -545,7 +569,7 @@ public class SwapService extends Service {
|
||||
}
|
||||
|
||||
Utils.debugLog(TAG, "Initializing swap timeout to " + TIMEOUT + "ms minutes");
|
||||
timer = new Timer();
|
||||
timer = new Timer(TAG, true);
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -557,7 +581,6 @@ public class SwapService extends Service {
|
||||
}, TIMEOUT);
|
||||
}
|
||||
|
||||
@SuppressWarnings("FieldCanBeLocal") // The constructor will get bloated if these are all local...
|
||||
private final Preferences.ChangeListener httpsEnabledListener = new Preferences.ChangeListener() {
|
||||
@Override
|
||||
public void onPreferenceChange() {
|
||||
@ -566,7 +589,6 @@ public class SwapService extends Service {
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("FieldCanBeLocal") // The constructor will get bloated if these are all local...
|
||||
private final BroadcastReceiver onWifiChange = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent i) {
|
||||
@ -575,4 +597,38 @@ public class SwapService extends Service {
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
private final BroadcastReceiver onBluetoothSwapStateChange = new SwapStateChangeReceiver();
|
||||
private final BroadcastReceiver onWifiSwapStateChange = new SwapStateChangeReceiver();
|
||||
|
||||
/**
|
||||
* When swapping is setup, then start the index polling.
|
||||
*/
|
||||
private class SwapStateChangeReceiver extends BroadcastReceiver {
|
||||
private final BroadcastReceiver pollForUpdatesReceiver = new PollForUpdatesReceiver();
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.hasExtra(SwapService.EXTRA_STARTED)) {
|
||||
localBroadcastManager.registerReceiver(pollForUpdatesReceiver,
|
||||
new IntentFilter());
|
||||
} else if (intent.hasExtra(SwapService.EXTRA_STOPPING) || intent.hasExtra(SwapService.EXTRA_STOPPED)) {
|
||||
localBroadcastManager.unregisterReceiver(pollForUpdatesReceiver);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reschedule an index update if the last one was successful.
|
||||
*/
|
||||
private class PollForUpdatesReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
switch (intent.getIntExtra(UpdateService.EXTRA_STATUS_CODE, -1)) {
|
||||
case UpdateService.STATUS_COMPLETE_AND_SAME:
|
||||
case UpdateService.STATUS_COMPLETE_WITH_CHANGES:
|
||||
startPollingConnectedSwapRepo();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@ import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
@ -31,7 +30,6 @@ import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import org.fdroid.fdroid.BuildConfig;
|
||||
import org.fdroid.fdroid.R;
|
||||
import org.fdroid.fdroid.UpdateService;
|
||||
import org.fdroid.fdroid.Utils;
|
||||
@ -47,8 +45,6 @@ import org.fdroid.fdroid.net.Downloader;
|
||||
import org.fdroid.fdroid.net.DownloaderService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* This is a view that shows a listing of all apps in the swap repo that this
|
||||
@ -94,8 +90,6 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal
|
||||
|
||||
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
|
||||
pollForUpdatesReceiver, new IntentFilter(UpdateService.LOCAL_ACTION_STATUS));
|
||||
|
||||
schedulePollForUpdates();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,29 +103,7 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal
|
||||
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(pollForUpdatesReceiver);
|
||||
}
|
||||
|
||||
private void pollForUpdates() {
|
||||
if (adapter.getCount() > 1 ||
|
||||
(adapter.getCount() == 1 && !new App((Cursor) adapter.getItem(0)).packageName.equals(BuildConfig.APPLICATION_ID))) { // NOCHECKSTYLE LineLength
|
||||
Utils.debugLog(TAG, "Not polling for new apps from swap repo, because we already have more than one.");
|
||||
return;
|
||||
}
|
||||
|
||||
Utils.debugLog(TAG, "Polling swap repo to see if it has any updates.");
|
||||
getActivity().getSwapService().refreshSwap();
|
||||
}
|
||||
|
||||
private void schedulePollForUpdates() {
|
||||
Utils.debugLog(TAG, "Scheduling poll for updated swap repo in 5 seconds.");
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
Looper.prepare();
|
||||
pollForUpdates();
|
||||
Looper.loop();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public CursorLoader onCreateLoader(int id, Bundle args) {
|
||||
Uri uri = TextUtils.isEmpty(currentFilterString)
|
||||
@ -143,12 +115,12 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
|
||||
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
|
||||
adapter.swapCursor(cursor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> loader) {
|
||||
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
|
||||
adapter.swapCursor(null);
|
||||
}
|
||||
|
||||
@ -392,17 +364,7 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case UpdateService.STATUS_ERROR_GLOBAL:
|
||||
// TODO: Well, if we can't get the index, we probably can't swapp apps.
|
||||
// Tell the user something helpful?
|
||||
break;
|
||||
|
||||
case UpdateService.STATUS_COMPLETE_AND_SAME:
|
||||
schedulePollForUpdates();
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user