1 /*
2  * Copyright (C) 2021 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.qc;
18 
19 import static com.android.car.qc.QCItem.QC_ACTION_TOGGLE_STATE;
20 import static com.android.car.settings.qc.SettingsQCRegistry.MOBILE_DATA_TILE_URI;
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.graphics.drawable.Icon;
25 import android.net.Uri;
26 import android.telephony.TelephonyManager;
27 import android.text.TextUtils;
28 
29 import androidx.annotation.VisibleForTesting;
30 
31 import com.android.car.qc.QCItem;
32 import com.android.car.qc.QCTile;
33 import com.android.car.settings.R;
34 import com.android.settingslib.net.DataUsageController;
35 
36 /**
37  * QCItem for showing a mobile data toggle.
38  */
39 public class MobileDataTile extends SettingsQCItem {
40 
41     private final DataUsageController mDataUsageController;
42 
MobileDataTile(Context context)43     public MobileDataTile(Context context) {
44         super(context);
45         mDataUsageController = getDataUsageController(context);
46     }
47 
48     @Override
getQCItem()49     QCItem getQCItem() {
50         if (!mDataUsageController.isMobileDataSupported()) {
51             return null;
52         }
53         TelephonyManager manager = getContext().getSystemService(TelephonyManager.class);
54         String subtitle = manager.getNetworkOperatorName();
55         if (TextUtils.isEmpty(subtitle)) {
56             subtitle = getContext().getString(R.string.mobile_network_toggle_title);
57         }
58         Icon icon = MobileNetworkQCUtils.getMobileNetworkSignalIcon(getContext());
59 
60         return new QCTile.Builder()
61                 .setIcon(icon)
62                 .setChecked(mDataUsageController.isMobileDataEnabled())
63                 .setAction(getBroadcastIntent())
64                 .setSubtitle(subtitle)
65                 .build();
66     }
67 
68     @Override
getUri()69     Uri getUri() {
70         return MOBILE_DATA_TILE_URI;
71     }
72 
73     @Override
onNotifyChange(Intent intent)74     void onNotifyChange(Intent intent) {
75         boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE,
76                 !mDataUsageController.isMobileDataEnabled());
77         mDataUsageController.setMobileDataEnabled(newState);
78     }
79 
80     @Override
getBackgroundWorkerClass()81     Class getBackgroundWorkerClass() {
82         return MobileDataTileWorker.class;
83     }
84 
85     @VisibleForTesting
getDataUsageController(Context context)86     DataUsageController getDataUsageController(Context context) {
87         return new DataUsageController(context);
88     }
89 }
90