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    public List<String> getLocationClusters() {
65        if (!retrieveAggregatorManager())
66            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
67        try {
68            return mAggregatorManager.getLocationClusters();
69        } catch (RemoteException e) {
70            Log.e(TAG,"Error getting location clusters");
71            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
72        }
73    }
74
75    public List<String> getTimeOfDayValues() {
76        if (!retrieveAggregatorManager())
77            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
78        try {
79            return mAggregatorManager.getTimeOfDayValues();
80        } catch (RemoteException e) {
81            Log.e(TAG,"Error getting time of day values");
82            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
83        }
84    }
85
86    public List<String> getDayOfWeekValues() {
87        if (!retrieveAggregatorManager())
88            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
89        try {
90            return mAggregatorManager.getDayOfWeekValues();
91        } catch (RemoteException e) {
92            Log.e(TAG,"Error getting day of week values");
93            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
94        }
95    }
96
97    public boolean setFakeLocation(final String name) {
98        if (!retrieveAggregatorManager())
99            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
100        try {
101            return mAggregatorManager.setFakeLocation(name);
102        } catch (RemoteException e) {
103            Log.e(TAG,"Error setting fake location:" + name);
104            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
105        }
106    }
107
108    public boolean setFakeTimeOfDay(final String time_of_day) {
109        if (!retrieveAggregatorManager())
110            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
111        try {
112            return mAggregatorManager.setFakeTimeOfDay(time_of_day);
113        } catch (RemoteException e) {
114            Log.e(TAG,"Error setting fake time of day:" + time_of_day);
115            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
116        }
117    }
118
119    public boolean setFakeDayOfWeek(final String day_of_week) {
120        if (!retrieveAggregatorManager())
121            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
122        try {
123            return mAggregatorManager.setFakeDayOfWeek(day_of_week);
124        } catch (RemoteException e) {
125            Log.e(TAG,"Error setting fake day of week:" + day_of_week);
126            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
127        }
128    }
129
130    public boolean getFakeMode() {
131        if (!retrieveAggregatorManager())
132            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
133        try {
134            return mAggregatorManager.getFakeMode();
135        } catch (RemoteException e) {
136            Log.e(TAG,"Error getting fake mode");
137            throw new RuntimeException(AggregatorManager_NOTAVAILABLE);
138        }
139    }
140
141    private Map<String, String> getMap(final List<StringString> sample) {
142        HashMap<String, String> map = new HashMap<String, String>();
143        for (int i =0; i < sample.size(); i++) {
144            map.put(sample.get(i).key, sample.get(i).value);
145        }
146        return (Map) map;
147    }
148}
149