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.savedaccesspoints;
18 
19 import android.annotation.Nullable;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.os.Bundle;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.SubSettingLauncher;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.wifi.WifiSettings;
31 import com.android.settings.wifi.details.WifiNetworkDetailsFragment;
32 import com.android.settingslib.wifi.AccessPoint;
33 import com.android.settingslib.wifi.AccessPointPreference;
34 
35 /**
36  * UI to manage saved networks/access points.
37  *
38  * Migrating from Wi-Fi SettingsLib to to WifiTrackerLib, this object will be removed in the near
39  * future, please develop in
40  * {@link com.android.settings.wifi.savedaccesspoints2.SavedAccessPointsWifiSettings2}.
41  */
42 public class SavedAccessPointsWifiSettings extends DashboardFragment {
43 
44     private static final String TAG = "SavedAccessPoints";
45 
46     @VisibleForTesting
47     Bundle mAccessPointSavedState;
48     private AccessPoint mSelectedAccessPoint;
49 
50     // Instance state key
51     private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state";
52 
53     @Override
getMetricsCategory()54     public int getMetricsCategory() {
55         return SettingsEnums.WIFI_SAVED_ACCESS_POINTS;
56     }
57 
58     @Override
getPreferenceScreenResId()59     protected int getPreferenceScreenResId() {
60         return R.xml.wifi_display_saved_access_points;
61     }
62 
63     @Override
getLogTag()64     protected String getLogTag() {
65         return TAG;
66     }
67 
68     @Override
onAttach(Context context)69     public void onAttach(Context context) {
70         super.onAttach(context);
71         use(SavedAccessPointsPreferenceController.class)
72                 .setHost(this);
73         use(SubscribedAccessPointsPreferenceController.class)
74                 .setHost(this);
75     }
76 
77     @Override
onCreate(Bundle savedInstanceState)78     public void onCreate(Bundle savedInstanceState) {
79         super.onCreate(savedInstanceState);
80         if (savedInstanceState != null) {
81             if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
82                 mAccessPointSavedState =
83                         savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE);
84             } else {
85                 mAccessPointSavedState = null;
86             }
87         }
88     }
89 
90     @Override
onStart()91     public void onStart() {
92         super.onStart();
93         if (mAccessPointSavedState != null) {
94             final PreferenceScreen screen = getPreferenceScreen();
95             use(SavedAccessPointsPreferenceController.class).displayPreference(screen);
96             use(SubscribedAccessPointsPreferenceController.class).displayPreference(screen);
97         }
98     }
99 
showWifiPage(@ullable AccessPointPreference accessPoint)100     public void showWifiPage(@Nullable AccessPointPreference accessPoint) {
101         removeDialog(WifiSettings.WIFI_DIALOG_ID);
102 
103         if (accessPoint != null) {
104             // Save the access point and edit mode
105             mSelectedAccessPoint = accessPoint.getAccessPoint();
106         } else {
107             // No access point is selected. Clear saved state.
108             mSelectedAccessPoint = null;
109             mAccessPointSavedState = null;
110         }
111 
112         if (mSelectedAccessPoint == null) {
113             mSelectedAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState);
114         }
115         final Bundle savedState = new Bundle();
116         mSelectedAccessPoint.saveWifiState(savedState);
117 
118         new SubSettingLauncher(getContext())
119                 .setTitleText(mSelectedAccessPoint.getTitle())
120                 .setDestination(WifiNetworkDetailsFragment.class.getName())
121                 .setArguments(savedState)
122                 .setSourceMetricsCategory(getMetricsCategory())
123                 .launch();
124     }
125 
126     @Override
onSaveInstanceState(Bundle outState)127     public void onSaveInstanceState(Bundle outState) {
128         super.onSaveInstanceState(outState);
129         // If the dialog is showing (indicated by the existence of mSelectedAccessPoint), then we
130         // save its state.
131         if (mSelectedAccessPoint != null) {
132             mAccessPointSavedState = new Bundle();
133             mSelectedAccessPoint.saveWifiState(mAccessPointSavedState);
134             outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState);
135         }
136     }
137 }
138