1 /* 2 * Copyright (C) 2017 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.dashboard; 18 19 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 20 21 import android.app.Activity; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.content.RestrictionsManager; 27 import android.os.Bundle; 28 import android.os.PersistableBundle; 29 import android.os.UserHandle; 30 import android.os.UserManager; 31 import android.view.View; 32 import android.widget.TextView; 33 34 import androidx.appcompat.app.AlertDialog; 35 36 import com.android.settings.R; 37 import com.android.settings.RestrictedSettingsFragment; 38 import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper; 39 import com.android.settingslib.RestrictedLockUtilsInternal; 40 41 /** 42 * Base class for settings screens that should be pin protected when in restricted mode or 43 * that will display an admin support message in case an admin has disabled the options. 44 * The constructor for this class will take the restriction key that this screen should be 45 * locked by. If {@link RestrictionsManager.hasRestrictionsProvider()} and 46 * {@link UserManager.hasUserRestriction()}, then the user will have to enter the restrictions 47 * pin before seeing the Settings screen. 48 * 49 * {@link RestrictionsManager.hasRestrictionsProvider()} returns true, pass in 50 * {@link RESTRICT_IF_OVERRIDABLE} to the constructor instead of a restrictions key. 51 * 52 * This fragment is a replacement of {@link RestrictedSettingsFragment} but extends 53 * from {@link DashboardFragment}, so we could also use 54 * {@link com.android.settingslib.core.AbstractPreferenceController} in this fragment. 55 */ 56 public abstract class RestrictedDashboardFragment extends DashboardFragment { 57 58 protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable"; 59 60 // No RestrictedSettingsFragment screens should use this number in startActivityForResult. 61 private static final int REQUEST_PIN_CHALLENGE = 12309; 62 63 private static final String KEY_CHALLENGE_SUCCEEDED = "chsc"; 64 private static final String KEY_CHALLENGE_REQUESTED = "chrq"; 65 66 // If the restriction PIN is entered correctly. 67 private boolean mChallengeSucceeded; 68 private boolean mChallengeRequested; 69 70 private UserManager mUserManager; 71 private RestrictionsManager mRestrictionsManager; 72 73 private final String mRestrictionKey; 74 private EnforcedAdmin mEnforcedAdmin; 75 private TextView mEmptyTextView; 76 77 private boolean mOnlyAvailableForAdmins = false; 78 private boolean mIsAdminUser; 79 80 // Receiver to clear pin status when the screen is turned off. 81 private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() { 82 @Override 83 public void onReceive(Context context, Intent intent) { 84 if (!mChallengeRequested) { 85 mChallengeSucceeded = false; 86 mChallengeRequested = false; 87 } 88 } 89 }; 90 private AlertDialog mActionDisabledDialog; 91 92 /** 93 * @param restrictionKey The restriction key to check before pin protecting 94 * this settings page. Pass in {@link RESTRICT_IF_OVERRIDABLE} if it should 95 * be protected whenever a restrictions provider is set. Pass in 96 * null if it should never be protected. 97 */ RestrictedDashboardFragment(String restrictionKey)98 public RestrictedDashboardFragment(String restrictionKey) { 99 mRestrictionKey = restrictionKey; 100 } 101 102 @Override onCreate(Bundle icicle)103 public void onCreate(Bundle icicle) { 104 super.onCreate(icicle); 105 106 mRestrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE); 107 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE); 108 mIsAdminUser = mUserManager.isAdminUser(); 109 110 if (icicle != null) { 111 mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false); 112 mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false); 113 } 114 115 IntentFilter offFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF); 116 offFilter.addAction(Intent.ACTION_USER_PRESENT); 117 getActivity().registerReceiver(mScreenOffReceiver, offFilter); 118 } 119 120 @Override onActivityCreated(Bundle savedInstanceState)121 public void onActivityCreated(Bundle savedInstanceState) { 122 super.onActivityCreated(savedInstanceState); 123 mEmptyTextView = initEmptyTextView(); 124 } 125 126 @Override onSaveInstanceState(Bundle outState)127 public void onSaveInstanceState(Bundle outState) { 128 super.onSaveInstanceState(outState); 129 130 if (getActivity().isChangingConfigurations()) { 131 outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested); 132 outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded); 133 } 134 } 135 136 @Override onResume()137 public void onResume() { 138 super.onResume(); 139 140 if (shouldBeProviderProtected(mRestrictionKey)) { 141 ensurePin(); 142 } 143 } 144 145 @Override onDestroy()146 public void onDestroy() { 147 getActivity().unregisterReceiver(mScreenOffReceiver); 148 super.onDestroy(); 149 } 150 151 @Override onActivityResult(int requestCode, int resultCode, Intent data)152 public void onActivityResult(int requestCode, int resultCode, Intent data) { 153 if (requestCode == REQUEST_PIN_CHALLENGE) { 154 if (resultCode == Activity.RESULT_OK) { 155 mChallengeSucceeded = true; 156 mChallengeRequested = false; 157 } else { 158 mChallengeSucceeded = false; 159 } 160 return; 161 } 162 163 super.onActivityResult(requestCode, resultCode, data); 164 } 165 ensurePin()166 private void ensurePin() { 167 if (!mChallengeSucceeded && !mChallengeRequested 168 && mRestrictionsManager.hasRestrictionsProvider()) { 169 Intent intent = mRestrictionsManager.createLocalApprovalIntent(); 170 if (intent != null) { 171 mChallengeRequested = true; 172 mChallengeSucceeded = false; 173 PersistableBundle request = new PersistableBundle(); 174 request.putString(RestrictionsManager.REQUEST_KEY_MESSAGE, 175 getResources().getString(R.string.restr_pin_enter_admin_pin)); 176 intent.putExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE, request); 177 startActivityForResult(intent, REQUEST_PIN_CHALLENGE); 178 } 179 } 180 } 181 182 /** 183 * Returns true if this activity is restricted, but no restrictions provider has been set. 184 * Used to determine if the settings UI should disable UI. 185 */ isRestrictedAndNotProviderProtected()186 protected boolean isRestrictedAndNotProviderProtected() { 187 if (mRestrictionKey == null || RESTRICT_IF_OVERRIDABLE.equals(mRestrictionKey)) { 188 return false; 189 } 190 return mUserManager.hasUserRestriction(mRestrictionKey) 191 && !mRestrictionsManager.hasRestrictionsProvider(); 192 } 193 hasChallengeSucceeded()194 protected boolean hasChallengeSucceeded() { 195 return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested; 196 } 197 198 /** 199 * Returns true if this restrictions key is locked down. 200 */ shouldBeProviderProtected(String restrictionKey)201 protected boolean shouldBeProviderProtected(String restrictionKey) { 202 if (restrictionKey == null) { 203 return false; 204 } 205 boolean restricted = RESTRICT_IF_OVERRIDABLE.equals(restrictionKey) 206 || mUserManager.hasUserRestriction(mRestrictionKey); 207 return restricted && mRestrictionsManager.hasRestrictionsProvider(); 208 } 209 initEmptyTextView()210 protected TextView initEmptyTextView() { 211 TextView emptyView = (TextView) getActivity().findViewById(android.R.id.empty); 212 return emptyView; 213 } 214 getRestrictionEnforcedAdmin()215 public EnforcedAdmin getRestrictionEnforcedAdmin() { 216 mEnforcedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getActivity(), 217 mRestrictionKey, UserHandle.myUserId()); 218 if (mEnforcedAdmin != null && mEnforcedAdmin.user == null) { 219 mEnforcedAdmin.user = UserHandle.of(UserHandle.myUserId()); 220 } 221 return mEnforcedAdmin; 222 } 223 getEmptyTextView()224 public TextView getEmptyTextView() { 225 return mEmptyTextView; 226 } 227 228 @Override onDataSetChanged()229 protected void onDataSetChanged() { 230 highlightPreferenceIfNeeded(); 231 if (isUiRestrictedByOnlyAdmin() 232 && (mActionDisabledDialog == null || !mActionDisabledDialog.isShowing())) { 233 final EnforcedAdmin admin = getRestrictionEnforcedAdmin(); 234 mActionDisabledDialog = new ActionDisabledByAdminDialogHelper(getActivity()) 235 .prepareDialogBuilder(mRestrictionKey, admin) 236 .setOnDismissListener(__ -> getActivity().finish()) 237 .show(); 238 setEmptyView(new View(getContext())); 239 } else if (mEmptyTextView != null) { 240 setEmptyView(mEmptyTextView); 241 } 242 super.onDataSetChanged(); 243 } 244 setIfOnlyAvailableForAdmins(boolean onlyForAdmins)245 public void setIfOnlyAvailableForAdmins(boolean onlyForAdmins) { 246 mOnlyAvailableForAdmins = onlyForAdmins; 247 } 248 249 /** 250 * Returns whether restricted or actionable UI elements should be removed or disabled. 251 */ isUiRestricted()252 protected boolean isUiRestricted() { 253 return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded() 254 || (!mIsAdminUser && mOnlyAvailableForAdmins); 255 } 256 isUiRestrictedByOnlyAdmin()257 protected boolean isUiRestrictedByOnlyAdmin() { 258 return isUiRestricted() && !mUserManager.hasBaseUserRestriction(mRestrictionKey, 259 UserHandle.of(UserHandle.myUserId())) && (mIsAdminUser || !mOnlyAvailableForAdmins); 260 } 261 } 262