• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
21 import android.os.Build;
22 import android.os.UserManager;
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 UserManager mUserManager;
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         mUserManager = UserManager.get(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 =
58                 DevelopmentSettingsUtil.isDevelopmentSettingsEnabled(getContext(), mUserManager)
59                         ? -1 : getTapsToBecomeDeveloper();
60     }
61 
62     @Override
updateState(Preference preference)63     protected void updateState(Preference preference) {
64         preference.setSummary(Build.DISPLAY);
65     }
66 
67     @Override
handlePreferenceClicked(Preference preference)68     protected boolean handlePreferenceClicked(Preference preference) {
69         if (!mUserManager.isAdminUser()) {
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