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 private String mFakeTimeOfDay = null; 38 private String mFakeDayOfWeek = null; 39 40 private AggregatorManager() { 41 sFeatureMap = new HashMap<String, Aggregator>(); 42 } 43 44 public static AggregatorManager getInstance() { 45 if (mManager == null ) 46 mManager = new AggregatorManager(); 47 return mManager; 48 } 49 50 public String[] getListOfFeatures() { 51 String[] s = new String[sFeatureMap.size()]; 52 int i = 0; 53 for (Map.Entry<String, Aggregator> x : sFeatureMap.entrySet()) { 54 s[i] = x.getKey(); 55 i++; 56 } 57 return s; 58 } 59 60 public void registerAggregator(Aggregator agg, AggregatorManager m) { 61 if (mAggregators.get(agg.getClass().getName()) != null) { 62 // only one instance 63 // throw new RuntimeException("Can't register more than one instance"); 64 } 65 mAggregators.put(agg.getClass().getName(), agg); 66 agg.setManager(m); 67 String[] fl = agg.getListOfFeatures(); 68 for ( int i = 0; i< fl.length; i ++) 69 sFeatureMap.put(fl[i], agg); 70 } 71 72 // Start of IAggregatorManager interface 73 public ArrayList<StringString> getData(String dataName) { 74 return getList(getDataMap(dataName)); 75 } 76 77 public List<String> getLocationClusters() { 78 LocationStatsAggregator agg = (LocationStatsAggregator) 79 mAggregators.get(LocationStatsAggregator.class.getName()); 80 if (agg == null) return new ArrayList<String> (); 81 return agg.getClusterNames(); 82 } 83 84 public List<String> getTimeOfDayValues() { 85 TimeStatsAggregator agg = (TimeStatsAggregator) 86 mAggregators.get(TimeStatsAggregator.class.getName()); 87 if (agg == null) return new ArrayList<String>(); 88 return agg.getTimeOfDayValues(); 89 } 90 91 public List<String> getDayOfWeekValues() { 92 TimeStatsAggregator agg = (TimeStatsAggregator) 93 mAggregators.get(TimeStatsAggregator.class.getName()); 94 if (agg == null) return new ArrayList<String>(); 95 return agg.getDayOfWeekValues(); 96 } 97 98 // Set an empty string "" to disable the fake location 99 public boolean setFakeLocation(String location) { 100 LocationStatsAggregator agg = (LocationStatsAggregator) 101 mAggregators.get(LocationStatsAggregator.class.getName()); 102 if (agg == null) return false; 103 agg.setFakeLocation(location); 104 mFakeLocation = location; 105 return true; 106 } 107 108 // Set an empty string "" to disable the fake time of day 109 public boolean setFakeTimeOfDay(String time_of_day) { 110 TimeStatsAggregator agg = (TimeStatsAggregator) 111 mAggregators.get(TimeStatsAggregator.class.getName()); 112 if (agg == null) return false; 113 agg.setFakeTimeOfDay(time_of_day); 114 mFakeTimeOfDay = time_of_day; 115 return true; 116 } 117 118 // Set an empty string "" to disable the fake day of week 119 public boolean setFakeDayOfWeek(String day_of_week) { 120 TimeStatsAggregator agg = (TimeStatsAggregator) 121 mAggregators.get(TimeStatsAggregator.class.getName()); 122 if (agg == null) return false; 123 agg.setFakeDayOfWeek(day_of_week); 124 mFakeDayOfWeek = day_of_week; 125 return true; 126 } 127 128 // Get the current mode, if fake mode return true 129 public boolean getFakeMode() { 130 boolean fakeMode = false; 131 // checking any features that are in the fake mode 132 if (mFakeLocation != null && mFakeLocation.length() != 0) 133 fakeMode = true; 134 if (mFakeTimeOfDay != null && mFakeTimeOfDay.length() != 0) 135 fakeMode = true; 136 if (mFakeDayOfWeek != null && mFakeDayOfWeek.length() != 0) 137 fakeMode = true; 138 return fakeMode; 139 } 140 // End of IAggregatorManger interface 141 142 public Map<String, String> getDataMap(String dataName) { 143 if (sFeatureMap.get(dataName) != null) 144 return sFeatureMap.get(dataName).getFeatureValue(dataName); 145 else 146 Log.e(TAG, "There is no feature called " + dataName); 147 return null; 148 } 149 150 private ArrayList<StringString> getList(final Map<String, String> sample) { 151 ArrayList<StringString> StringString_sample = new ArrayList<StringString>(); 152 for (Map.Entry<String, String> x : sample.entrySet()) { 153 StringString v = new StringString(); 154 v.key = x.getKey(); 155 v.value = x.getValue(); 156 StringString_sample.add(v); 157 } 158 return StringString_sample; 159 } 160} 161