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.deviceinfo.firmwareversion;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Build;
22 import android.os.SystemClock;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.text.TextUtils;
26 import android.util.Log;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 
31 import com.android.settings.Utils;
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settingslib.RestrictedLockUtils;
34 import com.android.settingslib.RestrictedLockUtilsInternal;
35 
36 public class FirmwareVersionDetailPreferenceController extends BasePreferenceController {
37 
38     private static final String TAG = "firmwareDialogCtrl";
39     private static final int DELAY_TIMER_MILLIS = 500;
40     private static final int ACTIVITY_TRIGGER_COUNT = 3;
41 
42     private final UserManager mUserManager;
43     private final long[] mHits = new long[ACTIVITY_TRIGGER_COUNT];
44 
45     private RestrictedLockUtils.EnforcedAdmin mFunDisallowedAdmin;
46     private boolean mFunDisallowedBySystem;
47 
FirmwareVersionDetailPreferenceController(Context context, String key)48     public FirmwareVersionDetailPreferenceController(Context context, String key) {
49         super(context, key);
50         mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
51         initializeAdminPermissions();
52     }
53 
54     @Override
getAvailabilityStatus()55     public int getAvailabilityStatus() {
56         return AVAILABLE;
57     }
58 
59     @Override
useDynamicSliceSummary()60     public boolean useDynamicSliceSummary() {
61         return true;
62     }
63 
64     @Override
isSliceable()65     public boolean isSliceable() {
66         return true;
67     }
68 
69     @Override
isPublicSlice()70     public boolean isPublicSlice() {
71         return true;
72     }
73 
74     @Override
getSummary()75     public CharSequence getSummary() {
76         return Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY;
77     }
78 
79     @Override
handlePreferenceTreeClick(Preference preference)80     public boolean handlePreferenceTreeClick(Preference preference) {
81         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
82             return false;
83         }
84         if (Utils.isMonkeyRunning()) {
85             return false;
86         }
87         arrayCopy();
88         mHits[mHits.length - 1] = SystemClock.uptimeMillis();
89         if (mHits[0] >= (SystemClock.uptimeMillis() - DELAY_TIMER_MILLIS)) {
90             if (mUserManager.hasUserRestriction(UserManager.DISALLOW_FUN)) {
91                 if (mFunDisallowedAdmin != null && !mFunDisallowedBySystem) {
92                     RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext,
93                             mFunDisallowedAdmin);
94                 }
95                 Log.d(TAG, "Sorry, no fun for you!");
96                 return true;
97             }
98 
99             final Intent intent = new Intent(Intent.ACTION_MAIN)
100                     .setClassName(
101                             "android", com.android.internal.app.PlatLogoActivity.class.getName())
102                     .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
103             try {
104                 mContext.startActivity(intent);
105             } catch (Exception e) {
106                 Log.e(TAG, "Unable to start activity " + intent.toString());
107             }
108         }
109         return true;
110     }
111 
112     /**
113      * Copies the array onto itself to remove the oldest hit.
114      */
115     @VisibleForTesting
arrayCopy()116     void arrayCopy() {
117         System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
118     }
119 
120     @VisibleForTesting
initializeAdminPermissions()121     void initializeAdminPermissions() {
122         mFunDisallowedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
123                 mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId());
124         mFunDisallowedBySystem = RestrictedLockUtilsInternal.hasBaseUserRestriction(
125                 mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId());
126     }
127 }
128