Skip to content

Commit

Permalink
Refactor the way connection notification works
Browse files Browse the repository at this point in the history
git-svn-id: https://connectbot.googlecode.com/svn/trunk/connectbot@505 df292f66-193f-0410-a5fc-6d59da041ff2
  • Loading branch information
kruton committed May 31, 2010
1 parent 29d9e77 commit 32bc0ed
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.connectbot"
android:versionName="1.7.0-rc1"
android:versionCode="301"
android:versionCode="302"
android:installLocation="auto">

<application
Expand Down
41 changes: 20 additions & 21 deletions src/org/connectbot/service/ConnectivityReceiver.java
Expand Up @@ -31,6 +31,8 @@ public class ConnectivityReceiver extends BroadcastReceiver {

private boolean mLockingWifi;

private Object[] mLock = new Object[0];

public ConnectivityReceiver(TerminalManager manager, boolean lockingWifi) {
mTerminalManager = manager;

Expand Down Expand Up @@ -93,54 +95,51 @@ public void cleanup() {
}

/**
* Increase the number of things using the network. Acquire a Wifi lock if necessary.
* Increase the number of things using the network. Acquire a Wi-Fi lock
* if necessary.
*/
public void incRef() {
synchronized (this) {
acquireWifiLockIfNecessary();
synchronized (mLock) {
acquireWifiLockIfNecessaryLocked();

mNetworkRef += 1;
}
}

/**
* Decrease the number of things using the network. Release the Wi-Fi lock
* if necessary.
*/
public void decRef() {
synchronized (this) {
synchronized (mLock) {
mNetworkRef -= 1;

releaseWifiLockIfNecessary();
releaseWifiLockIfNecessaryLocked();
}
}

/**
* @param mLockingWifi
*/
public void setWantWifiLock(boolean lockingWifi) {
synchronized (this) {
synchronized (mLock) {
mLockingWifi = lockingWifi;

if (mLockingWifi) {
acquireWifiLockIfNecessary();
} else if (!mLockingWifi) {
releaseWifiLockIfNecessary();
acquireWifiLockIfNecessaryLocked();
} else {
releaseWifiLockIfNecessaryLocked();
}
}
}

/**
*
*/
private void acquireWifiLockIfNecessary() {
synchronized (this) {
if (mLockingWifi && mNetworkRef > 0 && !mWifiLock.isHeld()) {
mWifiLock.acquire();
}
private void acquireWifiLockIfNecessaryLocked() {
if (mLockingWifi && mNetworkRef > 0 && !mWifiLock.isHeld()) {
mWifiLock.acquire();
}
}

/**
*
*/
private void releaseWifiLockIfNecessary() {
private void releaseWifiLockIfNecessaryLocked() {
if (mNetworkRef == 0 && mWifiLock.isHeld()) {
mWifiLock.release();
}
Expand Down
34 changes: 20 additions & 14 deletions src/org/connectbot/service/TerminalManager.java
Expand Up @@ -246,6 +246,10 @@ private TerminalBridge openConnection(HostBean host) throws IllegalArgumentExcep
connectivityManager.incRef();
}

if (prefs.getBoolean(PreferenceConstants.CONNECTION_PERSIST, true)) {
ConnectionNotifier.getInstance().showRunningNotification(this);
}

// also update database with new connected time
touchHost(host);

Expand Down Expand Up @@ -323,6 +327,8 @@ public TerminalBridge getConnectedBridge(final String nickname) {
* Called by child bridge when somehow it's been disconnected.
*/
public void onDisconnected(TerminalBridge bridge) {
boolean shouldHideRunningNotification = false;

synchronized (bridges) {
// remove this bridge from our list
bridges.remove(bridge);
Expand All @@ -333,16 +339,24 @@ public void onDisconnected(TerminalBridge bridge) {
if (bridge.isUsingNetwork()) {
connectivityManager.decRef();
}

if (bridges.size() == 0 &&
mPendingReconnect.size() == 0) {
shouldHideRunningNotification = true;
}
}

synchronized (disconnected) {
disconnected.add(bridge.host);
}

if (shouldHideRunningNotification) {
ConnectionNotifier.getInstance().hideRunningNotification(this);
}

// pass notification back up to gui
if (disconnectHandler != null)
Message.obtain(disconnectHandler, -1, bridge).sendToTarget();

}

public boolean isKeyLoaded(String nickname) {
Expand Down Expand Up @@ -465,8 +479,6 @@ public IBinder onBind(Intent intent) {

setResizeAllowed(true);

ConnectionNotifier.getInstance().hideRunningNotification(this);

stopIdleTimer();

// Make sure we stay running to maintain the bridges
Expand All @@ -481,8 +493,6 @@ public void onRebind(Intent intent) {

setResizeAllowed(true);

ConnectionNotifier.getInstance().hideRunningNotification(this);

Log.i(TAG, "Someone rebound to TerminalManager");

stopIdleTimer();
Expand All @@ -496,13 +506,6 @@ public boolean onUnbind(Intent intent) {

if (bridges.size() == 0) {
stopWithDelay();
} else {
/* If user wants the connections to stay alive at all costs,
* set the service to be "foreground."
*/
if (prefs.getBoolean(PreferenceConstants.CONNECTION_PERSIST, true)) {
ConnectionNotifier.getInstance().showRunningNotification(this);
}
}

return true;
Expand Down Expand Up @@ -565,7 +568,7 @@ public void playBeep() {
vibrate();
}

class BeepListener implements OnCompletionListener {
private static class BeepListener implements OnCompletionListener {
public void onCompletion(MediaPlayer mp) {
mp.seekTo(0);
}
Expand Down Expand Up @@ -647,6 +650,7 @@ public void run() {
disconnectAll(false);
}
};
t.setName("Disconnector");
t.start();
}

Expand All @@ -660,6 +664,7 @@ public void run() {
reconnectPending();
}
};
t.setName("Reconnector");
t.start();
}

Expand All @@ -673,7 +678,8 @@ public void run() {
public void requestReconnect(TerminalBridge bridge) {
synchronized (mPendingReconnect) {
mPendingReconnect.add(new WeakReference<TerminalBridge>(bridge));
if (connectivityManager.isConnected()) {
if (!bridge.isUsingNetwork() ||
connectivityManager.isConnected()) {
reconnectPending();
}
}
Expand Down

0 comments on commit 32bc0ed

Please sign in to comment.