• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.p2p;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22 import android.net.wifi.WifiManager;
23 import android.support.annotation.VisibleForTesting;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceScreen;
26 
27 import com.android.settings.core.PreferenceController;
28 import com.android.settings.core.lifecycle.Lifecycle;
29 import com.android.settings.core.lifecycle.LifecycleObserver;
30 import com.android.settings.core.lifecycle.events.OnPause;
31 import com.android.settings.core.lifecycle.events.OnResume;
32 
33 /**
34  * {@link PreferenceController} to toggle Wifi Direct preference on Wi-Fi state.
35  */
36 public class WifiP2pPreferenceController extends PreferenceController implements
37         LifecycleObserver, OnPause, OnResume {
38 
39     private static final String KEY_WIFI_DIRECT = "wifi_direct";
40 
41     private final WifiManager mWifiManager;
42     @VisibleForTesting
43     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
44         @Override
45         public void onReceive(Context context, Intent intent) {
46             togglePreferences();
47         }
48     };
49     private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
50 
51     private Preference mWifiDirectPref;
52 
WifiP2pPreferenceController( Context context, Lifecycle lifecycle, WifiManager wifiManager)53     public WifiP2pPreferenceController(
54             Context context, Lifecycle lifecycle, WifiManager wifiManager) {
55         super(context);
56         mWifiManager = wifiManager;
57         lifecycle.addObserver(this);
58     }
59 
60     @Override
displayPreference(PreferenceScreen screen)61     public void displayPreference(PreferenceScreen screen) {
62         super.displayPreference(screen);
63         mWifiDirectPref = screen.findPreference(KEY_WIFI_DIRECT);
64         togglePreferences();
65     }
66 
67     @Override
onResume()68     public void onResume() {
69         mContext.registerReceiver(mReceiver, mFilter);
70     }
71 
72     @Override
onPause()73     public void onPause() {
74         mContext.unregisterReceiver(mReceiver);
75     }
76 
77     @Override
isAvailable()78     public boolean isAvailable() {
79         // Always show preference.
80         return true;
81     }
82     @Override
getPreferenceKey()83     public String getPreferenceKey() {
84         return KEY_WIFI_DIRECT;
85     }
86 
togglePreferences()87     private void togglePreferences() {
88         if (mWifiDirectPref != null) {
89             mWifiDirectPref.setEnabled(mWifiManager.isWifiEnabled());
90         }
91     }
92 }
93