BordeauxAggregatorManager.java revision 7b5b77b038b846e8e2d3aaf0d94c206723a83ccf
1/* 2 * Copyright (C) 2012 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 17package android.bordeaux.services; 18 19import android.bordeaux.services.IAggregatorManager; 20import android.bordeaux.services.StringString; 21import android.content.Context; 22import android.os.RemoteException; 23import android.util.Log; 24import java.util.List; 25import java.util.ArrayList; 26import java.util.HashMap; 27import java.util.Map; 28 29/** AggregatorManger for Learning framework. 30 */ 31public class BordeauxAggregatorManager { 32 static final String TAG = "BordeauxAggregatorManager"; 33 static final String AggregatorManager_NOTAVAILABLE = "AggregatorManager not Available"; 34 private Context mContext; 35 private IAggregatorManager mAggregatorManager; 36 37 public boolean retrieveAggregatorManager() { 38 if (mAggregatorManager == null) 39 mAggregatorManager = BordeauxManagerService.getAggregatorManager(mContext); 40 if (mAggregatorManager == null) { 41 Log.e(TAG, AggregatorManager_NOTAVAILABLE); 42 return false; 43 } 44 return true; 45 } 46 47 public BordeauxAggregatorManager (Context context) { 48 mContext = context; 49 mAggregatorManager = BordeauxManagerService.getAggregatorManager(mContext); 50 } 51 52 public Map<String, String> GetData(final String dataName) { 53 if (!retrieveAggregatorManager()) 54 throw new RuntimeException(AggregatorManager_NOTAVAILABLE); 55 try { 56 return getMap(mAggregatorManager.getData(dataName)); 57 } catch (RemoteException e) { 58 Log.e(TAG,"Exception in Getting " + dataName); 59 throw new RuntimeException(AggregatorManager_NOTAVAILABLE); 60 } 61 } 62 63 private Map<String, String> getMap(final List<StringString> sample) { 64 HashMap<String, String> m = new HashMap<String, String>(); 65 for (int i =0; i < sample.size(); i++) 66 m.put(sample.get(i).key, sample.get(i).value); 67 return (Map) m; 68 } 69} 70