1 /* 2 * Copyright (C) 2024 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 androidx.preference.Preference 26 import com.android.internal.app.PlatLogoActivity 27 import com.android.settings.R 28 import com.android.settings.Utils 29 import com.android.settingslib.RestrictedLockUtils 30 import com.android.settingslib.RestrictedLockUtilsInternal 31 import com.android.settingslib.metadata.PreferenceMetadata 32 import com.android.settingslib.metadata.PreferenceSummaryProvider 33 import com.android.settingslib.preference.PreferenceBinding 34 35 // LINT.IfChange 36 class FirmwareVersionDetailPreference : 37 PreferenceMetadata, 38 PreferenceSummaryProvider, 39 PreferenceBinding, 40 Preference.OnPreferenceClickListener { 41 42 private val hits = LongArray(ACTIVITY_TRIGGER_COUNT) 43 44 override val key: String 45 get() = "os_firmware_version" 46 47 override val title: Int 48 get() = R.string.firmware_version 49 isIndexablenull50 override fun isIndexable(context: Context) = false 51 52 override fun intent(context: Context): Intent? = 53 Intent(Intent.ACTION_MAIN) 54 .setClassName("android", PlatLogoActivity::class.java.name) 55 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 56 57 override fun getSummary(context: Context): CharSequence? = 58 Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY 59 60 override fun bind(preference: Preference, metadata: PreferenceMetadata) { 61 super.bind(preference, metadata) 62 preference.isCopyingEnabled = true 63 preference.onPreferenceClickListener = this 64 } 65 66 // return true swallows the click event, while return false will start the intent onPreferenceClicknull67 override fun onPreferenceClick(preference: Preference): Boolean { 68 if (Utils.isMonkeyRunning()) return true 69 70 // remove oldest hit and check whether there are 3 clicks within 500ms 71 for (index in 1..<ACTIVITY_TRIGGER_COUNT) hits[index - 1] = hits[index] 72 hits[ACTIVITY_TRIGGER_COUNT - 1] = SystemClock.uptimeMillis() 73 if (hits[ACTIVITY_TRIGGER_COUNT - 1] - hits[0] > DELAY_TIMER_MILLIS) return true 74 75 val context = preference.context 76 val userManager = context.getSystemService(Context.USER_SERVICE) as? UserManager 77 if (userManager?.hasUserRestriction(UserManager.DISALLOW_FUN) != true) return false 78 79 // Sorry, no fun for you! 80 val myUserId = UserHandle.myUserId() 81 val enforcedAdmin = 82 RestrictedLockUtilsInternal.checkIfRestrictionEnforced( 83 context, 84 UserManager.DISALLOW_FUN, 85 myUserId, 86 ) ?: return true 87 val disallowedBySystem = 88 RestrictedLockUtilsInternal.hasBaseUserRestriction( 89 context, 90 UserManager.DISALLOW_FUN, 91 myUserId, 92 ) 93 if (!disallowedBySystem) { 94 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context, enforcedAdmin) 95 } 96 return true 97 } 98 99 companion object { 100 const val DELAY_TIMER_MILLIS = 500L 101 const val ACTIVITY_TRIGGER_COUNT = 3 102 } 103 } 104 // LINT.ThenChange(FirmwareVersionDetailPreferenceController.java) 105