Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 android.hardware.location; |
| 18 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.HashMap; |
| 23 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 24 | import android.Manifest; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 27 | import android.os.RemoteCallbackList; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 28 | import android.os.RemoteException; |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 29 | import android.os.ServiceManager; |
| 30 | import android.service.vr.IVrManager; |
| 31 | import android.service.vr.IVrStateCallbacks; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 34 | import java.io.FileDescriptor; |
| 35 | import java.io.PrintWriter; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 36 | import java.util.ArrayList; |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 37 | import java.util.concurrent.ConcurrentHashMap; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * @hide |
| 41 | */ |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 42 | public class ContextHubService extends IContextHubService.Stub { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 43 | public static final String CONTEXTHUB_SERVICE = "contexthub_service"; |
| 44 | |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 45 | private static final String TAG = "ContextHubService"; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 46 | private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE; |
| 47 | private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '" |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 48 | + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware"; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 49 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 50 | |
| 51 | public static final int ANY_HUB = -1; |
Ashutosh Joshi | cafdee9 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 52 | public static final int MSG_LOAD_NANO_APP = 3; |
| 53 | public static final int MSG_UNLOAD_NANO_APP = 4; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 54 | |
| 55 | private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown"; |
| 56 | private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN; |
| 57 | private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN; |
| 58 | private static final int PRE_LOADED_APP_MEM_REQ = 0; |
| 59 | |
| 60 | private static final int MSG_HEADER_SIZE = 4; |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 61 | private static final int HEADER_FIELD_MSG_TYPE = 0; |
| 62 | private static final int HEADER_FIELD_MSG_VERSION = 1; |
| 63 | private static final int HEADER_FIELD_HUB_HANDLE = 2; |
| 64 | private static final int HEADER_FIELD_APP_INSTANCE = 3; |
| 65 | |
| 66 | private static final int HEADER_FIELD_LOAD_APP_ID_LO = MSG_HEADER_SIZE; |
| 67 | private static final int HEADER_FIELD_LOAD_APP_ID_HI = MSG_HEADER_SIZE + 1; |
| 68 | private static final int MSG_LOAD_APP_HEADER_SIZE = MSG_HEADER_SIZE + 2; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 69 | |
| 70 | private static final int OS_APP_INSTANCE = -1; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 71 | |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 72 | private static final long APP_ID_ACTIVITY_RECOGNITION = 0x476f6f676c001000L; |
| 73 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 74 | private final Context mContext; |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 75 | private final ConcurrentHashMap<Integer, NanoAppInstanceInfo> mNanoAppHash = |
| 76 | new ConcurrentHashMap<>(); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 77 | private final ContextHubInfo[] mContextHubInfo; |
| 78 | private final RemoteCallbackList<IContextHubCallback> mCallbacksList = |
| 79 | new RemoteCallbackList<>(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 80 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 81 | private native int nativeSendMessage(int[] header, byte[] data); |
| 82 | private native ContextHubInfo[] nativeInitialize(); |
| 83 | |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 84 | private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() { |
| 85 | @Override |
| 86 | public void onVrStateChanged(boolean enabled) { |
| 87 | for (NanoAppInstanceInfo app : mNanoAppHash.values()) { |
| 88 | if (app.getAppId() == APP_ID_ACTIVITY_RECOGNITION) { |
| 89 | sendVrStateChangeMessageToApp(app, enabled); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | }; |
| 95 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 96 | public ContextHubService(Context context) { |
| 97 | mContext = context; |
| 98 | mContextHubInfo = nativeInitialize(); |
| 99 | |
| 100 | for (int i = 0; i < mContextHubInfo.length; i++) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 101 | Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId() |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 102 | + ", name: " + mContextHubInfo[i].getName()); |
| 103 | } |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 104 | |
| 105 | if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_VR_MODE)) { |
| 106 | IVrManager vrManager = |
| 107 | IVrManager.Stub.asInterface(ServiceManager.getService("vrmanager")); |
| 108 | if (vrManager != null) { |
| 109 | try { |
| 110 | vrManager.registerListener(mVrStateCallbacks); |
| 111 | } catch (RemoteException e) { |
| 112 | Log.e(TAG, "VR state listener registration failed", e); |
| 113 | } |
| 114 | } |
| 115 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 116 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 117 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 118 | @Override |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 119 | public int registerCallback(IContextHubCallback callback) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 120 | checkPermissions(); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 121 | mCallbacksList.register(callback); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public int[] getContextHubHandles() throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 127 | checkPermissions(); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 128 | int[] returnArray = new int[mContextHubInfo.length]; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 129 | |
| 130 | for (int i = 0; i < returnArray.length; ++i) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 131 | returnArray[i] = i; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 132 | Log.d(TAG, String.format("Hub %s is mapped to %d", |
| 133 | mContextHubInfo[i].getName(), returnArray[i])); |
| 134 | } |
| 135 | |
| 136 | return returnArray; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 141 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 142 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
| 143 | return null; // null means fail |
| 144 | } |
| 145 | |
| 146 | return mContextHubInfo[contextHubHandle]; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 151 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 152 | |
| 153 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 154 | Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle); |
| 155 | return -1; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 156 | } |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame^] | 157 | if (app == null) { |
| 158 | return -1; |
| 159 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 160 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 161 | int[] msgHeader = new int[MSG_LOAD_APP_HEADER_SIZE]; |
| 162 | msgHeader[HEADER_FIELD_HUB_HANDLE] = contextHubHandle; |
| 163 | msgHeader[HEADER_FIELD_APP_INSTANCE] = OS_APP_INSTANCE; |
| 164 | msgHeader[HEADER_FIELD_MSG_VERSION] = 0; |
| 165 | msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_LOAD_NANO_APP; |
| 166 | |
| 167 | long appId = app.getAppId(); |
| 168 | |
| 169 | msgHeader[HEADER_FIELD_LOAD_APP_ID_LO] = (int)(appId & 0xFFFFFFFF); |
| 170 | msgHeader[HEADER_FIELD_LOAD_APP_ID_HI] = (int)((appId >> 32) & 0xFFFFFFFF); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 171 | |
Ashutosh Joshi | 1186440 | 2016-07-15 13:46:22 -0700 | [diff] [blame] | 172 | int errVal = nativeSendMessage(msgHeader, app.getAppBinary()); |
| 173 | if (errVal != 0) { |
| 174 | Log.e(TAG, "Send Message returns error" + contextHubHandle); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 175 | return -1; |
| 176 | } |
Ashutosh Joshi | 1186440 | 2016-07-15 13:46:22 -0700 | [diff] [blame] | 177 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 178 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 179 | return 0; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 180 | } |
| 181 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 182 | @Override |
| 183 | public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException { |
| 184 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 185 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle); |
| 186 | if (info == null) { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 187 | return -1; //means failed |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Call Native interface here |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 191 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 192 | msgHeader[HEADER_FIELD_HUB_HANDLE] = ANY_HUB; |
| 193 | msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppInstanceHandle; |
| 194 | msgHeader[HEADER_FIELD_MSG_VERSION] = 0; |
| 195 | msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_UNLOAD_NANO_APP; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 196 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 197 | byte msg[] = new byte[0]; |
| 198 | |
| 199 | if (nativeSendMessage(msgHeader, msg) != 0) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 204 | return 0; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 205 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 206 | |
| 207 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 208 | public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 209 | throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 210 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 211 | // This assumes that all the nanoAppInfo is current. This is reasonable |
| 212 | // for the use cases for tightly controlled nanoApps. |
| 213 | if (mNanoAppHash.containsKey(nanoAppInstanceHandle)) { |
| 214 | return mNanoAppHash.get(nanoAppInstanceHandle); |
| 215 | } else { |
| 216 | return null; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | @Override |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 221 | public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 222 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 223 | ArrayList<Integer> foundInstances = new ArrayList<Integer>(); |
| 224 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 225 | for (Integer nanoAppInstance: mNanoAppHash.keySet()) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 226 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); |
| 227 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 228 | if (filter.testMatch(info)) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 229 | foundInstances.add(nanoAppInstance); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | int[] retArray = new int[foundInstances.size()]; |
| 234 | for (int i = 0; i < foundInstances.size(); i++) { |
| 235 | retArray[i] = foundInstances.get(i).intValue(); |
| 236 | } |
| 237 | |
| 238 | return retArray; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 242 | public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 243 | throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 244 | checkPermissions(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 245 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame^] | 246 | if (msg == null || msg.getData() == null) { |
| 247 | Log.w(TAG, "null ptr"); |
| 248 | return -1; |
| 249 | } |
| 250 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 251 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 252 | msgHeader[HEADER_FIELD_HUB_HANDLE] = hubHandle; |
| 253 | msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppHandle; |
| 254 | msgHeader[HEADER_FIELD_MSG_VERSION] = msg.getVersion(); |
| 255 | msgHeader[HEADER_FIELD_MSG_TYPE] = msg.getMsgType(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 256 | |
| 257 | return nativeSendMessage(msgHeader, msg.getData()); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 258 | } |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 259 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 260 | @Override |
| 261 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 262 | if (mContext.checkCallingOrSelfPermission("android.permission.DUMP") |
| 263 | != PackageManager.PERMISSION_GRANTED) { |
| 264 | pw.println("Permission Denial: can't dump contexthub_service"); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | pw.println("Dumping ContextHub Service"); |
| 269 | |
| 270 | pw.println(""); |
| 271 | // dump ContextHubInfo |
| 272 | pw.println("=================== CONTEXT HUBS ===================="); |
| 273 | for (int i = 0; i < mContextHubInfo.length; i++) { |
| 274 | pw.println("Handle " + i + " : " + mContextHubInfo[i].toString()); |
| 275 | } |
| 276 | pw.println(""); |
| 277 | pw.println("=================== NANOAPPS ===================="); |
| 278 | // Dump nanoAppHash |
| 279 | for (Integer nanoAppInstance: mNanoAppHash.keySet()) { |
| 280 | pw.println(nanoAppInstance + " : " + mNanoAppHash.get(nanoAppInstance).toString()); |
| 281 | } |
| 282 | |
| 283 | // dump eventLog |
| 284 | } |
| 285 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 286 | private void checkPermissions() { |
| 287 | mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE); |
| 288 | } |
Ashutosh Joshi | 2c697fb | 2016-04-01 20:48:13 +0000 | [diff] [blame] | 289 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 290 | private int onMessageReceipt(int[] header, byte[] data) { |
| 291 | if (header == null || data == null || header.length < MSG_HEADER_SIZE) { |
| 292 | return -1; |
| 293 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 294 | int callbacksCount = mCallbacksList.beginBroadcast(); |
| 295 | if (callbacksCount < 1) { |
| 296 | Log.v(TAG, "No message callbacks registered."); |
| 297 | return 0; |
| 298 | } |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 299 | |
| 300 | ContextHubMessage msg = new ContextHubMessage(header[HEADER_FIELD_MSG_TYPE], |
| 301 | header[HEADER_FIELD_MSG_VERSION], |
| 302 | data); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 303 | for (int i = 0; i < callbacksCount; ++i) { |
| 304 | IContextHubCallback callback = mCallbacksList.getBroadcastItem(i); |
| 305 | try { |
| 306 | callback.onMessageReceipt( |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 307 | header[HEADER_FIELD_HUB_HANDLE], |
| 308 | header[HEADER_FIELD_APP_INSTANCE], |
| 309 | msg); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 310 | } catch (RemoteException e) { |
| 311 | Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ")."); |
| 312 | continue; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 315 | mCallbacksList.finishBroadcast(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) { |
| 320 | // App Id encodes vendor & version |
| 321 | NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo(); |
| 322 | |
| 323 | appInfo.setAppId(appId); |
| 324 | appInfo.setAppVersion(appVersion); |
| 325 | appInfo.setName(PRE_LOADED_APP_NAME); |
| 326 | appInfo.setContexthubId(hubHandle); |
| 327 | appInfo.setHandle(appInstanceHandle); |
| 328 | appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER); |
| 329 | appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 330 | appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 331 | appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 332 | |
| 333 | mNanoAppHash.put(appInstanceHandle, appInfo); |
| 334 | Log.d(TAG, "Added app instance " + appInstanceHandle + " with id " + appId |
| 335 | + " version " + appVersion); |
| 336 | |
| 337 | return 0; |
| 338 | } |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 339 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 340 | private int deleteAppInstance(int appInstanceHandle) { |
| 341 | if (mNanoAppHash.remove(appInstanceHandle) == null) { |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 348 | private void sendVrStateChangeMessageToApp(NanoAppInstanceInfo app, boolean vrModeEnabled) { |
| 349 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 350 | msgHeader[HEADER_FIELD_MSG_TYPE] = 0; |
| 351 | msgHeader[HEADER_FIELD_MSG_VERSION] = 0; |
| 352 | msgHeader[HEADER_FIELD_HUB_HANDLE] = ANY_HUB; |
| 353 | msgHeader[HEADER_FIELD_APP_INSTANCE] = app.getHandle(); |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 354 | |
| 355 | byte[] data = new byte[1]; |
| 356 | data[0] = (byte) ((vrModeEnabled) ? 1 : 0); |
| 357 | int ret = nativeSendMessage(msgHeader, data); |
| 358 | if (ret != 0) { |
| 359 | Log.e(TAG, "Couldn't send VR state change notification (" + ret + ")!"); |
| 360 | } |
| 361 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 362 | } |