Skip to content

Commit

Permalink
Implemented Work Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek-singh-3212 committed Jun 3, 2022
1 parent c550746 commit f24e9b2
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 157 deletions.
3 changes: 3 additions & 0 deletions AnkiDroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -331,5 +331,8 @@ dependencies {

// GSON
implementation 'com.google.code.gson:gson:2.9.0'

// Work Manager
implementation "androidx.work:work-runtime-ktx:2.7.1"
}
apply from: "./kotlinMigration.gradle"
17 changes: 12 additions & 5 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,6 @@

<!-- Service to perform web API queries -->
<service android:name="com.ichi2.widget.AnkiDroidWidgetSmall$UpdateService" />
<service
android:enabled="true"
android:name=".services.DeckMetaDataService"
android:exported="false"/>


<!-- small widget -->
<receiver
Expand Down Expand Up @@ -496,6 +491,18 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>

<!-- Disabled the Initialization at startup. Using custom work manager Initialization-->
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data
android:name="androidx.work.WorkManagerInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
</application>

</manifest>
40 changes: 39 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.ichi2.anki.exception.StorageAccessException;
import com.ichi2.anki.services.BootService;
import com.ichi2.anki.services.NotificationService;
import com.ichi2.anki.worker.DeckMetaDataWorker;
import com.ichi2.compat.CompatHelper;
import com.ichi2.utils.AdaptionUtil;
import com.ichi2.utils.ExceptionUtil;
Expand All @@ -52,9 +53,13 @@

import java.io.InputStream;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import timber.log.Timber;

import static com.ichi2.anki.CrashReportService.sendExceptionReport;
Expand All @@ -63,7 +68,7 @@
/**
* Application class.
*/
public class AnkiDroidApp extends Application {
public class AnkiDroidApp extends Application implements androidx.work.Configuration.Provider {

/** Running under instrumentation. a "/androidTest" directory will be created which contains a test collection */
public static boolean INSTRUMENTATION_TESTING = false;
Expand Down Expand Up @@ -226,6 +231,9 @@ public void onCreate() {
Timber.i("AnkiDroidApp: Starting Services");
new BootService().onReceive(this, new Intent(this, BootService.class));

Timber.i("AnkiDroidApp: Starting Workers");
setupDeckMetaDataWorker();

// Register BroadcastReceiver NotificationService
NotificationService ns = new NotificationService();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
Expand Down Expand Up @@ -411,6 +419,36 @@ public static String getWebViewErrorMessage() {
return ExceptionUtil.getExceptionMessage(error);
}

/**
* This function setups the work manager which run periodically.
**/
private void setupDeckMetaDataWorker() {
Timber.tag("META").e("Setting up deck meta data worker...");

PeriodicWorkRequest deckMetaDataWorker = new PeriodicWorkRequest.Builder(DeckMetaDataWorker.class, 2, TimeUnit.MINUTES)
.addTag(DeckMetaDataWorker.DECK_META_WORKER)
.build();

WorkManager.getInstance(this)
.enqueueUniquePeriodicWork(
DeckMetaDataWorker.DECK_META_WORKER,
ExistingPeriodicWorkPolicy.REPLACE,
deckMetaDataWorker
);
}


/**
* This Method sets the Work Manager Configuration. We are using Custom work manager Initialization.
* */
@NonNull
@Override
public androidx.work.Configuration getWorkManagerConfiguration() {
return new androidx.work.Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.INFO)
.build();
}

/**
* A tree which logs necessary data for crash reporting.
*
Expand Down
24 changes: 20 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckMetaDataPreference.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/*
* Copyright (c) 2022 Prateek Singh <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.anki

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.ichi2.anki.services.DeckMetaDataService
import com.ichi2.anki.worker.DeckMetaDataWorker
import timber.log.Timber

class DeckMetaDataPreference(context: Context) {
Expand All @@ -21,22 +37,22 @@ class DeckMetaDataPreference(context: Context) {
return sharedPreferences.getString(key, default)!!
}

fun setMetaData(key: String, value: DeckMetaDataService.Meta) {
fun setMetaData(key: String, value: DeckMetaDataWorker.Meta) {
val json: String = Gson().toJson(value)
sharedPreferences.edit()
.putString(key, json)
.apply()
Timber.tag("META").e(json)
}

fun getMetaData(key: String): DeckMetaDataService.Meta? {
fun getMetaData(key: String): DeckMetaDataWorker.Meta? {
val jsonData = sharedPreferences
.getString(key, null)

return if (jsonData == null) {
null
} else {
Gson().fromJson(jsonData, DeckMetaDataService.Meta::class.java)
Gson().fromJson(jsonData, DeckMetaDataWorker.Meta::class.java)
}
}
}
12 changes: 0 additions & 12 deletions AnkiDroid/src/main/java/com/ichi2/anki/IntentHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.ichi2.anki.UIUtils.showThemedToast
import com.ichi2.anki.dialogs.DialogHandler
import com.ichi2.anki.dialogs.DialogHandler.Companion.storeMessage
import com.ichi2.anki.servicelayer.ScopedStorageService
import com.ichi2.anki.services.DeckMetaDataService
import com.ichi2.anki.services.ReminderService
import com.ichi2.themes.Themes.disableXiaomiForceDarkMode
import com.ichi2.utils.ImportUtils.handleFileImport
Expand All @@ -49,7 +48,6 @@ class IntentHandler : Activity() {
Timber.d("onCreate()")
super.onCreate(savedInstanceState)
disableXiaomiForceDarkMode(this)
launchDeckMetaDataService()
setContentView(R.layout.progress_bar)
val intent = intent
Timber.v(intent.toString())
Expand Down Expand Up @@ -138,16 +136,6 @@ class IntentHandler : Activity() {
finish()
}

private fun launchDeckMetaDataService() {
Timber.tag("META").e("Launching deck meta data service...")
val intent = Intent(this, DeckMetaDataService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
}

// COULD_BE_BETTER: Also extract the parameters into here to reduce coupling
@VisibleForTesting
enum class LaunchType {
Expand Down
135 changes: 0 additions & 135 deletions AnkiDroid/src/main/java/com/ichi2/anki/services/DeckMetaDataService.kt

This file was deleted.

Loading

0 comments on commit f24e9b2

Please sign in to comment.