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