BordeauxAggregatorManager.java revision f0f78449e8ab7d63894964c54b6ef390ca9ce044
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        }
45        return true;
46    }
47
48    public BordeauxAggregatorManager (Context context) {
49        mContext = context;
50        mAggregatorManager = BordeauxManagerService.getAggregatorManager(mContext);
51    }
52
53    public Map<String, String> GetData(final String dataName) {
54        if (!retrieveAggregatorManager())
55            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
56        try {
57            return getMap(mAggregatorManager.getData(dataName));
58        } catch (RemoteException e) {
59            Log.e(TAG,"Exception in Getting " + dataName);
60            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
61        }
62    }
63
64    private Map<String, String> getMap(final List<StringString> sample) {
65        HashMap<String, String> map = new HashMap<String, String>();
66        for (int i =0; i < sample.size(); i++) {
67            map.put(sample.get(i).key, sample.get(i).value);
68        }
69        return (Map) map;
70    }
71}
72