Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 18 | import android.annotation.RequiresPermission; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 19 | import android.os.RemoteException; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 20 | |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 21 | import dalvik.system.CloseGuard; |
| 22 | |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 23 | import java.io.Closeable; |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 24 | import java.util.concurrent.atomic.AtomicBoolean; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * A class describing a client of the Context Hub Service. |
| 28 | * |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 29 | * Clients can send messages to nanoapps at a Context Hub through this object. The APIs supported |
| 30 | * by this object are thread-safe and can be used without external synchronization. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 31 | * |
| 32 | * @hide |
| 33 | */ |
| 34 | public class ContextHubClient implements Closeable { |
| 35 | /* |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 36 | * The proxy to the client interface at the service. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 37 | */ |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 38 | private final IContextHubClient mClientProxy; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 41 | * The callback interface associated with this client. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 42 | */ |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 43 | private final IContextHubClientCallback mCallbackInterface; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * The Context Hub that this client is attached to. |
| 47 | */ |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 48 | private final ContextHubInfo mAttachedHub; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 49 | |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 50 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 51 | |
| 52 | private final AtomicBoolean mIsClosed = new AtomicBoolean(false); |
| 53 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 54 | /* package */ ContextHubClient( |
| 55 | IContextHubClient clientProxy, IContextHubClientCallback callback, |
| 56 | ContextHubInfo hubInfo) { |
| 57 | mClientProxy = clientProxy; |
| 58 | mCallbackInterface = callback; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 59 | mAttachedHub = hubInfo; |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 60 | mCloseGuard.open("close"); |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the hub that this client is attached to. |
| 65 | * |
| 66 | * @return the ContextHubInfo of the attached hub |
| 67 | */ |
| 68 | public ContextHubInfo getAttachedHub() { |
| 69 | return mAttachedHub; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Closes the connection for this client and the Context Hub Service. |
| 74 | * |
| 75 | * When this function is invoked, the messaging associated with this client is invalidated. |
| 76 | * All futures messages targeted for this client are dropped at the service. |
| 77 | */ |
| 78 | public void close() { |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 79 | if (!mIsClosed.getAndSet(true)) { |
| 80 | mCloseGuard.close(); |
| 81 | try { |
| 82 | mClientProxy.close(); |
| 83 | } catch (RemoteException e) { |
| 84 | throw e.rethrowFromSystemServer(); |
| 85 | } |
| 86 | } |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sends a message to a nanoapp through the Context Hub Service. |
| 91 | * |
| 92 | * This function returns TRANSACTION_SUCCESS if the message has reached the HAL, but |
| 93 | * does not guarantee delivery of the message to the target nanoapp. |
| 94 | * |
| 95 | * @param message the message object to send |
| 96 | * |
| 97 | * @return the result of sending the message defined as in ContextHubTransaction.Result |
| 98 | * |
| 99 | * @see NanoAppMessage |
| 100 | * @see ContextHubTransaction.Result |
| 101 | */ |
| 102 | @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) |
| 103 | @ContextHubTransaction.Result |
| 104 | public int sendMessageToNanoApp(NanoAppMessage message) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 105 | try { |
| 106 | return mClientProxy.sendMessageToNanoApp(message); |
| 107 | } catch (RemoteException e) { |
| 108 | throw e.rethrowFromSystemServer(); |
| 109 | } |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 110 | } |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | protected void finalize() throws Throwable { |
| 114 | try { |
| 115 | if (mCloseGuard != null) { |
| 116 | mCloseGuard.warnIfOpen(); |
| 117 | } |
| 118 | close(); |
| 119 | } finally { |
| 120 | super.finalize(); |
| 121 | } |
| 122 | } |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 123 | } |