1 /* 2 * Copyright (C) 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.content.Context; 20 import android.net.NetworkPolicyManager; 21 import android.net.NetworkTemplate; 22 import android.os.Bundle; 23 import android.telephony.SubscriptionManager; 24 import android.telephony.TelephonyManager; 25 26 import androidx.annotation.Nullable; 27 import androidx.annotation.XmlRes; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.SettingsFragment; 31 import com.android.settingslib.NetworkPolicyEditor; 32 33 import java.util.Arrays; 34 import java.util.List; 35 36 /** Screen to set data warning and limit thresholds. */ 37 public class DataWarningAndLimitFragment extends SettingsFragment { 38 39 private TelephonyManager mTelephonyManager; 40 private SubscriptionManager mSubscriptionManager; 41 private NetworkPolicyEditor mPolicyEditor; 42 private NetworkTemplate mNetworkTemplate; 43 44 /** 45 * Creates a new instance of {@link DataWarningAndLimitFragment} with the given template. If the 46 * template is {@code null}, the fragment will use the default data network template. 47 */ newInstance(@ullable NetworkTemplate template)48 public static DataWarningAndLimitFragment newInstance(@Nullable NetworkTemplate template) { 49 DataWarningAndLimitFragment fragment = new DataWarningAndLimitFragment(); 50 Bundle args = new Bundle(); 51 args.putParcelable(NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE, template); 52 fragment.setArguments(args); 53 return fragment; 54 } 55 56 @Override 57 @XmlRes getPreferenceScreenResId()58 protected int getPreferenceScreenResId() { 59 return R.xml.data_warning_and_limit_fragment; 60 } 61 62 @Override onAttach(Context context)63 public void onAttach(Context context) { 64 super.onAttach(context); 65 66 mPolicyEditor = new NetworkPolicyEditor(NetworkPolicyManager.from(context)); 67 mNetworkTemplate = getArguments().getParcelable( 68 NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE); 69 if (mNetworkTemplate == null) { 70 mTelephonyManager = context.getSystemService(TelephonyManager.class); 71 mSubscriptionManager = context.getSystemService(SubscriptionManager.class); 72 mNetworkTemplate = DataUsageUtils.getMobileNetworkTemplate(mTelephonyManager, 73 DataUsageUtils.getDefaultSubscriptionId(mSubscriptionManager)); 74 } 75 76 // Loads the current policies to the policy editor cache. 77 mPolicyEditor.read(); 78 79 List<DataWarningAndLimitBasePreferenceController> preferenceControllers = 80 Arrays.asList( 81 use(CycleResetDayOfMonthPickerPreferenceController.class, 82 R.string.pk_data_usage_cycle), 83 use(DataWarningPreferenceController.class, R.string.pk_data_warning_group), 84 use(DataLimitPreferenceController.class, R.string.pk_data_limit_group)); 85 86 for (DataWarningAndLimitBasePreferenceController preferenceController : 87 preferenceControllers) { 88 preferenceController.setNetworkPolicyEditor(mPolicyEditor); 89 preferenceController.setNetworkTemplate(mNetworkTemplate); 90 } 91 } 92 } 93