Use RxJava 3 types in InstalledAppProviderService.

This commit is contained in:
Isira Seneviratne 2020-10-24 11:19:54 +05:30 committed by Hans-Christoph Steiner
parent 5fad229dbe
commit 93a160b40d

View File

@ -14,6 +14,10 @@ import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.JobIntentService;
import org.acra.ACRA; import org.acra.ACRA;
import org.fdroid.fdroid.AppUpdateStatusManager; import org.fdroid.fdroid.AppUpdateStatusManager;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
@ -28,12 +32,9 @@ import java.util.Map;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import androidx.annotation.NonNull; import io.reactivex.rxjava3.disposables.CompositeDisposable;
import androidx.annotation.Nullable; import io.reactivex.rxjava3.schedulers.Schedulers;
import androidx.core.app.JobIntentService; import io.reactivex.rxjava3.subjects.PublishSubject;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
/** /**
* Handles all updates to {@link InstalledAppProvider}, whether checking the contents * Handles all updates to {@link InstalledAppProvider}, whether checking the contents
@ -65,13 +66,15 @@ public class InstalledAppProviderService extends JobIntentService {
private static final String EXTRA_PACKAGE_INFO = "org.fdroid.fdroid.data.extra.PACKAGE_INFO"; private static final String EXTRA_PACKAGE_INFO = "org.fdroid.fdroid.data.extra.PACKAGE_INFO";
/** /**
* This is for notifing the users of this {@link android.content.ContentProvider} * This is for notifying the users of this {@link android.content.ContentProvider}
* that the contents has changed. Since {@link Intent}s can come in slow * that the contents have changed. Since {@link Intent}s can come in slow
* or fast, and this can trigger a lot of UI updates, the actual * or fast, and this can trigger a lot of UI updates, the actual
* notifications are rate limited to one per second. * notifications are rate limited to one per second.
*/ */
private PublishSubject<String> packageChangeNotifier; private PublishSubject<String> packageChangeNotifier;
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
@ -81,17 +84,16 @@ public class InstalledAppProviderService extends JobIntentService {
// only emit an event to the subscriber after it has not received any new events for one second. // only emit an event to the subscriber after it has not received any new events for one second.
// This ensures that we don't constantly ask our lists of apps to update as we iterate over // This ensures that we don't constantly ask our lists of apps to update as we iterate over
// the list of installed apps and insert them to the database... // the list of installed apps and insert them to the database...
compositeDisposable.add(
packageChangeNotifier packageChangeNotifier
.subscribeOn(Schedulers.newThread()) .subscribeOn(Schedulers.newThread())
.debounce(3, TimeUnit.SECONDS) .debounce(3, TimeUnit.SECONDS)
.subscribe(new Action1<String>() { .subscribe(packageName -> {
@Override
public void call(String packageName) {
Utils.debugLog(TAG, "Notifying content providers (so they can update the relevant views)."); Utils.debugLog(TAG, "Notifying content providers (so they can update the relevant views).");
getContentResolver().notifyChange(AppProvider.getContentUri(), null); getContentResolver().notifyChange(AppProvider.getContentUri(), null);
getContentResolver().notifyChange(ApkProvider.getContentUri(), null); getContentResolver().notifyChange(ApkProvider.getContentUri(), null);
} })
}); );
// ...alternatively, this non-debounced version will instantly emit an event about the // ...alternatively, this non-debounced version will instantly emit an event about the
// particular package being updated. This is required so that our AppDetails view can update // particular package being updated. This is required so that our AppDetails view can update
@ -100,14 +102,18 @@ public class InstalledAppProviderService extends JobIntentService {
// only for changes to specific URIs in the AppProvider. These are triggered when a more // only for changes to specific URIs in the AppProvider. These are triggered when a more
// general notification (e.g. to AppProvider.getContentUri()) is fired, but not when a // general notification (e.g. to AppProvider.getContentUri()) is fired, but not when a
// sibling such as AppProvider.getHighestPriorityMetadataUri() is fired. // sibling such as AppProvider.getHighestPriorityMetadataUri() is fired.
packageChangeNotifier.subscribeOn(Schedulers.newThread()) compositeDisposable.add(
.subscribe(new Action1<String>() { packageChangeNotifier
@Override .subscribeOn(Schedulers.newThread())
public void call(String packageName) { .subscribe(packageName -> getContentResolver()
getContentResolver() .notifyChange(AppProvider.getHighestPriorityMetadataUri(packageName), null))
.notifyChange(AppProvider.getHighestPriorityMetadataUri(packageName), null); );
} }
});
@Override
public void onDestroy() {
compositeDisposable.dispose();
super.onDestroy();
} }
/** /**
@ -243,12 +249,7 @@ public class InstalledAppProviderService extends JobIntentService {
public static File getPathToInstalledApk(PackageInfo packageInfo) { public static File getPathToInstalledApk(PackageInfo packageInfo) {
File apk = new File(packageInfo.applicationInfo.publicSourceDir); File apk = new File(packageInfo.applicationInfo.publicSourceDir);
if (apk.isDirectory()) { if (apk.isDirectory()) {
FilenameFilter filter = new FilenameFilter() { FilenameFilter filter = (dir, name) -> name.endsWith(".apk");
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".apk");
}
};
File[] files = apk.listFiles(filter); File[] files = apk.listFiles(filter);
if (files == null) { if (files == null) {
String msg = packageInfo.packageName + " sourceDir has no APKs: " + apk.getAbsolutePath(); String msg = packageInfo.packageName + " sourceDir has no APKs: " + apk.getAbsolutePath();