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.provider.SearchIndexableResource;
24 import android.util.Log;
25 
26 import androidx.appcompat.app.AlertDialog;
27 import androidx.fragment.app.Fragment;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.FeatureFlags;
31 import com.android.settings.dashboard.DashboardFragment;
32 import com.android.settings.development.featureflags.FeatureFlagPersistent;
33 import com.android.settings.network.MobilePlanPreferenceController.MobilePlanPreferenceHost;
34 import com.android.settings.search.BaseSearchIndexProvider;
35 import com.android.settings.wifi.WifiMasterSwitchPreferenceController;
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 (FeatureFlagPersistent.isEnabled(getContext(), FeatureFlags.NETWORK_INTERNET_V2)) {
64             return R.xml.network_and_internet_v2;
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 (FeatureFlagPersistent.isEnabled(context, FeatureFlags.NETWORK_INTERNET_V2)) {
75             use(MultiNetworkHeaderController.class).init(getSettingsLifecycle());
76         }
77         use(AirplaneModePreferenceController.class).setFragment(this);
78     }
79 
80     @Override
getHelpResource()81     public int getHelpResource() {
82         return R.string.help_url_network_dashboard;
83     }
84 
85     @Override
createPreferenceControllers(Context context)86     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
87         return buildPreferenceControllers(context, getSettingsLifecycle(), mMetricsFeatureProvider,
88                 this /* fragment */, this /* mobilePlanHost */);
89     }
90 
buildPreferenceControllers(Context context, Lifecycle lifecycle, MetricsFeatureProvider metricsFeatureProvider, Fragment fragment, MobilePlanPreferenceHost mobilePlanHost)91     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
92             Lifecycle lifecycle, MetricsFeatureProvider metricsFeatureProvider, Fragment fragment,
93             MobilePlanPreferenceHost mobilePlanHost) {
94         final MobilePlanPreferenceController mobilePlanPreferenceController =
95                 new MobilePlanPreferenceController(context, mobilePlanHost);
96         final WifiMasterSwitchPreferenceController wifiPreferenceController =
97                 new WifiMasterSwitchPreferenceController(context, metricsFeatureProvider);
98         MobileNetworkPreferenceController mobileNetworkPreferenceController = null;
99         if (!FeatureFlagPersistent.isEnabled(context, FeatureFlags.NETWORK_INTERNET_V2)) {
100             mobileNetworkPreferenceController = new MobileNetworkPreferenceController(context);
101         }
102 
103         final VpnPreferenceController vpnPreferenceController =
104                 new VpnPreferenceController(context);
105         final PrivateDnsPreferenceController privateDnsPreferenceController =
106                 new PrivateDnsPreferenceController(context);
107 
108         if (lifecycle != null) {
109             lifecycle.addObserver(mobilePlanPreferenceController);
110             lifecycle.addObserver(wifiPreferenceController);
111             if (mobileNetworkPreferenceController != null) {
112                 lifecycle.addObserver(mobileNetworkPreferenceController);
113             }
114             lifecycle.addObserver(vpnPreferenceController);
115             lifecycle.addObserver(privateDnsPreferenceController);
116         }
117 
118         final List<AbstractPreferenceController> controllers = new ArrayList<>();
119 
120         if (FeatureFlagPersistent.isEnabled(context, FeatureFlags.NETWORK_INTERNET_V2)) {
121             controllers.add(new MobileNetworkSummaryController(context, lifecycle));
122         }
123         if (mobileNetworkPreferenceController != null) {
124             controllers.add(mobileNetworkPreferenceController);
125         }
126         controllers.add(new TetherPreferenceController(context, lifecycle));
127         controllers.add(vpnPreferenceController);
128         controllers.add(new ProxyPreferenceController(context));
129         controllers.add(mobilePlanPreferenceController);
130         controllers.add(wifiPreferenceController);
131         controllers.add(privateDnsPreferenceController);
132         return controllers;
133     }
134 
135     @Override
showMobilePlanMessageDialog()136     public void showMobilePlanMessageDialog() {
137         showDialog(MANAGE_MOBILE_PLAN_DIALOG_ID);
138     }
139 
140     @Override
onCreateDialog(int dialogId)141     public Dialog onCreateDialog(int dialogId) {
142         Log.d(TAG, "onCreateDialog: dialogId=" + dialogId);
143         switch (dialogId) {
144             case MANAGE_MOBILE_PLAN_DIALOG_ID:
145                 final MobilePlanPreferenceController controller =
146                         use(MobilePlanPreferenceController.class);
147                 return new AlertDialog.Builder(getActivity())
148                         .setMessage(controller.getMobilePlanDialogMessage())
149                         .setCancelable(false)
150                         .setPositiveButton(com.android.internal.R.string.ok,
151                                 (dialog, id) -> controller.setMobilePlanDialogMessage(null))
152                         .create();
153         }
154         return super.onCreateDialog(dialogId);
155     }
156 
157     @Override
getDialogMetricsCategory(int dialogId)158     public int getDialogMetricsCategory(int dialogId) {
159         if (MANAGE_MOBILE_PLAN_DIALOG_ID == dialogId) {
160             return SettingsEnums.DIALOG_MANAGE_MOBILE_PLAN;
161         }
162         return 0;
163     }
164 
165     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
166             new BaseSearchIndexProvider() {
167                 @Override
168                 public List<SearchIndexableResource> getXmlResourcesToIndex(
169                         Context context, boolean enabled) {
170                     final SearchIndexableResource sir = new SearchIndexableResource(context);
171                     if (FeatureFlagPersistent.isEnabled(context,
172                             FeatureFlags.NETWORK_INTERNET_V2)) {
173                         sir.xmlResId = R.xml.network_and_internet_v2;
174                     } else {
175                         sir.xmlResId = R.xml.network_and_internet;
176                     }
177                     return Arrays.asList(sir);
178                 }
179 
180                 @Override
181                 public List<AbstractPreferenceController> createPreferenceControllers(Context
182                         context) {
183                     return buildPreferenceControllers(context, null /* lifecycle */,
184                             null /* metricsFeatureProvider */, null /* fragment */,
185                             null /* mobilePlanHost */);
186                 }
187 
188                 @Override
189                 public List<String> getNonIndexableKeys(Context context) {
190                     List<String> keys = super.getNonIndexableKeys(context);
191                     // Remove master switch as a result
192                     keys.add(WifiMasterSwitchPreferenceController.KEY_TOGGLE_WIFI);
193                     return keys;
194                 }
195             };
196 }
197