• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.network;
17 
18 import static com.android.settings.network.MobilePlanPreferenceController.MANAGE_MOBILE_PLAN_DIALOG_ID;
19 
20 import android.app.Dialog;
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.provider.SearchIndexableResource;
25 import android.util.Log;
26 
27 import androidx.appcompat.app.AlertDialog;
28 import androidx.fragment.app.Fragment;
29 
30 import com.android.settings.R;
31 import com.android.settings.Utils;
32 import com.android.settings.dashboard.DashboardFragment;
33 import com.android.settings.network.MobilePlanPreferenceController.MobilePlanPreferenceHost;
34 import com.android.settings.search.BaseSearchIndexProvider;
35 import com.android.settings.wifi.WifiPrimarySwitchPreferenceController;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
38 import com.android.settingslib.core.lifecycle.Lifecycle;
39 import com.android.settingslib.search.SearchIndexable;
40 
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.List;
44 
45 @SearchIndexable
46 public class NetworkDashboardFragment extends DashboardFragment implements
47         MobilePlanPreferenceHost {
48 
49     private static final String TAG = "NetworkDashboardFrag";
50 
51     @Override
getMetricsCategory()52     public int getMetricsCategory() {
53         return SettingsEnums.SETTINGS_NETWORK_CATEGORY;
54     }
55 
56     @Override
getLogTag()57     protected String getLogTag() {
58         return TAG;
59     }
60 
61     @Override
getPreferenceScreenResId()62     protected int getPreferenceScreenResId() {
63         if (Utils.isProviderModelEnabled(getContext())) {
64             return R.xml.network_provider_internet;
65         } else {
66             return R.xml.network_and_internet;
67         }
68     }
69 
70     @Override
onAttach(Context context)71     public void onAttach(Context context) {
72         super.onAttach(context);
73 
74         if (!Utils.isProviderModelEnabled(context)) {
75             use(MultiNetworkHeaderController.class).init(getSettingsLifecycle());
76         }
77         use(AirplaneModePreferenceController.class).setFragment(this);
78         getSettingsLifecycle().addObserver(use(AllInOneTetherPreferenceController.class));
79     }
80 
81     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)82     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
83         super.onCreatePreferences(savedInstanceState, rootKey);
84         use(AllInOneTetherPreferenceController.class).initEnabler(getSettingsLifecycle());
85     }
86 
87     @Override
getHelpResource()88     public int getHelpResource() {
89         return R.string.help_url_network_dashboard;
90     }
91 
92     @Override
createPreferenceControllers(Context context)93     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
94         return buildPreferenceControllers(context, getSettingsLifecycle(), mMetricsFeatureProvider,
95                 this /* fragment */, this /* mobilePlanHost */);
96     }
97 
buildPreferenceControllers(Context context, Lifecycle lifecycle, MetricsFeatureProvider metricsFeatureProvider, Fragment fragment, MobilePlanPreferenceHost mobilePlanHost)98     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
99             Lifecycle lifecycle, MetricsFeatureProvider metricsFeatureProvider, Fragment fragment,
100             MobilePlanPreferenceHost mobilePlanHost) {
101         final MobilePlanPreferenceController mobilePlanPreferenceController =
102                 new MobilePlanPreferenceController(context, mobilePlanHost);
103         final WifiPrimarySwitchPreferenceController wifiPreferenceController =
104                 Utils.isProviderModelEnabled(context)
105                         ? null
106                         : new WifiPrimarySwitchPreferenceController(
107                                 context,
108                                 metricsFeatureProvider);
109         final InternetPreferenceController internetPreferenceController =
110                 Utils.isProviderModelEnabled(context)
111                         ? new InternetPreferenceController(context, lifecycle)
112                         : null;
113 
114         final VpnPreferenceController vpnPreferenceController =
115                 new VpnPreferenceController(context);
116         final PrivateDnsPreferenceController privateDnsPreferenceController =
117                 new PrivateDnsPreferenceController(context);
118 
119         if (lifecycle != null) {
120             lifecycle.addObserver(mobilePlanPreferenceController);
121             if (wifiPreferenceController != null) {
122                 lifecycle.addObserver(wifiPreferenceController);
123             }
124             lifecycle.addObserver(vpnPreferenceController);
125             lifecycle.addObserver(privateDnsPreferenceController);
126         }
127 
128         final List<AbstractPreferenceController> controllers = new ArrayList<>();
129 
130         controllers.add(new MobileNetworkSummaryController(context, lifecycle));
131         controllers.add(new TetherPreferenceController(context, lifecycle));
132         controllers.add(vpnPreferenceController);
133         controllers.add(new ProxyPreferenceController(context));
134         controllers.add(mobilePlanPreferenceController);
135         if (wifiPreferenceController != null) {
136             controllers.add(wifiPreferenceController);
137         }
138         if (internetPreferenceController != null) {
139             controllers.add(internetPreferenceController);
140         }
141         controllers.add(privateDnsPreferenceController);
142         if (Utils.isProviderModelEnabled(context)) {
143             controllers.add(new NetworkProviderCallsSmsController(context, lifecycle));
144         }
145         return controllers;
146     }
147 
148     @Override
showMobilePlanMessageDialog()149     public void showMobilePlanMessageDialog() {
150         showDialog(MANAGE_MOBILE_PLAN_DIALOG_ID);
151     }
152 
153     @Override
onCreateDialog(int dialogId)154     public Dialog onCreateDialog(int dialogId) {
155         Log.d(TAG, "onCreateDialog: dialogId=" + dialogId);
156         switch (dialogId) {
157             case MANAGE_MOBILE_PLAN_DIALOG_ID:
158                 final MobilePlanPreferenceController controller =
159                         use(MobilePlanPreferenceController.class);
160                 return new AlertDialog.Builder(getActivity())
161                         .setMessage(controller.getMobilePlanDialogMessage())
162                         .setCancelable(false)
163                         .setPositiveButton(com.android.internal.R.string.ok,
164                                 (dialog, id) -> controller.setMobilePlanDialogMessage(null))
165                         .create();
166         }
167         return super.onCreateDialog(dialogId);
168     }
169 
170     @Override
getDialogMetricsCategory(int dialogId)171     public int getDialogMetricsCategory(int dialogId) {
172         if (MANAGE_MOBILE_PLAN_DIALOG_ID == dialogId) {
173             return SettingsEnums.DIALOG_MANAGE_MOBILE_PLAN;
174         }
175         return 0;
176     }
177 
178     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
179             new BaseSearchIndexProvider(R.xml.network_and_internet) {
180 
181                 @Override
182                 // TODO(b/167474581): Should remove this method when Provider Model finished.
183                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
184                         boolean enabled) {
185                     if (Utils.isProviderModelEnabled(context)) {
186                         final SearchIndexableResource sir = new SearchIndexableResource(context);
187                         sir.xmlResId = R.xml.network_provider_internet;
188                         return Arrays.asList(sir);
189                     }
190                     return super.getXmlResourcesToIndex(context, enabled);
191                 }
192 
193                 @Override
194                 public List<AbstractPreferenceController> createPreferenceControllers(Context
195                         context) {
196                     return buildPreferenceControllers(context, null /* lifecycle */,
197                             null /* metricsFeatureProvider */, null /* fragment */,
198                             null /* mobilePlanHost */);
199                 }
200             };
201 }
202