AggregatorManager.java revision 9c3a7dc466e2f8de02e15030b2b7f4096ba97e5a
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
19
20import android.bordeaux.services.StringString;
21import android.content.Context;
22import android.util.Log;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27
28class AggregatorManager extends IAggregatorManager.Stub  {
29    private final String TAG = "AggregatorMnager";
30    // this maps from the aggregator name to the registered aggregator instance
31    private static HashMap<String, Aggregator> mAggregators = new HashMap<String, Aggregator>();
32    // this maps from the feature names to the aggregator that generates the feature
33    private static HashMap<String, Aggregator> sFeatureMap = new HashMap<String, Aggregator>();
34    private static AggregatorManager mManager = null;
35
36    private String mFakeLocation = null;
37
38    private AggregatorManager() {
39        sFeatureMap = new HashMap<String, Aggregator>();
40    }
41
42    public static AggregatorManager getInstance() {
43        if (mManager == null )
44            mManager = new AggregatorManager();
45        return mManager;
46    }
47
48    public String[] getListOfFeatures() {
49        String[] s = new String[sFeatureMap.size()];
50        int i = 0;
51        for (Map.Entry<String, Aggregator> x : sFeatureMap.entrySet()) {
52           s[i] = x.getKey();
53           i++;
54        }
55        return s;
56    }
57
58    public void registerAggregator(Aggregator agg, AggregatorManager m) {
59        if (mAggregators.get(agg.getClass().getName()) != null) {
60            // only one instance
61            throw new RuntimeException("Can't register more than one instance");
62        }
63        mAggregators.put(agg.getClass().getName(), agg);
64        agg.setManager(m);
65        String[] fl = agg.getListOfFeatures();
66        for ( int i  = 0; i< fl.length; i ++)
67            sFeatureMap.put(fl[i], agg);
68    }
69    // Start of IAggregatorManager interface
70    public ArrayList<StringString> getData(String dataName) {
71        return getList(getDataMap(dataName));
72    }
73
74    public List<String> getLocationClusters() {
75        LocationStatsAggregator agg = (LocationStatsAggregator)
76                mAggregators.get(LocationStatsAggregator.class.getName());
77        if (agg == null) return new ArrayList<String> ();
78        ArrayList<String> clusters = new ArrayList<String>();
79        return agg.getClusterNames();
80    }
81
82    // Set an empty string "" to disable the fake location
83    public boolean setFakeLocation(String location) {
84        LocationStatsAggregator agg = (LocationStatsAggregator)
85                mAggregators.get(LocationStatsAggregator.class.getName());
86        if (agg == null) return false;
87        agg.setFakeLocation(location);
88        mFakeLocation = location;
89        return true;
90    }
91
92    // Get the current mode, if fake mode return true
93    public boolean getFakeMode() {
94        boolean fakeMode = false;
95        // checking any features that are in the fake mode
96        if (mFakeLocation != null && mFakeLocation.length() != 0)
97            fakeMode = true;
98        return fakeMode;
99    }
100    // End of IAggregatorManger interface
101
102    public Map<String, String> getDataMap(String dataName) {
103        if (sFeatureMap.get(dataName) != null)
104            return sFeatureMap.get(dataName).getFeatureValue(dataName);
105        else
106            Log.e(TAG, "There is no feature called " + dataName);
107        return null;
108    }
109
110    private ArrayList<StringString> getList(final Map<String, String> sample) {
111        ArrayList<StringString> StringString_sample = new ArrayList<StringString>();
112        for (Map.Entry<String, String> x : sample.entrySet()) {
113           StringString v = new StringString();
114           v.key = x.getKey();
115           v.value = x.getValue();
116           StringString_sample.add(v);
117        }
118        return StringString_sample;
119    }
120}
121