• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.qc.QCItem.QC_TYPE_ACTION_SWITCH;
21 import static com.android.car.settings.qc.QCUtils.getActionDisabledDialogIntent;
22 import static com.android.car.settings.qc.SettingsQCRegistry.MOBILE_DATA_ROW_URI;
23 
24 import android.content.Context;
25 import android.content.Intent;
26 import android.graphics.drawable.Icon;
27 import android.net.Uri;
28 import android.os.UserManager;
29 
30 import androidx.annotation.VisibleForTesting;
31 
32 import com.android.car.qc.QCActionItem;
33 import com.android.car.qc.QCItem;
34 import com.android.car.qc.QCList;
35 import com.android.car.qc.QCRow;
36 import com.android.car.settings.R;
37 import com.android.car.settings.enterprise.EnterpriseUtils;
38 import com.android.settingslib.net.DataUsageController;
39 
40 /**
41  * QCItem for showing a mobile data row element.
42  * The row contains a data icon, the current default network name, and a switch
43  * to enable/disable mobile data.
44  */
45 public class MobileDataRow extends SettingsQCItem {
46     private final DataUsageController mDataUsageController;
47 
MobileDataRow(Context context)48     public MobileDataRow(Context context) {
49         super(context);
50         mDataUsageController = getDataUsageController(context);
51     }
52 
53     @Override
getQCItem()54     QCItem getQCItem() {
55         if (!mDataUsageController.isMobileDataSupported()) {
56             return null;
57         }
58         boolean dataEnabled = mDataUsageController.isMobileDataEnabled();
59         String subtitle = MobileNetworkQCUtils.getMobileNetworkSummary(getContext(), dataEnabled);
60         Icon icon = MobileNetworkQCUtils.getMobileNetworkSignalIcon(getContext());
61 
62         String userRestriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
63         boolean hasDpmRestrictions = EnterpriseUtils.hasUserRestrictionByDpm(getContext(),
64                 userRestriction);
65         boolean hasUmRestrictions = EnterpriseUtils.hasUserRestrictionByUm(getContext(),
66                 userRestriction);
67 
68         QCActionItem dataToggle = new QCActionItem.Builder(QC_TYPE_ACTION_SWITCH)
69                 .setChecked(dataEnabled)
70                 .setAction(getBroadcastIntent())
71                 .setEnabled(!hasUmRestrictions && !hasDpmRestrictions)
72                 .setClickableWhileDisabled(hasDpmRestrictions)
73                 .setDisabledClickAction(getActionDisabledDialogIntent(getContext(),
74                         userRestriction))
75                 .build();
76 
77         QCRow dataRow = new QCRow.Builder()
78                 .setTitle(getContext().getString(R.string.mobile_network_settings))
79                 .setSubtitle(subtitle)
80                 .setIcon(icon)
81                 .addEndItem(dataToggle)
82                 .build();
83 
84         return new QCList.Builder()
85                 .addRow(dataRow)
86                 .build();
87     }
88 
89     @Override
getUri()90     Uri getUri() {
91         return MOBILE_DATA_ROW_URI;
92     }
93 
94     @Override
onNotifyChange(Intent intent)95     void onNotifyChange(Intent intent) {
96         boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE,
97                 !mDataUsageController.isMobileDataEnabled());
98         mDataUsageController.setMobileDataEnabled(newState);
99     }
100 
101     @Override
getBackgroundWorkerClass()102     Class getBackgroundWorkerClass() {
103         return MobileDataRowWorker.class;
104     }
105 
106     @VisibleForTesting
getDataUsageController(Context context)107     DataUsageController getDataUsageController(Context context) {
108         return new DataUsageController(context);
109     }
110 }
111