|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2014-2015 The CyanogenMod Project |
| 3 | + * SPDX-FileCopyrightText: 2017-2023 The LineageOS Project |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package org.somethingos.somethingsettings.fragments.ui; |
| 8 | + |
| 9 | +import android.Manifest; |
| 10 | +import android.content.Context; |
| 11 | +import android.content.pm.UserInfo; |
| 12 | +import android.os.Bundle; |
| 13 | +import android.os.UserHandle; |
| 14 | +import android.os.UserManager; |
| 15 | +import android.provider.Settings; |
| 16 | +import android.service.controls.ControlsProviderService; |
| 17 | + |
| 18 | +import androidx.preference.CheckBoxPreference; |
| 19 | +import androidx.preference.Preference; |
| 20 | +import androidx.preference.PreferenceCategory; |
| 21 | + |
| 22 | +import org.somethingos.somethingsettings.utils.TelephonyUtils; |
| 23 | + |
| 24 | +import com.android.internal.util.EmergencyAffordanceManager; |
| 25 | +import com.android.settingslib.applications.ServiceListing; |
| 26 | + |
| 27 | +import com.android.internal.util.aospa.PowerMenuConstants; |
| 28 | +import com.android.internal.util.aospa.PowerMenuUtils; |
| 29 | +import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 30 | +import com.android.settings.R; |
| 31 | +import com.android.settings.SettingsPreferenceFragment; |
| 32 | +import com.android.internal.lineage.app.LineageGlobalActions; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +import static com.android.internal.util.aospa.PowerMenuConstants.*; |
| 37 | + |
| 38 | +public class PowerMenuActions extends SettingsPreferenceFragment { |
| 39 | + final static String TAG = "PowerMenuActions"; |
| 40 | + |
| 41 | + private static final String CATEGORY_POWER_MENU_ITEMS = "power_menu_items"; |
| 42 | + |
| 43 | + private PreferenceCategory mPowerMenuItemsCategory; |
| 44 | + |
| 45 | + private CheckBoxPreference mScreenshotPref; |
| 46 | + private CheckBoxPreference mAirplanePref; |
| 47 | + private CheckBoxPreference mUsersPref; |
| 48 | + private CheckBoxPreference mBugReportPref; |
| 49 | + private CheckBoxPreference mEmergencyPref; |
| 50 | + private CheckBoxPreference mDeviceControlsPref; |
| 51 | + private CheckBoxPreference mRestartSystemUIPref; |
| 52 | + |
| 53 | + private LineageGlobalActions mLineageGlobalActions; |
| 54 | + |
| 55 | + private EmergencyAffordanceManager mEmergencyAffordanceManager; |
| 56 | + private boolean mForceEmergCheck = false; |
| 57 | + |
| 58 | + Context mContext; |
| 59 | + private UserManager mUserManager; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onCreate(Bundle savedInstanceState) { |
| 63 | + super.onCreate(savedInstanceState); |
| 64 | + |
| 65 | + addPreferencesFromResource(R.xml.power_menu_actions_settings); |
| 66 | + requireActivity().setTitle(R.string.power_menu_title); |
| 67 | + mContext = requireActivity().getApplicationContext(); |
| 68 | + mUserManager = UserManager.get(mContext); |
| 69 | + mLineageGlobalActions = mContext.getSystemService(LineageGlobalActions.class); |
| 70 | + mEmergencyAffordanceManager = new EmergencyAffordanceManager(mContext); |
| 71 | + |
| 72 | + mPowerMenuItemsCategory = findPreference(CATEGORY_POWER_MENU_ITEMS); |
| 73 | + |
| 74 | + for (String action : PowerMenuConstants.getAllActions()) { |
| 75 | + if (action.equals(GLOBAL_ACTION_KEY_SCREENSHOT)) { |
| 76 | + mScreenshotPref = findPreference(GLOBAL_ACTION_KEY_SCREENSHOT); |
| 77 | + } else if (action.equals(GLOBAL_ACTION_KEY_AIRPLANE)) { |
| 78 | + mAirplanePref = findPreference(GLOBAL_ACTION_KEY_AIRPLANE); |
| 79 | + } else if (action.equals(GLOBAL_ACTION_KEY_USERS)) { |
| 80 | + mUsersPref = findPreference(GLOBAL_ACTION_KEY_USERS); |
| 81 | + } else if (action.equals(GLOBAL_ACTION_KEY_BUGREPORT)) { |
| 82 | + mBugReportPref = findPreference(GLOBAL_ACTION_KEY_BUGREPORT); |
| 83 | + } else if (action.equals(GLOBAL_ACTION_KEY_EMERGENCY)) { |
| 84 | + mEmergencyPref = findPreference(GLOBAL_ACTION_KEY_EMERGENCY); |
| 85 | + } else if (action.equals(GLOBAL_ACTION_KEY_DEVICECONTROLS)) { |
| 86 | + mDeviceControlsPref = findPreference(GLOBAL_ACTION_KEY_DEVICECONTROLS); |
| 87 | + } else if (action.equals(GLOBAL_ACTION_KEY_RESTART_SYSTEMUI)) { |
| 88 | + mRestartSystemUIPref = findPreference(GLOBAL_ACTION_KEY_RESTART_SYSTEMUI); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (!TelephonyUtils.isVoiceCapable(requireActivity())) { |
| 93 | + mPowerMenuItemsCategory.removePreference(mEmergencyPref); |
| 94 | + mEmergencyPref = null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onStart() { |
| 100 | + super.onStart(); |
| 101 | + |
| 102 | + if (mScreenshotPref != null) { |
| 103 | + mScreenshotPref.setChecked(mLineageGlobalActions.userConfigContains( |
| 104 | + GLOBAL_ACTION_KEY_SCREENSHOT)); |
| 105 | + } |
| 106 | + |
| 107 | + if (mAirplanePref != null) { |
| 108 | + mAirplanePref.setChecked(mLineageGlobalActions.userConfigContains( |
| 109 | + GLOBAL_ACTION_KEY_AIRPLANE)); |
| 110 | + } |
| 111 | + |
| 112 | + if (mUsersPref != null) { |
| 113 | + if (!UserHandle.MU_ENABLED || !UserManager.supportsMultipleUsers()) { |
| 114 | + mPowerMenuItemsCategory.removePreference(mUsersPref); |
| 115 | + mUsersPref = null; |
| 116 | + } else { |
| 117 | + List<UserInfo> users = mUserManager.getUsers(); |
| 118 | + boolean enabled = (users.size() > 1); |
| 119 | + mUsersPref.setChecked(mLineageGlobalActions.userConfigContains( |
| 120 | + GLOBAL_ACTION_KEY_USERS) && enabled); |
| 121 | + mUsersPref.setEnabled(enabled); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (mBugReportPref != null) { |
| 126 | + mBugReportPref.setChecked(mLineageGlobalActions.userConfigContains( |
| 127 | + GLOBAL_ACTION_KEY_BUGREPORT)); |
| 128 | + } |
| 129 | + |
| 130 | + if (mEmergencyPref != null) { |
| 131 | + mForceEmergCheck = mEmergencyAffordanceManager.needsEmergencyAffordance(); |
| 132 | + mEmergencyPref.setChecked(mLineageGlobalActions.userConfigContains( |
| 133 | + GLOBAL_ACTION_KEY_EMERGENCY) || mForceEmergCheck); |
| 134 | + mEmergencyPref.setEnabled(!mForceEmergCheck); |
| 135 | + } |
| 136 | + |
| 137 | + if (mDeviceControlsPref != null) { |
| 138 | + mDeviceControlsPref.setChecked(mLineageGlobalActions.userConfigContains( |
| 139 | + GLOBAL_ACTION_KEY_DEVICECONTROLS)); |
| 140 | + |
| 141 | + // Enable preference if any device control app is installed |
| 142 | + ServiceListing serviceListing = new ServiceListing.Builder(mContext) |
| 143 | + .setIntentAction(ControlsProviderService.SERVICE_CONTROLS) |
| 144 | + .setPermission(Manifest.permission.BIND_CONTROLS) |
| 145 | + .setNoun("Controls Provider") |
| 146 | + .setSetting("controls_providers") |
| 147 | + .setTag("controls_providers") |
| 148 | + .build(); |
| 149 | + serviceListing.addCallback( |
| 150 | + services -> mDeviceControlsPref.setEnabled(!services.isEmpty())); |
| 151 | + serviceListing.reload(); |
| 152 | + } |
| 153 | + |
| 154 | + if (mRestartSystemUIPref != null) { |
| 155 | + mRestartSystemUIPref.setChecked(mLineageGlobalActions.userConfigContains( |
| 156 | + GLOBAL_ACTION_KEY_RESTART_SYSTEMUI)); |
| 157 | + } |
| 158 | + |
| 159 | + updatePreferences(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onResume() { |
| 164 | + super.onResume(); |
| 165 | + updatePreferences(); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public boolean onPreferenceTreeClick(Preference preference) { |
| 170 | + boolean value; |
| 171 | + |
| 172 | + if (preference == mScreenshotPref) { |
| 173 | + value = mScreenshotPref.isChecked(); |
| 174 | + mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_SCREENSHOT); |
| 175 | + |
| 176 | + } else if (preference == mAirplanePref) { |
| 177 | + value = mAirplanePref.isChecked(); |
| 178 | + mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_AIRPLANE); |
| 179 | + |
| 180 | + } else if (preference == mUsersPref) { |
| 181 | + value = mUsersPref.isChecked(); |
| 182 | + mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_USERS); |
| 183 | + |
| 184 | + } else if (preference == mBugReportPref) { |
| 185 | + value = mBugReportPref.isChecked(); |
| 186 | + mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_BUGREPORT); |
| 187 | + Settings.Global.putInt(getContentResolver(), |
| 188 | + Settings.Global.BUGREPORT_IN_POWER_MENU, value ? 1 : 0); |
| 189 | + |
| 190 | + } else if (preference == mEmergencyPref) { |
| 191 | + value = mEmergencyPref.isChecked(); |
| 192 | + mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_EMERGENCY); |
| 193 | + |
| 194 | + } else if (preference == mDeviceControlsPref) { |
| 195 | + value = mDeviceControlsPref.isChecked(); |
| 196 | + mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_DEVICECONTROLS); |
| 197 | + |
| 198 | + } else if (preference == mRestartSystemUIPref) { |
| 199 | + value = mRestartSystemUIPref.isChecked(); |
| 200 | + mLineageGlobalActions.updateUserConfig(value, GLOBAL_ACTION_KEY_RESTART_SYSTEMUI); |
| 201 | + |
| 202 | + } else { |
| 203 | + return super.onPreferenceTreeClick(preference); |
| 204 | + } |
| 205 | + return true; |
| 206 | + } |
| 207 | + |
| 208 | + private void updatePreferences() { |
| 209 | + UserInfo currentUser = mUserManager.getUserInfo(UserHandle.myUserId()); |
| 210 | + boolean developmentSettings = Settings.Global.getInt( |
| 211 | + getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1; |
| 212 | + boolean bugReport = Settings.Global.getInt( |
| 213 | + getContentResolver(), Settings.Global.BUGREPORT_IN_POWER_MENU, 0) == 1; |
| 214 | + boolean isPrimaryUser = currentUser == null || currentUser.isPrimary(); |
| 215 | + if (mBugReportPref != null) { |
| 216 | + mBugReportPref.setEnabled(developmentSettings && isPrimaryUser); |
| 217 | + if (!developmentSettings) { |
| 218 | + mBugReportPref.setChecked(false); |
| 219 | + mBugReportPref.setSummary(R.string.power_menu_bug_report_devoptions_unavailable); |
| 220 | + } else if (!isPrimaryUser) { |
| 221 | + mBugReportPref.setChecked(false); |
| 222 | + mBugReportPref.setSummary(R.string.power_menu_bug_report_unavailable_for_user); |
| 223 | + } else { |
| 224 | + mBugReportPref.setChecked(bugReport); |
| 225 | + mBugReportPref.setSummary(null); |
| 226 | + } |
| 227 | + } |
| 228 | + if (mEmergencyPref != null) { |
| 229 | + if (mForceEmergCheck) { |
| 230 | + mEmergencyPref.setSummary(R.string.power_menu_emergency_affordance_enabled); |
| 231 | + } else { |
| 232 | + mEmergencyPref.setSummary(null); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public int getMetricsCategory() { |
| 239 | + return -1; |
| 240 | + } |
| 241 | +} |
0 commit comments