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;
18 
19 import static com.android.settings.fuelgauge.BatteryUtils.formatElapsedTimeWithoutComma;
20 
21 import android.content.Context;
22 import android.text.TextUtils;
23 import android.text.format.DateUtils;
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 
32 public class PowerUsageTimeController extends BasePreferenceController {
33     private static final String TAG = "PowerUsageTimeController";
34 
35     private static final String KEY_POWER_USAGE_TIME = "battery_usage_time_category";
36     private static final String KEY_SCREEN_TIME_PREF = "battery_usage_screen_time";
37     private static final String KEY_BACKGROUND_TIME_PREF = "battery_usage_background_time";
38 
39     @VisibleForTesting PreferenceCategory mPowerUsageTimeCategory;
40     @VisibleForTesting PowerUsageTimePreference mScreenTimePreference;
41     @VisibleForTesting PowerUsageTimePreference mBackgroundTimePreference;
42 
PowerUsageTimeController(Context context)43     public PowerUsageTimeController(Context context) {
44         super(context, KEY_POWER_USAGE_TIME);
45     }
46 
47     @Override
getAvailabilityStatus()48     public int getAvailabilityStatus() {
49         return AVAILABLE;
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         mPowerUsageTimeCategory = screen.findPreference(KEY_POWER_USAGE_TIME);
56         mScreenTimePreference = screen.findPreference(KEY_SCREEN_TIME_PREF);
57         mBackgroundTimePreference = screen.findPreference(KEY_BACKGROUND_TIME_PREF);
58         mPowerUsageTimeCategory.setVisible(false);
59     }
60 
handleScreenTimeUpdated( final String slotTime, final long screenOnTimeInMs, final long backgroundTimeInMs, final String anomalyHintPrefKey, final String anomalyHintText)61     void handleScreenTimeUpdated(
62             final String slotTime,
63             final long screenOnTimeInMs,
64             final long backgroundTimeInMs,
65             final String anomalyHintPrefKey,
66             final String anomalyHintText) {
67         final boolean isShowScreenOnTime =
68                 showTimePreference(
69                         mScreenTimePreference,
70                         R.string.power_usage_detail_screen_time,
71                         screenOnTimeInMs,
72                         anomalyHintPrefKey,
73                         anomalyHintText);
74         final boolean isShowBackgroundTime =
75                 showTimePreference(
76                         mBackgroundTimePreference,
77                         R.string.power_usage_detail_background_time,
78                         backgroundTimeInMs,
79                         anomalyHintPrefKey,
80                         anomalyHintText);
81         if (isShowScreenOnTime || isShowBackgroundTime) {
82             showCategoryTitle(slotTime);
83         }
84     }
85 
showTimePreference( PowerUsageTimePreference preference, int titleResId, long summaryTimeMs, String anomalyHintKey, String anomalyHintText)86     boolean showTimePreference(
87             PowerUsageTimePreference preference,
88             int titleResId,
89             long summaryTimeMs,
90             String anomalyHintKey,
91             String anomalyHintText) {
92         if (preference == null
93                 || (summaryTimeMs == 0 && !TextUtils.equals(anomalyHintKey, preference.getKey()))) {
94             return false;
95         }
96         preference.setTimeTitle(mContext.getString(titleResId));
97         preference.setTimeSummary(getPowerUsageTimeInfo(summaryTimeMs));
98         if (TextUtils.equals(anomalyHintKey, preference.getKey())) {
99             preference.setAnomalyHint(anomalyHintText);
100         }
101         preference.setVisible(true);
102         return true;
103     }
104 
getPowerUsageTimeInfo(long timeInMs)105     private CharSequence getPowerUsageTimeInfo(long timeInMs) {
106         if (timeInMs < DateUtils.MINUTE_IN_MILLIS) {
107             return mContext.getString(R.string.power_usage_time_less_than_one_minute);
108         }
109         return formatElapsedTimeWithoutComma(
110                 mContext,
111                 (double) timeInMs,
112                 /* withSeconds= */ false,
113                 /* collapseTimeUnit= */ false);
114     }
115 
116     @VisibleForTesting
showCategoryTitle(String slotTimestamp)117     void showCategoryTitle(String slotTimestamp) {
118         mPowerUsageTimeCategory.setTitle(
119                 slotTimestamp == null
120                         ? mContext.getString(R.string.battery_app_usage)
121                         : mContext.getString(R.string.battery_app_usage_for, slotTimestamp));
122         mPowerUsageTimeCategory.setVisible(true);
123     }
124 }
125