1 /* 2 * Copyright (C) 2014 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.settings.notification; 18 19 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS; 20 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS; 21 import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS; 22 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS; 23 24 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 25 26 import android.app.settings.SettingsEnums; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.os.Bundle; 30 import android.os.UserManager; 31 import android.provider.Settings; 32 import android.view.LayoutInflater; 33 import android.view.View; 34 import android.view.ViewGroup; 35 import android.widget.RadioButton; 36 import android.widget.RadioGroup; 37 import android.widget.TextView; 38 39 import com.android.settings.R; 40 import com.android.settings.RestrictedRadioButton; 41 import com.android.settings.SettingsActivity; 42 import com.android.settings.SettingsPreferenceFragment; 43 import com.android.settings.SetupRedactionInterstitial; 44 import com.android.settings.SetupWizardUtils; 45 import com.android.settings.Utils; 46 import com.android.settingslib.RestrictedLockUtilsInternal; 47 48 import com.google.android.setupcompat.template.FooterBarMixin; 49 import com.google.android.setupcompat.template.FooterButton; 50 import com.google.android.setupcompat.util.WizardManagerHelper; 51 import com.google.android.setupdesign.GlifLayout; 52 import com.google.android.setupdesign.util.ThemeHelper; 53 54 public class RedactionInterstitial extends SettingsActivity { 55 56 @Override getIntent()57 public Intent getIntent() { 58 Intent modIntent = new Intent(super.getIntent()); 59 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, RedactionInterstitialFragment.class.getName()); 60 return modIntent; 61 } 62 63 @Override isValidFragment(String fragmentName)64 protected boolean isValidFragment(String fragmentName) { 65 return RedactionInterstitialFragment.class.getName().equals(fragmentName); 66 } 67 68 @Override onCreate(Bundle savedInstance)69 protected void onCreate(Bundle savedInstance) { 70 setTheme(SetupWizardUtils.getTheme(this, getIntent())); 71 ThemeHelper.trySetDynamicColor(this); 72 super.onCreate(savedInstance); 73 findViewById(R.id.content_parent).setFitsSystemWindows(false); 74 } 75 76 @Override isToolbarEnabled()77 protected boolean isToolbarEnabled() { 78 return false; 79 } 80 81 /** 82 * Create an intent for launching RedactionInterstitial. 83 * 84 * @return An intent to launch the activity is if is available, @null if the activity is not 85 * available to be launched. 86 */ createStartIntent(Context ctx, int userId)87 public static Intent createStartIntent(Context ctx, int userId) { 88 return new Intent(ctx, RedactionInterstitial.class) 89 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, 90 UserManager.get(ctx).isManagedProfile(userId) 91 ? R.string.lock_screen_notifications_interstitial_title_profile 92 : R.string.lock_screen_notifications_interstitial_title) 93 .putExtra(Intent.EXTRA_USER_ID, userId); 94 } 95 96 public static class RedactionInterstitialFragment extends SettingsPreferenceFragment 97 implements RadioGroup.OnCheckedChangeListener { 98 99 private RadioGroup mRadioGroup; 100 private RestrictedRadioButton mShowAllButton; 101 private RestrictedRadioButton mRedactSensitiveButton; 102 private int mUserId; 103 104 @Override getMetricsCategory()105 public int getMetricsCategory() { 106 return SettingsEnums.NOTIFICATION_REDACTION; 107 } 108 109 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)110 public View onCreateView(LayoutInflater inflater, ViewGroup container, 111 Bundle savedInstanceState) { 112 return inflater.inflate(R.layout.redaction_interstitial, container, false); 113 } 114 115 @Override onViewCreated(View view, Bundle savedInstanceState)116 public void onViewCreated(View view, Bundle savedInstanceState) { 117 super.onViewCreated(view, savedInstanceState); 118 mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_group); 119 mShowAllButton = (RestrictedRadioButton) view.findViewById(R.id.show_all); 120 mRedactSensitiveButton = 121 (RestrictedRadioButton) view.findViewById(R.id.redact_sensitive); 122 123 mRadioGroup.setOnCheckedChangeListener(this); 124 mUserId = Utils.getUserIdFromBundle( 125 getContext(), getActivity().getIntent().getExtras()); 126 if (UserManager.get(getContext()).isManagedProfile(mUserId)) { 127 ((TextView) view.findViewById(R.id.sud_layout_description)) 128 .setText(R.string.lock_screen_notifications_interstitial_message_profile); 129 mShowAllButton.setText(R.string.lock_screen_notifications_summary_show_profile); 130 mRedactSensitiveButton 131 .setText(R.string.lock_screen_notifications_summary_hide_profile); 132 133 ((RadioButton) view.findViewById(R.id.hide_all)).setVisibility(View.GONE); 134 } 135 136 final GlifLayout layout = view.findViewById(R.id.setup_wizard_layout); 137 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class); 138 mixin.setPrimaryButton( 139 new FooterButton.Builder(getContext()) 140 .setText(R.string.app_notifications_dialog_done) 141 .setListener(this::onDoneButtonClicked) 142 .setButtonType(FooterButton.ButtonType.NEXT) 143 .setTheme(R.style.SudGlifButton_Primary) 144 .build() 145 ); 146 } 147 onDoneButtonClicked(View view)148 private void onDoneButtonClicked(View view) { 149 // If the activity starts by Setup Wizard, then skip disable component which avoids the 150 // framework force closing all activities on the same task when the system is busy. 151 if (!WizardManagerHelper.isAnySetupWizard(getIntent())) { 152 SetupRedactionInterstitial.setEnabled(getContext(), false); 153 } 154 final RedactionInterstitial activity = (RedactionInterstitial) getActivity(); 155 if (activity != null) { 156 activity.setResult(RESULT_CANCELED, null); 157 finish(); 158 } 159 } 160 161 @Override onResume()162 public void onResume() { 163 super.onResume(); 164 // Disable buttons according to policy. 165 166 checkNotificationFeaturesAndSetDisabled(mShowAllButton, 167 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS | 168 KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS); 169 checkNotificationFeaturesAndSetDisabled(mRedactSensitiveButton, 170 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS); 171 loadFromSettings(); 172 } 173 checkNotificationFeaturesAndSetDisabled(RestrictedRadioButton button, int keyguardNotifications)174 private void checkNotificationFeaturesAndSetDisabled(RestrictedRadioButton button, 175 int keyguardNotifications) { 176 EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled( 177 getActivity(), keyguardNotifications, mUserId); 178 button.setDisabledByAdmin(admin); 179 } 180 loadFromSettings()181 private void loadFromSettings() { 182 final boolean managedProfile = UserManager.get(getContext()).isManagedProfile(mUserId); 183 // Hiding all notifications is device-wide setting, managed profiles can only set 184 // whether their notifications are show in full or redacted. 185 final boolean showNotifications = managedProfile || Settings.Secure.getIntForUser( 186 getContentResolver(), LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, mUserId) != 0; 187 final boolean showUnredacted = Settings.Secure.getIntForUser( 188 getContentResolver(), LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mUserId) != 0; 189 190 int checkedButtonId = R.id.hide_all; 191 if (showNotifications) { 192 if (showUnredacted && !mShowAllButton.isDisabledByAdmin()) { 193 checkedButtonId = R.id.show_all; 194 } else if (!mRedactSensitiveButton.isDisabledByAdmin()) { 195 checkedButtonId = R.id.redact_sensitive; 196 } 197 } 198 199 mRadioGroup.check(checkedButtonId); 200 } 201 202 @Override onCheckedChanged(RadioGroup group, int checkedId)203 public void onCheckedChanged(RadioGroup group, int checkedId) { 204 final boolean show = (checkedId == R.id.show_all); 205 final boolean enabled = (checkedId != R.id.hide_all); 206 207 Settings.Secure.putIntForUser(getContentResolver(), 208 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0, mUserId); 209 Settings.Secure.putIntForUser(getContentResolver(), 210 LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0, mUserId); 211 212 } 213 } 214 } 215