blob: 0a21083a02626256fab82d85e2582db8c7563d59 [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
Arthur Ishiguroac7b9592018-01-02 09:46:42 -080018import android.annotation.NonNull;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070019import android.annotation.RequiresPermission;
Arthur Ishigurof2b6f012017-11-28 15:21:38 -080020import android.annotation.SystemApi;
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080021import android.os.RemoteException;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070022
Arthur Ishiguroac7b9592018-01-02 09:46:42 -080023import com.android.internal.util.Preconditions;
24
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080025import dalvik.system.CloseGuard;
26
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070027import java.io.Closeable;
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080028import java.util.concurrent.atomic.AtomicBoolean;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070029
30/**
31 * A class describing a client of the Context Hub Service.
32 *
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080033 * Clients can send messages to nanoapps at a Context Hub through this object. The APIs supported
34 * by this object are thread-safe and can be used without external synchronization.
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070035 *
36 * @hide
37 */
Arthur Ishigurof2b6f012017-11-28 15:21:38 -080038@SystemApi
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070039public class ContextHubClient implements Closeable {
40 /*
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080041 * The proxy to the client interface at the service.
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070042 */
Arthur Ishiguro78493a12018-01-02 10:44:21 -080043 private IContextHubClient mClientProxy = null;
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 Ishiguro78493a12018-01-02 10:44:21 -080054 /* package */ ContextHubClient(ContextHubInfo hubInfo) {
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070055 mAttachedHub = hubInfo;
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080056 mCloseGuard.open("close");
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070057 }
58
59 /**
Arthur Ishiguro78493a12018-01-02 10:44:21 -080060 * Sets the proxy interface of the client at the service. This method should always be called
61 * by the ContextHubManager after the client is registered at the service, and should only be
62 * called once.
63 *
64 * @param clientProxy the proxy of the client at the service
65 */
66 /* package */ void setClientProxy(IContextHubClient clientProxy) {
67 Preconditions.checkNotNull(clientProxy, "IContextHubClient cannot be null");
68 if (mClientProxy != null) {
69 throw new IllegalStateException("Cannot change client proxy multiple times");
70 }
71
72 mClientProxy = clientProxy;
73 }
74
75 /**
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070076 * Returns the hub that this client is attached to.
77 *
78 * @return the ContextHubInfo of the attached hub
79 */
Arthur Ishiguroac7b9592018-01-02 09:46:42 -080080 @NonNull
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070081 public ContextHubInfo getAttachedHub() {
82 return mAttachedHub;
83 }
84
85 /**
86 * Closes the connection for this client and the Context Hub Service.
87 *
88 * When this function is invoked, the messaging associated with this client is invalidated.
89 * All futures messages targeted for this client are dropped at the service.
90 */
91 public void close() {
Arthur Ishiguro49e030f2017-11-16 11:54:42 -080092 if (!mIsClosed.getAndSet(true)) {
93 mCloseGuard.close();
94 try {
95 mClientProxy.close();
96 } catch (RemoteException e) {
97 throw e.rethrowFromSystemServer();
98 }
99 }
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -0700100 }
101
102 /**
103 * Sends a message to a nanoapp through the Context Hub Service.
104 *
105 * This function returns TRANSACTION_SUCCESS if the message has reached the HAL, but
106 * does not guarantee delivery of the message to the target nanoapp.
107 *
108 * @param message the message object to send
109 *
110 * @return the result of sending the message defined as in ContextHubTransaction.Result
111 *
Arthur Ishiguroac7b9592018-01-02 09:46:42 -0800112 * @throws NullPointerException if NanoAppMessage is null
113 *
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -0700114 * @see NanoAppMessage
115 * @see ContextHubTransaction.Result
116 */
117 @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
118 @ContextHubTransaction.Result
Arthur Ishiguroac7b9592018-01-02 09:46:42 -0800119 public int sendMessageToNanoApp(@NonNull NanoAppMessage message) {
120 Preconditions.checkNotNull(message, "NanoAppMessage cannot be null");
121
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -0800122 try {
123 return mClientProxy.sendMessageToNanoApp(message);
124 } catch (RemoteException e) {
125 throw e.rethrowFromSystemServer();
126 }
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -0700127 }
Arthur Ishiguro49e030f2017-11-16 11:54:42 -0800128
129 @Override
130 protected void finalize() throws Throwable {
131 try {
132 if (mCloseGuard != null) {
133 mCloseGuard.warnIfOpen();
134 }
135 close();
136 } finally {
137 super.finalize();
138 }
139 }
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -0700140}