1 /*
2  * Copyright (C) 2020 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.tv.settings;
18 
19 import static com.android.tv.settings.overlay.FlavorUtils.X_EXPERIENCE_FLAVORS_MASK;
20 
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.wifi.WifiConfiguration;
25 import android.net.wifi.WifiManager;
26 import android.os.AsyncTask;
27 import android.os.PowerManager;
28 import android.util.Log;
29 
30 import com.android.tv.settings.overlay.FlavorUtils;
31 
32 import java.util.List;
33 
34 /** BroadcastReceiver to handle the request from setup */
35 public class DeviceSettingBroadcastReceiver extends BroadcastReceiver {
36     private static final String TAG = "DeviceSettingsReceiver";
37     private static final String ACTION_REBOOT_DEVICE = "com.android.tv.settings.REBOOT_DEVICE";
38     private static final String ACTION_REMOVE_WIFI = "com.android.tv.settings.REMOVE_WIFI";
39 
40     @Override
onReceive(Context context, Intent intent)41     public void onReceive(Context context, Intent intent) {
42         if ((FlavorUtils.getFlavor(context) & X_EXPERIENCE_FLAVORS_MASK) == 0) {
43             Log.w(TAG, "Not supported in this flavor.");
44             return;
45         }
46         switch (intent.getAction()) {
47             case ACTION_REBOOT_DEVICE:
48                 restartDevice(context);
49                 break;
50             case ACTION_REMOVE_WIFI:
51                 removeWifis(context);
52                 break;
53             default:
54                 // no-op
55         }
56     }
57 
restartDevice(Context context)58     private void restartDevice(Context context) {
59         final PowerManager pm = context.getSystemService(PowerManager.class);
60 
61         new AsyncTask<Void, Void, Void>() {
62             @Override
63             protected Void doInBackground(Void... params) {
64                 pm.reboot(null);
65                 return null;
66             }
67         }.execute();
68     }
69 
removeWifis(Context context)70     private void removeWifis(Context context) {
71         WifiManager wifiManager = context.getSystemService(WifiManager.class);
72         List<WifiConfiguration> wifiList = wifiManager.getConfiguredNetworks();
73         if (wifiList != null && !wifiList.isEmpty()) {
74             for (WifiConfiguration wifiConfiguration : wifiList) {
75                 wifiManager.removeNetwork(wifiConfiguration.networkId);
76             }
77         }
78     }
79 }
80