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 // LINT.IfChange 37 public class FirmwareVersionDetailPreferenceController extends BasePreferenceController { 38 39 private static final String TAG = "firmwareDialogCtrl"; 40 private static final int DELAY_TIMER_MILLIS = 500; 41 private static final int ACTIVITY_TRIGGER_COUNT = 3; 42 43 private final UserManager mUserManager; 44 private final long[] mHits = new long[ACTIVITY_TRIGGER_COUNT]; 45 46 private RestrictedLockUtils.EnforcedAdmin mFunDisallowedAdmin; 47 private boolean mFunDisallowedBySystem; 48 FirmwareVersionDetailPreferenceController(Context context, String key)49 public FirmwareVersionDetailPreferenceController(Context context, String key) { 50 super(context, key); 51 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 52 initializeAdminPermissions(); 53 } 54 55 @Override getAvailabilityStatus()56 public int getAvailabilityStatus() { 57 return AVAILABLE; 58 } 59 60 @Override useDynamicSliceSummary()61 public boolean useDynamicSliceSummary() { 62 return true; 63 } 64 65 @Override isSliceable()66 public boolean isSliceable() { 67 return true; 68 } 69 70 @Override isPublicSlice()71 public boolean isPublicSlice() { 72 return true; 73 } 74 75 @Override getSummary()76 public CharSequence getSummary() { 77 return Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY; 78 } 79 80 @Override handlePreferenceTreeClick(Preference preference)81 public boolean handlePreferenceTreeClick(Preference preference) { 82 if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) { 83 return false; 84 } 85 if (Utils.isMonkeyRunning()) { 86 return false; 87 } 88 arrayCopy(); 89 mHits[mHits.length - 1] = SystemClock.uptimeMillis(); 90 if (mHits[0] >= (SystemClock.uptimeMillis() - DELAY_TIMER_MILLIS)) { 91 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_FUN)) { 92 if (mFunDisallowedAdmin != null && !mFunDisallowedBySystem) { 93 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, 94 mFunDisallowedAdmin); 95 } 96 Log.d(TAG, "Sorry, no fun for you!"); 97 return true; 98 } 99 100 final Intent intent = new Intent(Intent.ACTION_MAIN) 101 .setClassName( 102 "android", com.android.internal.app.PlatLogoActivity.class.getName()) 103 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 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 // LINT.ThenChange(FirmwareVersionDetailPreference.kt) 130