aboutsummaryrefslogtreecommitdiffstats
path: root/apps/SdkController/SdkControllerApp/src/com/android/tools/sdkcontroller/activities/BaseBindingActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SdkController/SdkControllerApp/src/com/android/tools/sdkcontroller/activities/BaseBindingActivity.java')
-rwxr-xr-xapps/SdkController/SdkControllerApp/src/com/android/tools/sdkcontroller/activities/BaseBindingActivity.java26
1 files changed, 22 insertions, 4 deletions
diff --git a/apps/SdkController/SdkControllerApp/src/com/android/tools/sdkcontroller/activities/BaseBindingActivity.java b/apps/SdkController/SdkControllerApp/src/com/android/tools/sdkcontroller/activities/BaseBindingActivity.java
index 53e9158..ab5306d 100755
--- a/apps/SdkController/SdkControllerApp/src/com/android/tools/sdkcontroller/activities/BaseBindingActivity.java
+++ b/apps/SdkController/SdkControllerApp/src/com/android/tools/sdkcontroller/activities/BaseBindingActivity.java
@@ -52,10 +52,17 @@ public abstract class BaseBindingActivity extends Activity {
/**
* Called when the activity resumes.
* This automatically binds to the service, starting it as needed.
+ * <p/>
+ * Since on resume we automatically bind to the service, the {@link ServiceConnection}
+ * will is restored and {@link #onServiceConnected()} is called as necessary.
+ * Derived classes that need to initialize anything that is related to the service
+ * (e.g. getting their handler) should thus do so in {@link #onServiceConnected()} and
+ * <em>not</em> in {@link #onResume()} -- since binding to the service is asynchronous
+ * there is <em>no</em> guarantee that {@link #getServiceBinder()} returns non-null
+ * when this call finishes.
*/
@Override
protected void onResume() {
- if (DEBUG) Log.d(TAG, "onResume");
super.onResume();
bindToService();
}
@@ -66,7 +73,6 @@ public abstract class BaseBindingActivity extends Activity {
*/
@Override
protected void onPause() {
- if (DEBUG) Log.d(TAG, "onPause");
super.onPause();
unbindFromService();
}
@@ -81,13 +87,16 @@ public abstract class BaseBindingActivity extends Activity {
/**
* Called by the service once the activity is connected (bound) to it.
+ * <p/>
* When this is called, {@link #getServiceBinder()} returns a non-null binder that
* can be used by the activity to control the service.
*/
protected abstract void onServiceConnected();
/**
- * Called by the service when it is disconnected (unbound).
+ * Called by the service when it is forcibly disconnected OR when we know
+ * we're unbinding the service.
+ * <p/>
* When this is called, {@link #getServiceBinder()} returns a null binder and
* the activity should stop using that binder and remove any reference to it.
*/
@@ -101,14 +110,22 @@ public abstract class BaseBindingActivity extends Activity {
final ControllerListener listener = createControllerListener();
mServiceConnection = new ServiceConnection() {
+ /**
+ * Called when the service is connected.
+ * Allows us to retrieve the binder to talk to the service.
+ */
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (DEBUG) Log.d(TAG, "Activity connected to service");
mServiceBinder = (ControllerBinder) service;
- mServiceBinder.addListener(listener);
+ mServiceBinder.addControllerListener(listener);
BaseBindingActivity.this.onServiceConnected();
}
+ /**
+ * Called when the service got disconnected, e.g. because it crashed.
+ * This is <em>not</em> called when we unbind from the service.
+ */
@Override
public void onServiceDisconnected(ComponentName name) {
if (DEBUG) Log.d(TAG, "Activity disconnected from service");
@@ -134,6 +151,7 @@ public abstract class BaseBindingActivity extends Activity {
protected void unbindFromService() {
if (mServiceConnection != null) {
if (DEBUG) Log.d(TAG, "unbind service");
+ mServiceConnection.onServiceDisconnected(null /*name*/);
unbindService(mServiceConnection);
mServiceConnection = null;
}