• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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;
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.annotation.VisibleForTesting;
35 import androidx.appcompat.app.AlertDialog;
36 
37 import com.android.settings.dashboard.RestrictedDashboardFragment;
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  * If this settings screen should be pin protected whenever
50  * {@link RestrictionsManager.hasRestrictionsProvider()} returns true, pass in
51  * {@link RESTRICT_IF_OVERRIDABLE} to the constructor instead of a restrictions key.
52  *
53  * @deprecated Use {@link RestrictedDashboardFragment} instead
54  */
55 @Deprecated
56 public abstract class RestrictedSettingsFragment extends SettingsPreferenceFragment {
57 
58     protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable";
59 
60     // No RestrictedSettingsFragment screens should use this number in startActivityForResult.
61     @VisibleForTesting 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 
91     @VisibleForTesting
92     AlertDialog mActionDisabledDialog;
93 
94     /**
95      * @param restrictionKey The restriction key to check before pin protecting
96      *            this settings page. Pass in {@link RESTRICT_IF_OVERRIDABLE} if it should
97      *            be protected whenever a restrictions provider is set. Pass in
98      *            null if it should never be protected.
99      */
RestrictedSettingsFragment(String restrictionKey)100     public RestrictedSettingsFragment(String restrictionKey) {
101         mRestrictionKey = restrictionKey;
102     }
103 
104     @Override
onCreate(Bundle icicle)105     public void onCreate(Bundle icicle) {
106         super.onCreate(icicle);
107 
108         mRestrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
109         mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
110         mIsAdminUser = mUserManager.isAdminUser();
111 
112         if (icicle != null) {
113             mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
114             mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
115         }
116 
117         IntentFilter offFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
118         offFilter.addAction(Intent.ACTION_USER_PRESENT);
119         getActivity().registerReceiver(mScreenOffReceiver, offFilter);
120     }
121 
122     @Override
onActivityCreated(Bundle savedInstanceState)123     public void onActivityCreated(Bundle savedInstanceState) {
124         super.onActivityCreated(savedInstanceState);
125         mEmptyTextView = initEmptyTextView();
126     }
127 
128     @Override
onSaveInstanceState(Bundle outState)129     public void onSaveInstanceState(Bundle outState) {
130         super.onSaveInstanceState(outState);
131 
132         if (getActivity().isChangingConfigurations()) {
133             outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
134             outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
135         }
136     }
137 
138     @Override
onResume()139     public void onResume() {
140         super.onResume();
141 
142         if (shouldBeProviderProtected(mRestrictionKey)) {
143             ensurePin();
144         }
145     }
146 
147     @Override
onDestroy()148     public void onDestroy() {
149         getActivity().unregisterReceiver(mScreenOffReceiver);
150         super.onDestroy();
151     }
152 
153     @Override
onActivityResult(int requestCode, int resultCode, Intent data)154     public void onActivityResult(int requestCode, int resultCode, Intent data) {
155         if (requestCode == REQUEST_PIN_CHALLENGE) {
156             if (resultCode == Activity.RESULT_OK) {
157                 mChallengeSucceeded = true;
158                 mChallengeRequested = false;
159                 if (mActionDisabledDialog != null && mActionDisabledDialog.isShowing()) {
160                     mActionDisabledDialog.setOnDismissListener(null);
161                     mActionDisabledDialog.dismiss();
162                 }
163             } else {
164                 mChallengeSucceeded = false;
165             }
166             return;
167         }
168 
169         super.onActivityResult(requestCode, resultCode, data);
170     }
171 
ensurePin()172     private void ensurePin() {
173         if (!mChallengeSucceeded && !mChallengeRequested
174                 && mRestrictionsManager.hasRestrictionsProvider()) {
175             Intent intent = mRestrictionsManager.createLocalApprovalIntent();
176             if (intent != null) {
177                 mChallengeRequested = true;
178                 mChallengeSucceeded = false;
179                 PersistableBundle request = new PersistableBundle();
180                 request.putString(RestrictionsManager.REQUEST_KEY_MESSAGE,
181                         getResources().getString(R.string.restr_pin_enter_admin_pin));
182                 intent.putExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE, request);
183                 startActivityForResult(intent, REQUEST_PIN_CHALLENGE);
184             }
185         }
186     }
187 
188     /**
189      * Returns true if this activity is restricted, but no restrictions provider has been set.
190      * Used to determine if the settings UI should disable UI.
191      */
isRestrictedAndNotProviderProtected()192     protected boolean isRestrictedAndNotProviderProtected() {
193         if (mRestrictionKey == null || RESTRICT_IF_OVERRIDABLE.equals(mRestrictionKey)) {
194             return false;
195         }
196         return mUserManager.hasUserRestriction(mRestrictionKey)
197                 && !mRestrictionsManager.hasRestrictionsProvider();
198     }
199 
hasChallengeSucceeded()200     protected boolean hasChallengeSucceeded() {
201         return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested;
202     }
203 
204     /**
205      * Returns true if this restrictions key is locked down.
206      */
shouldBeProviderProtected(String restrictionKey)207     protected boolean shouldBeProviderProtected(String restrictionKey) {
208         if (restrictionKey == null) {
209             return false;
210         }
211         boolean restricted = RESTRICT_IF_OVERRIDABLE.equals(restrictionKey)
212                 || mUserManager.hasUserRestriction(mRestrictionKey);
213         return restricted && mRestrictionsManager.hasRestrictionsProvider();
214     }
215 
initEmptyTextView()216     protected TextView initEmptyTextView() {
217         TextView emptyView = (TextView) getActivity().findViewById(android.R.id.empty);
218         return emptyView;
219     }
220 
getRestrictionEnforcedAdmin()221     public EnforcedAdmin getRestrictionEnforcedAdmin() {
222         mEnforcedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getActivity(),
223                 mRestrictionKey, UserHandle.myUserId());
224         if (mEnforcedAdmin != null && mEnforcedAdmin.user == null) {
225             mEnforcedAdmin.user = UserHandle.of(UserHandle.myUserId());
226         }
227         return mEnforcedAdmin;
228     }
229 
getEmptyTextView()230     public TextView getEmptyTextView() {
231         return mEmptyTextView;
232     }
233 
234     @Override
onDataSetChanged()235     protected void onDataSetChanged() {
236         highlightPreferenceIfNeeded();
237         if (isUiRestrictedByOnlyAdmin()
238                 && (mActionDisabledDialog == null || !mActionDisabledDialog.isShowing())) {
239             final EnforcedAdmin admin = getRestrictionEnforcedAdmin();
240             mActionDisabledDialog = new ActionDisabledByAdminDialogHelper(getActivity())
241                     .prepareDialogBuilder(mRestrictionKey, admin)
242                     .setOnDismissListener(__ -> getActivity().finish())
243                     .show();
244             setEmptyView(new View(getContext()));
245         } else if (mEmptyTextView != null) {
246             setEmptyView(mEmptyTextView);
247         }
248         super.onDataSetChanged();
249     }
250 
setIfOnlyAvailableForAdmins(boolean onlyForAdmins)251     public void setIfOnlyAvailableForAdmins(boolean onlyForAdmins) {
252         mOnlyAvailableForAdmins = onlyForAdmins;
253     }
254 
255     /**
256      * Returns whether restricted or actionable UI elements should be removed or disabled.
257      */
isUiRestricted()258     protected boolean isUiRestricted() {
259         return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded()
260                 || (!mIsAdminUser && mOnlyAvailableForAdmins);
261     }
262 
isUiRestrictedByOnlyAdmin()263     protected boolean isUiRestrictedByOnlyAdmin() {
264         return isUiRestricted() && !mUserManager.hasBaseUserRestriction(mRestrictionKey,
265                 UserHandle.of(UserHandle.myUserId())) && (mIsAdminUser || !mOnlyAvailableForAdmins);
266     }
267 }
268