diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0014e0bf14cb44940664399b680a504ec9b55970..0dcd3211e9a0342c1816ca3c89c496b50427cbb4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ + diff --git a/app/src/main/java/foundation/e/drive/utils/AppConstants.java b/app/src/main/java/foundation/e/drive/utils/AppConstants.java index 69be96f2dbb32fb0c6a2c1beac154117ea5c0809..bad256e6ab2ec8b0e8698d509008d26dd305c1d0 100644 --- a/app/src/main/java/foundation/e/drive/utils/AppConstants.java +++ b/app/src/main/java/foundation/e/drive/utils/AppConstants.java @@ -28,7 +28,6 @@ public abstract class AppConstants { public static final String INITIALIZATION_HAS_BEEN_DONE = "initService_has_run"; public static final String INITIALFOLDERS_NUMBER = "initial_folder_number"; public static final String APPLICATIONS_LIST_FILE_NAME = "packages_list.csv"; - public static final String APPLICATIONS_LIST_FILE_NAME_TMP = "tmp_packages_list.csv"; public static final String SHARED_PREFERENCE_NAME = "preferences"; public static final String KEY_LAST_SYNC_TIME = "lastSyncTimestamp"; diff --git a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java index 6663c14352f32f57c8ddfea5a659dc4348cce9f8..7cdbf4f15c6eb94fde151eec34e1b7d64a148d60 100644 --- a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java @@ -9,6 +9,7 @@ package foundation.e.drive.work; import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; +import static foundation.e.drive.work.WorkRequestFactory.WorkType.ONE_TIME_APP_LIST; import static foundation.e.drive.work.WorkRequestFactory.WorkType.PERIODIC_SCAN; import android.content.Context; @@ -41,6 +42,9 @@ public class FirstStartWorker extends Worker { public Result doWork() { Timber.v("FirstStartWorker.doWork()"); final Context appContext = getApplicationContext(); + + generateFirstAppListFile(appContext); + appContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) .edit() @@ -58,6 +62,12 @@ public class FirstStartWorker extends Worker { return Result.success(); } + + private void generateFirstAppListFile(@NonNull final Context context) { + final WorkManager workManager = WorkManager.getInstance(context); + workManager.enqueue(WorkRequestFactory.getOneTimeWorkRequest(ONE_TIME_APP_LIST, null)); + } + private void registerPeriodicWork(@NonNull final Context context) { final WorkManager workManager = WorkManager.getInstance(context); diff --git a/app/src/main/java/foundation/e/drive/work/ListAppsWorker.java b/app/src/main/java/foundation/e/drive/work/ListAppsWorker.java index be79084ddb8c9bd329b7d3f6e233db6c6db83689..b04e91c5f09580c9c198086ac9ebb39f76895b75 100644 --- a/app/src/main/java/foundation/e/drive/work/ListAppsWorker.java +++ b/app/src/main/java/foundation/e/drive/work/ListAppsWorker.java @@ -32,9 +32,10 @@ import timber.log.Timber; * @author vincent Bourgmayer */ public class ListAppsWorker extends Worker { - private final static String PWA_PLAYER = "content://foundation.e.pwaplayer.provider/pwa"; - private final static String SEPARATOR =","; - private final static String PWA_SECTION_SEPARATOR = "\n---- PWAs ----\n"; + private static final String PWA_PLAYER = "content://foundation.e.pwaplayer.provider/pwa"; + private static final String SEPARATOR = ","; + private static final String PWA_SECTION_SEPARATOR = "\n---- PWAs ----\n"; + private static final String APPLICATIONS_LIST_FILE_NAME_TMP = ".tmp_packages_list.csv"; public ListAppsWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); @@ -66,6 +67,7 @@ public class ListAppsWorker extends Worker { } private void listPWAs(@NonNull final Context context, @NonNull final StringBuilder stringBuilder) { + Timber.v("ListPWAs"); final Cursor cursor = context.getContentResolver().query( Uri.parse(PWA_PLAYER), null, null, null, null); @@ -73,7 +75,7 @@ public class ListAppsWorker extends Worker { stringBuilder.append(PWA_SECTION_SEPARATOR); cursor.moveToFirst(); - + Timber.v("Writing list of PWA"); do { try { final String pwaTitle = cursor.getString(cursor.getColumnIndexOrThrow("title")); @@ -95,13 +97,13 @@ public class ListAppsWorker extends Worker { } private boolean writeToFile(@NonNull final StringBuilder fileContents) { - try (final FileOutputStream tmp = getApplicationContext().openFileOutput(AppConstants.APPLICATIONS_LIST_FILE_NAME_TMP, Context.MODE_PRIVATE); + try (final FileOutputStream tmp = getApplicationContext().openFileOutput(APPLICATIONS_LIST_FILE_NAME_TMP, Context.MODE_PRIVATE); ) { tmp.write(fileContents.toString().getBytes()); final String filesDir = getApplicationContext().getFilesDir().getCanonicalPath() + PATH_SEPARATOR; - final File tmp_file = new File(filesDir+AppConstants.APPLICATIONS_LIST_FILE_NAME_TMP); - final File real_file = new File(filesDir+AppConstants.APPLICATIONS_LIST_FILE_NAME); + final File tmp_file = new File(filesDir + APPLICATIONS_LIST_FILE_NAME_TMP); + final File real_file = new File(filesDir + AppConstants.APPLICATIONS_LIST_FILE_NAME); if (tmp_file.length() != real_file.length()) { tmp_file.renameTo(real_file); diff --git a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java index 8627ceb6197562d62135560c65aa39caaa1f1eed..52084c10f3d664bd87838ee2c64c474f0d4a8532 100644 --- a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java +++ b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java @@ -26,6 +26,7 @@ import androidx.work.Constraints; import androidx.work.Data; import androidx.work.NetworkType; import androidx.work.OneTimeWorkRequest; +import androidx.work.OutOfQuotaPolicy; import androidx.work.PeriodicWorkRequest; import java.security.InvalidParameterException; @@ -122,13 +123,17 @@ public class WorkRequestFactory { } } + /** + * Create a workRequest to generate file which contains list of installed apps + * @param expedited if request is expedited + * @return the workRequest + */ private static OneTimeWorkRequest createOneTimeAppListGenerationWorkRequest() { - final OneTimeWorkRequest workRequest = - new OneTimeWorkRequest.Builder(ListAppsWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) - .addTag(AppConstants.WORK_GENERIC_TAG) - .build(); - return workRequest; + final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(ListAppsWorker.class); + + return builder.setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) + .addTag(AppConstants.WORK_GENERIC_TAG) + .build(); } /** @@ -140,13 +145,12 @@ public class WorkRequestFactory { private static OneTimeWorkRequest createOneTimeFullScanWorkRequest() { final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); - final OneTimeWorkRequest workRequest = - new OneTimeWorkRequest.Builder(FullScanWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) + final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(FullScanWorker.class); + + return builder.setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) .setConstraints(constraints) .addTag(AppConstants.WORK_GENERIC_TAG) .build(); - return workRequest; } @@ -157,13 +161,13 @@ public class WorkRequestFactory { private static OneTimeWorkRequest createOneTimeGetUserInfoWorkRequest() { final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); - final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(AccountUserInfoWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) + final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(AccountUserInfoWorker.class); + + return builder.setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) .addTag(AppConstants.WORK_GENERIC_TAG) .addTag(AppConstants.WORK_INITIALIZATION_TAG) .setConstraints(constraints) .build(); - return workRequest; } /**