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.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.wifi.WifiManager;
25 import android.os.Bundle;
26 import android.util.FeatureFlagUtils;
27 import android.util.Log;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.search.BaseSearchIndexProvider;
35 import com.android.settings.wifi.p2p.WifiP2pPreferenceController;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 import com.android.settingslib.search.SearchIndexable;
38 
39 import java.util.ArrayList;
40 import java.util.List;
41 
42 @SearchIndexable
43 public class ConfigureWifiSettings extends DashboardFragment {
44 
45     private static final String TAG = "ConfigureWifiSettings";
46     private static final String KEY_INSTALL_CREDENTIALS = "install_credentials";
47     private static final String ACTION_INSTALL_CERTS = "android.credentials.INSTALL";
48     private static final String PACKAGE_INSTALL_CERTS = "com.android.certinstaller";
49     private static final String CLASS_INSTALL_CERTS = "com.android.certinstaller.CertInstallerMain";
50     private static final String KEY_INSTALL_CERTIFICATE = "certificate_install_usage";
51     private static final String INSTALL_CERTIFICATE_VALUE = "wifi";
52 
53     public static final int WIFI_WAKEUP_REQUEST_CODE = 600;
54 
55     private WifiWakeupPreferenceController mWifiWakeupPreferenceController;
56     private Preference mCertinstallerPreference;
57 
58     @Override
onCreate(Bundle icicle)59     public void onCreate(Bundle icicle) {
60         super.onCreate(icicle);
61         getActivity().setTitle(R.string.network_and_internet_preferences_title);
62 
63         mCertinstallerPreference = findPreference(KEY_INSTALL_CREDENTIALS);
64         if (mCertinstallerPreference != null) {
65             mCertinstallerPreference.setOnPreferenceClickListener(preference -> {
66                 Intent intent = new Intent(ACTION_INSTALL_CERTS);
67                 intent.setFlags(
68                         Intent.FLAG_ACTIVITY_NEW_TASK);
69                 intent.setComponent(
70                         new ComponentName(PACKAGE_INSTALL_CERTS, CLASS_INSTALL_CERTS));
71                 intent.putExtra(KEY_INSTALL_CERTIFICATE, INSTALL_CERTIFICATE_VALUE);
72                 getContext().startActivity(intent);
73                 return true;
74             });
75         } else {
76             Log.d(TAG, "Can not find the preference.");
77         }
78     }
79 
80     @Override
getMetricsCategory()81     public int getMetricsCategory() {
82         return SettingsEnums.CONFIGURE_WIFI;
83     }
84 
85     @Override
getLogTag()86     protected String getLogTag() {
87         return TAG;
88     }
89 
90     @Override
getPreferenceScreenResId()91     protected int getPreferenceScreenResId() {
92         return R.xml.wifi_configure_settings;
93     }
94 
95     @Override
createPreferenceControllers(Context context)96     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
97         final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
98         final List<AbstractPreferenceController> controllers = new ArrayList<>();
99         controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),
100                 wifiManager));
101         return controllers;
102     }
103 
104     @Override
onAttach(Context context)105     public void onAttach(Context context) {
106         super.onAttach(context);
107 
108 
109         mWifiWakeupPreferenceController = use(WifiWakeupPreferenceController.class);
110         mWifiWakeupPreferenceController.setFragment(this);
111     }
112 
113     @Override
onActivityResult(int requestCode, int resultCode, Intent data)114     public void onActivityResult(int requestCode, int resultCode, Intent data) {
115         if (requestCode == WIFI_WAKEUP_REQUEST_CODE) {
116             mWifiWakeupPreferenceController.onActivityResult(requestCode, resultCode);
117             return;
118         }
119         super.onActivityResult(requestCode, resultCode, data);
120     }
121 
122     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
123             new BaseSearchIndexProvider(R.xml.wifi_configure_settings) {
124                 protected boolean isPageSearchEnabled(Context context) {
125                     return context.getResources()
126                             .getBoolean(R.bool.config_show_wifi_settings);
127                 }
128             };
129 }
130