Skip to content

Commit

Permalink
Update preference fragment to request for notification permission
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 23, 2023
1 parent a76e563 commit b5df094
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static org.openobservatory.ooniprobe.common.PreferenceManager.COUNT_WEBSITE_CATEGORIES;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -17,10 +19,14 @@
import android.view.MenuItem;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallerLauncher;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.XmlRes;
import androidx.core.content.ContextCompat;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
Expand Down Expand Up @@ -48,6 +54,7 @@ public class PreferenceFragment extends ExtendedPreferenceFragment<PreferenceFra
public static final String ARG_PREFERENCES_RES_ID = "org.openobservatory.ooniprobe.fragment.PreferenceFragment.PREF_RES_ID";
private static final String ARG_CONTAINER_RES_ID = "org.openobservatory.ooniprobe.fragment.PreferenceFragment.CONTAINER_VIEW_ID";
private String rootKey;
private ActivityResultLauncher<String> requestPermissionLauncher;

public static PreferenceFragment newInstance(@XmlRes int preferencesResId, @IdRes int preferencesContainerResId, String rootKey) {
PreferenceFragment fragment = new PreferenceFragment();
Expand All @@ -59,6 +66,12 @@ public static PreferenceFragment newInstance(@XmlRes int preferencesResId, @IdRe
return fragment;
}

@Override
public void onAttach(@NonNull Context context) {
requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), (result) -> {});
super.onAttach(context);
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
this.rootKey = rootKey;
Expand Down Expand Up @@ -171,9 +184,19 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
if (key.equals(getString(R.string.automated_testing_charging)) ||
key.equals(getString(R.string.automated_testing_wifionly))){
//stop and re-enable scheduler in case of wifi charging option changed

ServiceUtil.stopJob(getContext());
ServiceUtil.scheduleJob(getContext());
}
if (key.equals(getString(R.string.test_progress_notifications_enabled))){
if (ContextCompat.checkSelfPermission(
getContext(), Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
}
}
if (key.equals(getString(R.string.send_crash)) ||
key.equals(getString(R.string.notifications_enabled))){
ThirdPartyServices.reloadConsents((Application) getActivity().getApplication());
Expand Down

0 comments on commit b5df094

Please sign in to comment.