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