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.QCUtils.getActionDisabledDialogIntent;
21 import static com.android.car.settings.qc.QCUtils.getAvailabilityStatusForZoneFromXml;
22 import static com.android.car.settings.qc.SettingsQCRegistry.MOBILE_DATA_TILE_URI;
23 
24 import android.app.PendingIntent;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.graphics.drawable.Icon;
28 import android.net.Uri;
29 import android.os.UserManager;
30 
31 import androidx.annotation.VisibleForTesting;
32 
33 import com.android.car.qc.QCItem;
34 import com.android.car.qc.QCTile;
35 import com.android.car.settings.R;
36 import com.android.car.settings.enterprise.EnterpriseUtils;
37 import com.android.settingslib.net.DataUsageController;
38 
39 /**
40  * QCItem for showing a mobile data toggle.
41  */
42 public class MobileDataTile extends SettingsQCItem {
43 
44     private final DataUsageController mDataUsageController;
45 
MobileDataTile(Context context)46     public MobileDataTile(Context context) {
47         super(context);
48         setAvailabilityStatusForZone(getAvailabilityStatusForZoneFromXml(context,
49                 R.xml.network_and_internet_fragment, R.string.pk_mobile_network_settings_entry));
50         mDataUsageController = getDataUsageController(context);
51     }
52 
53     @Override
getQCItem()54     QCItem getQCItem() {
55         if (!mDataUsageController.isMobileDataSupported() || isHiddenForZone()) {
56             return null;
57         }
58         boolean dataEnabled = mDataUsageController.isMobileDataEnabled();
59         String subtitle = MobileNetworkQCUtils.getMobileNetworkSummary(getContext(), dataEnabled);
60         if (subtitle == null) {
61             subtitle = getContext().getString(R.string.mobile_network_toggle_title);
62         }
63         Icon icon = MobileNetworkQCUtils.getMobileNetworkSignalIcon(getContext());
64 
65         String userRestriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
66         boolean hasDpmRestrictions = EnterpriseUtils.hasUserRestrictionByDpm(getContext(),
67                 userRestriction);
68         boolean hasUmRestrictions = EnterpriseUtils.hasUserRestrictionByUm(getContext(),
69                 userRestriction);
70 
71         boolean isReadOnlyForZone = isReadOnlyForZone();
72         PendingIntent disabledPendingIntent = isReadOnlyForZone
73                 ? QCUtils.getDisabledToastBroadcastIntent(getContext())
74                 : getActionDisabledDialogIntent(getContext(), userRestriction);
75 
76         return new QCTile.Builder()
77                 .setIcon(icon)
78                 .setChecked(dataEnabled)
79                 .setAction(getBroadcastIntent())
80                 .setSubtitle(subtitle)
81                 .setEnabled(!hasUmRestrictions && !hasDpmRestrictions && isWritableForZone())
82                 .setClickableWhileDisabled(hasDpmRestrictions | isReadOnlyForZone)
83                 .setDisabledClickAction(disabledPendingIntent)
84                 .build();
85     }
86 
87     @Override
getUri()88     Uri getUri() {
89         return MOBILE_DATA_TILE_URI;
90     }
91 
92     @Override
onNotifyChange(Intent intent)93     void onNotifyChange(Intent intent) {
94         boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE,
95                 !mDataUsageController.isMobileDataEnabled());
96         mDataUsageController.setMobileDataEnabled(newState);
97     }
98 
99     @Override
getBackgroundWorkerClass()100     Class getBackgroundWorkerClass() {
101         return MobileDataTileWorker.class;
102     }
103 
104     @VisibleForTesting
getDataUsageController(Context context)105     DataUsageController getDataUsageController(Context context) {
106         return new DataUsageController(context);
107     }
108 }
109