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 
17 package com.android.deskclock.worldclock;
18 
19 import android.content.SharedPreferences;
20 import android.util.Log;
21 
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 
26 public class Cities {
27 
28     public static final String WORLDCLOCK_UPDATE_INTENT = "com.android.deskclock.worldclock.update";
29     private static final String NUMBER_OF_CITIES = "number_of_cities";
30 
saveCitiesToSharedPrefs( SharedPreferences prefs, HashMap<String, CityObj> cities)31     public static void saveCitiesToSharedPrefs(
32             SharedPreferences prefs, HashMap<String, CityObj> cities) {
33         SharedPreferences.Editor editor = prefs.edit();
34         editor.putInt(NUMBER_OF_CITIES, cities.size());
35         Collection<CityObj> col = cities.values();
36         Iterator<CityObj> i = col.iterator();
37         int count = 0;
38         while (i.hasNext()) {
39             CityObj c = i.next();
40             c.saveCityToSharedPrefs(editor, count);
41             count++;
42         }
43         editor.apply();
44     }
45 
readCitiesFromSharedPrefs(SharedPreferences prefs)46     public static  HashMap<String, CityObj> readCitiesFromSharedPrefs(SharedPreferences prefs) {
47         int size = prefs.getInt(NUMBER_OF_CITIES, -1);
48         HashMap<String, CityObj> c = new HashMap<String, CityObj>();
49         if (size > 0) {
50             for (int i = 0; i < size; i++) {
51                 CityObj o = new CityObj(prefs, i);
52                 if (o.mCityName != null && o.mTimeZone != null) {
53                     c.put(o.mCityId, o);
54                 }
55             }
56         }
57         return c;
58     }
59 
dumpCities(SharedPreferences prefs, String title)60     private static void dumpCities(SharedPreferences prefs, String title) {
61         int size = prefs.getInt(NUMBER_OF_CITIES, -1);
62         Log.d("Cities", "Selected Cities List " + title);
63         Log.d("Cities", "Number of cities " + size);
64         HashMap<String, CityObj> c = new HashMap<String, CityObj>();
65         if (size > 0) {
66             for (int i = 0; i < size; i++) {
67                 CityObj o = new CityObj(prefs, i);
68                 if (o.mCityName != null && o.mTimeZone != null) {
69                     Log.d("Cities", "Name " + o.mCityName + " tz " + o.mTimeZone);
70                 }
71             }
72         }
73     }
74 }
75