1 /* 2 * Copyright (C) 2018 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.car.settings.system; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.car.userlib.CarUserManagerHelper; 21 import android.content.Context; 22 import android.os.Build; 23 import android.widget.Toast; 24 25 import androidx.preference.Preference; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.FragmentController; 29 import com.android.car.settings.common.PreferenceController; 30 import com.android.car.settings.development.DevelopmentSettingsUtil; 31 32 /** Updates the build number entry summary with the build number. */ 33 public class BuildNumberPreferenceController extends PreferenceController<Preference> { 34 35 private final CarUserManagerHelper mCarUserManagerHelper; 36 private Toast mDevHitToast; 37 private int mDevHitCountdown; 38 BuildNumberPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)39 public BuildNumberPreferenceController(Context context, String preferenceKey, 40 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 41 super(context, preferenceKey, fragmentController, uxRestrictions); 42 mCarUserManagerHelper = new CarUserManagerHelper(context); 43 } 44 45 @Override getPreferenceType()46 protected Class<Preference> getPreferenceType() { 47 return Preference.class; 48 } 49 50 /** 51 * Reset the toast and counter which tracks how many more clicks until development settings is 52 * enabled. 53 */ 54 @Override onResumeInternal()55 protected void onResumeInternal() { 56 mDevHitToast = null; 57 mDevHitCountdown = DevelopmentSettingsUtil.isDevelopmentSettingsEnabled(getContext(), 58 mCarUserManagerHelper) ? -1 : getTapsToBecomeDeveloper(); 59 } 60 61 @Override updateState(Preference preference)62 protected void updateState(Preference preference) { 63 preference.setSummary(Build.DISPLAY); 64 } 65 66 @Override handlePreferenceClicked(Preference preference)67 protected boolean handlePreferenceClicked(Preference preference) { 68 if (!mCarUserManagerHelper.isCurrentProcessAdminUser() 69 && !mCarUserManagerHelper.isCurrentProcessDemoUser()) { 70 return false; 71 } 72 73 if (!DevelopmentSettingsUtil.isDeviceProvisioned(getContext())) { 74 return false; 75 } 76 77 if (mDevHitCountdown > 0) { 78 mDevHitCountdown--; 79 if (mDevHitCountdown == 0) { 80 DevelopmentSettingsUtil.setDevelopmentSettingsEnabled(getContext(), true); 81 showToast(getContext().getString(R.string.show_dev_on), Toast.LENGTH_LONG); 82 } else if (mDevHitCountdown <= getTapsToBecomeDeveloper() - getTapsToShowToast()) { 83 showToast(getContext().getResources().getQuantityString( 84 R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown), 85 Toast.LENGTH_SHORT); 86 } 87 } else if (mDevHitCountdown < 0) { 88 showToast(getContext().getString(R.string.show_dev_already), Toast.LENGTH_LONG); 89 } 90 return true; 91 } 92 showToast(String text, @Toast.Duration int duration)93 private void showToast(String text, @Toast.Duration int duration) { 94 if (mDevHitToast != null) { 95 mDevHitToast.cancel(); 96 } 97 mDevHitToast = Toast.makeText(getContext(), text, duration); 98 mDevHitToast.show(); 99 } 100 getTapsToBecomeDeveloper()101 private int getTapsToBecomeDeveloper() { 102 return getContext().getResources().getInteger( 103 R.integer.enable_developer_settings_click_count); 104 } 105 getTapsToShowToast()106 private int getTapsToShowToast() { 107 return getContext().getResources().getInteger( 108 R.integer.enable_developer_settings_clicks_to_show_toast_count); 109 } 110 } 111