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