Skip to content

Commit

Permalink
feat: fix background service not stopping
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 26, 2024
1 parent f48a392 commit a7c3fe2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,30 @@ protected void onCreate(Bundle savedInstanceState) {
private void registerPermissionRequest() {
requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), (result) -> {
if (!result) {
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//for Android 5-7
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);

// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());

startActivity(intent);
Snackbar.make(binding.getRoot(), "Please grant Notification permission from App Settings", Snackbar.LENGTH_LONG).setAction(R.string.Settings_Title, view -> {
launchAppNotificationSettings();
}).show();
}
setResult(result ? Activity.RESULT_OK : Activity.RESULT_CANCELED);
finish();
});
}

private void launchAppNotificationSettings() {
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//for Android 5-7
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);

// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());

startActivity(intent);
}

private void setUpClickListeners() {
OnPromptAction actions;
switch (prompt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import static java.util.Locale.ENGLISH;

import android.Manifest;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
Expand All @@ -18,7 +16,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import org.openobservatory.ooniprobe.R;
Expand Down Expand Up @@ -75,10 +72,7 @@ public static void runAsForegroundService(AbstractActivity context,
OnTestServiceStartedListener onTestServiceStartedListener,
PreferenceManager iPreferenceManager) {

if (iPreferenceManager.shouldShowTestProgressConsent() || ActivityCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED
){
if (iPreferenceManager.shouldShowTestProgressConsent()){
context.startActivity(PromptActivity.newIntent(context, PromptActivity.Prompt.TEST_PROGRESS_CONSENT));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ public void stopTest() {
*/
public synchronized void interrupt() {
task.interrupt();
if (task.isInterrupted()) {
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE);
}
}

private class ProgressBroadcastReceiver extends BroadcastReceiver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ class DashboardFragment : Fragment(), View.OnClickListener {

binding.testsCompleted.isVisible = updatesViewModel.testRunComplete.value ?: false

if ((requireActivity().application as Application).isTestRunning) {
binding.runAll.visibility = View.GONE
binding.lastTested.visibility = View.GONE
}

}

fun updateDescriptors() {
Expand Down

0 comments on commit a7c3fe2

Please sign in to comment.