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