• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 package com.android.server.location.contexthub;
18 
19 import android.hardware.location.ContextHubTransaction;
20 import android.hardware.location.NanoAppState;
21 
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24 
25 /**
26  * An abstract class representing transactions requested to the Context Hub Service.
27  *
28  * @hide
29  */
30 /* package */ abstract class ContextHubServiceTransaction {
31     private final int mTransactionId;
32     @ContextHubTransaction.Type
33     private final int mTransactionType;
34 
35     /* The ID of the nanoapp this transaction is targeted for, null if not applicable. */
36     private final Long mNanoAppId;
37 
38     /*
39      * The host package associated with this transaction.
40      */
41     private final String mPackage;
42 
43     /*
44      * true if the transaction has already completed, false otherwise
45      */
46     private boolean mIsComplete = false;
47 
ContextHubServiceTransaction(int id, int type, String packageName)48     /* package */ ContextHubServiceTransaction(int id, int type, String packageName) {
49         mTransactionId = id;
50         mTransactionType = type;
51         mNanoAppId = null;
52         mPackage = packageName;
53     }
54 
ContextHubServiceTransaction(int id, int type, long nanoAppId, String packageName)55     /* package */ ContextHubServiceTransaction(int id, int type, long nanoAppId,
56             String packageName) {
57         mTransactionId = id;
58         mTransactionType = type;
59         mNanoAppId = nanoAppId;
60         mPackage = packageName;
61     }
62 
63     /**
64      * Starts this transaction with a Context Hub.
65      *
66      * All instances of this class must implement this method by making an asynchronous request to
67      * a hub.
68      *
69      * @return the synchronous error code of the transaction start
70      */
71     /* package */
onTransact()72     abstract int onTransact();
73 
74     /**
75      * A function to invoke when the transaction completes.
76      *
77      * For transactions with expected contents (such as a query), the class instance should
78      * implement the appropriate behavior (e.g. invoke onQueryResponse with an empty list).
79      *
80      * @param result the result of the transaction
81      */
onTransactionComplete(@ontextHubTransaction.Result int result)82     /* package */ void onTransactionComplete(@ContextHubTransaction.Result int result) {
83     }
84 
85     /**
86      * A function to invoke when a query transaction completes.
87      *
88      * Only relevant for query transactions.
89      *
90      * @param result           the result of the query
91      * @param nanoAppStateList the list of nanoapps given by the query response
92      */
onQueryResponse( @ontextHubTransaction.Result int result, List<NanoAppState> nanoAppStateList)93     /* package */ void onQueryResponse(
94             @ContextHubTransaction.Result int result, List<NanoAppState> nanoAppStateList) {
95     }
96 
97     /**
98      * @return the ID of this transaction
99      */
getTransactionId()100     /* package */ int getTransactionId() {
101         return mTransactionId;
102     }
103 
104     /**
105      * @return the type of this transaction
106      * @see ContextHubTransaction.Type
107      */
108     @ContextHubTransaction.Type
getTransactionType()109     /* package */ int getTransactionType() {
110         return mTransactionType;
111     }
112 
113     /**
114      * Gets the timeout period as defined in IContexthub.hal
115      *
116      * @return the timeout of this transaction in the specified time unit
117      */
getTimeout(TimeUnit unit)118     /* package */ long getTimeout(TimeUnit unit) {
119         switch (mTransactionType) {
120             case ContextHubTransaction.TYPE_LOAD_NANOAPP:
121                 return unit.convert(30L, TimeUnit.SECONDS);
122             case ContextHubTransaction.TYPE_UNLOAD_NANOAPP:
123             case ContextHubTransaction.TYPE_ENABLE_NANOAPP:
124             case ContextHubTransaction.TYPE_DISABLE_NANOAPP:
125             case ContextHubTransaction.TYPE_QUERY_NANOAPPS:
126                 // Note: query timeout is not specified at the HAL
127             default: /* fall through */
128                 return unit.convert(5L, TimeUnit.SECONDS);
129         }
130     }
131 
132     /**
133      * Marks the transaction as complete.
134      *
135      * Should only be called as a result of a response from a Context Hub callback
136      */
setComplete()137     /* package */ void setComplete() {
138         mIsComplete = true;
139     }
140 
141     /**
142      * @return true if the transaction has already completed, false otherwise
143      */
isComplete()144     /* package */ boolean isComplete() {
145         return mIsComplete;
146     }
147 
148     @Override
toString()149     public String toString() {
150         String out = ContextHubTransaction.typeToString(mTransactionType, true /* upperCase */)
151                 + " (";
152         if (mNanoAppId != null) {
153             out += "appId = 0x" + Long.toHexString(mNanoAppId) + ", ";
154         }
155         out += "package = " + mPackage + ")";
156 
157         return out;
158     }
159 }
160