OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.text.TextUtils; | 8 import android.text.TextUtils; |
9 | 9 |
10 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
11 | 11 |
12 import java.util.ArrayList; | 12 import java.util.ArrayList; |
13 import java.util.Arrays; | 13 import java.util.Arrays; |
14 import java.util.List; | 14 import java.util.List; |
15 | 15 |
16 /** | 16 /** |
17 * A manager for the capabilities of the Android client. Based on the negotiated
set of | 17 * A manager for the capabilities of the Android client. Based on the negotiated
set of |
18 * capabilities, it creates the associated ClientExtensions, and enables their c
ommunication with | 18 * capabilities, it creates the associated ClientExtensions, and enables their c
ommunication with |
19 * the Chromoting host by dispatching extension messages appropriately. | 19 * the Chromoting host by dispatching extension messages appropriately. |
20 * | 20 * |
21 * The CapabilityManager mirrors how the Chromoting host handles extension messa
ges. For each | 21 * The CapabilityManager mirrors how the Chromoting host handles extension messa
ges. For each |
22 * incoming extension message, runs through a list of HostExtensionSession objec
ts, giving each one | 22 * incoming extension message, runs through a list of HostExtensionSession objec
ts, giving each one |
23 * a chance to handle the message. | 23 * a chance to handle the message. |
24 * | |
25 * The CapabilityManager is a singleton class so we can manage client extensions
on an application | |
26 * level. The singleton object may be used from multiple Activities, thus allowi
ng it to support | |
27 * different capabilities at different stages of the application. | |
28 */ | 24 */ |
29 public class CapabilityManager { | 25 public class CapabilityManager { |
30 /** Used to allow objects to receive notifications when the host capabilites
are received. */ | 26 /** Used to allow objects to receive notifications when the host capabilites
are received. */ |
31 public interface CapabilitiesChangedListener { | 27 public interface CapabilitiesChangedListener { |
32 void onCapabilitiesChanged(List<String> newCapabilities); | 28 void onCapabilitiesChanged(List<String> newCapabilities); |
33 } | 29 } |
34 | 30 |
35 /** Tracks whether the remote host supports a capability. */ | 31 /** Tracks whether the remote host supports a capability. */ |
36 public enum HostCapability { | 32 public enum HostCapability { |
37 UNKNOWN, | 33 UNKNOWN, |
38 SUPPORTED, | 34 SUPPORTED, |
39 UNSUPPORTED; | 35 UNSUPPORTED; |
40 | 36 |
41 public boolean isSet() { | 37 public boolean isSet() { |
42 return this != UNKNOWN; | 38 return this != UNKNOWN; |
43 } | 39 } |
44 | 40 |
45 public boolean isSupported() { | 41 public boolean isSupported() { |
46 assert isSet(); | 42 assert isSet(); |
47 return this == SUPPORTED; | 43 return this == SUPPORTED; |
48 } | 44 } |
49 } | 45 } |
50 | 46 |
51 private static final String TAG = "Chromoting"; | 47 private static final String TAG = "Chromoting"; |
52 | 48 |
53 /** Lazily-initialized singleton object that can be used from different Acti
vities. */ | |
54 private static CapabilityManager sInstance; | |
55 | |
56 /** Protects access to |sInstance|. */ | |
57 private static final Object sInstanceLock = new Object(); | |
58 | |
59 /** List of all capabilities that are supported by the application. */ | 49 /** List of all capabilities that are supported by the application. */ |
60 private List<String> mLocalCapabilities; | 50 private List<String> mLocalCapabilities; |
61 | 51 |
62 /** List of negotiated capabilities received from the host. */ | 52 /** List of negotiated capabilities received from the host. */ |
63 private List<String> mNegotiatedCapabilities; | 53 private List<String> mNegotiatedCapabilities; |
64 | 54 |
65 /** List of extensions to the client based on capabilities negotiated with t
he host. */ | 55 /** List of extensions to the client based on capabilities negotiated with t
he host. */ |
66 private List<ClientExtension> mClientExtensions; | 56 private List<ClientExtension> mClientExtensions; |
67 | 57 |
68 /** Maintains a list of listeners to notify when host capabilities are recei
ved. */ | 58 /** Maintains a list of listeners to notify when host capabilities are recei
ved. */ |
69 private List<CapabilitiesChangedListener> mCapabilitiesChangedListeners; | 59 private List<CapabilitiesChangedListener> mCapabilitiesChangedListeners; |
70 | 60 |
71 private CapabilityManager() { | 61 public CapabilityManager() { |
72 mLocalCapabilities = new ArrayList<String>(); | 62 mLocalCapabilities = new ArrayList<String>(); |
73 mClientExtensions = new ArrayList<ClientExtension>(); | 63 mClientExtensions = new ArrayList<ClientExtension>(); |
74 | 64 |
75 mLocalCapabilities.add(Capabilities.CAST_CAPABILITY); | 65 mLocalCapabilities.add(Capabilities.CAST_CAPABILITY); |
76 mLocalCapabilities.add(Capabilities.TOUCH_CAPABILITY); | 66 mLocalCapabilities.add(Capabilities.TOUCH_CAPABILITY); |
77 | 67 |
78 mCapabilitiesChangedListeners = new ArrayList<CapabilitiesChangedListene
r>(); | 68 mCapabilitiesChangedListeners = new ArrayList<CapabilitiesChangedListene
r>(); |
79 } | 69 } |
80 | 70 |
81 /** | 71 /** |
82 * Returns the singleton object. Thread-safe. | |
83 */ | |
84 public static CapabilityManager getInstance() { | |
85 synchronized (sInstanceLock) { | |
86 if (sInstance == null) { | |
87 sInstance = new CapabilityManager(); | |
88 } | |
89 return sInstance; | |
90 } | |
91 } | |
92 | |
93 /** | |
94 * Cleans up host specific state when the connection has been terminated. | 72 * Cleans up host specific state when the connection has been terminated. |
95 */ | 73 */ |
96 public void onHostDisconnect() { | 74 public void onHostDisconnect() { |
97 mNegotiatedCapabilities = null; | 75 mNegotiatedCapabilities = null; |
98 } | 76 } |
99 | 77 |
100 /** | 78 /** |
101 * Returns a space-separated list (required by host) of the capabilities sup
ported by | 79 * Returns a space-separated list (required by host) of the capabilities sup
ported by |
102 * this client. | 80 * this client. |
103 */ | 81 */ |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 return new DummyClientExtension(); | 192 return new DummyClientExtension(); |
215 } catch (InstantiationException e) { | 193 } catch (InstantiationException e) { |
216 Log.w(TAG, "Failed to create CastExtensionHandler."); | 194 Log.w(TAG, "Failed to create CastExtensionHandler."); |
217 return new DummyClientExtension(); | 195 return new DummyClientExtension(); |
218 } catch (IllegalAccessException e) { | 196 } catch (IllegalAccessException e) { |
219 Log.w(TAG, "Failed to create CastExtensionHandler."); | 197 Log.w(TAG, "Failed to create CastExtensionHandler."); |
220 return new DummyClientExtension(); | 198 return new DummyClientExtension(); |
221 } | 199 } |
222 } | 200 } |
223 } | 201 } |
OLD | NEW |