blob: 52527ed67ae4d8d9d294183bb3304a67d829ba6d [file] [log] [blame]
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -07001/*
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 */
16package android.hardware.location;
17
18import android.annotation.RequiresPermission;
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080019import android.os.RemoteException;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070020
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080021import dalvik.system.CloseGuard;
22
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070023import java.io.Closeable;
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080024import java.util.concurrent.atomic.AtomicBoolean;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070025
26/**
27 * A class describing a client of the Context Hub Service.
28 *
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080029 * 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 Ishigurob9ae7bd2017-10-09 12:47:52 -070031 *
32 * @hide
33 */
34public class ContextHubClient implements Closeable {
35 /*
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080036 * The proxy to the client interface at the service.
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070037 */
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080038 private final IContextHubClient mClientProxy;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070039
40 /*
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080041 * The callback interface associated with this client.
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070042 */
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080043 private final IContextHubClientCallback mCallbackInterface;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070044
45 /*
46 * The Context Hub that this client is attached to.
47 */
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080048 private final ContextHubInfo mAttachedHub;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070049
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080050 private final CloseGuard mCloseGuard = CloseGuard.get();
51
52 private final AtomicBoolean mIsClosed = new AtomicBoolean(false);
53
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080054 /* package */ ContextHubClient(
55 IContextHubClient clientProxy, IContextHubClientCallback callback,
56 ContextHubInfo hubInfo) {
57 mClientProxy = clientProxy;
58 mCallbackInterface = callback;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070059 mAttachedHub = hubInfo;
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080060 mCloseGuard.open("close");
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070061 }
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 Ishiguro49e030f2017-11-16 11:54:42 -080079 if (!mIsClosed.getAndSet(true)) {
80 mCloseGuard.close();
81 try {
82 mClientProxy.close();
83 } catch (RemoteException e) {
84 throw e.rethrowFromSystemServer();
85 }
86 }
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070087 }
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 Ishiguro4e39aa12017-11-14 14:59:08 -0800105 try {
106 return mClientProxy.sendMessageToNanoApp(message);
107 } catch (RemoteException e) {
108 throw e.rethrowFromSystemServer();
109 }
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -0700110 }
Arthur Ishiguro49e030f2017-11-16 11:54:42 -0800111
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 Ishigurob9ae7bd2017-10-09 12:47:52 -0700123}