1 /*
2  * Copyright (C) 2015 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.wifi;
17 
18 import static android.content.Context.WIFI_SERVICE;
19 
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.ConnectivityManager;
24 import android.net.NetworkInfo;
25 import android.net.wifi.WifiManager;
26 import android.provider.SearchIndexableResource;
27 
28 import com.android.settings.R;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settings.search.Indexable;
32 import com.android.settings.wifi.p2p.WifiP2pPreferenceController;
33 import com.android.settingslib.core.AbstractPreferenceController;
34 import com.android.settingslib.search.SearchIndexable;
35 
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 
40 @SearchIndexable
41 public class ConfigureWifiSettings extends DashboardFragment {
42 
43     private static final String TAG = "ConfigureWifiSettings";
44 
45     public static final String KEY_IP_ADDRESS = "current_ip_address";
46     public static final int WIFI_WAKEUP_REQUEST_CODE = 600;
47 
48     private WifiWakeupPreferenceController mWifiWakeupPreferenceController;
49     private UseOpenWifiPreferenceController mUseOpenWifiPreferenceController;
50 
51     @Override
getMetricsCategory()52     public int getMetricsCategory() {
53         return SettingsEnums.CONFIGURE_WIFI;
54     }
55 
56     @Override
getLogTag()57     protected String getLogTag() {
58         return TAG;
59     }
60 
61     @Override
getInitialExpandedChildCount()62     public int getInitialExpandedChildCount() {
63         int tileLimit = 2;
64         if (mUseOpenWifiPreferenceController.isAvailable()) {
65             tileLimit++;
66         }
67         return tileLimit;
68     }
69 
70     @Override
getPreferenceScreenResId()71     protected int getPreferenceScreenResId() {
72         return R.xml.wifi_configure_settings;
73     }
74 
75     @Override
createPreferenceControllers(Context context)76     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
77         mWifiWakeupPreferenceController = new WifiWakeupPreferenceController(context, this,
78                 getSettingsLifecycle());
79         mUseOpenWifiPreferenceController = new UseOpenWifiPreferenceController(context, this,
80                 getSettingsLifecycle());
81         final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
82         final List<AbstractPreferenceController> controllers = new ArrayList<>();
83         controllers.add(mWifiWakeupPreferenceController);
84         controllers.add(new NotifyOpenNetworksPreferenceController(context,
85                 getSettingsLifecycle()));
86         controllers.add(mUseOpenWifiPreferenceController);
87         controllers.add(new WifiInfoPreferenceController(context, getSettingsLifecycle(),
88                 wifiManager));
89         controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),
90                 wifiManager));
91         return controllers;
92     }
93 
94     @Override
onActivityResult(int requestCode, int resultCode, Intent data)95     public void onActivityResult(int requestCode, int resultCode, Intent data) {
96         if (requestCode == WIFI_WAKEUP_REQUEST_CODE && mWifiWakeupPreferenceController != null) {
97             mWifiWakeupPreferenceController.onActivityResult(requestCode, resultCode);
98             return;
99         }
100         if (requestCode == UseOpenWifiPreferenceController.REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY
101                 && mUseOpenWifiPreferenceController != null) {
102             mUseOpenWifiPreferenceController.onActivityResult(requestCode, resultCode);
103             return;
104         }
105         super.onActivityResult(requestCode, resultCode, data);
106     }
107 
108     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
109             new BaseSearchIndexProvider() {
110                 @Override
111                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
112                         boolean enabled) {
113                     final SearchIndexableResource sir = new SearchIndexableResource(context);
114                     sir.xmlResId = R.xml.wifi_configure_settings;
115                     return Arrays.asList(sir);
116                 }
117 
118                 @Override
119                 public List<String> getNonIndexableKeys(Context context) {
120                     List<String> keys = super.getNonIndexableKeys(context);
121 
122                     // If connected to WiFi, this IP address will be the same as the Status IP.
123                     // Or, if there is no connection they will say unavailable.
124                     ConnectivityManager cm = (ConnectivityManager)
125                             context.getSystemService(Context.CONNECTIVITY_SERVICE);
126                     NetworkInfo info = cm.getActiveNetworkInfo();
127                     if (info == null
128                             || info.getType() == ConnectivityManager.TYPE_WIFI) {
129                         keys.add(KEY_IP_ADDRESS);
130                     }
131 
132                     return keys;
133                 }
134 
135                 protected boolean isPageSearchEnabled(Context context) {
136                     return context.getResources()
137                             .getBoolean(R.bool.config_show_wifi_settings);
138                 }
139             };
140 }
141