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 | |
| 19 | import android.app.Service; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.os.IBinder; |
| 23 | import android.os.RemoteException; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.HashMap; |
| 28 | |
| 29 | /** |
| 30 | * @hide |
| 31 | */ |
| 32 | public class ContextHubService extends Service { |
| 33 | |
| 34 | private static final String TAG = "ContextHubService"; |
| 35 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 36 | |
| 37 | private static ContextHubService sSingletonInstance; |
| 38 | private static final Object sSingletonInstanceLock = new Object(); |
| 39 | |
| 40 | private HashMap<Integer, ContextHubInfo> mHubHash; |
| 41 | private HashMap<Integer, NanoAppInstanceInfo> mNanoAppHash; |
| 42 | private ContextHubInfo[] mContexthubInfo; |
| 43 | |
| 44 | |
| 45 | private native int nativeSendMessage(int[] header, byte[] data); |
| 46 | private native ContextHubInfo[] nativeInitialize(); |
| 47 | |
| 48 | private int onMessageReceipt(int[] header, byte[] data) { |
| 49 | return 0; |
| 50 | } |
| 51 | private void initialize() { |
| 52 | mContexthubInfo = nativeInitialize(); |
| 53 | |
| 54 | mHubHash = new HashMap<Integer, ContextHubInfo>(); |
| 55 | |
| 56 | for (int i = 0; i < mContexthubInfo.length; i++) { |
| 57 | mHubHash.put(i + 1, mContexthubInfo[i]); // Avoiding zero |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private ContextHubService(Context context) { |
| 62 | initialize(); |
| 63 | Log.d(TAG, "Created from " + context.toString()); |
| 64 | } |
| 65 | |
| 66 | public static ContextHubService getInstance(Context context) { |
| 67 | synchronized (sSingletonInstanceLock) { |
| 68 | if (sSingletonInstance == null) { |
| 69 | sSingletonInstance = new ContextHubService(context); |
| 70 | } |
| 71 | return sSingletonInstance; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onCreate() { |
| 77 | super.onCreate(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public IBinder onBind(Intent intent) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | private final IContextHubService.Stub mBinder = new IContextHubService.Stub() { |
| 86 | |
| 87 | private IContextHubCallback callback; |
| 88 | |
| 89 | @Override |
| 90 | public int registerCallBack(IContextHubCallback callback) throws RemoteException{ |
| 91 | this.callback = callback; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int[] getContextHubHandles() throws RemoteException { |
| 97 | int [] returnArray = new int[mHubHash.size()]; |
| 98 | int i = 0; |
| 99 | for (int key : mHubHash.keySet()) { |
| 100 | // Add any filtering here |
| 101 | returnArray[i] = key; |
| 102 | i++; |
| 103 | } |
| 104 | return returnArray; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public ContextHubInfo getContextHubInfo(int contexthubHandle) throws RemoteException { |
| 109 | return mHubHash.get(contexthubHandle); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int loadNanoApp(int hubHandle, NanoApp app) throws RemoteException { |
| 114 | if (!mHubHash.containsKey(hubHandle)) { |
| 115 | return -1; |
| 116 | } else { |
| 117 | // Call Native interface here |
| 118 | int[] msgHeader = new int[8]; |
| 119 | msgHeader[0] = ContextHubManager.MSG_LOAD_NANO_APP; |
| 120 | msgHeader[1] = app.getAppId(); |
| 121 | msgHeader[2] = app.getAppVersion(); |
| 122 | msgHeader[3] = 0; // LOADING_HINTS |
| 123 | msgHeader[4] = hubHandle; |
| 124 | |
| 125 | int handle = nativeSendMessage(msgHeader, app.getAppBinary()); |
| 126 | |
| 127 | // if successful, add an entry to mNanoAppHash |
| 128 | |
| 129 | if(handle > 0) { |
| 130 | return 0; |
| 131 | } else { |
| 132 | |
| 133 | return -1; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException { |
| 140 | if(!mNanoAppHash.containsKey(nanoAppInstanceHandle)) { |
| 141 | return -1; |
| 142 | } else { |
| 143 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle); |
| 144 | // Call Native interface here |
| 145 | int[] msgHeader = new int[8]; |
| 146 | msgHeader[0] = ContextHubManager.MSG_UNLOAD_NANO_APP; |
| 147 | msgHeader[1] = info.getContexthubId(); |
| 148 | msgHeader[2] = info.getHandle(); |
| 149 | |
| 150 | int result = nativeSendMessage(msgHeader, null); |
| 151 | // if successful, remove the entry in mNanoAppHash |
| 152 | if(result == 0) { |
| 153 | mNanoAppHash.remove(nanoAppInstanceHandle); |
| 154 | } |
| 155 | return(result); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) throws RemoteException { |
| 161 | // This assumes that all the nanoAppInfo is current. This is reasonable |
| 162 | // for the use cases for tightly controlled nanoApps. |
| 163 | // |
| 164 | if(!mNanoAppHash.containsKey(nanoAppInstanceHandle)) { |
| 165 | return(mNanoAppHash.get(nanoAppInstanceHandle)); |
| 166 | } else { |
| 167 | return null; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException { |
| 173 | ArrayList<Integer> foundInstances = new ArrayList<Integer>(); |
| 174 | |
| 175 | for(Integer nanoAppInstance : mNanoAppHash.keySet()) { |
| 176 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); |
| 177 | |
| 178 | if(filter.testMatch(info)){ |
| 179 | foundInstances.add(nanoAppInstance); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | int[] retArray = new int[foundInstances.size()]; |
| 184 | for (int i = 0; i < foundInstances.size(); i++) { |
| 185 | retArray[i] = foundInstances.get(i).intValue(); |
| 186 | } |
| 187 | |
| 188 | return retArray; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) throws RemoteException { |
| 193 | int[] msgHeader = new int[8]; |
| 194 | msgHeader[0] = ContextHubManager.MSG_DATA_SEND; |
| 195 | msgHeader[1] = hubHandle; |
| 196 | msgHeader[2] = nanoAppHandle; |
| 197 | msgHeader[3] = msg.getMsgType(); |
| 198 | msgHeader[4] = msg.getVersion(); |
| 199 | |
| 200 | return (nativeSendMessage(msgHeader, msg.getData())); |
| 201 | } |
| 202 | }; |
| 203 | } |