Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.Activity; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
| 22 | import android.app.DialogFragment; |
| 23 | import android.app.FragmentManager; |
| 24 | import android.content.ComponentName; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.DialogInterface; |
| 28 | import android.content.Intent; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 30 | import android.preference.ListPreference; |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 31 | import android.preference.Preference; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 32 | import android.preference.PreferenceFragment; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 33 | import android.provider.Settings; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 35 | import com.android.launcher3.graphics.IconShapeOverride; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 36 | import com.android.launcher3.notification.NotificationListener; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 37 | import com.android.launcher3.util.SettingsObserver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 38 | import com.android.launcher3.views.ButtonPreference; |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 39 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 40 | /** |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 41 | * Settings activity for Launcher. Currently implements the following setting: Allow rotation |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 42 | */ |
| 43 | public class SettingsActivity extends Activity { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 44 | |
| 45 | private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging"; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 46 | /** Hidden field Settings.Secure.NOTIFICATION_BADGING */ |
| 47 | public static final String NOTIFICATION_BADGING = "notification_badging"; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 48 | /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */ |
| 49 | private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners"; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 50 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 51 | @Override |
| 52 | protected void onCreate(Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
| 54 | |
Mario Bertschler | 2de2d67 | 2017-06-30 18:51:46 -0700 | [diff] [blame] | 55 | if (savedInstanceState == null) { |
| 56 | // Display the fragment as the main content. |
| 57 | getFragmentManager().beginTransaction() |
| 58 | .replace(android.R.id.content, new LauncherSettingsFragment()) |
| 59 | .commit(); |
| 60 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * This fragment shows the launcher preferences. |
| 65 | */ |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 66 | public static class LauncherSettingsFragment extends PreferenceFragment { |
| 67 | |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 68 | private IconBadgingObserver mIconBadgingObserver; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 69 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 70 | @Override |
| 71 | public void onCreate(Bundle savedInstanceState) { |
| 72 | super.onCreate(savedInstanceState); |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 73 | getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY); |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 74 | addPreferencesFromResource(R.xml.launcher_preferences); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 75 | |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 76 | ContentResolver resolver = getActivity().getContentResolver(); |
| 77 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 78 | ButtonPreference iconBadgingPref = |
| 79 | (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY); |
Hyunyoung Song | e24cb63 | 2017-09-11 11:18:03 -0700 | [diff] [blame] | 80 | if (!Utilities.ATLEAST_OREO) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 81 | getPreferenceScreen().removePreference( |
| 82 | findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY)); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 83 | getPreferenceScreen().removePreference(iconBadgingPref); |
Tony Wickham | c138515 | 2017-08-14 14:31:03 -0700 | [diff] [blame] | 84 | } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) { |
| 85 | getPreferenceScreen().removePreference(iconBadgingPref); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 86 | } else { |
| 87 | // Listen to system notification badge settings while this UI is active. |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 88 | mIconBadgingObserver = new IconBadgingObserver( |
| 89 | iconBadgingPref, resolver, getFragmentManager()); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 90 | mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 91 | } |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 92 | |
| 93 | Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE); |
| 94 | if (iconShapeOverride != null) { |
| 95 | if (IconShapeOverride.isSupported(getActivity())) { |
| 96 | IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride); |
| 97 | } else { |
| 98 | getPreferenceScreen().removePreference(iconShapeOverride); |
| 99 | } |
| 100 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 101 | } |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 102 | |
| 103 | @Override |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 104 | public void onDestroy() { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 105 | if (mIconBadgingObserver != null) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 106 | mIconBadgingObserver.unregister(); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 107 | mIconBadgingObserver = null; |
| 108 | } |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 109 | super.onDestroy(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 114 | * Content observer which listens for system badging setting changes, |
| 115 | * and updates the launcher badging setting subtext accordingly. |
| 116 | */ |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 117 | private static class IconBadgingObserver extends SettingsObserver.Secure |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 118 | implements Preference.OnPreferenceClickListener { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 119 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 120 | private final ButtonPreference mBadgingPref; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 121 | private final ContentResolver mResolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 122 | private final FragmentManager mFragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 123 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 124 | public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver, |
| 125 | FragmentManager fragmentManager) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 126 | super(resolver); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 127 | mBadgingPref = badgingPref; |
| 128 | mResolver = resolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 129 | mFragmentManager = fragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 133 | public void onSettingChanged(boolean enabled) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 134 | int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off; |
| 135 | |
| 136 | boolean serviceEnabled = true; |
| 137 | if (enabled) { |
| 138 | // Check if the listener is enabled or not. |
| 139 | String enabledListeners = |
| 140 | Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS); |
| 141 | ComponentName myListener = |
| 142 | new ComponentName(mBadgingPref.getContext(), NotificationListener.class); |
| 143 | serviceEnabled = enabledListeners != null && |
| 144 | (enabledListeners.contains(myListener.flattenToString()) || |
| 145 | enabledListeners.contains(myListener.flattenToShortString())); |
| 146 | if (!serviceEnabled) { |
| 147 | summary = R.string.title_missing_notification_access; |
| 148 | } |
| 149 | } |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 150 | mBadgingPref.setWidgetFrameVisible(!serviceEnabled); |
| 151 | mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this); |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 152 | mBadgingPref.setSummary(summary); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | @Override |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 157 | public boolean onPreferenceClick(Preference preference) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 158 | new NotificationAccessConfirmation().show(mFragmentManager, "notification_access"); |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 159 | return true; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 163 | public static class NotificationAccessConfirmation |
| 164 | extends DialogFragment implements DialogInterface.OnClickListener { |
| 165 | |
| 166 | @Override |
| 167 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 168 | final Context context = getActivity(); |
| 169 | String msg = context.getString(R.string.msg_missing_notification_access, |
| 170 | context.getString(R.string.derived_app_name)); |
| 171 | return new AlertDialog.Builder(context) |
| 172 | .setTitle(R.string.title_missing_notification_access) |
| 173 | .setMessage(msg) |
| 174 | .setNegativeButton(android.R.string.cancel, null) |
| 175 | .setPositiveButton(R.string.title_change_settings, this) |
| 176 | .create(); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onClick(DialogInterface dialogInterface, int i) { |
| 181 | ComponentName cn = new ComponentName(getActivity(), NotificationListener.class); |
| 182 | Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) |
| 183 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 184 | .putExtra(":settings:fragment_args_key", cn.flattenToString()); |
| 185 | getActivity().startActivity(intent); |
| 186 | } |
| 187 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 188 | } |