1 /* 2 * Copyright (C) 2011 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.settings.wifi.p2p; 18 19 import com.android.settings.R; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.net.wifi.p2p.WifiP2pManager; 26 import android.os.Message; 27 import android.preference.CheckBoxPreference; 28 import android.preference.Preference; 29 import android.provider.Settings; 30 import android.util.Log; 31 32 /** 33 * WifiP2pEnabler is a helper to manage the Wifi p2p on/off 34 */ 35 public class WifiP2pEnabler implements Preference.OnPreferenceChangeListener { 36 private static final String TAG = "WifiP2pEnabler"; 37 38 private final Context mContext; 39 private final CheckBoxPreference mCheckBox; 40 private final IntentFilter mIntentFilter; 41 private WifiP2pManager mWifiP2pManager; 42 private WifiP2pManager.Channel mChannel; 43 44 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 45 @Override 46 public void onReceive(Context context, Intent intent) { 47 String action = intent.getAction(); 48 49 if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { 50 handleP2pStateChanged(intent.getIntExtra( 51 WifiP2pManager.EXTRA_WIFI_STATE, WifiP2pManager.WIFI_P2P_STATE_DISABLED)); 52 } 53 } 54 }; 55 56 public WifiP2pEnabler(Context context, CheckBoxPreference checkBox) { 57 mContext = context; 58 mCheckBox = checkBox; 59 60 mWifiP2pManager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE); 61 if (mWifiP2pManager != null) { 62 mChannel = mWifiP2pManager.initialize(mContext, mContext.getMainLooper(), null); 63 if (mChannel == null) { 64 //Failure to set up connection 65 Log.e(TAG, "Failed to set up connection with wifi p2p service"); 66 mWifiP2pManager = null; 67 mCheckBox.setEnabled(false); 68 } 69 } else { 70 Log.e(TAG, "mWifiP2pManager is null!"); 71 } 72 mIntentFilter = new IntentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); 73 74 } 75 76 public void resume() { 77 if (mWifiP2pManager == null) return; 78 mContext.registerReceiver(mReceiver, mIntentFilter); 79 mCheckBox.setOnPreferenceChangeListener(this); 80 } 81 82 public void pause() { 83 if (mWifiP2pManager == null) return; 84 mContext.unregisterReceiver(mReceiver); 85 mCheckBox.setOnPreferenceChangeListener(null); 86 } 87 88 public boolean onPreferenceChange(Preference preference, Object value) { 89 90 if (mWifiP2pManager == null) return false; 91 92 mCheckBox.setEnabled(false); 93 final boolean enable = (Boolean) value; 94 if (enable) { 95 mWifiP2pManager.enableP2p(mChannel); 96 } else { 97 mWifiP2pManager.disableP2p(mChannel); 98 } 99 return false; 100 } 101 102 private void handleP2pStateChanged(int state) { 103 mCheckBox.setEnabled(true); 104 switch (state) { 105 case WifiP2pManager.WIFI_P2P_STATE_ENABLED: 106 mCheckBox.setChecked(true); 107 break; 108 case WifiP2pManager.WIFI_P2P_STATE_DISABLED: 109 mCheckBox.setChecked(false); 110 break; 111 default: 112 Log.e(TAG,"Unhandled wifi state " + state); 113 break; 114 } 115 } 116 117 } 118