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