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.tv.settings;
18 
19 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20 
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.RestrictionsManager;
28 import android.os.Bundle;
29 import android.os.PersistableBundle;
30 import android.os.UserHandle;
31 import android.os.UserManager;
32 import android.view.View;
33 import android.widget.TextView;
34 
35 import com.android.settingslib.RestrictedLockUtilsInternal;
36 
37 /**
38  * Base class for settings screens that should be pin protected when in restricted mode or
39  * that will display an admin support message in case an admin has disabled the options.
40  * The constructor for this class will take the restriction key that this screen should be
41  * locked by.  If {@link RestrictionsManager.hasRestrictionsProvider()} and
42  * {@link UserManager.hasUserRestriction()}, then the user will have to enter the restrictions
43  * pin before seeing the Settings screen.
44  *
45  * If this settings screen should be pin protected whenever
46  * {@link RestrictionsManager.hasRestrictionsProvider()} returns true, pass in
47  * {@link RESTRICT_IF_OVERRIDABLE} to the constructor instead of a restrictions key.
48  *
49  * Forked from:
50  * Settings/src/com/android/settings/RestrictedSettingsFragment.java
51  */
52 public abstract class RestrictedSettingsFragment extends SettingsPreferenceFragment {
53 
54     protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable";
55 
56     // No RestrictedSettingsFragment screens should use this number in startActivityForResult.
57     static final int REQUEST_PIN_CHALLENGE = 12309;
58 
59     private static final String KEY_CHALLENGE_SUCCEEDED = "chsc";
60     private static final String KEY_CHALLENGE_REQUESTED = "chrq";
61 
62     // If the restriction PIN is entered correctly.
63     private boolean mChallengeSucceeded;
64     private boolean mChallengeRequested;
65 
66     private UserManager mUserManager;
67     private RestrictionsManager mRestrictionsManager;
68 
69     private final String mRestrictionKey;
70     private EnforcedAdmin mEnforcedAdmin;
71     private TextView mEmptyTextView;
72 
73     private boolean mOnlyAvailableForAdmins = false;
74     private boolean mIsAdminUser;
75 
76     private View mEmptyView;
77 
78     // Receiver to clear pin status when the screen is turned off.
79     private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
80         @Override
81         public void onReceive(Context context, Intent intent) {
82             if (!mChallengeRequested) {
83                 mChallengeSucceeded = false;
84                 mChallengeRequested = false;
85             }
86         }
87     };
88 
89     AlertDialog mActionDisabledDialog;
90 
91     /**
92      * @param restrictionKey The restriction key to check before pin protecting
93      *            this settings page. Pass in {@link RESTRICT_IF_OVERRIDABLE} if it should
94      *            be protected whenever a restrictions provider is set. Pass in
95      *            null if it should never be protected.
96      */
RestrictedSettingsFragment(String restrictionKey)97     public RestrictedSettingsFragment(String restrictionKey) {
98         mRestrictionKey = restrictionKey;
99     }
100 
101     @Override
onCreate(Bundle icicle)102     public void onCreate(Bundle icicle) {
103         mRestrictionsManager = (RestrictionsManager) getActivity().getSystemService(
104                 Context.RESTRICTIONS_SERVICE);
105         mUserManager = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
106         mIsAdminUser = mUserManager.isAdminUser();
107 
108         super.onCreate(icicle);
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                 if (mActionDisabledDialog != null && mActionDisabledDialog.isShowing()) {
158                     mActionDisabledDialog.setOnDismissListener(null);
159                     mActionDisabledDialog.dismiss();
160                 }
161             } else {
162                 mChallengeSucceeded = false;
163             }
164             return;
165         }
166 
167         super.onActivityResult(requestCode, resultCode, data);
168     }
169 
ensurePin()170     private void ensurePin() {
171         if (!mChallengeSucceeded && !mChallengeRequested
172                 && mRestrictionsManager.hasRestrictionsProvider()) {
173             Intent intent = mRestrictionsManager.createLocalApprovalIntent();
174             if (intent != null) {
175                 mChallengeRequested = true;
176                 mChallengeSucceeded = false;
177                 PersistableBundle request = new PersistableBundle();
178                 request.putString(RestrictionsManager.REQUEST_KEY_MESSAGE,
179                         "getResources().getString(R.string.restr_pin_enter_admin_pin)");
180                 intent.putExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE, request);
181                 startActivityForResult(intent, REQUEST_PIN_CHALLENGE);
182             }
183         }
184     }
185 
186     /**
187      * Returns true if this activity is restricted, but no restrictions provider has been set.
188      * Used to determine if the settings UI should disable UI.
189      */
isRestrictedAndNotProviderProtected()190     protected boolean isRestrictedAndNotProviderProtected() {
191         if (mRestrictionKey == null || RESTRICT_IF_OVERRIDABLE.equals(mRestrictionKey)) {
192             return false;
193         }
194         return mUserManager.hasUserRestriction(mRestrictionKey)
195                 && !mRestrictionsManager.hasRestrictionsProvider();
196     }
197 
hasChallengeSucceeded()198     protected boolean hasChallengeSucceeded() {
199         return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested;
200     }
201 
202     /**
203      * Returns true if this restrictions key is locked down.
204      */
shouldBeProviderProtected(String restrictionKey)205     protected boolean shouldBeProviderProtected(String restrictionKey) {
206         if (restrictionKey == null) {
207             return false;
208         }
209         boolean restricted = RESTRICT_IF_OVERRIDABLE.equals(restrictionKey)
210                 || mUserManager.hasUserRestriction(mRestrictionKey);
211         return restricted && mRestrictionsManager.hasRestrictionsProvider();
212     }
213 
initEmptyTextView()214     protected TextView initEmptyTextView() {
215         TextView emptyView = (TextView) getActivity().findViewById(android.R.id.empty);
216         return emptyView;
217     }
218 
getRestrictionEnforcedAdmin()219     protected EnforcedAdmin getRestrictionEnforcedAdmin() {
220         mEnforcedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getActivity(),
221                 mRestrictionKey, UserHandle.myUserId());
222         if (mEnforcedAdmin != null && mEnforcedAdmin.user == null) {
223             mEnforcedAdmin.user = UserHandle.of(UserHandle.myUserId());
224         }
225         return mEnforcedAdmin;
226     }
227 
getEmptyTextView()228     protected TextView getEmptyTextView() {
229         return mEmptyTextView;
230     }
231 
showAdminDialog()232     protected void showAdminDialog() {
233         if (isUiRestrictedByOnlyAdmin()
234                 && (mActionDisabledDialog == null || !mActionDisabledDialog.isShowing())) {
235             final EnforcedAdmin admin = getRestrictionEnforcedAdmin();
236             mActionDisabledDialog = new ActionDisabledByAdminDialogHelper(getActivity())
237                     .prepareDialogBuilder(mRestrictionKey, admin)
238                     .setOnDismissListener(__ -> getActivity().finish())
239                     .show();
240             setEmptyView(new View(getContext()));
241         } else if (mEmptyTextView != null) {
242             setEmptyView(mEmptyTextView);
243         }
244     }
245 
setIfOnlyAvailableForAdmins(boolean onlyForAdmins)246     public void setIfOnlyAvailableForAdmins(boolean onlyForAdmins) {
247         mOnlyAvailableForAdmins = onlyForAdmins;
248     }
249 
250     /**
251      * Returns whether restricted or actionable UI elements should be removed or disabled.
252      */
isUiRestricted()253     protected boolean isUiRestricted() {
254         return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded()
255                 || (!mIsAdminUser && mOnlyAvailableForAdmins);
256     }
257 
isUiRestrictedByOnlyAdmin()258     protected boolean isUiRestrictedByOnlyAdmin() {
259         return isUiRestricted() && !mUserManager.hasBaseUserRestriction(mRestrictionKey,
260                 UserHandle.of(UserHandle.myUserId())) && (mIsAdminUser || !mOnlyAvailableForAdmins);
261     }
262 
updateEmptyView()263     void updateEmptyView() {
264         if (mEmptyView == null) return;
265         if (getPreferenceScreen() != null) {
266             final View listContainer = getActivity().findViewById(android.R.id.list_container);
267             boolean show = getPreferenceScreen().getPreferenceCount() <= 0
268                     || (listContainer != null && listContainer.getVisibility() != View.VISIBLE);
269             mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
270         } else {
271             mEmptyView.setVisibility(View.VISIBLE);
272         }
273     }
274 
setEmptyView(View v)275     protected void setEmptyView(View v) {
276         if (mEmptyView != null) {
277             mEmptyView.setVisibility(View.GONE);
278         }
279         mEmptyView = v;
280         updateEmptyView();
281     }
282 
getEmptyView()283     protected View getEmptyView() {
284         return mEmptyView;
285     }
286 }
287