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 17 package com.android.server.location; 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 /* 36 * true if the transaction has already completed, false otherwise 37 */ 38 private boolean mIsComplete = false; 39 40 /* package */ ContextHubServiceTransaction(int id, int type) { 41 mTransactionId = id; 42 mTransactionType = type; 43 } 44 45 /** 46 * Starts this transaction with a Context Hub. 47 * 48 * All instances of this class must implement this method by making an asynchronous request to 49 * a hub. 50 * 51 * @return the synchronous error code of the transaction start 52 */ 53 /* package */ 54 abstract int onTransact(); 55 56 /** 57 * A function to invoke when the transaction completes. 58 * 59 * For transactions with expected contents (such as a query), the class instance should 60 * implement the appropriate behavior (e.g. invoke onQueryResponse with an empty list). 61 * 62 * @param result the result of the transaction 63 */ 64 /* package */ void onTransactionComplete(@ContextHubTransaction.Result int result) { 65 } 66 67 /** 68 * A function to invoke when a query transaction completes. 69 * 70 * Only relevant for query transactions. 71 * 72 * @param result the result of the query 73 * @param nanoAppStateList the list of nanoapps given by the query response 74 */ 75 /* package */ void onQueryResponse( 76 @ContextHubTransaction.Result int result, List<NanoAppState> nanoAppStateList) { 77 } 78 79 /** 80 * @return the ID of this transaction 81 */ 82 /* package */ int getTransactionId() { 83 return mTransactionId; 84 } 85 86 /** 87 * @return the type of this transaction 88 * @see ContextHubTransaction.Type 89 */ 90 @ContextHubTransaction.Type 91 /* package */ int getTransactionType() { 92 return mTransactionType; 93 } 94 95 /** 96 * Gets the timeout period as defined in IContexthub.hal 97 * 98 * @return the timeout of this transaction in the specified time unit 99 */ 100 /* package */ long getTimeout(TimeUnit unit) { 101 switch (mTransactionType) { 102 case ContextHubTransaction.TYPE_LOAD_NANOAPP: 103 return unit.convert(30L, TimeUnit.SECONDS); 104 case ContextHubTransaction.TYPE_UNLOAD_NANOAPP: 105 case ContextHubTransaction.TYPE_ENABLE_NANOAPP: 106 case ContextHubTransaction.TYPE_DISABLE_NANOAPP: 107 case ContextHubTransaction.TYPE_QUERY_NANOAPPS: 108 // Note: query timeout is not specified at the HAL 109 default: /* fall through */ 110 return unit.convert(5L, TimeUnit.SECONDS); 111 } 112 } 113 114 /** 115 * Marks the transaction as complete. 116 * 117 * Should only be called as a result of a response from a Context Hub callback 118 */ 119 /* package */ void setComplete() { 120 mIsComplete = true; 121 } 122 123 /** 124 * @return true if the transaction has already completed, false otherwise 125 */ 126 /* package */ boolean isComplete() { 127 return mIsComplete; 128 } 129 130 @Override 131 public String toString() { 132 return ContextHubTransaction.typeToString(mTransactionType, true /* upperCase */) 133 + " transaction (ID = " + mTransactionId + ")"; 134 } 135 } 136