1 /* 2 * Copyright (C) 2017 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 package com.android.settings.deviceinfo; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.os.Build; 21 import android.os.SystemClock; 22 import android.os.UserHandle; 23 import android.os.UserManager; 24 import android.support.v7.preference.Preference; 25 import android.support.v7.preference.PreferenceScreen; 26 import android.text.TextUtils; 27 import android.util.Log; 28 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settingslib.RestrictedLockUtils; 31 import com.android.settingslib.core.AbstractPreferenceController; 32 import com.android.settingslib.core.lifecycle.Lifecycle; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnResume; 35 36 37 public class FirmwareVersionPreferenceController extends AbstractPreferenceController implements 38 PreferenceControllerMixin, LifecycleObserver, OnResume { 39 40 private static final String TAG = "FirmwareVersionPref"; 41 private static final String KEY_FIRMWARE_VERSION = "firmware_version"; 42 43 private final UserManager mUserManager; 44 45 private RestrictedLockUtils.EnforcedAdmin mFunDisallowedAdmin; 46 private boolean mFunDisallowedBySystem; 47 48 private long[] mHits = new long[3]; 49 FirmwareVersionPreferenceController(Context context, Lifecycle lifecycle)50 public FirmwareVersionPreferenceController(Context context, Lifecycle lifecycle) { 51 super(context); 52 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 53 if (lifecycle != null) { 54 lifecycle.addObserver(this); 55 } 56 } 57 58 @Override isAvailable()59 public boolean isAvailable() { 60 return true; 61 } 62 63 @Override displayPreference(PreferenceScreen screen)64 public void displayPreference(PreferenceScreen screen) { 65 super.displayPreference(screen); 66 final Preference pref = screen.findPreference(KEY_FIRMWARE_VERSION); 67 if (pref != null) { 68 pref.setSummary(Build.VERSION.RELEASE); 69 } 70 } 71 72 @Override getPreferenceKey()73 public String getPreferenceKey() { 74 return KEY_FIRMWARE_VERSION; 75 } 76 77 @Override onResume()78 public void onResume() { 79 mFunDisallowedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced( 80 mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId()); 81 mFunDisallowedBySystem = RestrictedLockUtils.hasBaseUserRestriction( 82 mContext, UserManager.DISALLOW_FUN, UserHandle.myUserId()); 83 } 84 85 @Override handlePreferenceTreeClick(Preference preference)86 public boolean handlePreferenceTreeClick(Preference preference) { 87 if (!TextUtils.equals(preference.getKey(), KEY_FIRMWARE_VERSION)) { 88 return false; 89 } 90 System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1); 91 mHits[mHits.length - 1] = SystemClock.uptimeMillis(); 92 if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) { 93 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_FUN)) { 94 if (mFunDisallowedAdmin != null && !mFunDisallowedBySystem) { 95 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, 96 mFunDisallowedAdmin); 97 } 98 Log.d(TAG, "Sorry, no fun for you!"); 99 return false; 100 } 101 102 final Intent intent = new Intent(Intent.ACTION_MAIN) 103 .setClassName( 104 "android", com.android.internal.app.PlatLogoActivity.class.getName()); 105 try { 106 mContext.startActivity(intent); 107 return true; 108 } catch (Exception e) { 109 Log.e(TAG, "Unable to start activity " + intent.toString()); 110 } 111 } 112 return false; 113 } 114 } 115