Skip to content

Commit

Permalink
Add background task for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalupahana committed Jul 1, 2024
1 parent 7111543 commit 03fd93d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 59 deletions.
2 changes: 2 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ dependencies {
implementation project(':capacitor-cordova-android-plugins')

implementation "com.google.firebase:firebase-crashlytics:18.2.13"
implementation 'androidx.work:work-runtime:2.9.0'
implementation 'com.google.guava:guava:29.0-android'
}

apply from: 'capacitor.build.gradle'
Expand Down
1 change: 0 additions & 1 deletion android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ dependencies {
implementation project(':capacitor-share')
implementation project(':capacitor-status-bar')
implementation project(':capacitor-cloudkit')

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@

import androidx.appcompat.app.AlertDialog;
import androidx.webkit.WebViewCompat;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import com.getcapacitor.BridgeActivity;

import java.util.concurrent.TimeUnit;

public class MainActivity extends BridgeActivity {
final String WEBVIEW_PACKAGE = "com.google.android.webview";

@Override
public void onStart() {
super.onStart();
// Force dark mode based on UI mode
int nightModeFlags = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
WebSettings webSettings = this.bridge.getWebView().getSettings();
if (nightModeFlags == Configuration.UI_MODE_NIGHT_YES && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
webSettings.setForceDark(WebSettings.FORCE_DARK_ON);
}

// Disable weird accessibility text scaling
WebSettings settings = this.bridge.getWebView().getSettings();
settings.setTextZoom(100);

// Show update notice if WebView is too old
SharedPreferences prefs = bridge.getActivity().getPreferences(Context.MODE_PRIVATE);
PackageInfo webViewInfo = WebViewCompat.getCurrentWebViewPackage(bridge.getContext());

Expand All @@ -44,7 +52,7 @@ public void onStart() {
.setTitle("Warning")
.setMessage("You have an old version of Android WebView installed, which will " +
"cause significant display issues on baseline since it's what we use to display the app. " +
"Click below to go the Google Play Store and update 'Android System WebView' to fix this.")
"Click below to go to the Google Play Store and update 'Android System WebView' to fix this.")
.setPositiveButton("Open Play Store", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Expand All @@ -66,6 +74,17 @@ public void onClick(DialogInterface dialogInterface, int i) {
.setCanceledOnTouchOutside(false);
}
}

// Schedule background notification clearing
PeriodicWorkRequest request =
new PeriodicWorkRequest.Builder(NotificationWorker.class,
30, TimeUnit.MINUTES,
30, TimeUnit.MINUTES)
.build();

WorkManager.getInstance(bridge.getContext())
.enqueueUniquePeriodicWork("app.getbaseline.baseline.notifications",
ExistingPeriodicWorkPolicy.UPDATE, request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app.getbaseline.baseline;

import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.StatusBarNotification;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import java.util.ArrayList;

public class NotificationWorker extends Worker {
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {
// For some reason, you can only pull delivered notifications at Android M,
// so it's only worth running any of this if we're at that SDK level
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
// Find the latest notification in all delivered notifications
NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
ArrayList<StatusBarNotification> notifs = new ArrayList<>();
StatusBarNotification latestNotif = null;
for (StatusBarNotification notif : notificationManager.getActiveNotifications()) {
notifs.add(notif);
if (latestNotif == null || latestNotif.getPostTime() < notif.getPostTime()) {
latestNotif = notif;
}
}

// Remove the latest from the list of all delivered
if (latestNotif != null) {
notifs.remove(latestNotif);
}

// And remove all other notifications
for (StatusBarNotification notif : notifs) {
notificationManager.cancel(notif.getTag(), notif.getId());
}
}

return Result.success();
}
}
57 changes: 0 additions & 57 deletions patches/@capacitor-firebase+messaging+6.0.0.patch

This file was deleted.

0 comments on commit 03fd93d

Please sign in to comment.