Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.hardware.location; |
| 17 | |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 18 | import android.annotation.CallbackExecutor; |
| 19 | import android.annotation.NonNull; |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 20 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
| 22 | import android.annotation.SuppressLint; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 24 | import android.annotation.SystemService; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 26 | import android.os.Handler; |
Arthur Ishiguro | 9b9c9d8 | 2017-12-20 16:11:53 -0800 | [diff] [blame] | 27 | import android.os.HandlerExecutor; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 28 | import android.os.Looper; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 29 | import android.os.RemoteException; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 30 | import android.os.ServiceManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 31 | import android.os.ServiceManager.ServiceNotFoundException; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 34 | import com.android.internal.util.Preconditions; |
| 35 | |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 36 | import java.util.List; |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 37 | import java.util.concurrent.Executor; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 38 | |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 39 | /** |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 40 | * A class that exposes the Context hubs on a device to applications. |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 41 | * |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 42 | * Please note that this class is not expected to be used by unbundled applications. Also, calling |
| 43 | * applications are expected to have LOCATION_HARDWARE permissions to use this class. |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 44 | * |
| 45 | * @hide |
| 46 | */ |
| 47 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 48 | @SystemService(Context.CONTEXTHUB_SERVICE) |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 49 | public final class ContextHubManager { |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 50 | private static final String TAG = "ContextHubManager"; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 51 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 52 | private final Looper mMainLooper; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 53 | private final IContextHubService mService; |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 54 | private Callback mCallback; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 55 | private Handler mCallbackHandler; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 58 | * @deprecated Use {@code mCallback} instead. |
| 59 | */ |
| 60 | @Deprecated |
| 61 | private ICallback mLocalCallback; |
| 62 | |
| 63 | /** |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 64 | * An interface to receive asynchronous communication from the context hub. |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 65 | */ |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 66 | public abstract static class Callback { |
| 67 | protected Callback() {} |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 68 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 69 | /** |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 70 | * Callback function called on message receipt from context hub. |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 71 | * |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 72 | * @param hubHandle Handle (system-wide unique identifier) of the hub of the message. |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 73 | * @param nanoAppHandle Handle (unique identifier) for app instance that sent the message. |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 74 | * @param message The context hub message. |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 75 | * |
| 76 | * @see ContextHubMessage |
| 77 | */ |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 78 | public abstract void onMessageReceipt( |
| 79 | int hubHandle, |
| 80 | int nanoAppHandle, |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 81 | @NonNull ContextHubMessage message); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 82 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 85 | * @deprecated Use {@link Callback} instead. |
| 86 | * @hide |
| 87 | */ |
| 88 | @Deprecated |
| 89 | public interface ICallback { |
| 90 | /** |
| 91 | * Callback function called on message receipt from context hub. |
| 92 | * |
| 93 | * @param hubHandle Handle (system-wide unique identifier) of the hub of the message. |
| 94 | * @param nanoAppHandle Handle (unique identifier) for app instance that sent the message. |
| 95 | * @param message The context hub message. |
| 96 | * |
| 97 | * @see ContextHubMessage |
| 98 | */ |
| 99 | void onMessageReceipt(int hubHandle, int nanoAppHandle, ContextHubMessage message); |
| 100 | } |
| 101 | |
| 102 | /** |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 103 | * Get a handle to all the context hubs in the system |
| 104 | * @return array of context hub handles |
| 105 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 106 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 107 | public int[] getContextHubHandles() { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 108 | try { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 109 | return mService.getContextHubHandles(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 110 | } catch (RemoteException e) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 111 | throw e.rethrowFromSystemServer(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 112 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get more information about a specific hub. |
| 117 | * |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 118 | * @param hubHandle Handle (system-wide unique identifier) of a context hub. |
| 119 | * @return ContextHubInfo Information about the requested context hub. |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 120 | * |
| 121 | * @see ContextHubInfo |
| 122 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 123 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 124 | public ContextHubInfo getContextHubInfo(int hubHandle) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 125 | try { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 126 | return mService.getContextHubInfo(hubHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 127 | } catch (RemoteException e) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 128 | throw e.rethrowFromSystemServer(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 129 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 133 | * Load a nano app on a specified context hub. |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 134 | * |
Greg Kaiser | fac5be2 | 2016-08-17 15:33:10 -0700 | [diff] [blame] | 135 | * Note that loading is asynchronous. When we return from this method, |
| 136 | * the nano app (probably) hasn't loaded yet. Assuming a return of 0 |
| 137 | * from this method, then the final success/failure for the load, along |
| 138 | * with the "handle" for the nanoapp, is all delivered in a byte |
| 139 | * string via a call to Callback.onMessageReceipt. |
| 140 | * |
| 141 | * TODO(b/30784270): Provide a better success/failure and "handle" delivery. |
| 142 | * |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 143 | * @param hubHandle handle of context hub to load the app on. |
| 144 | * @param app the nanoApp to load on the hub |
| 145 | * |
Greg Kaiser | fac5be2 | 2016-08-17 15:33:10 -0700 | [diff] [blame] | 146 | * @return 0 if the command for loading was sent to the context hub; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 147 | * -1 otherwise |
| 148 | * |
| 149 | * @see NanoApp |
| 150 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 151 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 152 | public int loadNanoApp(int hubHandle, @NonNull NanoApp app) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 153 | try { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 154 | return mService.loadNanoApp(hubHandle, app); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 155 | } catch (RemoteException e) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 156 | throw e.rethrowFromSystemServer(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 157 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Unload a specified nanoApp |
| 162 | * |
Greg Kaiser | fac5be2 | 2016-08-17 15:33:10 -0700 | [diff] [blame] | 163 | * Note that unloading is asynchronous. When we return from this method, |
| 164 | * the nano app (probably) hasn't unloaded yet. Assuming a return of 0 |
| 165 | * from this method, then the final success/failure for the unload is |
| 166 | * delivered in a byte string via a call to Callback.onMessageReceipt. |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 167 | * |
Greg Kaiser | fac5be2 | 2016-08-17 15:33:10 -0700 | [diff] [blame] | 168 | * TODO(b/30784270): Provide a better success/failure delivery. |
| 169 | * |
| 170 | * @param nanoAppHandle handle of the nanoApp to unload |
| 171 | * |
| 172 | * @return 0 if the command for unloading was sent to the context hub; |
| 173 | * -1 otherwise |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 174 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 175 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 176 | public int unloadNanoApp(int nanoAppHandle) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 177 | try { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 178 | return mService.unloadNanoApp(nanoAppHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 179 | } catch (RemoteException e) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 180 | throw e.rethrowFromSystemServer(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 181 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * get information about the nano app instance |
| 186 | * |
Greg Kaiser | 3be73d3 | 2016-08-18 10:13:52 -0700 | [diff] [blame] | 187 | * NOTE: The returned NanoAppInstanceInfo does _not_ contain correct |
| 188 | * information for several fields, specifically: |
| 189 | * - getName() |
| 190 | * - getPublisher() |
| 191 | * - getNeededExecMemBytes() |
| 192 | * - getNeededReadMemBytes() |
| 193 | * - getNeededWriteMemBytes() |
| 194 | * |
| 195 | * For example, say you call loadNanoApp() with a NanoApp that has |
| 196 | * getName() returning "My Name". Later, if you call getNanoAppInstanceInfo |
| 197 | * for that nanoapp, the returned NanoAppInstanceInfo's getName() |
| 198 | * method will claim "Preloaded app, unknown", even though you would |
| 199 | * have expected "My Name". For now, as the user, you'll need to |
| 200 | * separately track the above fields if they are of interest to you. |
| 201 | * |
| 202 | * TODO(b/30943489): Have the returned NanoAppInstanceInfo contain the |
| 203 | * correct information. |
| 204 | * |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 205 | * @param nanoAppHandle handle of the nanoapp instance |
| 206 | * @return NanoAppInstanceInfo the NanoAppInstanceInfo of the nanoapp, or null if the nanoapp |
| 207 | * does not exist |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 208 | * |
| 209 | * @see NanoAppInstanceInfo |
| 210 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 211 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 212 | @Nullable public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppHandle) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 213 | try { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 214 | return mService.getNanoAppInstanceInfo(nanoAppHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 215 | } catch (RemoteException e) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 216 | throw e.rethrowFromSystemServer(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 217 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Find a specified nano app on the system |
| 222 | * |
| 223 | * @param hubHandle handle of hub to search for nano app |
| 224 | * @param filter filter specifying the search criteria for app |
| 225 | * |
| 226 | * @see NanoAppFilter |
| 227 | * |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 228 | * @return int[] Array of handles to any found nano apps |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 229 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 230 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 231 | @NonNull public int[] findNanoAppOnHub(int hubHandle, @NonNull NanoAppFilter filter) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 232 | try { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 233 | return mService.findNanoAppOnHub(hubHandle, filter); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 234 | } catch (RemoteException e) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 235 | throw e.rethrowFromSystemServer(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 236 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 240 | * Send a message to a specific nano app instance on a context hub. |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 241 | * |
Greg Kaiser | fac5be2 | 2016-08-17 15:33:10 -0700 | [diff] [blame] | 242 | * Note that the return value of this method only speaks of success |
| 243 | * up to the point of sending this to the Context Hub. It is not |
| 244 | * an assurance that the Context Hub successfully sent this message |
| 245 | * on to the nanoapp. If assurance is desired, a protocol should be |
| 246 | * established between your code and the nanoapp, with the nanoapp |
| 247 | * sending a confirmation message (which will be reported via |
| 248 | * Callback.onMessageReceipt). |
| 249 | * |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 250 | * @param hubHandle handle of the hub to send the message to |
| 251 | * @param nanoAppHandle handle of the nano app to send to |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 252 | * @param message Message to be sent |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 253 | * |
| 254 | * @see ContextHubMessage |
| 255 | * |
| 256 | * @return int 0 on success, -1 otherwise |
| 257 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 258 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 259 | public int sendMessage(int hubHandle, int nanoAppHandle, @NonNull ContextHubMessage message) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 260 | try { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 261 | return mService.sendMessage(hubHandle, nanoAppHandle, message); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 262 | } catch (RemoteException e) { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 263 | throw e.rethrowFromSystemServer(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 264 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 267 | /** |
Arthur Ishiguro | fdbbd46 | 2017-11-27 16:33:36 -0800 | [diff] [blame] | 268 | * Returns the list of ContextHubInfo objects describing the available Context Hubs. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 269 | * |
Arthur Ishiguro | fdbbd46 | 2017-11-27 16:33:36 -0800 | [diff] [blame] | 270 | * @return the list of ContextHubInfo objects |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 271 | * |
| 272 | * @see ContextHubInfo |
| 273 | * |
| 274 | * @hide |
| 275 | */ |
| 276 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 277 | @NonNull public List<ContextHubInfo> getContextHubs() { |
Arthur Ishiguro | fdbbd46 | 2017-11-27 16:33:36 -0800 | [diff] [blame] | 278 | try { |
| 279 | return mService.getContextHubs(); |
| 280 | } catch (RemoteException e) { |
| 281 | throw e.rethrowFromSystemServer(); |
| 282 | } |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 285 | /** |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 286 | * Helper function to generate a stub for a non-query transaction callback. |
| 287 | * |
| 288 | * @param transaction the transaction to unblock when complete |
| 289 | * |
| 290 | * @return the callback |
| 291 | * |
| 292 | * @hide |
| 293 | */ |
| 294 | private IContextHubTransactionCallback createTransactionCallback( |
| 295 | ContextHubTransaction<Void> transaction) { |
| 296 | return new IContextHubTransactionCallback.Stub() { |
| 297 | @Override |
| 298 | public void onQueryResponse(int result, List<NanoAppState> nanoappList) { |
| 299 | Log.e(TAG, "Received a query callback on a non-query request"); |
| 300 | transaction.setResponse(new ContextHubTransaction.Response<Void>( |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 301 | ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null)); |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void onTransactionComplete(int result) { |
| 306 | transaction.setResponse(new ContextHubTransaction.Response<Void>(result, null)); |
| 307 | } |
| 308 | }; |
| 309 | } |
| 310 | |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 311 | /** |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 312 | * Helper function to generate a stub for a query transaction callback. |
| 313 | * |
| 314 | * @param transaction the transaction to unblock when complete |
| 315 | * |
| 316 | * @return the callback |
| 317 | * |
| 318 | * @hide |
| 319 | */ |
| 320 | private IContextHubTransactionCallback createQueryCallback( |
| 321 | ContextHubTransaction<List<NanoAppState>> transaction) { |
| 322 | return new IContextHubTransactionCallback.Stub() { |
| 323 | @Override |
| 324 | public void onQueryResponse(int result, List<NanoAppState> nanoappList) { |
| 325 | transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>( |
| 326 | result, nanoappList)); |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void onTransactionComplete(int result) { |
| 331 | Log.e(TAG, "Received a non-query callback on a query request"); |
| 332 | transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>( |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 333 | ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null)); |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 334 | } |
| 335 | }; |
| 336 | } |
| 337 | |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 338 | /** |
| 339 | * Loads a nanoapp at the specified Context Hub. |
| 340 | * |
| 341 | * After the nanoapp binary is successfully loaded at the specified hub, the nanoapp will be in |
| 342 | * the enabled state. |
| 343 | * |
| 344 | * @param hubInfo the hub to load the nanoapp on |
| 345 | * @param appBinary The app binary to load |
| 346 | * |
| 347 | * @return the ContextHubTransaction of the request |
| 348 | * |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 349 | * @throws NullPointerException if hubInfo or NanoAppBinary is null |
| 350 | * |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 351 | * @see NanoAppBinary |
| 352 | * |
| 353 | * @hide |
| 354 | */ |
| 355 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 356 | @NonNull public ContextHubTransaction<Void> loadNanoApp( |
| 357 | @NonNull ContextHubInfo hubInfo, @NonNull NanoAppBinary appBinary) { |
| 358 | Preconditions.checkNotNull(hubInfo, "ContextHubInfo cannot be null"); |
| 359 | Preconditions.checkNotNull(appBinary, "NanoAppBinary cannot be null"); |
| 360 | |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 361 | ContextHubTransaction<Void> transaction = |
| 362 | new ContextHubTransaction<>(ContextHubTransaction.TYPE_LOAD_NANOAPP); |
| 363 | IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 364 | |
| 365 | try { |
| 366 | mService.loadNanoAppOnHub(hubInfo.getId(), callback, appBinary); |
| 367 | } catch (RemoteException e) { |
| 368 | throw e.rethrowFromSystemServer(); |
| 369 | } |
| 370 | |
| 371 | return transaction; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Unloads a nanoapp at the specified Context Hub. |
| 376 | * |
| 377 | * @param hubInfo the hub to unload the nanoapp from |
| 378 | * @param nanoAppId the app to unload |
| 379 | * |
| 380 | * @return the ContextHubTransaction of the request |
| 381 | * |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 382 | * @throws NullPointerException if hubInfo is null |
| 383 | * |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 384 | * @hide |
| 385 | */ |
| 386 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 387 | @NonNull public ContextHubTransaction<Void> unloadNanoApp( |
| 388 | @NonNull ContextHubInfo hubInfo, long nanoAppId) { |
| 389 | Preconditions.checkNotNull(hubInfo, "ContextHubInfo cannot be null"); |
| 390 | |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 391 | ContextHubTransaction<Void> transaction = |
| 392 | new ContextHubTransaction<>(ContextHubTransaction.TYPE_UNLOAD_NANOAPP); |
| 393 | IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 394 | |
| 395 | try { |
| 396 | mService.unloadNanoAppFromHub(hubInfo.getId(), callback, nanoAppId); |
| 397 | } catch (RemoteException e) { |
| 398 | throw e.rethrowFromSystemServer(); |
| 399 | } |
| 400 | |
| 401 | return transaction; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Enables a nanoapp at the specified Context Hub. |
| 406 | * |
| 407 | * @param hubInfo the hub to enable the nanoapp on |
| 408 | * @param nanoAppId the app to enable |
| 409 | * |
| 410 | * @return the ContextHubTransaction of the request |
| 411 | * |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 412 | * @throws NullPointerException if hubInfo is null |
| 413 | * |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 414 | * @hide |
| 415 | */ |
| 416 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 417 | @NonNull public ContextHubTransaction<Void> enableNanoApp( |
| 418 | @NonNull ContextHubInfo hubInfo, long nanoAppId) { |
| 419 | Preconditions.checkNotNull(hubInfo, "ContextHubInfo cannot be null"); |
| 420 | |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 421 | ContextHubTransaction<Void> transaction = |
| 422 | new ContextHubTransaction<>(ContextHubTransaction.TYPE_ENABLE_NANOAPP); |
| 423 | IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 424 | |
| 425 | try { |
| 426 | mService.enableNanoApp(hubInfo.getId(), callback, nanoAppId); |
| 427 | } catch (RemoteException e) { |
| 428 | throw e.rethrowFromSystemServer(); |
| 429 | } |
| 430 | |
| 431 | return transaction; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Disables a nanoapp at the specified Context Hub. |
| 436 | * |
| 437 | * @param hubInfo the hub to disable the nanoapp on |
| 438 | * @param nanoAppId the app to disable |
| 439 | * |
| 440 | * @return the ContextHubTransaction of the request |
| 441 | * |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 442 | * @throws NullPointerException if hubInfo is null |
| 443 | * |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 444 | * @hide |
| 445 | */ |
| 446 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 447 | @NonNull public ContextHubTransaction<Void> disableNanoApp( |
| 448 | @NonNull ContextHubInfo hubInfo, long nanoAppId) { |
| 449 | Preconditions.checkNotNull(hubInfo, "ContextHubInfo cannot be null"); |
| 450 | |
Arthur Ishiguro | 54e1a89 | 2017-12-12 15:09:31 -0800 | [diff] [blame] | 451 | ContextHubTransaction<Void> transaction = |
| 452 | new ContextHubTransaction<>(ContextHubTransaction.TYPE_DISABLE_NANOAPP); |
| 453 | IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 454 | |
| 455 | try { |
| 456 | mService.disableNanoApp(hubInfo.getId(), callback, nanoAppId); |
| 457 | } catch (RemoteException e) { |
| 458 | throw e.rethrowFromSystemServer(); |
| 459 | } |
| 460 | |
| 461 | return transaction; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Requests a query for nanoapps loaded at the specified Context Hub. |
| 466 | * |
| 467 | * @param hubInfo the hub to query a list of nanoapps from |
| 468 | * |
| 469 | * @return the ContextHubTransaction of the request |
| 470 | * |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 471 | * @throws NullPointerException if hubInfo is null |
| 472 | * |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 473 | * @hide |
| 474 | */ |
| 475 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 476 | @NonNull public ContextHubTransaction<List<NanoAppState>> queryNanoApps( |
| 477 | @NonNull ContextHubInfo hubInfo) { |
| 478 | Preconditions.checkNotNull(hubInfo, "ContextHubInfo cannot be null"); |
| 479 | |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 480 | ContextHubTransaction<List<NanoAppState>> transaction = |
| 481 | new ContextHubTransaction<>(ContextHubTransaction.TYPE_QUERY_NANOAPPS); |
| 482 | IContextHubTransactionCallback callback = createQueryCallback(transaction); |
| 483 | |
| 484 | try { |
| 485 | mService.queryNanoApps(hubInfo.getId(), callback); |
| 486 | } catch (RemoteException e) { |
| 487 | throw e.rethrowFromSystemServer(); |
| 488 | } |
| 489 | |
| 490 | return transaction; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /** |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 494 | * Set a callback to receive messages from the context hub |
| 495 | * |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 496 | * @param callback Callback object |
| 497 | * |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 498 | * @see Callback |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 499 | * |
| 500 | * @return int 0 on success, -1 otherwise |
| 501 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 502 | @SuppressLint("Doclava125") |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 503 | public int registerCallback(@NonNull Callback callback) { |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 504 | return registerCallback(callback, null); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /** |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 508 | * @deprecated Use {@link #registerCallback(Callback)} instead. |
| 509 | * @hide |
| 510 | */ |
| 511 | @Deprecated |
| 512 | public int registerCallback(ICallback callback) { |
| 513 | if (mLocalCallback != null) { |
| 514 | Log.w(TAG, "Max number of local callbacks reached!"); |
| 515 | return -1; |
| 516 | } |
| 517 | mLocalCallback = callback; |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | /** |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 522 | * Set a callback to receive messages from the context hub |
| 523 | * |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 524 | * @param callback Callback object |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 525 | * @param handler Handler object |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 526 | * |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 527 | * @see Callback |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 528 | * |
| 529 | * @return int 0 on success, -1 otherwise |
| 530 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 531 | @SuppressLint("Doclava125") |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 532 | public int registerCallback(Callback callback, Handler handler) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 533 | synchronized(this) { |
| 534 | if (mCallback != null) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 535 | Log.w(TAG, "Max number of callbacks reached!"); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 536 | return -1; |
| 537 | } |
| 538 | mCallback = callback; |
| 539 | mCallbackHandler = handler; |
| 540 | } |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | /** |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 545 | * Creates an interface to the ContextHubClient to send down to the service. |
| 546 | * |
| 547 | * @param callback the callback to invoke at the client process |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 548 | * @param executor the executor to invoke callbacks for this client |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 549 | * |
| 550 | * @return the callback interface |
| 551 | */ |
| 552 | private IContextHubClientCallback createClientCallback( |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 553 | ContextHubClientCallback callback, Executor executor) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 554 | return new IContextHubClientCallback.Stub() { |
| 555 | @Override |
| 556 | public void onMessageFromNanoApp(NanoAppMessage message) { |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 557 | executor.execute(() -> callback.onMessageFromNanoApp(message)); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | @Override |
| 561 | public void onHubReset() { |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 562 | executor.execute(() -> callback.onHubReset()); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public void onNanoAppAborted(long nanoAppId, int abortCode) { |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 567 | executor.execute(() -> callback.onNanoAppAborted(nanoAppId, abortCode)); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void onNanoAppLoaded(long nanoAppId) { |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 572 | executor.execute(() -> callback.onNanoAppLoaded(nanoAppId)); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public void onNanoAppUnloaded(long nanoAppId) { |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 577 | executor.execute(() -> callback.onNanoAppUnloaded(nanoAppId)); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public void onNanoAppEnabled(long nanoAppId) { |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 582 | executor.execute(() -> callback.onNanoAppEnabled(nanoAppId)); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | @Override |
| 586 | public void onNanoAppDisabled(long nanoAppId) { |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 587 | executor.execute(() -> callback.onNanoAppDisabled(nanoAppId)); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 588 | } |
| 589 | }; |
| 590 | } |
| 591 | |
| 592 | /** |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 593 | * Creates and registers a client and its callback with the Context Hub Service. |
| 594 | * |
| 595 | * A client is registered with the Context Hub Service for a specified Context Hub. When the |
| 596 | * registration succeeds, the client can send messages to nanoapps through the returned |
| 597 | * {@link ContextHubClient} object, and receive notifications through the provided callback. |
| 598 | * |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 599 | * @param hubInfo the hub to attach this client to |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 600 | * @param callback the notification callback to register |
| 601 | * @param executor the executor to invoke the callback |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 602 | * @return the registered client object |
| 603 | * |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 604 | * @throws IllegalArgumentException if hubInfo does not represent a valid hub |
| 605 | * @throws IllegalStateException if there were too many registered clients at the service |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 606 | * @throws NullPointerException if callback, hubInfo, or executor is null |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 607 | * |
| 608 | * @hide |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 609 | * @see ContextHubClientCallback |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 610 | */ |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 611 | @NonNull public ContextHubClient createClient( |
| 612 | @NonNull ContextHubInfo hubInfo, @NonNull ContextHubClientCallback callback, |
| 613 | @NonNull @CallbackExecutor Executor executor) { |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 614 | Preconditions.checkNotNull(callback, "Callback cannot be null"); |
| 615 | Preconditions.checkNotNull(hubInfo, "ContextHubInfo cannot be null"); |
| 616 | Preconditions.checkNotNull(executor, "Executor cannot be null"); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 617 | |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 618 | IContextHubClientCallback clientInterface = createClientCallback(callback, executor); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 619 | |
| 620 | IContextHubClient client; |
| 621 | try { |
| 622 | client = mService.createClient(clientInterface, hubInfo.getId()); |
| 623 | } catch (RemoteException e) { |
| 624 | throw e.rethrowFromSystemServer(); |
| 625 | } |
| 626 | |
| 627 | return new ContextHubClient(client, clientInterface, hubInfo); |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
Arthur Ishiguro | 9b9c9d8 | 2017-12-20 16:11:53 -0800 | [diff] [blame] | 631 | * Equivalent to {@link #createClient(ContextHubInfo, ContextHubClientCallback, Executor)} |
| 632 | * with the executor using the main thread's Looper. |
| 633 | * |
| 634 | * @param hubInfo the hub to attach this client to |
| 635 | * @param callback the notification callback to register |
| 636 | * @return the registered client object |
| 637 | * |
| 638 | * @throws IllegalArgumentException if hubInfo does not represent a valid hub |
| 639 | * @throws IllegalStateException if there were too many registered clients at the service |
| 640 | * @throws NullPointerException if callback or hubInfo is null |
| 641 | * @hide |
| 642 | * @see ContextHubClientCallback |
| 643 | */ |
| 644 | @NonNull public ContextHubClient createClient( |
| 645 | @NonNull ContextHubInfo hubInfo, @NonNull ContextHubClientCallback callback) { |
| 646 | return createClient(hubInfo, callback, new HandlerExecutor(Handler.getMain())); |
| 647 | } |
| 648 | |
| 649 | /** |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 650 | * Unregister a callback for receive messages from the context hub. |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 651 | * |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 652 | * @see Callback |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 653 | * |
| 654 | * @param callback method to deregister |
| 655 | * |
| 656 | * @return int 0 on success, -1 otherwise |
| 657 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 658 | @SuppressLint("Doclava125") |
Arthur Ishiguro | ac7b959 | 2018-01-02 09:46:42 -0800 | [diff] [blame^] | 659 | public int unregisterCallback(@NonNull Callback callback) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 660 | synchronized(this) { |
| 661 | if (callback != mCallback) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 662 | Log.w(TAG, "Cannot recognize callback!"); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | mCallback = null; |
| 667 | mCallbackHandler = null; |
| 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 672 | /** |
| 673 | * @deprecated Use {@link #unregisterCallback(Callback)} instead. |
| 674 | * @hide |
| 675 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 676 | @Deprecated |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 677 | public synchronized int unregisterCallback(ICallback callback) { |
| 678 | if (callback != mLocalCallback) { |
| 679 | Log.w(TAG, "Cannot recognize local callback!"); |
| 680 | return -1; |
| 681 | } |
| 682 | mLocalCallback = null; |
| 683 | return 0; |
| 684 | } |
| 685 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 686 | private final IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() { |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 687 | @Override |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 688 | public void onMessageReceipt(final int hubId, final int nanoAppId, |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 689 | final ContextHubMessage message) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 690 | if (mCallback != null) { |
| 691 | synchronized(this) { |
Greg Kaiser | 6ba60e6 | 2016-03-18 10:08:39 -0700 | [diff] [blame] | 692 | final Callback callback = mCallback; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 693 | Handler handler = mCallbackHandler == null ? |
| 694 | new Handler(mMainLooper) : mCallbackHandler; |
| 695 | handler.post(new Runnable() { |
| 696 | @Override |
| 697 | public void run() { |
| 698 | callback.onMessageReceipt(hubId, nanoAppId, message); |
| 699 | } |
| 700 | }); |
| 701 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 702 | } else if (mLocalCallback != null) { |
| 703 | // we always ensure that mCallback takes precedence, because mLocalCallback is only |
| 704 | // for internal compatibility |
| 705 | synchronized (this) { |
| 706 | mLocalCallback.onMessageReceipt(hubId, nanoAppId, message); |
| 707 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 708 | } else { |
| 709 | Log.d(TAG, "Context hub manager client callback is NULL"); |
| 710 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 711 | } |
| 712 | }; |
| 713 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 714 | /** @throws ServiceNotFoundException |
| 715 | * @hide */ |
| 716 | public ContextHubManager(Context context, Looper mainLooper) throws ServiceNotFoundException { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 717 | mMainLooper = mainLooper; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 718 | mService = IContextHubService.Stub.asInterface( |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 719 | ServiceManager.getServiceOrThrow(Context.CONTEXTHUB_SERVICE)); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 720 | try { |
| 721 | mService.registerCallback(mClientCallback); |
| 722 | } catch (RemoteException e) { |
| 723 | Log.w(TAG, "Could not register callback:" + e); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 724 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 725 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 726 | } |