• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.fuelgauge.batteryusage;
18 
19 import android.content.Context;
20 import android.text.SpannableString;
21 import android.text.Spanned;
22 import android.text.TextUtils;
23 import android.text.style.AbsoluteSizeSpan;
24 
25 import androidx.preference.PreferenceCategory;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 import com.android.settings.fuelgauge.BatteryUtils;
32 
33 import java.util.Locale;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 
37 /** Controller for screen on time in battery usage page. */
38 public class ScreenOnTimeController extends BasePreferenceController {
39     private static final String TAG = "ScreenOnTimeController";
40     private static final String ROOT_PREFERENCE_KEY = "screen_on_time_category";
41     private static final String SCREEN_ON_TIME_TEXT_PREFERENCE_KEY = "screen_on_time_text";
42     private static final Pattern NUMBER_PATTERN = Pattern.compile("[\\d]*[\\.,]?[\\d]+");
43     private static final Locale IW_LOCALE = new Locale("iw");
44 
45     @VisibleForTesting
46     Context mPrefContext;
47     @VisibleForTesting
48     PreferenceCategory mRootPreference;
49     @VisibleForTesting
50     TextViewPreference mScreenOnTimeTextPreference;
51 
ScreenOnTimeController(Context context)52     public ScreenOnTimeController(Context context) {
53         super(context, ROOT_PREFERENCE_KEY);
54     }
55 
56     @Override
getAvailabilityStatus()57     public int getAvailabilityStatus() {
58         return AVAILABLE;
59     }
60 
61     @Override
displayPreference(PreferenceScreen screen)62     public void displayPreference(PreferenceScreen screen) {
63         super.displayPreference(screen);
64         mPrefContext = screen.getContext();
65         mRootPreference = screen.findPreference(ROOT_PREFERENCE_KEY);
66         mScreenOnTimeTextPreference = screen.findPreference(SCREEN_ON_TIME_TEXT_PREFERENCE_KEY);
67     }
68 
handleSceenOnTimeUpdated(Long screenOnTime, String slotTimestamp)69     void handleSceenOnTimeUpdated(Long screenOnTime, String slotTimestamp) {
70         if (screenOnTime == null) {
71             mRootPreference.setVisible(false);
72             mScreenOnTimeTextPreference.setVisible(false);
73             return;
74         }
75         showCategoryTitle(slotTimestamp);
76         showScreenOnTimeText(screenOnTime);
77     }
78 
79     @VisibleForTesting
showCategoryTitle(String slotTimestamp)80     void showCategoryTitle(String slotTimestamp) {
81         mRootPreference.setTitle(slotTimestamp == null
82                 ? mPrefContext.getString(
83                         R.string.screen_time_category_last_full_charge)
84                 : mPrefContext.getString(
85                         R.string.screen_time_category_for_slot, slotTimestamp));
86         mRootPreference.setVisible(true);
87     }
88 
89     @VisibleForTesting
showScreenOnTimeText(Long screenOnTime)90     void showScreenOnTimeText(Long screenOnTime) {
91         final CharSequence timeSequence =
92                 BatteryUtils.formatElapsedTimeWithoutComma(mPrefContext, (double) screenOnTime,
93                         /*withSeconds=*/ false, /*collapseTimeUnit=*/ false);
94         mScreenOnTimeTextPreference.setText(
95                 enlargeFontOfNumberIfNeeded(mPrefContext, timeSequence));
96         mScreenOnTimeTextPreference.setVisible(true);
97     }
98 
99     @VisibleForTesting
enlargeFontOfNumberIfNeeded(Context context, CharSequence text)100     static CharSequence enlargeFontOfNumberIfNeeded(Context context, CharSequence text) {
101         if (TextUtils.isEmpty(text)) {
102             return "";
103         }
104 
105         final Locale locale = context.getResources().getConfiguration().getLocales().get(0);
106         if (locale != null && IW_LOCALE.getLanguage().equals(locale.getLanguage())) {
107             return text;
108         }
109 
110         final SpannableString spannableText =  new SpannableString(text);
111         final Matcher matcher = NUMBER_PATTERN.matcher(text);
112         while (matcher.find()) {
113             spannableText.setSpan(new AbsoluteSizeSpan(36, true /* dip */), matcher.start(),
114                     matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
115         }
116         return spannableText;
117     }
118 }
119