1 /* 2 * Copyright (C) 2014 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.settings.wifi; 18 19 import android.app.Dialog; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.net.wifi.WifiManager; 23 import android.os.Bundle; 24 import android.support.v7.preference.Preference; 25 import android.support.v7.preference.PreferenceScreen; 26 import android.util.Log; 27 28 import com.android.internal.logging.MetricsProto.MetricsEvent; 29 import com.android.settings.R; 30 import com.android.settings.SettingsPreferenceFragment; 31 import com.android.settings.search.BaseSearchIndexProvider; 32 import com.android.settings.search.Indexable; 33 import com.android.settings.search.SearchIndexableRaw; 34 import com.android.settingslib.wifi.AccessPoint; 35 import com.android.settingslib.wifi.AccessPointPreference; 36 import com.android.settingslib.wifi.WifiTracker; 37 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.Comparator; 41 import java.util.List; 42 43 /** 44 * UI to manage saved networks/access points. 45 */ 46 public class SavedAccessPointsWifiSettings extends SettingsPreferenceFragment 47 implements Indexable, WifiDialog.WifiDialogListener { 48 private static final String TAG = "SavedAccessPointsWifiSettings"; 49 50 private WifiDialog mDialog; 51 private WifiManager mWifiManager; 52 private AccessPoint mDlgAccessPoint; 53 private Bundle mAccessPointSavedState; 54 private AccessPoint mSelectedAccessPoint; 55 56 private AccessPointPreference.UserBadgeCache mUserBadgeCache; 57 58 // Instance state key 59 private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state"; 60 61 @Override getMetricsCategory()62 protected int getMetricsCategory() { 63 return MetricsEvent.WIFI_SAVED_ACCESS_POINTS; 64 } 65 66 @Override onCreate(Bundle savedInstanceState)67 public void onCreate(Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 addPreferencesFromResource(R.xml.wifi_display_saved_access_points); 70 mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager()); 71 } 72 73 @Override onResume()74 public void onResume() { 75 super.onResume(); 76 initPreferences(); 77 } 78 79 @Override onActivityCreated(Bundle savedInstanceState)80 public void onActivityCreated(Bundle savedInstanceState) { 81 super.onActivityCreated(savedInstanceState); 82 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 83 84 if (savedInstanceState != null) { 85 if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) { 86 mAccessPointSavedState = 87 savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE); 88 } 89 } 90 } 91 initPreferences()92 private void initPreferences() { 93 PreferenceScreen preferenceScreen = getPreferenceScreen(); 94 final Context context = getPrefContext(); 95 96 final List<AccessPoint> accessPoints = WifiTracker.getCurrentAccessPoints(context, true, 97 false, true); 98 Collections.sort(accessPoints, new Comparator<AccessPoint>() { 99 public int compare(AccessPoint ap1, AccessPoint ap2) { 100 if (ap1.getConfigName() != null) { 101 return ap1.getConfigName().compareTo(ap2.getConfigName()); 102 } else { 103 return -1; 104 } 105 } 106 }); 107 preferenceScreen.removeAll(); 108 109 final int accessPointsSize = accessPoints.size(); 110 for (int i = 0; i < accessPointsSize; ++i){ 111 LongPressAccessPointPreference preference = 112 new LongPressAccessPointPreference(accessPoints.get(i), context, 113 mUserBadgeCache, true, this); 114 preference.setIcon(null); 115 preferenceScreen.addPreference(preference); 116 } 117 118 if(getPreferenceScreen().getPreferenceCount() < 1) { 119 Log.w(TAG, "Saved networks activity loaded, but there are no saved networks!"); 120 } 121 } 122 showDialog(LongPressAccessPointPreference accessPoint, boolean edit)123 private void showDialog(LongPressAccessPointPreference accessPoint, boolean edit) { 124 if (mDialog != null) { 125 removeDialog(WifiSettings.WIFI_DIALOG_ID); 126 mDialog = null; 127 } 128 129 // Save the access point and edit mode 130 mDlgAccessPoint = accessPoint.getAccessPoint(); 131 132 showDialog(WifiSettings.WIFI_DIALOG_ID); 133 } 134 135 @Override onCreateDialog(int dialogId)136 public Dialog onCreateDialog(int dialogId) { 137 switch (dialogId) { 138 case WifiSettings.WIFI_DIALOG_ID: 139 if (mDlgAccessPoint == null) { // For re-launch from saved state 140 mDlgAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState); 141 // Reset the saved access point data 142 mAccessPointSavedState = null; 143 } 144 mSelectedAccessPoint = mDlgAccessPoint; 145 146 mDialog = new WifiDialog(getActivity(), this, mDlgAccessPoint, 147 WifiConfigUiBase.MODE_VIEW, true /* hide the submit button */); 148 return mDialog; 149 150 } 151 return super.onCreateDialog(dialogId); 152 } 153 154 @Override onSaveInstanceState(Bundle outState)155 public void onSaveInstanceState(Bundle outState) { 156 super.onSaveInstanceState(outState); 157 158 // If the dialog is showing, save its state. 159 if (mDialog != null && mDialog.isShowing()) { 160 if (mDlgAccessPoint != null) { 161 mAccessPointSavedState = new Bundle(); 162 mDlgAccessPoint.saveWifiState(mAccessPointSavedState); 163 outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState); 164 } 165 } 166 } 167 168 @Override onForget(WifiDialog dialog)169 public void onForget(WifiDialog dialog) { 170 if (mSelectedAccessPoint != null) { 171 mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId, null); 172 getPreferenceScreen().removePreference((Preference) mSelectedAccessPoint.getTag()); 173 mSelectedAccessPoint = null; 174 } 175 } 176 177 @Override onSubmit(WifiDialog dialog)178 public void onSubmit(WifiDialog dialog) { 179 // Ignored 180 } 181 182 @Override onPreferenceTreeClick(Preference preference)183 public boolean onPreferenceTreeClick(Preference preference) { 184 if (preference instanceof LongPressAccessPointPreference) { 185 showDialog((LongPressAccessPointPreference) preference, false); 186 return true; 187 } else{ 188 return super.onPreferenceTreeClick(preference); 189 } 190 } 191 192 /** 193 * For search. 194 */ 195 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 196 new BaseSearchIndexProvider() { 197 @Override 198 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 199 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 200 final Resources res = context.getResources(); 201 final String title = res.getString(R.string.wifi_saved_access_points_titlebar); 202 203 // Add fragment title 204 SearchIndexableRaw data = new SearchIndexableRaw(context); 205 data.title = title; 206 data.screenTitle = title; 207 data.enabled = enabled; 208 result.add(data); 209 210 // Add available Wi-Fi access points 211 final List<AccessPoint> accessPoints = WifiTracker.getCurrentAccessPoints(context, 212 true, false, true); 213 214 final int accessPointsSize = accessPoints.size(); 215 for (int i = 0; i < accessPointsSize; ++i){ 216 data = new SearchIndexableRaw(context); 217 data.title = accessPoints.get(i).getSsidStr(); 218 data.screenTitle = title; 219 data.enabled = enabled; 220 result.add(data); 221 } 222 223 return result; 224 } 225 }; 226 } 227