Index: remoting/android/host/src/org/chromium/chromoting/host/MainActivity.java |
diff --git a/remoting/android/host/src/org/chromium/chromoting/host/MainActivity.java b/remoting/android/host/src/org/chromium/chromoting/host/MainActivity.java |
index aae303f8aa2fa5acc042030a59d154633ff6dcde..f437750275fd6d8194feecb1677f23c2b5025715 100644 |
--- a/remoting/android/host/src/org/chromium/chromoting/host/MainActivity.java |
+++ b/remoting/android/host/src/org/chromium/chromoting/host/MainActivity.java |
@@ -5,8 +5,11 @@ |
package org.chromium.chromoting.host; |
import android.accounts.AccountManager; |
+import android.content.ComponentName; |
import android.content.Intent; |
+import android.content.ServiceConnection; |
import android.os.Bundle; |
+import android.os.IBinder; |
import android.support.v7.app.AppCompatActivity; |
import android.support.v7.widget.Toolbar; |
import android.view.View; |
@@ -15,13 +18,13 @@ import android.widget.TextView; |
import org.chromium.base.Log; |
import org.chromium.chromoting.base.OAuthTokenFetcher; |
-import org.chromium.chromoting.host.jni.Host; |
import org.chromium.chromoting.host.jni.It2MeHostObserver; |
/** |
* Main screen of the Chromoting Host application. |
*/ |
-public class MainActivity extends AppCompatActivity implements It2MeHostObserver { |
+public class MainActivity extends AppCompatActivity implements It2MeHostObserver, |
+ ServiceConnection { |
private static final String TAG = "host"; |
/** Scope to use when fetching the OAuth token. */ |
@@ -29,7 +32,9 @@ public class MainActivity extends AppCompatActivity implements It2MeHostObserver |
private static final int REQUEST_CODE_CHOOSE_ACCOUNT = 0; |
- private Host mHost; |
+ // Reference to the bound HostService, or null if it is unbound. |
+ private HostService mHostService; |
+ |
private String mAccountName; |
private Button mShareButton; |
@@ -48,14 +53,33 @@ public class MainActivity extends AppCompatActivity implements It2MeHostObserver |
mShareButton = (Button) findViewById(R.id.share_button); |
mDisconnectButton = (Button) findViewById(R.id.disconnect_button); |
+ |
+ // UI should be disabled until this activity binds to the host service and receives a |
+ // status update. |
mDisconnectButton.setEnabled(false); |
+ mShareButton.setEnabled(false); |
mStatusText = (TextView) findViewById(R.id.status_text); |
mAccessCode = (TextView) findViewById(R.id.access_code); |
+ } |
- if (mHost == null) { |
- mHost = new Host(); |
- } |
+ @Override |
+ public void onStart() { |
+ super.onStart(); |
+ |
+ // Ensure the service is started so that it continues to run even after this activity |
+ // unbinds from it. |
+ Intent intent = new Intent(this, HostService.class); |
+ startService(intent); |
+ |
+ bindService(intent, this, BIND_AUTO_CREATE); |
+ } |
+ |
+ @Override |
+ public void onStop() { |
+ super.onStop(); |
+ |
+ unbindService(this); |
} |
@SuppressWarnings("deprecation") |
@@ -66,7 +90,9 @@ public class MainActivity extends AppCompatActivity implements It2MeHostObserver |
} |
public void onDisconnectClicked(View view) { |
- mHost.disconnect(); |
+ if (mHostService != null) { |
+ mHostService.disconnect(); |
+ } |
} |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
@@ -88,7 +114,9 @@ public class MainActivity extends AppCompatActivity implements It2MeHostObserver |
new OAuthTokenFetcher(this, mAccountName, TOKEN_SCOPE, new OAuthTokenFetcher.Callback() { |
@Override |
public void onTokenFetched(String token) { |
- mHost.connect(mAccountName, token, MainActivity.this); |
+ if (mHostService != null) { |
+ mHostService.connect(mAccountName, token); |
+ } |
} |
@Override |
@@ -108,8 +136,19 @@ public class MainActivity extends AppCompatActivity implements It2MeHostObserver |
mStatusText.setText(state.name()); |
mShareButton.setEnabled(state == It2MeHostObserver.State.DISCONNECTED); |
mDisconnectButton.setEnabled(state != It2MeHostObserver.State.DISCONNECTED); |
- if (state == It2MeHostObserver.State.DISCONNECTED) { |
+ if (state != It2MeHostObserver.State.RECEIVED_ACCESS_CODE) { |
mAccessCode.setText(""); |
} |
} |
+ |
+ @Override |
+ public void onServiceConnected(ComponentName name, IBinder service) { |
+ mHostService = HostService.getFromIBinder(service); |
+ mHostService.setObserver(this); |
+ } |
+ |
+ @Override |
+ public void onServiceDisconnected(ComponentName name) { |
+ mHostService = null; |
+ } |
} |