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 android.app.settings.SettingsEnums;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.wifi.WifiManager;
23 import android.os.Bundle;
24 import android.os.UserManager;
25 import android.util.EventLog;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.TextView;
29 
30 import androidx.annotation.Nullable;
31 import androidx.annotation.VisibleForTesting;
32 import androidx.preference.Preference;
33 
34 import com.android.settings.R;
35 import com.android.settings.dashboard.DashboardFragment;
36 import com.android.settings.search.BaseSearchIndexProvider;
37 import com.android.settings.wifi.p2p.WifiP2pPreferenceController;
38 import com.android.settingslib.core.AbstractPreferenceController;
39 import com.android.settingslib.search.SearchIndexable;
40 
41 import java.util.ArrayList;
42 import java.util.List;
43 
44 @SearchIndexable
45 public class ConfigureWifiSettings extends DashboardFragment {
46 
47     private static final String TAG = "ConfigureWifiSettings";
48     @VisibleForTesting
49     static final String KEY_INSTALL_CREDENTIALS = "install_credentials";
50     private static final String ACTION_INSTALL_CERTS = "android.credentials.INSTALL";
51     private static final String PACKAGE_INSTALL_CERTS = "com.android.certinstaller";
52     private static final String CLASS_INSTALL_CERTS = "com.android.certinstaller.CertInstallerMain";
53     private static final String KEY_INSTALL_CERTIFICATE = "certificate_install_usage";
54     private static final String INSTALL_CERTIFICATE_VALUE = "wifi";
55 
56     public static final int WIFI_WAKEUP_REQUEST_CODE = 600;
57 
58     private WifiWakeupPreferenceController mWifiWakeupPreferenceController;
59 
60     @Override
onAttach(Context context)61     public void onAttach(Context context) {
62         super.onAttach(context);
63         if (isGuestUser(context)) return;
64 
65         mWifiWakeupPreferenceController = use(WifiWakeupPreferenceController.class);
66         mWifiWakeupPreferenceController.setFragment(this);
67     }
68 
69     @Override
onCreate(Bundle icicle)70     public void onCreate(Bundle icicle) {
71         super.onCreate(icicle);
72         getActivity().setTitle(R.string.network_and_internet_preferences_title);
73 
74         if (isGuestUser(getContext())) return;
75 
76         final Preference installCredentialsPref = findPreference(KEY_INSTALL_CREDENTIALS);
77         if (installCredentialsPref != null) {
78             installCredentialsPref.setOnPreferenceClickListener(preference -> {
79                 Intent intent = new Intent(ACTION_INSTALL_CERTS);
80                 intent.setFlags(
81                         Intent.FLAG_ACTIVITY_NEW_TASK);
82                 intent.setComponent(
83                         new ComponentName(PACKAGE_INSTALL_CERTS, CLASS_INSTALL_CERTS));
84                 intent.putExtra(KEY_INSTALL_CERTIFICATE, INSTALL_CERTIFICATE_VALUE);
85                 getContext().startActivity(intent);
86                 return true;
87             });
88         } else {
89             Log.d(TAG, "Can not find the preference.");
90         }
91     }
92 
93     @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)94     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
95         super.onViewCreated(view, savedInstanceState);
96         if (!isGuestUser(getContext())) return;
97 
98         Log.w(TAG, "Displays the restricted UI because the user is a guest.");
99         EventLog.writeEvent(0x534e4554, "231987122", -1 /* UID */, "User is a guest");
100 
101         // Restricted UI
102         final TextView emptyView = getActivity().findViewById(android.R.id.empty);
103         if (emptyView != null) {
104             emptyView.setVisibility(View.VISIBLE);
105             emptyView.setText(R.string.wifi_empty_list_user_restricted);
106         }
107         getPreferenceScreen().removeAll();
108     }
109 
110     @Override
getMetricsCategory()111     public int getMetricsCategory() {
112         return SettingsEnums.CONFIGURE_WIFI;
113     }
114 
115     @Override
getLogTag()116     protected String getLogTag() {
117         return TAG;
118     }
119 
120     @Override
getPreferenceScreenResId()121     protected int getPreferenceScreenResId() {
122         return R.xml.wifi_configure_settings;
123     }
124 
125     @Override
createPreferenceControllers(Context context)126     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
127         if (isGuestUser(context)) return null;
128 
129         final WifiManager wifiManager = getSystemService(WifiManager.class);
130         final List<AbstractPreferenceController> controllers = new ArrayList<>();
131         controllers.add(new WifiP2pPreferenceController(context, getSettingsLifecycle(),
132                 wifiManager));
133         return controllers;
134     }
135 
136     @Override
onActivityResult(int requestCode, int resultCode, Intent data)137     public void onActivityResult(int requestCode, int resultCode, Intent data) {
138         if (mWifiWakeupPreferenceController != null && requestCode == WIFI_WAKEUP_REQUEST_CODE) {
139             mWifiWakeupPreferenceController.onActivityResult(requestCode, resultCode);
140             return;
141         }
142         super.onActivityResult(requestCode, resultCode, data);
143     }
144 
145     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
146             new BaseSearchIndexProvider(R.xml.wifi_configure_settings) {
147                 protected boolean isPageSearchEnabled(Context context) {
148                     if (isGuestUser(context)) return false;
149                     return context.getResources()
150                             .getBoolean(R.bool.config_show_wifi_settings);
151                 }
152             };
153 
isGuestUser(Context context)154     private static boolean isGuestUser(Context context) {
155         if (context == null) return false;
156         final UserManager userManager = context.getSystemService(UserManager.class);
157         if (userManager == null) return false;
158         return userManager.isGuestUser();
159     }
160 }
161