Mercurial > 510Connectbot
comparison src/com/five_ten_sg/connectbot/service/ConnectivityReceiver.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package com.five_ten_sg.connectbot.service; | |
5 | |
6 import android.content.BroadcastReceiver; | |
7 import android.content.Context; | |
8 import android.content.Intent; | |
9 import android.content.IntentFilter; | |
10 import android.net.ConnectivityManager; | |
11 import android.net.NetworkInfo; | |
12 import android.net.NetworkInfo.State; | |
13 import android.net.wifi.WifiManager; | |
14 import android.net.wifi.WifiManager.WifiLock; | |
15 import android.util.Log; | |
16 | |
17 /** | |
18 * @author kroot | |
19 * | |
20 */ | |
21 public class ConnectivityReceiver extends BroadcastReceiver { | |
22 private static final String TAG = "ConnectBot.ConnectivityManager"; | |
23 | |
24 private boolean mIsConnected = false; | |
25 | |
26 final private TerminalManager mTerminalManager; | |
27 | |
28 final private WifiLock mWifiLock; | |
29 | |
30 private int mNetworkRef = 0; | |
31 | |
32 private boolean mLockingWifi; | |
33 | |
34 private Object[] mLock = new Object[0]; | |
35 | |
36 public ConnectivityReceiver(TerminalManager manager, boolean lockingWifi) { | |
37 mTerminalManager = manager; | |
38 final ConnectivityManager cm = | |
39 (ConnectivityManager) manager.getSystemService(Context.CONNECTIVITY_SERVICE); | |
40 final WifiManager wm = (WifiManager) manager.getSystemService(Context.WIFI_SERVICE); | |
41 mWifiLock = wm.createWifiLock(TAG); | |
42 final NetworkInfo info = cm.getActiveNetworkInfo(); | |
43 | |
44 if (info != null) { | |
45 mIsConnected = (info.getState() == State.CONNECTED); | |
46 } | |
47 | |
48 mLockingWifi = lockingWifi; | |
49 final IntentFilter filter = new IntentFilter(); | |
50 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); | |
51 manager.registerReceiver(this, filter); | |
52 } | |
53 | |
54 /* (non-Javadoc) | |
55 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent) | |
56 */ | |
57 @Override | |
58 public void onReceive(Context context, Intent intent) { | |
59 final String action = intent.getAction(); | |
60 | |
61 if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { | |
62 Log.w(TAG, "onReceived() called: " + intent); | |
63 return; | |
64 } | |
65 | |
66 boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); | |
67 boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false); | |
68 Log.d(TAG, "onReceived() called; noConnectivity? " + noConnectivity + "; isFailover? " + isFailover); | |
69 | |
70 if (noConnectivity && !isFailover && mIsConnected) { | |
71 mIsConnected = false; | |
72 mTerminalManager.onConnectivityLost(); | |
73 } | |
74 else if (!mIsConnected) { | |
75 NetworkInfo info = (NetworkInfo) intent.getExtras() | |
76 .get(ConnectivityManager.EXTRA_NETWORK_INFO); | |
77 | |
78 if (mIsConnected = (info.getState() == State.CONNECTED)) { | |
79 mTerminalManager.onConnectivityRestored(); | |
80 } | |
81 } | |
82 } | |
83 | |
84 /** | |
85 * | |
86 */ | |
87 public void cleanup() { | |
88 if (mWifiLock.isHeld()) | |
89 mWifiLock.release(); | |
90 | |
91 mTerminalManager.unregisterReceiver(this); | |
92 } | |
93 | |
94 /** | |
95 * Increase the number of things using the network. Acquire a Wi-Fi lock | |
96 * if necessary. | |
97 */ | |
98 public void incRef() { | |
99 synchronized (mLock) { | |
100 mNetworkRef += 1; | |
101 acquireWifiLockIfNecessaryLocked(); | |
102 } | |
103 } | |
104 | |
105 /** | |
106 * Decrease the number of things using the network. Release the Wi-Fi lock | |
107 * if necessary. | |
108 */ | |
109 public void decRef() { | |
110 synchronized (mLock) { | |
111 mNetworkRef -= 1; | |
112 releaseWifiLockIfNecessaryLocked(); | |
113 } | |
114 } | |
115 | |
116 /** | |
117 * @param mLockingWifi | |
118 */ | |
119 public void setWantWifiLock(boolean lockingWifi) { | |
120 synchronized (mLock) { | |
121 mLockingWifi = lockingWifi; | |
122 | |
123 if (mLockingWifi) { | |
124 acquireWifiLockIfNecessaryLocked(); | |
125 } | |
126 else { | |
127 releaseWifiLockIfNecessaryLocked(); | |
128 } | |
129 } | |
130 } | |
131 | |
132 private void acquireWifiLockIfNecessaryLocked() { | |
133 if (mLockingWifi && mNetworkRef > 0 && !mWifiLock.isHeld()) { | |
134 mWifiLock.acquire(); | |
135 } | |
136 } | |
137 | |
138 private void releaseWifiLockIfNecessaryLocked() { | |
139 if (mNetworkRef == 0 && mWifiLock.isHeld()) { | |
140 mWifiLock.release(); | |
141 } | |
142 } | |
143 | |
144 /** | |
145 * @return whether we're connected to a network | |
146 */ | |
147 public boolean isConnected() { | |
148 return mIsConnected; | |
149 } | |
150 } |