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