turn LocalRepoService Intents into status Intents like other Services

This commit is contained in:
Hans-Christoph Steiner 2019-05-22 09:55:03 +02:00
parent effcf4bfa9
commit da66949b9e
2 changed files with 39 additions and 65 deletions

View File

@ -8,7 +8,6 @@ import android.support.v4.content.LocalBroadcastManager;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.views.swap.SwapWorkflowActivity;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
@ -16,11 +15,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import static org.fdroid.fdroid.views.swap.SwapWorkflowActivity.PrepareSwapRepo.EXTRA_TYPE;
import static org.fdroid.fdroid.views.swap.SwapWorkflowActivity.PrepareSwapRepo.TYPE_COMPLETE;
import static org.fdroid.fdroid.views.swap.SwapWorkflowActivity.PrepareSwapRepo.TYPE_ERROR;
import static org.fdroid.fdroid.views.swap.SwapWorkflowActivity.PrepareSwapRepo.TYPE_STATUS;
/**
* Handles setting up and generating the local repo used to swap apps, including
* the {@code index.jar}, the symlinks to the shared APKs, etc.
@ -35,15 +29,15 @@ import static org.fdroid.fdroid.views.swap.SwapWorkflowActivity.PrepareSwapRepo.
public class LocalRepoService extends IntentService {
public static final String TAG = "LocalRepoService";
public static final String ACTION_PROGRESS = "org.fdroid.fdroid.localrepo.LocalRepoService.action.PROGRESS";
public static final String ACTION_COMPLETE = "org.fdroid.fdroid.localrepo.LocalRepoService.action.COMPLETE";
public static final String ACTION_ERROR = "org.fdroid.fdroid.localrepo.LocalRepoService.action.ERROR";
public static final String EXTRA_MESSAGE = "org.fdroid.fdroid.localrepo.LocalRepoService.extra.MESSAGE";
public static final String ACTION_CREATE = "org.fdroid.fdroid.localrepo.action.CREATE";
public static final String EXTRA_PACKAGE_NAMES = "org.fdroid.fdroid.localrepo.extra.PACKAGE_NAMES";
public static final String ACTION_STATUS = "localRepoStatusAction";
public static final String EXTRA_STATUS = "localRepoStatusExtra";
public static final int STATUS_STARTED = 0;
public static final int STATUS_PROGRESS = 1;
public static final int STATUS_ERROR = 2;
private String[] currentlyProcessedApps = new String[0];
private GenerateLocalRepoThread thread;
@ -105,19 +99,19 @@ public class LocalRepoService extends IntentService {
public static void runProcess(Context context, String[] selectedApps) {
try {
final LocalRepoManager lrm = LocalRepoManager.get(context);
broadcast(context, ACTION_PROGRESS, R.string.deleting_repo);
broadcast(context, STATUS_PROGRESS, R.string.deleting_repo);
lrm.deleteRepo();
for (String app : selectedApps) {
broadcast(context, ACTION_PROGRESS, context.getString(R.string.adding_apks_format, app));
broadcast(context, STATUS_PROGRESS, context.getString(R.string.adding_apks_format, app));
lrm.addApp(context, app);
}
String urlString = Utils.getSharingUri(FDroidApp.repo).toString();
lrm.writeIndexPage(urlString);
broadcast(context, ACTION_PROGRESS, R.string.writing_index_jar);
broadcast(context, STATUS_PROGRESS, R.string.writing_index_jar);
lrm.writeIndexJar();
broadcast(context, ACTION_PROGRESS, R.string.linking_apks);
broadcast(context, STATUS_PROGRESS, R.string.linking_apks);
lrm.copyApksToRepo();
broadcast(context, ACTION_PROGRESS, R.string.copying_icons);
broadcast(context, STATUS_PROGRESS, R.string.copying_icons);
// run the icon copy without progress, its not a blocker
new Thread() {
@Override
@ -127,9 +121,9 @@ public class LocalRepoService extends IntentService {
}
}.start();
broadcast(context, ACTION_COMPLETE, null);
broadcast(context, STATUS_STARTED, null);
} catch (IOException | XmlPullParserException | LocalRepoKeyStore.InitException e) {
broadcast(context, ACTION_ERROR, e.getLocalizedMessage());
broadcast(context, STATUS_ERROR, e.getLocalizedMessage());
e.printStackTrace();
}
}
@ -137,30 +131,16 @@ public class LocalRepoService extends IntentService {
/**
* Translate Android style broadcast {@link Intent}s to {@code PrepareSwapRepo}
*/
static void broadcast(Context context, String action, String message) {
Intent intent = new Intent(context, SwapWorkflowActivity.class);
intent.setAction(SwapWorkflowActivity.PrepareSwapRepo.ACTION);
switch (action) {
case ACTION_PROGRESS:
intent.putExtra(EXTRA_TYPE, TYPE_STATUS);
break;
case ACTION_COMPLETE:
intent.putExtra(EXTRA_TYPE, TYPE_COMPLETE);
break;
case ACTION_ERROR:
intent.putExtra(EXTRA_TYPE, TYPE_ERROR);
break;
default:
throw new IllegalArgumentException("unsupported action");
}
static void broadcast(Context context, int status, String message) {
Intent intent = new Intent(ACTION_STATUS);
intent.putExtra(EXTRA_STATUS, status);
if (message != null) {
Utils.debugLog(TAG, "Preparing swap: " + message);
intent.putExtra(EXTRA_MESSAGE, message);
intent.putExtra(Intent.EXTRA_TEXT, message);
}
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
static void broadcast(Context context, String action, int resId) {
broadcast(context, action, context.getString(resId));
static void broadcast(Context context, int status, int resId) {
broadcast(context, status, context.getString(resId));
}
}

View File

@ -342,8 +342,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
localBroadcastManager.registerReceiver(onWifiStateChanged,
new IntentFilter(WifiStateChangeService.BROADCAST));
localBroadcastManager.registerReceiver(prepareSwapReceiver,
new IntentFilter(SwapWorkflowActivity.PrepareSwapRepo.ACTION));
localBroadcastManager.registerReceiver(localRepoStatus, new IntentFilter(LocalRepoService.ACTION_STATUS));
localBroadcastManager.registerReceiver(repoUpdateReceiver,
new IntentFilter(UpdateService.LOCAL_ACTION_STATUS));
localBroadcastManager.registerReceiver(bonjourStatus, new IntentFilter(BonjourManager.ACTION_STATUS));
@ -361,7 +360,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
super.onPause();
localBroadcastManager.unregisterReceiver(onWifiStateChanged);
localBroadcastManager.unregisterReceiver(prepareSwapReceiver);
localBroadcastManager.unregisterReceiver(localRepoStatus);
localBroadcastManager.unregisterReceiver(repoUpdateReceiver);
localBroadcastManager.unregisterReceiver(bonjourStatus);
}
@ -777,15 +776,6 @@ public class SwapWorkflowActivity extends AppCompatActivity {
service.getBluetoothSwap().startInBackground(); // TODO replace with Intent to SwapService
}
public class PrepareSwapRepo {
public static final String ACTION = "PrepareSwapRepo.Action";
public static final String EXTRA_MESSAGE = "PrepareSwapRepo.Status.Message";
public static final String EXTRA_TYPE = "PrepareSwapRepo.Action.Type";
public static final int TYPE_STATUS = 0;
public static final int TYPE_COMPLETE = 1;
public static final int TYPE_ERROR = 2;
}
/**
* Helper class to try and make sense of what the swap workflow is currently doing.
* The more technologies are involved in the process (e.g. Bluetooth/Wifi/NFC/etc)
@ -1177,10 +1167,10 @@ public class SwapWorkflowActivity extends AppCompatActivity {
* etc. Icons will be copied to the webroot in the background and so are
* not part of this process.
*/
private final BroadcastReceiver prepareSwapReceiver = new BroadcastReceiver() {
private final BroadcastReceiver localRepoStatus = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setUpConnectingProgressText(intent.getStringExtra(SwapWorkflowActivity.PrepareSwapRepo.EXTRA_MESSAGE));
setUpConnectingProgressText(intent.getStringExtra(Intent.EXTRA_TEXT));
ProgressBar progressBar = container.findViewById(R.id.progress_bar);
Button tryAgainButton = container.findViewById(R.id.try_again);
@ -1190,18 +1180,22 @@ public class SwapWorkflowActivity extends AppCompatActivity {
return;
}
int type = intent.getIntExtra(SwapWorkflowActivity.PrepareSwapRepo.EXTRA_TYPE, -1);
if (type == SwapWorkflowActivity.PrepareSwapRepo.TYPE_ERROR) {
progressBar.setVisibility(View.GONE);
tryAgainButton.setVisibility(View.VISIBLE);
return;
} else {
progressBar.setVisibility(View.VISIBLE);
tryAgainButton.setVisibility(View.GONE);
}
if (type == SwapWorkflowActivity.PrepareSwapRepo.TYPE_COMPLETE) {
onLocalRepoPrepared();
switch (intent.getIntExtra(LocalRepoService.EXTRA_STATUS, -1)) {
case LocalRepoService.STATUS_PROGRESS:
progressBar.setVisibility(View.VISIBLE);
tryAgainButton.setVisibility(View.GONE);
break;
case LocalRepoService.STATUS_STARTED:
progressBar.setVisibility(View.VISIBLE);
tryAgainButton.setVisibility(View.GONE);
onLocalRepoPrepared();
break;
case LocalRepoService.STATUS_ERROR:
progressBar.setVisibility(View.GONE);
tryAgainButton.setVisibility(View.VISIBLE);
break;
default:
throw new IllegalArgumentException("Bogus intent: " + intent);
}
}
};