Index: remoting/android/host/src/org/chromium/chromoting/host/HostService.java |
diff --git a/remoting/android/host/src/org/chromium/chromoting/host/HostService.java b/remoting/android/host/src/org/chromium/chromoting/host/HostService.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3fdf76c23a7ea8727221fafdbc7342a6fada8386 |
--- /dev/null |
+++ b/remoting/android/host/src/org/chromium/chromoting/host/HostService.java |
@@ -0,0 +1,113 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chromoting.host; |
+ |
+import android.app.Service; |
+import android.content.Intent; |
+import android.os.Binder; |
+import android.os.IBinder; |
+ |
+import org.chromium.chromoting.host.jni.Host; |
+import org.chromium.chromoting.host.jni.It2MeHostObserver; |
+ |
+/** |
+ * This class runs the host as a background service, and maintains host state across multiple |
+ * Activity lifecycles. It keeps track of the host state (and any It2Me access-code) and allows |
+ * a client Activity to access this state on binding to this service. |
+ */ |
+public class HostService extends Service implements It2MeHostObserver { |
+ private class BinderImpl extends Binder { |
+ private HostService getService() { |
+ return HostService.this; |
+ } |
+ } |
+ |
+ private Host mHost; |
+ |
+ // Host state. |
+ private It2MeHostObserver.State mState = It2MeHostObserver.State.DISCONNECTED; |
+ private String mAccessCode; |
+ private int mLifetimeSeconds; |
+ |
+ // Observer allowing a client to receive notifications from the service. This is set when a |
+ // client binds to this service, and unset when the client is unbound. It is assumed that only |
+ // 1 client may be bound at any given time. |
+ private It2MeHostObserver mObserver; |
+ |
+ /** Allows access to the HostService from the returned IBinder object. */ |
+ public static HostService getFromIBinder(IBinder binder) { |
+ return ((BinderImpl) binder).getService(); |
+ } |
+ |
+ /** |
+ * Sets the observer. Called by the client after binding to this service. This will immediately |
+ * trigger simulated callbacks to reflect the current state. */ |
+ public void setObserver(It2MeHostObserver observer) { |
+ mObserver = observer; |
+ if (mState == It2MeHostObserver.State.RECEIVED_ACCESS_CODE) { |
+ // TODO(lambroslambrou): Provide the expected expiry time of the access-code, instead |
+ // of passing on the received duration here. |
+ observer.onAccessCodeReceived(mAccessCode, mLifetimeSeconds); |
+ } |
+ observer.onStateChanged(mState, ""); |
+ } |
+ |
+ /** Begins a client It2Me session. */ |
+ public void connect(String userName, String authToken) { |
+ mHost.connect(userName, authToken, this); |
+ } |
+ |
+ /** Disconnects from any It2Me session. */ |
+ public void disconnect() { |
+ mHost.disconnect(); |
+ } |
+ |
+ @Override |
+ public void onCreate() { |
+ super.onCreate(); |
+ mHost = new Host(); |
+ } |
+ |
+ @Override |
+ public void onDestroy() { |
+ super.onDestroy(); |
+ mHost.destroy(); |
+ mHost = null; |
+ } |
+ |
+ @Override |
+ public int onStartCommand(Intent intent, int flags, int startId) { |
+ return START_STICKY; |
+ } |
+ |
+ @Override |
+ public IBinder onBind(Intent intent) { |
+ return new BinderImpl(); |
+ } |
+ |
+ @Override |
+ public boolean onUnbind(Intent intent) { |
+ super.onUnbind(intent); |
+ mObserver = null; |
+ return false; |
+ } |
+ |
+ @Override |
+ public void onAccessCodeReceived(String accessCode, int lifetimeSeconds) { |
+ mAccessCode = accessCode; |
+ mLifetimeSeconds = lifetimeSeconds; |
+ if (mObserver != null) { |
+ mObserver.onAccessCodeReceived(accessCode, lifetimeSeconds); |
+ } |
+ } |
+ |
+ @Override |
+ public void onStateChanged(State state, String errorMessage) { |
+ mState = state; |
+ if (mObserver != null) { |
+ mObserver.onStateChanged(state, errorMessage); |
+ } |
+ } |
+} |