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.example.android.wifidirect;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.NetworkInfo;
23 import android.net.wifi.p2p.WifiP2pDevice;
24 import android.net.wifi.p2p.WifiP2pManager;
25 import android.net.wifi.p2p.WifiP2pManager.Channel;
26 import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
27 import android.util.Log;
28 
29 /**
30  * A BroadcastReceiver that notifies of important wifi p2p events.
31  */
32 public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
33 
34     private WifiP2pManager manager;
35     private Channel channel;
36     private WiFiDirectActivity activity;
37 
38     /**
39      * @param manager WifiP2pManager system service
40      * @param channel Wifi p2p channel
41      * @param activity activity associated with the receiver
42      */
WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, WiFiDirectActivity activity)43     public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
44             WiFiDirectActivity activity) {
45         super();
46         this.manager = manager;
47         this.channel = channel;
48         this.activity = activity;
49     }
50 
51     /*
52      * (non-Javadoc)
53      * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
54      * android.content.Intent)
55      */
56     @Override
onReceive(Context context, Intent intent)57     public void onReceive(Context context, Intent intent) {
58         String action = intent.getAction();
59         if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
60 
61             // UI update to indicate wifi p2p status.
62             int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
63             if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
64                 // Wifi Direct mode is enabled
65                 activity.setIsWifiP2pEnabled(true);
66             } else {
67                 activity.setIsWifiP2pEnabled(false);
68                 activity.resetData();
69 
70             }
71             Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
72         } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
73 
74             // request available peers from the wifi p2p manager. This is an
75             // asynchronous call and the calling activity is notified with a
76             // callback on PeerListListener.onPeersAvailable()
77             if (manager != null) {
78                 manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
79                         .findFragmentById(R.id.frag_list));
80             }
81             Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
82         } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
83 
84             if (manager == null) {
85                 return;
86             }
87 
88             NetworkInfo networkInfo = (NetworkInfo) intent
89                     .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
90 
91             if (networkInfo.isConnected()) {
92 
93                 // we are connected with the other device, request connection
94                 // info to find group owner IP
95 
96                 DeviceDetailFragment fragment = (DeviceDetailFragment) activity
97                         .getFragmentManager().findFragmentById(R.id.frag_detail);
98                 manager.requestConnectionInfo(channel, fragment);
99             } else {
100                 // It's a disconnect
101                 activity.resetData();
102             }
103         } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
104             DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
105                     .findFragmentById(R.id.frag_list);
106             fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
107                     WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
108 
109         }
110     }
111 }
112