1 /*
2  * Copyright 2019 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.datausage;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 
22 import androidx.preference.Preference;
23 
24 import com.android.car.settings.R;
25 import com.android.car.settings.common.FragmentController;
26 import com.android.car.settings.network.NetworkBasePreferenceController;
27 import com.android.settingslib.net.DataUsageController;
28 import com.android.settingslib.utils.StringUtil;
29 
30 import java.util.concurrent.TimeUnit;
31 
32 /**
33  * Controller to handle the business logic for AppDataUsage preference on the data usage screen
34  */
35 public class DataUsagePreferenceController extends
36         NetworkBasePreferenceController<Preference> {
37 
38     private static final long MILLIS_IN_A_DAY = TimeUnit.DAYS.toMillis(1);
39     private static final String STRING_FORMAT = "%s, %s";
40 
41     private final DataUsageController mDataUsageController;
42 
DataUsagePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43     public DataUsagePreferenceController(Context context, String preferenceKey,
44             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
45         super(context, preferenceKey, fragmentController, uxRestrictions);
46         mDataUsageController = new DataUsageController(context);
47     }
48 
49     @Override
getPreferenceType()50     protected Class<Preference> getPreferenceType() {
51         return Preference.class;
52     }
53 
54     @Override
handlePreferenceClicked(Preference preference)55     protected boolean handlePreferenceClicked(Preference preference) {
56         AppDataUsageFragment appDataUsageFragment = AppDataUsageFragment.newInstance(getSubId());
57         getFragmentController().launchFragment(appDataUsageFragment);
58         return true;
59     }
60 
61     @Override
updateState(Preference preference)62     protected void updateState(Preference preference) {
63         DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo(
64                 getNetworkTemplate());
65         getPreference().setSummary(String.format(STRING_FORMAT, getUsageText(info),
66                 getRemainingBillingCycleTimeText(info)));
67     }
68 
getUsageText(DataUsageController.DataUsageInfo info)69     private CharSequence getUsageText(DataUsageController.DataUsageInfo info) {
70         return DataUsageUtils.bytesToIecUnits(getContext(), info.usageLevel);
71     }
72 
getRemainingBillingCycleTimeText(DataUsageController.DataUsageInfo info)73     private CharSequence getRemainingBillingCycleTimeText(DataUsageController.DataUsageInfo info) {
74         long millisLeft = info.cycleEnd - System.currentTimeMillis();
75         if (millisLeft <= 0) {
76             return getContext().getString(R.string.billing_cycle_none_left);
77         } else {
78             int daysLeft = (int) (millisLeft / MILLIS_IN_A_DAY);
79             if (daysLeft < 1) {
80                 return getContext().getString(R.string.billing_cycle_less_than_one_day_left);
81             } else {
82                 return StringUtil.getIcuPluralsString(getContext(), daysLeft,
83                         R.string.billing_cycle_days_left);
84             }
85         }
86     }
87 }
88