1 /* 2 * Copyright (C) 2019 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 23 import android.app.KeyguardManager; 24 import android.content.Context; 25 import android.database.ContentObserver; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.provider.Settings; 31 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.internal.widget.LockPatternUtils; 35 import com.android.settings.R; 36 import com.android.settings.core.TogglePreferenceController; 37 import com.android.settings.overlay.FeatureFactory; 38 import com.android.settingslib.RestrictedLockUtils; 39 import com.android.settingslib.RestrictedLockUtilsInternal; 40 import com.android.settingslib.RestrictedSwitchPreference; 41 import com.android.settingslib.core.lifecycle.LifecycleObserver; 42 import com.android.settingslib.core.lifecycle.events.OnStart; 43 import com.android.settingslib.core.lifecycle.events.OnStop; 44 45 /** 46 * The controller of the sensitive notifications. 47 */ 48 public class RedactNotificationPreferenceController extends TogglePreferenceController implements 49 LifecycleObserver, OnStart, OnStop { 50 private static final String TAG = "LockScreenNotifPref"; 51 52 static final String KEY_LOCKSCREEN_REDACT = "lock_screen_redact"; 53 static final String KEY_LOCKSCREEN_WORK_PROFILE_REDACT = "lock_screen_work_redact"; 54 55 private UserManager mUm; 56 private KeyguardManager mKm; 57 int mProfileUserId; 58 private RestrictedSwitchPreference mPreference; 59 private ContentObserver mContentObserver = 60 new ContentObserver(new Handler(Looper.getMainLooper())) { 61 @Override 62 public void onChange(boolean selfChange) { 63 if (mPreference != null) { 64 mPreference.setEnabled( 65 getAvailabilityStatus() != DISABLED_DEPENDENT_SETTING); 66 } 67 } 68 }; 69 RedactNotificationPreferenceController(Context context, String settingKey)70 public RedactNotificationPreferenceController(Context context, String settingKey) { 71 super(context, settingKey); 72 73 mUm = context.getSystemService(UserManager.class); 74 mKm = context.getSystemService(KeyguardManager.class); 75 76 mProfileUserId = UserHandle.myUserId(); 77 final int[] profileIds = mUm.getProfileIdsWithDisabled(UserHandle.myUserId()); 78 79 for (int profileId : profileIds) { 80 if (profileId != UserHandle.myUserId()) { 81 mProfileUserId = profileId; 82 } 83 } 84 } 85 86 @Override displayPreference(PreferenceScreen screen)87 public void displayPreference(PreferenceScreen screen) { 88 super.displayPreference(screen); 89 mPreference = screen.findPreference(getPreferenceKey()); 90 91 int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey()) 92 ? UserHandle.myUserId() : mProfileUserId; 93 if (userId != UserHandle.USER_NULL) { 94 mPreference.setDisabledByAdmin(getEnforcedAdmin(userId)); 95 } 96 } 97 98 @Override isChecked()99 public boolean isChecked() { 100 int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey()) 101 ? UserHandle.myUserId() : mProfileUserId; 102 103 return getAllowPrivateNotifications(userId); 104 } 105 106 @Override setChecked(boolean isChecked)107 public boolean setChecked(boolean isChecked) { 108 int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey()) 109 ? UserHandle.myUserId() : mProfileUserId; 110 111 Settings.Secure.putIntForUser(mContext.getContentResolver(), 112 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, isChecked ? 1 : 0, userId); 113 return true; 114 } 115 116 @Override getAvailabilityStatus()117 public int getAvailabilityStatus() { 118 // hide work profile setting if no work profile 119 if (KEY_LOCKSCREEN_WORK_PROFILE_REDACT.equals(getPreferenceKey()) 120 && mProfileUserId == UserHandle.myUserId()) { 121 return CONDITIONALLY_UNAVAILABLE; 122 } 123 124 int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey()) 125 ? UserHandle.myUserId() : mProfileUserId; 126 127 // hide if lockscreen isn't secure for this user 128 final LockPatternUtils utils = FeatureFactory.getFactory(mContext) 129 .getSecurityFeatureProvider() 130 .getLockPatternUtils(mContext); 131 if (!utils.isSecure(userId)) { 132 return CONDITIONALLY_UNAVAILABLE; 133 } 134 135 // all notifs hidden? disabled 136 if (!getLockscreenNotificationsEnabled(userId)) { 137 return DISABLED_DEPENDENT_SETTING; 138 } 139 140 // specifically the work profile setting requires the work profile to be unlocked 141 if (KEY_LOCKSCREEN_WORK_PROFILE_REDACT.equals(getPreferenceKey())) { 142 if (mKm.isDeviceLocked(mProfileUserId)) { 143 return DISABLED_DEPENDENT_SETTING; 144 } 145 } 146 147 return AVAILABLE; 148 } 149 150 @Override getSliceHighlightMenuRes()151 public int getSliceHighlightMenuRes() { 152 return R.string.menu_key_notifications; 153 } 154 155 @Override onStart()156 public void onStart() { 157 mContext.getContentResolver().registerContentObserver( 158 Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS), 159 false /* notifyForDescendants */, mContentObserver); 160 } 161 162 @Override onStop()163 public void onStop() { 164 mContext.getContentResolver().unregisterContentObserver(mContentObserver); 165 } 166 getEnforcedAdmin(int userId)167 private RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin(int userId) { 168 RestrictedLockUtils.EnforcedAdmin admin = 169 RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled( 170 mContext, KEYGUARD_DISABLE_SECURE_NOTIFICATIONS, userId); 171 if (admin != null) { 172 return admin; 173 } 174 admin = RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled( 175 mContext, KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS, userId); 176 return admin; 177 } 178 getAllowPrivateNotifications(int userId)179 private boolean getAllowPrivateNotifications(int userId) { 180 return Settings.Secure.getIntForUser(mContext.getContentResolver(), 181 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, userId) != 0 182 && getEnforcedAdmin(userId) == null; 183 } 184 getLockscreenNotificationsEnabled(int userId)185 private boolean getLockscreenNotificationsEnabled(int userId) { 186 return Settings.Secure.getIntForUser(mContext.getContentResolver(), 187 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1, userId) != 0; 188 } 189 } 190