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