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.Utils; 31 import com.android.settings.core.BasePreferenceController; 32 import com.android.settings.fuelgauge.BatteryUtils; 33 import com.android.settingslib.widget.SettingsThemeHelper; 34 35 import java.util.Locale; 36 import java.util.regex.Matcher; 37 import java.util.regex.Pattern; 38 39 /** Controller for screen on time in battery usage page. */ 40 public class ScreenOnTimeController extends BasePreferenceController { 41 private static final String TAG = "ScreenOnTimeController"; 42 private static final String ROOT_PREFERENCE_KEY = "screen_on_time_category"; 43 private static final String SCREEN_ON_TIME_TEXT_PREFERENCE_KEY = "screen_on_time_text"; 44 private static final Pattern NUMBER_PATTERN = Pattern.compile("[\\d]*[\\.,]?[\\d]+"); 45 private static final Locale IW_LOCALE = new Locale("iw"); 46 47 @VisibleForTesting Context mPrefContext; 48 @VisibleForTesting PreferenceCategory mRootPreference; 49 @VisibleForTesting TextViewPreference mScreenOnTimeTextPreference; 50 @VisibleForTesting String mScreenTimeCategoryLastFullChargeText; 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 mScreenTimeCategoryLastFullChargeText = 68 mPrefContext.getString(R.string.screen_time_category_last_full_charge); 69 } 70 handleScreenOnTimeUpdated( Long screenOnTime, String slotTimestamp, String accessibilitySlotTimestamp)71 void handleScreenOnTimeUpdated( 72 Long screenOnTime, String slotTimestamp, String accessibilitySlotTimestamp) { 73 if (screenOnTime == null) { 74 mRootPreference.setVisible(false); 75 mScreenOnTimeTextPreference.setVisible(false); 76 return; 77 } 78 showCategoryTitle(slotTimestamp, accessibilitySlotTimestamp); 79 showScreenOnTimeText(screenOnTime); 80 } 81 82 @VisibleForTesting showCategoryTitle(String slotTimestamp, String accessibilitySlotTimestamp)83 void showCategoryTitle(String slotTimestamp, String accessibilitySlotTimestamp) { 84 final String displayTitle = 85 slotTimestamp == null 86 ? mScreenTimeCategoryLastFullChargeText 87 : mPrefContext.getString( 88 R.string.screen_time_category_for_slot, slotTimestamp); 89 final String accessibilityTitle = 90 accessibilitySlotTimestamp == null 91 ? mScreenTimeCategoryLastFullChargeText 92 : mPrefContext.getString( 93 R.string.screen_time_category_for_slot, accessibilitySlotTimestamp); 94 mRootPreference.setTitle(Utils.createAccessibleSequence(displayTitle, accessibilityTitle)); 95 mRootPreference.setVisible(true); 96 } 97 98 @VisibleForTesting showScreenOnTimeText(Long screenOnTime)99 void showScreenOnTimeText(Long screenOnTime) { 100 final CharSequence timeSequence = 101 BatteryUtils.formatElapsedTimeWithoutComma( 102 mPrefContext, 103 (double) screenOnTime, 104 /* withSeconds= */ false, 105 /* collapseTimeUnit= */ false); 106 mScreenOnTimeTextPreference.setText( 107 enlargeFontOfNumberIfNeeded(mPrefContext, timeSequence)); 108 mScreenOnTimeTextPreference.setVisible(true); 109 } 110 111 @VisibleForTesting enlargeFontOfNumberIfNeeded(Context context, CharSequence text)112 static CharSequence enlargeFontOfNumberIfNeeded(Context context, CharSequence text) { 113 if (TextUtils.isEmpty(text)) { 114 return ""; 115 } 116 117 final Locale locale = context.getResources().getConfiguration().getLocales().get(0); 118 if (locale != null && IW_LOCALE.getLanguage().equals(locale.getLanguage())) { 119 return text; 120 } 121 122 final SpannableString spannableText = new SpannableString(text); 123 final int enlargeFontSizeDp = SettingsThemeHelper.isExpressiveTheme(context) ? 64 : 36; 124 final Matcher matcher = NUMBER_PATTERN.matcher(text); 125 while (matcher.find()) { 126 spannableText.setSpan( 127 new AbsoluteSizeSpan(enlargeFontSizeDp, true /* dip */), 128 matcher.start(), 129 matcher.end(), 130 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 131 } 132 return spannableText; 133 } 134 } 135