WifiP2pSettings.java revision 1610a74a26763046c147b2cf5cda7d84a62b95ff
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 17package com.android.settings.wifi.p2p; 18 19import android.app.ActionBar; 20import android.app.Activity; 21import android.app.AlertDialog; 22import android.app.Dialog; 23import android.content.BroadcastReceiver; 24import android.content.Context; 25import android.content.DialogInterface; 26import android.content.DialogInterface.OnClickListener; 27import android.content.Intent; 28import android.content.IntentFilter; 29import android.net.NetworkInfo; 30import android.net.wifi.p2p.WifiP2pConfig; 31import android.net.wifi.p2p.WifiP2pDevice; 32import android.net.wifi.p2p.WifiP2pDeviceList; 33import android.net.wifi.p2p.WifiP2pManager; 34import android.os.Bundle; 35import android.os.Handler; 36import android.os.Message; 37import android.preference.Preference; 38import android.preference.PreferenceActivity; 39import android.preference.PreferenceScreen; 40import android.util.Log; 41import android.view.Gravity; 42import android.view.Menu; 43import android.view.MenuInflater; 44import android.view.MenuItem; 45 46import com.android.settings.R; 47import com.android.settings.SettingsPreferenceFragment; 48 49import java.util.Arrays; 50import java.util.List; 51import java.util.Collection; 52 53/* 54 * Displays Wi-fi p2p settings UI 55 */ 56public class WifiP2pSettings extends SettingsPreferenceFragment { 57 58 private static final String TAG = "WifiP2pSettings"; 59 private static final int MENU_ID_SEARCH = Menu.FIRST; 60 private static final int MENU_ID_CREATE_GROUP = Menu.FIRST + 1; 61 private static final int MENU_ID_ADVANCED = Menu.FIRST +2; 62 63 64 private final IntentFilter mIntentFilter = new IntentFilter(); 65 private final Handler mHandler = new WifiP2pHandler(); 66 private WifiP2pManager mWifiP2pManager; 67 private WifiP2pManager.Channel mChannel; 68 private WifiP2pDialog mConnectDialog; 69 private OnClickListener mConnectListener; 70 private OnClickListener mDisconnectListener; 71 private WifiP2pPeer mSelectedWifiPeer; 72 73 private static final int DIALOG_CONNECT = 1; 74 private static final int DIALOG_DISCONNECT = 2; 75 76 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 77 @Override 78 public void onReceive(Context context, Intent intent) { 79 String action = intent.getAction(); 80 81 if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { 82 //TODO: nothing right now 83 } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { 84 if (mWifiP2pManager != null) mWifiP2pManager.requestPeers(mChannel); 85 } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { 86 if (mWifiP2pManager == null) return; 87 NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra( 88 WifiP2pManager.EXTRA_NETWORK_INFO); 89 if (networkInfo.isConnected()) { 90 Log.d(TAG, "Start peer connections"); 91 mWifiP2pManager.startPeerCommunication(); 92 } else { 93 Log.d(TAG, "Stop peer connections"); 94 mWifiP2pManager.stopPeerCommunication(); 95 } 96 } 97 } 98 }; 99 100 @Override 101 public void onCreate(Bundle icicle) { 102 super.onCreate(icicle); 103 addPreferencesFromResource(R.xml.wifi_p2p_settings); 104 105 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); 106 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); 107 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); 108 109 final Activity activity = getActivity(); 110 mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); 111 if (mWifiP2pManager != null) { 112 mChannel = mWifiP2pManager.initialize(activity, mHandler); 113 if (mChannel == null) { 114 //Failure to set up connection 115 Log.e(TAG, "Failed to set up connection with wifi p2p service"); 116 mWifiP2pManager = null; 117 } 118 } else { 119 Log.e(TAG, "mWifiP2pManager is null !"); 120 } 121 122 //connect dialog listener 123 mConnectListener = new OnClickListener() { 124 @Override 125 public void onClick(DialogInterface dialog, int which) { 126 if (which == DialogInterface.BUTTON_POSITIVE) { 127 WifiP2pConfig config = mConnectDialog.getConfig(); 128 if (mWifiP2pManager != null) { 129 mWifiP2pManager.connect(mChannel, config); 130 } 131 } 132 } 133 }; 134 135 //disconnect dialog listener 136 mDisconnectListener = new OnClickListener() { 137 @Override 138 public void onClick(DialogInterface dialog, int which) { 139 if (which == DialogInterface.BUTTON_POSITIVE) { 140 if (mWifiP2pManager != null) { 141 mWifiP2pManager.removeGroup(mChannel); 142 } 143 } 144 } 145 }; 146 setHasOptionsMenu(true); 147 } 148 149 @Override 150 public void onResume() { 151 super.onResume(); 152 getActivity().registerReceiver(mReceiver, mIntentFilter); 153 154 if (mWifiP2pManager != null) mWifiP2pManager.discoverPeers(mChannel); 155 } 156 157 @Override 158 public void onPause() { 159 super.onPause(); 160 getActivity().unregisterReceiver(mReceiver); 161 } 162 163 @Override 164 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 165 menu.add(Menu.NONE, MENU_ID_SEARCH, 0, R.string.wifi_p2p_menu_search) 166 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 167 menu.add(Menu.NONE, MENU_ID_CREATE_GROUP, 0, R.string.wifi_p2p_menu_create_group) 168 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 169 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_p2p_menu_advanced) 170 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 171 super.onCreateOptionsMenu(menu, inflater); 172 } 173 174 @Override 175 public boolean onOptionsItemSelected(MenuItem item) { 176 switch (item.getItemId()) { 177 case MENU_ID_SEARCH: 178 if (mWifiP2pManager != null) { 179 mWifiP2pManager.discoverPeers(mChannel); 180 } 181 return true; 182 case MENU_ID_CREATE_GROUP: 183 if (mWifiP2pManager != null) { 184 mWifiP2pManager.createGroup(mChannel); 185 } 186 return true; 187 case MENU_ID_ADVANCED: 188 //TODO: add advanced settings for p2p 189 return true; 190 } 191 return super.onOptionsItemSelected(item); 192 } 193 194 @Override 195 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { 196 if (preference instanceof WifiP2pPeer) { 197 mSelectedWifiPeer = (WifiP2pPeer) preference; 198 if (mSelectedWifiPeer.device.status == WifiP2pDevice.Status.CONNECTED) { 199 showDialog(DIALOG_DISCONNECT); 200 } else { 201 showDialog(DIALOG_CONNECT); 202 } 203 } 204 return super.onPreferenceTreeClick(screen, preference); 205 } 206 207 @Override 208 public Dialog onCreateDialog(int id) { 209 if (id == DIALOG_CONNECT) { 210 mConnectDialog = new WifiP2pDialog(getActivity(), mConnectListener, 211 mSelectedWifiPeer.device); 212 return mConnectDialog; 213 } else if (id == DIALOG_DISCONNECT) { 214 AlertDialog dialog = new AlertDialog.Builder(getActivity()) 215 .setTitle("Disconnect ?") 216 .setMessage("Do you want to disconnect ?") 217 .setPositiveButton(getActivity().getString(R.string.dlg_ok), mDisconnectListener) 218 .setNegativeButton(getActivity().getString(R.string.dlg_cancel), null) 219 .create(); 220 return dialog; 221 } 222 return null; 223 } 224 225 private void updatePeers(WifiP2pDeviceList peers) { 226 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 227 preferenceScreen.removeAll(); 228 229 for (WifiP2pDevice peer: peers.getDeviceList()) { 230 preferenceScreen.addPreference(new WifiP2pPeer(getActivity(), peer)); 231 } 232 } 233 234 private class WifiP2pHandler extends Handler { 235 @Override 236 public void handleMessage(Message message) { 237 switch (message.what) { 238 case WifiP2pManager.HANDLER_DISCONNECTION: 239 //Failure to set up connection 240 Log.e(TAG, "Lost connection with wifi p2p service"); 241 mWifiP2pManager = null; 242 break; 243 case WifiP2pManager.RESPONSE_PEERS: 244 updatePeers(mWifiP2pManager.peersInResponse(message)); 245 break; 246 default: 247 //Ignore 248 break; 249 } 250 } 251 } 252 253} 254