1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware.location;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.service.vr.IVrManager;
import android.service.vr.IVrStateCallbacks;
import android.util.Log;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
/**
* @hide
*/
public class ContextHubService extends IContextHubService.Stub {
public static final String CONTEXTHUB_SERVICE = "contexthub_service";
private static final String TAG = "ContextHubService";
private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE;
private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '"
+ HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware";
public static final int ANY_HUB = -1;
public static final int MSG_LOAD_NANO_APP = 3;
public static final int MSG_UNLOAD_NANO_APP = 4;
private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown";
private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN;
private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN;
private static final int PRE_LOADED_APP_MEM_REQ = 0;
private static final int MSG_HEADER_SIZE = 4;
private static final int MSG_FIELD_TYPE = 0;
private static final int MSG_FIELD_VERSION = 1;
private static final int MSG_FIELD_HUB_HANDLE = 2;
private static final int MSG_FIELD_APP_INSTANCE = 3;
private static final int OS_APP_INSTANCE = -1;
private static final long APP_ID_ACTIVITY_RECOGNITION = 0x476f6f676c001000L;
private final Context mContext;
private final ConcurrentHashMap<Integer, NanoAppInstanceInfo> mNanoAppHash =
new ConcurrentHashMap<>();
private final ContextHubInfo[] mContextHubInfo;
private final RemoteCallbackList<IContextHubCallback> mCallbacksList =
new RemoteCallbackList<>();
private native int nativeSendMessage(int[] header, byte[] data);
private native ContextHubInfo[] nativeInitialize();
private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
@Override
public void onVrStateChanged(boolean enabled) {
for (NanoAppInstanceInfo app : mNanoAppHash.values()) {
if (app.getAppId() == APP_ID_ACTIVITY_RECOGNITION) {
sendVrStateChangeMessageToApp(app, enabled);
break;
}
}
}
};
public ContextHubService(Context context) {
mContext = context;
mContextHubInfo = nativeInitialize();
for (int i = 0; i < mContextHubInfo.length; i++) {
Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
+ ", name: " + mContextHubInfo[i].getName());
}
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_VR_MODE)) {
IVrManager vrManager =
IVrManager.Stub.asInterface(ServiceManager.getService("vrmanager"));
if (vrManager != null) {
try {
vrManager.registerListener(mVrStateCallbacks);
} catch (RemoteException e) {
Log.e(TAG, "VR state listener registration failed", e);
}
}
}
}
@Override
public int registerCallback(IContextHubCallback callback) throws RemoteException {
checkPermissions();
mCallbacksList.register(callback);
return 0;
}
@Override
public int[] getContextHubHandles() throws RemoteException {
checkPermissions();
int[] returnArray = new int[mContextHubInfo.length];
for (int i = 0; i < returnArray.length; ++i) {
returnArray[i] = i;
Log.d(TAG, String.format("Hub %s is mapped to %d",
mContextHubInfo[i].getName(), returnArray[i]));
}
return returnArray;
}
@Override
public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException {
checkPermissions();
if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) {
return null; // null means fail
}
return mContextHubInfo[contextHubHandle];
}
@Override
public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException {
checkPermissions();
if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) {
Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle);
return -1;
}
int[] msgHeader = new int[MSG_HEADER_SIZE];
msgHeader[MSG_FIELD_HUB_HANDLE] = contextHubHandle;
msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
msgHeader[MSG_FIELD_VERSION] = 0;
msgHeader[MSG_FIELD_TYPE] = MSG_LOAD_NANO_APP;
if (nativeSendMessage(msgHeader, app.getAppBinary()) != 0) {
return -1;
}
// Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
return 0;
}
@Override
public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException {
checkPermissions();
NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle);
if (info == null) {
return -1; //means failed
}
// Call Native interface here
int[] msgHeader = new int[MSG_HEADER_SIZE];
msgHeader[MSG_FIELD_HUB_HANDLE] = ANY_HUB;
msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
msgHeader[MSG_FIELD_VERSION] = 0;
msgHeader[MSG_FIELD_TYPE] = MSG_UNLOAD_NANO_APP;
if (nativeSendMessage(msgHeader, null) != 0) {
return -1;
}
// Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
return 0;
}
@Override
public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle)
throws RemoteException {
checkPermissions();
// This assumes that all the nanoAppInfo is current. This is reasonable
// for the use cases for tightly controlled nanoApps.
if (mNanoAppHash.containsKey(nanoAppInstanceHandle)) {
return mNanoAppHash.get(nanoAppInstanceHandle);
} else {
return null;
}
}
@Override
public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException {
checkPermissions();
ArrayList<Integer> foundInstances = new ArrayList<Integer>();
for (Integer nanoAppInstance: mNanoAppHash.keySet()) {
NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance);
if (filter.testMatch(info)) {
foundInstances.add(nanoAppInstance);
}
}
int[] retArray = new int[foundInstances.size()];
for (int i = 0; i < foundInstances.size(); i++) {
retArray[i] = foundInstances.get(i).intValue();
}
return retArray;
}
@Override
public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg)
throws RemoteException {
checkPermissions();
int[] msgHeader = new int[MSG_HEADER_SIZE];
msgHeader[MSG_FIELD_HUB_HANDLE] = hubHandle;
msgHeader[MSG_FIELD_APP_INSTANCE] = nanoAppHandle;
msgHeader[MSG_FIELD_VERSION] = msg.getVersion();
msgHeader[MSG_FIELD_TYPE] = msg.getMsgType();
return nativeSendMessage(msgHeader, msg.getData());
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
!= PackageManager.PERMISSION_GRANTED) {
pw.println("Permission Denial: can't dump contexthub_service");
return;
}
pw.println("Dumping ContextHub Service");
pw.println("");
// dump ContextHubInfo
pw.println("=================== CONTEXT HUBS ====================");
for (int i = 0; i < mContextHubInfo.length; i++) {
pw.println("Handle " + i + " : " + mContextHubInfo[i].toString());
}
pw.println("");
pw.println("=================== NANOAPPS ====================");
// Dump nanoAppHash
for (Integer nanoAppInstance: mNanoAppHash.keySet()) {
pw.println(nanoAppInstance + " : " + mNanoAppHash.get(nanoAppInstance).toString());
}
// dump eventLog
}
private void checkPermissions() {
mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE);
}
private int onMessageReceipt(int[] header, byte[] data) {
if (header == null || data == null || header.length < MSG_HEADER_SIZE) {
return -1;
}
int callbacksCount = mCallbacksList.beginBroadcast();
if (callbacksCount < 1) {
Log.v(TAG, "No message callbacks registered.");
return 0;
}
ContextHubMessage message =
new ContextHubMessage(header[MSG_FIELD_TYPE], header[MSG_FIELD_VERSION], data);
for (int i = 0; i < callbacksCount; ++i) {
IContextHubCallback callback = mCallbacksList.getBroadcastItem(i);
try {
callback.onMessageReceipt(
header[MSG_FIELD_HUB_HANDLE],
header[MSG_FIELD_APP_INSTANCE],
message);
} catch (RemoteException e) {
Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ").");
continue;
}
}
mCallbacksList.finishBroadcast();
return 0;
}
private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) {
// App Id encodes vendor & version
NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo();
appInfo.setAppId(appId);
appInfo.setAppVersion(appVersion);
appInfo.setName(PRE_LOADED_APP_NAME);
appInfo.setContexthubId(hubHandle);
appInfo.setHandle(appInstanceHandle);
appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER);
appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ);
appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ);
appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ);
mNanoAppHash.put(appInstanceHandle, appInfo);
Log.d(TAG, "Added app instance " + appInstanceHandle + " with id " + appId
+ " version " + appVersion);
return 0;
}
private void sendVrStateChangeMessageToApp(NanoAppInstanceInfo app, boolean vrModeEnabled) {
int[] msgHeader = new int[MSG_HEADER_SIZE];
msgHeader[MSG_FIELD_TYPE] = 0;
msgHeader[MSG_FIELD_VERSION] = 0;
msgHeader[MSG_FIELD_HUB_HANDLE] = ANY_HUB;
msgHeader[MSG_FIELD_APP_INSTANCE] = app.getHandle();
byte[] data = new byte[1];
data[0] = (byte) ((vrModeEnabled) ? 1 : 0);
int ret = nativeSendMessage(msgHeader, data);
if (ret != 0) {
Log.e(TAG, "Couldn't send VR state change notification (" + ret + ")!");
}
}
}
|