blob: f43445ba5dd88dbde13d869304e56c1ef41b3715 [file] [log] [blame]
jackqdyuleie407e672017-05-05 16:07:48 -07001/*
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
17package com.android.settings.dashboard;
18
19import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20
21import android.app.Activity;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.RestrictionsManager;
27import android.os.Bundle;
28import android.os.PersistableBundle;
29import android.os.UserHandle;
30import android.os.UserManager;
31import android.view.View;
32import android.widget.TextView;
33
Fan Zhang23f8d592018-08-28 15:11:40 -070034import androidx.appcompat.app.AlertDialog;
35
jackqdyuleie407e672017-05-05 16:07:48 -070036import com.android.settings.R;
37import com.android.settings.RestrictedSettingsFragment;
Fan Zhangc7162cd2018-06-18 15:21:41 -070038import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070039import com.android.settingslib.RestrictedLockUtilsInternal;
jackqdyuleie407e672017-05-05 16:07:48 -070040
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
Tony Mantler1d583e12017-06-13 13:09:25 -070054 * {@link com.android.settingslib.core.AbstractPreferenceController} in this fragment.
jackqdyuleie407e672017-05-05 16:07:48 -070055 */
56public 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;
jackqdyuleie407e672017-05-05 16:07:48 -070074 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 };
arangelov93081542017-10-27 13:51:28 +010090 private AlertDialog mActionDisabledDialog;
jackqdyuleie407e672017-05-05 16:07:48 -070091
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 */
98 public RestrictedDashboardFragment(String restrictionKey) {
99 mRestrictionKey = restrictionKey;
100 }
101
102 @Override
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
121 public void onActivityCreated(Bundle savedInstanceState) {
122 super.onActivityCreated(savedInstanceState);
jackqdyuleie407e672017-05-05 16:07:48 -0700123 mEmptyTextView = initEmptyTextView();
124 }
125
126 @Override
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
137 public void onResume() {
138 super.onResume();
139
140 if (shouldBeProviderProtected(mRestrictionKey)) {
141 ensurePin();
142 }
143 }
144
145 @Override
146 public void onDestroy() {
147 getActivity().unregisterReceiver(mScreenOffReceiver);
148 super.onDestroy();
149 }
150
151 @Override
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
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 */
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
194 protected boolean hasChallengeSucceeded() {
195 return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested;
196 }
197
198 /**
199 * Returns true if this restrictions key is locked down.
200 */
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
jackqdyuleie407e672017-05-05 16:07:48 -0700210 protected TextView initEmptyTextView() {
211 TextView emptyView = (TextView) getActivity().findViewById(android.R.id.empty);
212 return emptyView;
213 }
214
215 public EnforcedAdmin getRestrictionEnforcedAdmin() {
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700216 mEnforcedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getActivity(),
jackqdyuleie407e672017-05-05 16:07:48 -0700217 mRestrictionKey, UserHandle.myUserId());
Philip P. Moltmann035ea3b2018-10-08 10:46:04 -0700218 if (mEnforcedAdmin != null && mEnforcedAdmin.user == null) {
219 mEnforcedAdmin.user = UserHandle.of(UserHandle.myUserId());
jackqdyuleie407e672017-05-05 16:07:48 -0700220 }
221 return mEnforcedAdmin;
222 }
223
224 public TextView getEmptyTextView() {
225 return mEmptyTextView;
226 }
227
228 @Override
229 protected void onDataSetChanged() {
230 highlightPreferenceIfNeeded();
arangelov93081542017-10-27 13:51:28 +0100231 if (isUiRestrictedByOnlyAdmin()
232 && (mActionDisabledDialog == null || !mActionDisabledDialog.isShowing())) {
jackqdyuleie407e672017-05-05 16:07:48 -0700233 final EnforcedAdmin admin = getRestrictionEnforcedAdmin();
arangelov93081542017-10-27 13:51:28 +0100234 mActionDisabledDialog = new ActionDisabledByAdminDialogHelper(getActivity())
235 .prepareDialogBuilder(mRestrictionKey, admin)
236 .setOnDismissListener(__ -> getActivity().finish())
237 .show();
238 setEmptyView(new View(getContext()));
jackqdyuleie407e672017-05-05 16:07:48 -0700239 } else if (mEmptyTextView != null) {
240 setEmptyView(mEmptyTextView);
241 }
242 super.onDataSetChanged();
243 }
244
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 */
252 protected boolean isUiRestricted() {
253 return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded()
254 || (!mIsAdminUser && mOnlyAvailableForAdmins);
255 }
256
257 protected boolean isUiRestrictedByOnlyAdmin() {
258 return isUiRestricted() && !mUserManager.hasBaseUserRestriction(mRestrictionKey,
259 UserHandle.of(UserHandle.myUserId())) && (mIsAdminUser || !mOnlyAvailableForAdmins);
260 }
261}