Reset notification preference when resume

This commit is contained in:
Howard Wu 2023-06-09 12:05:06 +08:00
parent cb32c70951
commit cf05ea4160
2 changed files with 41 additions and 15 deletions

View File

@ -134,6 +134,25 @@ public class SettingsFragment extends BaseFragment {
parentFragment = null;
}
@Override
public void onResume() {
super.onResume();
MaterialSwitchPreference notificationPreference = findPreference("enable_status_notification");
if (notificationPreference != null && notificationPreference.isVisible()) {
setNotificationPreferenceEnabled(notificationPreference, ShortcutUtil.isLaunchShortcutPinned());
}
}
private void setNotificationPreferenceEnabled(MaterialSwitchPreference notificationPreference, boolean enabled) {
if (notificationPreference != null) {
notificationPreference.setEnabled(!ConfigManager.enableStatusNotification() || enabled);
notificationPreference.setSummaryOn(enabled ?
notificationPreference.getContext().getString(R.string.settings_enable_status_notification_summary) :
notificationPreference.getContext().getString(R.string.settings_enable_status_notification_summary) + "\n" +
notificationPreference.getContext().getString(R.string.disable_status_notification_error));
}
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
final String SYSTEM = "SYSTEM";
@ -158,18 +177,16 @@ public class SettingsFragment extends BaseFragment {
});
}
MaterialSwitchPreference notification = findPreference("enable_status_notification");
if (notification != null) {
MaterialSwitchPreference notificationPreference = findPreference("enable_status_notification");
if (notificationPreference != null) {
if (App.isParasitic && !ShortcutUtil.isLaunchShortcutPinned()) {
var s = notification.getContext().getString(R.string.disable_status_notification_error);
notification.setSummaryOn(notification.getSummary() + "\n" + s);
if (ConfigManager.enableStatusNotification()) notification.setEnabled(false);
setNotificationPreferenceEnabled(notificationPreference, false);
}
notification.setVisible(installed);
notification.setChecked(installed && ConfigManager.enableStatusNotification());
notification.setOnPreferenceChangeListener((p, v) -> {
notificationPreference.setVisible(installed);
notificationPreference.setChecked(installed && ConfigManager.enableStatusNotification());
notificationPreference.setOnPreferenceChangeListener((p, v) -> {
if ((boolean) v && App.isParasitic && !ShortcutUtil.isLaunchShortcutPinned()) {
p.setEnabled(false);
setNotificationPreferenceEnabled(notificationPreference, false);
}
return ConfigManager.setEnableStatusNotification((boolean) v);
});
@ -188,10 +205,7 @@ public class SettingsFragment extends BaseFragment {
if (!ShortcutUtil.requestPinLaunchShortcut(() -> {
shortcut.setEnabled(false);
shortcut.setSummary(R.string.settings_created_shortcut_summary);
if (notification != null) {
notification.setEnabled(true);
notification.setSummaryOn(R.string.settings_enable_status_notification_summary);
}
setNotificationPreferenceEnabled(notificationPreference, true);
App.getPreferences().edit().putBoolean("never_show_welcome", true).apply();
parentFragment.showHint(R.string.settings_shortcut_pinned_hint, false);
})) {

View File

@ -38,6 +38,7 @@ import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.util.Log;
import androidx.annotation.Nullable;
@ -106,26 +107,33 @@ public class ShortcutUtil {
@SuppressLint("InlinedApi")
private static IntentSender registerReceiver(Context context, Runnable task) {
Log.d(App.TAG, "registerReceiver called");
if (task == null) return null;
Log.d(App.TAG, "registerReceiver: task=" + task);
var uuid = UUID.randomUUID().toString();
var filter = new IntentFilter(uuid);
var permission = "android.permission.CREATE_USERS";
var receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
Log.d(App.TAG, "registerReceiver: onReceive: " + intent.getAction());
if (!uuid.equals(intent.getAction())) return;
Log.d(App.TAG, "registerReceiver: onReceive: unregisterReceiver");
context.unregisterReceiver(this);
Log.d(App.TAG, "registerReceiver: onReceive: task.run()");
task.run();
defaultLauncherPackageName = getDefaultLauncherPackageName(context);
shortcutPinned = true;
Log.d(App.TAG, "registerReceiver: onReceive: shortcutPinned=" + shortcutPinned + " defaultLauncherPackageName=" + defaultLauncherPackageName);
}
};
Log.d(App.TAG, "registerReceiver: filterAction=" + filter.getAction(0) + " permission=" + permission + " receiver=" + receiver);
context.registerReceiver(receiver, filter, permission,
null/* main thread */, Context.RECEIVER_NOT_EXPORTED);
var intent = new Intent(uuid);
int flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
Log.d(App.TAG, "registerReceiver: intent=" + intent + " flags=" + flags);
return PendingIntent.getBroadcast(context, 0, intent, flags).getIntentSender();
}
@ -194,6 +202,10 @@ public class ShortcutUtil {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
var resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
return resolveInfo.activityInfo.packageName;
if (resolveInfo != null) {
return resolveInfo.activityInfo.packageName;
} else {
return null;
}
}
}