• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 import android.net.ConnectivityManager;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.SubscriptionPlan;
24 import android.util.Range;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.car.settings.common.FragmentController;
29 import com.android.car.settings.common.PreferenceController;
30 import com.android.car.settings.network.NetworkUtils;
31 
32 import java.time.ZonedDateTime;
33 import java.util.Iterator;
34 
35 /** Preference controller which shows how much data has been used so far. */
36 public class DataUsageEntryPreferenceController extends PreferenceController<Preference> {
37 
DataUsageEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)38     public DataUsageEntryPreferenceController(Context context, String preferenceKey,
39             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
40         super(context, preferenceKey, fragmentController, uxRestrictions);
41     }
42 
43     @Override
getPreferenceType()44     protected Class<Preference> getPreferenceType() {
45         return Preference.class;
46     }
47 
48     @Override
getAvailabilityStatus()49     protected int getAvailabilityStatus() {
50         if (!NetworkUtils.hasMobileNetwork(
51                 getContext().getSystemService(ConnectivityManager.class))) {
52             return UNSUPPORTED_ON_DEVICE;
53         }
54         return AVAILABLE;
55     }
56 
57     @Override
updateState(Preference preference)58     protected void updateState(Preference preference) {
59         preference.setSummary(formatUsedData());
60     }
61 
formatUsedData()62     private CharSequence formatUsedData() {
63         SubscriptionManager subscriptionManager = getContext().getSystemService(
64                 SubscriptionManager.class);
65         int defaultSubId = subscriptionManager.getDefaultSubscriptionId();
66         if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
67             return null;
68         }
69         SubscriptionPlan defaultPlan = DataUsageUtils.getPrimaryPlan(subscriptionManager,
70                 defaultSubId);
71         if (defaultPlan == null) {
72             return null;
73         }
74         Iterator<Range<ZonedDateTime>> cycle = defaultPlan.cycleIterator();
75         long start = cycle.next().getLower().toInstant().toEpochMilli();
76         return DataUsageUtils.formatDataUsageInCycle(getContext(), defaultPlan.getDataUsageBytes(),
77                 start);
78     }
79 }
80