summaryrefslogtreecommitdiffstats
path: root/src/com/android/deskclock/data/CityModel.java
blob: 0b345e74d8d3768760da8600555273ae486533d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.deskclock.data;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

import com.android.deskclock.R;
import com.android.deskclock.Utils;
import com.android.deskclock.data.DataModel.CitySort;
import com.android.deskclock.settings.SettingsActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

/**
 * All {@link City} data is accessed via this model.
 */
final class CityModel {

    private final Context mContext;

    /** The model from which settings are fetched. */
    private final SettingsModel mSettingsModel;

    /**
     * Retain a hard reference to the shared preference observer to prevent it from being garbage
     * collected. See {@link SharedPreferences#registerOnSharedPreferenceChangeListener} for detail.
     */
    @SuppressWarnings("FieldCanBeLocal")
    private final OnSharedPreferenceChangeListener mPreferenceListener = new PreferenceListener();

    /** Clears data structures containing data that is locale-sensitive. */
    @SuppressWarnings("FieldCanBeLocal")
    private final BroadcastReceiver mLocaleChangedReceiver = new LocaleChangedReceiver();

    /** Maps city ID to city instance. */
    private Map<String, City> mCityMap;

    /** List of city instances in display order. */
    private List<City> mAllCities;

    /** List of selected city instances in display order. */
    private List<City> mSelectedCities;

    /** List of unselected city instances in display order. */
    private List<City> mUnselectedCities;

    /** A city instance representing the home timezone of the user. */
    private City mHomeCity;

    CityModel(Context context, SettingsModel settingsModel) {
        mContext = context;
        mSettingsModel = settingsModel;

        // Clear caches affected by locale when locale changes.
        final IntentFilter localeBroadcastFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
        mContext.registerReceiver(mLocaleChangedReceiver, localeBroadcastFilter);

        // Clear caches affected by preferences when preferences change.
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        prefs.registerOnSharedPreferenceChangeListener(mPreferenceListener);
    }

    /**
     * @return a list of all cities in their display order
     */
    List<City> getAllCities() {
        if (mAllCities == null) {
            // Create a set of selections to identify the unselected cities.
            final List<City> selected = new ArrayList<>(getSelectedCities());

            // Sort the selected cities alphabetically by name.
            Collections.sort(selected, new City.NameComparator());

            // Combine selected and unselected cities into a single list.
            final List<City> allCities = new ArrayList<>(getCityMap().size());
            allCities.addAll(selected);
            allCities.addAll(getUnselectedCities());
            mAllCities = Collections.unmodifiableList(allCities);
        }

        return mAllCities;
    }

    /**
     * @param cityName the case-insensitive city name to search for
     * @return the city with the given {@code cityName}; {@code null} if no such city exists
     */
    City getCity(String cityName) {
        cityName = cityName.toUpperCase();

        for (City city : getAllCities()) {
            if (cityName.equals(city.getNameUpperCase())) {
                return city;
            }
        }

        return null;
    }

    /**
     * @return a city representing the user's home timezone
     */
    City getHomeCity() {
        if (mHomeCity == null) {
            final String name = mContext.getString(R.string.home_label);
            final TimeZone timeZone = mSettingsModel.getHomeTimeZone();
            mHomeCity = new City(null, -1, null, name, name, timeZone);
        }

        return mHomeCity;
    }

    /**
     * @return a list of cities not selected for display
     */
    List<City> getUnselectedCities() {
        if (mUnselectedCities == null) {
            // Create a set of selections to identify the unselected cities.
            final List<City> selected = new ArrayList<>(getSelectedCities());
            final Set<City> selectedSet = Utils.newArraySet(selected);

            final Collection<City> all = getCityMap().values();
            final List<City> unselected = new ArrayList<>(all.size() - selectedSet.size());
            for (City city : all) {
                if (!selectedSet.contains(city)) {
                    unselected.add(city);
                }
            }

            // Sort the unselected cities according by the user's preferred sort.
            Collections.sort(unselected, getCitySortComparator());
            mUnselectedCities = Collections.unmodifiableList(unselected);
        }

        return mUnselectedCities;
    }

    /**
     * @return a list of cities selected for display
     */
    List<City> getSelectedCities() {
        if (mSelectedCities == null) {
            final List<City> selectedCities = CityDAO.getSelectedCities(mContext, getCityMap());
            Collections.sort(selectedCities, new City.UtcOffsetComparator());
            mSelectedCities = Collections.unmodifiableList(selectedCities);
        }

        return mSelectedCities;
    }

    /**
     * @param cities the new collection of cities selected for display by the user
     */
    void setSelectedCities(Collection<City> cities) {
        CityDAO.setSelectedCities(mContext, cities);

        // Clear caches affected by this update.
        mAllCities = null;
        mSelectedCities = null;
        mUnselectedCities = null;
    }

    /**
     * @return a comparator used to locate index positions
     */
    Comparator<City> getCityIndexComparator() {
        final CitySort citySort = mSettingsModel.getCitySort();
        switch (citySort) {
            case NAME: return new City.NameIndexComparator();
            case UTC_OFFSET: return new City.UtcOffsetIndexComparator();
        }
        throw new IllegalStateException("unexpected city sort: " + citySort);
    }

    /**
     * @return the order in which cities are sorted
     */
    CitySort getCitySort() {
        return mSettingsModel.getCitySort();
    }

    /**
     * Adjust the order in which cities are sorted.
     */
    void toggleCitySort() {
        mSettingsModel.toggleCitySort();

        // Clear caches affected by this update.
        mAllCities = null;
        mUnselectedCities = null;
    }

    private Map<String, City> getCityMap() {
        if (mCityMap == null) {
            mCityMap = CityDAO.getCities(mContext);
        }

        return mCityMap;
    }

    private Comparator<City> getCitySortComparator() {
        final CitySort citySort = mSettingsModel.getCitySort();
        switch (citySort) {
            case NAME: return new City.NameComparator();
            case UTC_OFFSET: return new City.UtcOffsetComparator();
        }
        throw new IllegalStateException("unexpected city sort: " + citySort);
    }

    /**
     * Cached information that is locale-sensitive must be cleared in response to locale changes.
     */
    private final class LocaleChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            mCityMap = null;
            mHomeCity = null;
            mAllCities = null;
            mSelectedCities = null;
            mUnselectedCities = null;
        }
    }

    /**
     * This receiver is notified when shared preferences change. Cached information built on
     * preferences must be cleared.
     */
    private final class PreferenceListener implements OnSharedPreferenceChangeListener {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            switch (key) {
                case SettingsActivity.KEY_HOME_TZ:
                    mHomeCity = null;
            }
        }
    }
}