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
getSummary()72     public CharSequence getSummary() {
73         return Build.VERSION.RELEASE;
74     }
75 
76     @Override
handlePreferenceTreeClick(Preference preference)77     public boolean handlePreferenceTreeClick(Preference preference) {
78         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
79             return false;
80         }
81         if (Utils.isMonkeyRunning()) {
82             return false;
83         }
84         arrayCopy();
85         mHits[mHits.length - 1] = SystemClock.uptimeMillis();
86         if (mHits[0] >= (SystemClock.uptimeMillis() - DELAY_TIMER_MILLIS)) {
87             if (mUserManager.hasUserRestriction(UserManager.DISALLOW_FUN)) {
88                 if (mFunDisallowedAdmin != null && !mFunDisallowedBySystem) {
89                     RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext,
90                             mFunDisallowedAdmin);
91                 }
92                 Log.d(TAG, "Sorry, no fun for you!");
93                 return true;
94             }
95 
96             final Intent intent = new Intent(Intent.ACTION_MAIN)
97                     .setClassName(
98                             "android", com.android.internal.app.PlatLogoActivity.class.getName());
99             try {
100                 mContext.startActivity(intent);
101             } catch (Exception e) {
102                 Log.e(TAG, "Unable to start activity " + intent.toString());
103             }
104         }
105         return true;
106     }
107 
108     /**
109      * Copies the array onto itself to remove the oldest hit.
110      */
111     @VisibleForTesting
arrayCopy()112     void arrayCopy() {
113         System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
114     }
115 
116     @VisibleForTesting
initializeAdminPermissions()117     void initializeAdminPermissions() {
118         mFunDisallowedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
119                 mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId());
120         mFunDisallowedBySystem = RestrictedLockUtilsInternal.hasBaseUserRestriction(
121                 mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId());
122     }
123 
124     @Override
copy()125     public void copy() {
126         Sliceable.setCopyContent(mContext, getSummary(),
127                 mContext.getText(R.string.firmware_version));
128     }
129 }
130