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 import android.telephony.SubscriptionManager;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.car.settings.common.FragmentController;
26 import com.android.car.settings.common.PreferenceController;
27 
28 /**
29  * Controller to handle the business logic for AppDataUsage preference on the data usage screen
30  */
31 public class DataUsagePreferenceController extends PreferenceController<Preference> {
32 
33     private int mSubId = Integer.MIN_VALUE;
34 
35     private SubscriptionManager mSubscriptionManager;
36 
DataUsagePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37     public DataUsagePreferenceController(Context context, String preferenceKey,
38             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
39         super(context, preferenceKey, fragmentController, uxRestrictions);
40         mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
41     }
42 
43     @Override
getPreferenceType()44     protected Class<Preference> getPreferenceType() {
45         return Preference.class;
46     }
47 
48     @Override
handlePreferenceClicked(Preference preference)49     protected boolean handlePreferenceClicked(Preference preference) {
50         int subId = mSubId != Integer.MIN_VALUE ? mSubId : getDefaultSubId();
51         AppDataUsageFragment appDataUsageFragment = AppDataUsageFragment.newInstance(subId);
52         getFragmentController().launchFragment(appDataUsageFragment);
53         return true;
54     }
55 
56     /**
57      * Sets the subId for which data usage will be loaded. If this is not set then default subId
58      * will be used to load data.
59      */
setSubId(int subId)60     public void setSubId(int subId) {
61         mSubId = subId;
62     }
63 
getDefaultSubId()64     private int getDefaultSubId() {
65         return DataUsageUtils.getDefaultSubscriptionId(mSubscriptionManager);
66     }
67 }
68