summaryrefslogtreecommitdiffstats
path: root/src/com/android/deskclock/ClockFragment.java
blob: f7ef1a9c6279cebb378755ace08171e5d3224598 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * Copyright (C) 2012 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;

import android.app.Activity;
import android.app.AlarmManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextClock;
import android.widget.TextView;

import com.android.deskclock.data.City;
import com.android.deskclock.data.DataModel;
import com.android.deskclock.worldclock.CitySelectionActivity;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import static java.util.Calendar.DAY_OF_WEEK;

/**
 * Fragment that shows the clock (analog or digital), the next alarm info and the world clock.
 */
public class ClockFragment extends DeskClockFragment {

    private TextClock mDigitalClock;
    private View mAnalogClock, mClockFrame;
    private SelectedCitiesAdapter mCityAdapter;
    private ListView mCityList;
    private String mDateFormat;
    private String mDateFormatForAccessibility;

    private final Handler mHandler = new Handler();

    // Updates the UI in response to system setting changes that alter time values and time display.
    private final BroadcastReceiver mBroadcastReceiver = new SystemBroadcastReceiver();

    // Detects changes to the next scheduled alarm pre-L.
    private final ContentObserver mAlarmObserver = Utils.isPreL() ? new AlarmObserverPreL() : null;

    // Updates dates in the UI on every quarter-hour.
    private final Runnable mQuarterHourUpdater = new QuarterHourRunnable();

    /** The public no-arg constructor required by all fragments. */
    public ClockFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {
        super.onCreateView(inflater, container, icicle);

        final OnTouchListener startScreenSaverListener = new StartScreenSaverListener();
        final View footerView = inflater.inflate(R.layout.blank_footer_view, mCityList, false);
        final View fragmentView = inflater.inflate(R.layout.clock_fragment, container, false);

        mCityAdapter = new SelectedCitiesAdapter(getActivity());

        mCityList = (ListView) fragmentView.findViewById(R.id.cities);
        mCityList.setDivider(null);
        mCityList.setAdapter(mCityAdapter);
        mCityList.addFooterView(footerView, null, false);
        mCityList.setOnTouchListener(startScreenSaverListener);

        // On tablet landscape, the clock frame will be a distinct view. Otherwise, it'll be added
        // on as a header to the main listview.
        mClockFrame = fragmentView.findViewById(R.id.main_clock_left_pane);
        if (mClockFrame == null) {
            mClockFrame = inflater.inflate(R.layout.main_clock_frame, mCityList, false);
            mCityList.addHeaderView(mClockFrame, null, false);
            final View hairline = mClockFrame.findViewById(R.id.hairline);
            hairline.setVisibility(mCityAdapter.getCount() == 0 ? View.GONE : View.VISIBLE);
        } else {
            final View hairline = mClockFrame.findViewById(R.id.hairline);
            hairline.setVisibility(View.GONE);
            // The main clock frame needs its own touch listener for night mode now.
            fragmentView.setOnTouchListener(startScreenSaverListener);
        }

        mDigitalClock = (TextClock) mClockFrame.findViewById(R.id.digital_clock);
        mAnalogClock = mClockFrame.findViewById(R.id.analog_clock);

        return fragmentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final int size = getResources().getDimensionPixelSize(R.dimen.main_ampm_font_size);
        Utils.setTimeFormat(getActivity(), mDigitalClock, size);
    }

    @Override
    public void onResume() {
        super.onResume();

        final Activity activity = getActivity();
        setFabAppearance();
        setLeftRightButtonAppearance();

        mDateFormat = getString(R.string.abbrev_wday_month_day_no_year);
        mDateFormatForAccessibility = getString(R.string.full_wday_month_day_no_year);

        // Schedule a runnable to update the date every quarter hour.
        Utils.setQuarterHourUpdater(mHandler, mQuarterHourUpdater);

        // Watch for system events that effect clock time or format.
        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
        activity.registerReceiver(mBroadcastReceiver, filter);

        // Resume can be invoked after changing the clock style.
        Utils.setClockStyle(mDigitalClock, mAnalogClock);

        final View view = getView();
        if (view != null && view.findViewById(R.id.main_clock_left_pane) != null) {
            // Center the main clock frame by hiding the world clocks when none are selected.
            mCityList.setVisibility(mCityAdapter.getCount() == 0 ? View.GONE : View.VISIBLE);
        }

        refreshDates();
        refreshAlarm();

        if (mAlarmObserver != null) {
            final Uri uri = Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED);
            activity.getContentResolver().registerContentObserver(uri, false, mAlarmObserver);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Utils.cancelQuarterHourUpdater(mHandler, mQuarterHourUpdater);

        final Activity activity = getActivity();
        activity.unregisterReceiver(mBroadcastReceiver);
        if (mAlarmObserver != null) {
            activity.getContentResolver().unregisterContentObserver(mAlarmObserver);
        }
    }

    @Override
    public void onFabClick(View view) {
        startActivity(new Intent(getActivity(), CitySelectionActivity.class));
    }

    @Override
    public void setFabAppearance() {
        if (getSelectedTab() != DeskClock.CLOCK_TAB_INDEX) {
            return;
        }

        if (mFab != null) {
            mFab.setVisibility(View.VISIBLE);
            mFab.setImageResource(R.drawable.ic_language_white_24dp);
            mFab.setContentDescription(getString(R.string.button_cities));
        }
    }

    @Override
    public void setLeftRightButtonAppearance() {
        if (getSelectedTab() != DeskClock.CLOCK_TAB_INDEX) {
            return;
        }

        if (mLeftButton != null) {
            mLeftButton.setVisibility(View.INVISIBLE);
        }

        if (mRightButton != null) {
            mRightButton.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * Refresh the displayed dates in response to a change that may have changed them.
     */
    private void refreshDates() {
        // Refresh the date in the main clock.
        Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mClockFrame);

        // Refresh the day-of-week in each world clock.
        mCityAdapter.notifyDataSetChanged();
    }

    /**
     * Refresh the next alarm time.
     */
    private void refreshAlarm() {
        Utils.refreshAlarm(getActivity(), mClockFrame);
    }

    /**
     * Long pressing over the main clock or any world clock item starts the screen saver.
     */
    private final class StartScreenSaverListener implements OnTouchListener, Runnable {

        private float mTouchSlop = -1;
        private int mLongPressTimeout = -1;
        private float mLastTouchX, mLastTouchY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mTouchSlop == -1) {
                mTouchSlop = ViewConfiguration.get(getActivity()).getScaledTouchSlop();
                mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
            }

            switch (event.getAction()) {
                case (MotionEvent.ACTION_DOWN):
                    // Create and post a runnable to start the screen saver in the future.
                    mHandler.postDelayed(this, mLongPressTimeout);
                    mLastTouchX = event.getX();
                    mLastTouchY = event.getY();
                    return true;

                case (MotionEvent.ACTION_MOVE):
                    final float xDiff = Math.abs(event.getX() - mLastTouchX);
                    final float yDiff = Math.abs(event.getY() - mLastTouchY);
                    if (xDiff >= mTouchSlop || yDiff >= mTouchSlop) {
                        mHandler.removeCallbacks(this);
                    }
                    break;
                default:
                    mHandler.removeCallbacks(this);
            }
            return false;
        }

        @Override
        public void run() {
            startActivity(new Intent(getActivity(), ScreensaverActivity.class));
        }
    }

    /**
     * This runnable executes at every quarter-hour (e.g. 1:00, 1:15, 1:30, 1:45, etc...) and
     * updates the dates displayed within the UI. Quarter-hour increments were chosen to accommodate
     * the "weirdest" timezones (e.g. Nepal is UTC/GMT +05:45).
     */
    private final class QuarterHourRunnable implements Runnable {
        @Override
        public void run() {
            refreshDates();

            // Schedule the next quarter-hour callback.
            Utils.setQuarterHourUpdater(mHandler, mQuarterHourUpdater);
        }
    }

    /**
     * Prior to L, a ContentObserver was used to monitor changes to the next scheduled alarm.
     * In L and beyond this is accomplished via a system broadcast of
     * {@link AlarmManager#ACTION_NEXT_ALARM_CLOCK_CHANGED}.
     */
    private final class AlarmObserverPreL extends ContentObserver {
        public AlarmObserverPreL() {
            super(mHandler);
        }

        @Override
        public void onChange(boolean selfChange) {
            Utils.refreshAlarm(getActivity(), mClockFrame);
        }
    }

    /**
     * Handle system broadcasts that influence the display of this fragment. Since this fragment
     * displays time-related information, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED both
     * alter the actual time values displayed. ACTION_NEXT_ALARM_CLOCK_CHANGED indicates the time at
     * which the next alarm will fire has changed.
     */
    private final class SystemBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case Intent.ACTION_TIME_CHANGED:
                case Intent.ACTION_TIMEZONE_CHANGED:
                    refreshDates();

                case AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED:
                    refreshAlarm();
            }
        }
    }

    /**
     * This adapter lists all of the selected world clocks. Optionally, it also includes a clock at
     * the top for the home timezone if "Automatic home clock" is turned on in settings and the
     * current time at home does not match the current time in the timezone of the current location.
     */
    private static final class SelectedCitiesAdapter extends BaseAdapter {

        private final LayoutInflater mInflater;
        private final Context mContext;

        public SelectedCitiesAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            final int homeClockCount = getShowHomeClock() ? 1 : 0;
            final int worldClockCount = getCities().size();
            return homeClockCount + worldClockCount;
        }

        @Override
        public Object getItem(int position) {
            if (getShowHomeClock()) {
                return position == 0 ? getHomeCity() : getCities().get(position - 1);
            }

            return getCities().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            // Retrieve the city to bind.
            final City city = (City) getItem(position);

            // Inflate a new view for the city, if necessary.
            if (view == null) {
                view = mInflater.inflate(R.layout.world_clock_list_item, parent, false);
            }

            final View clock = view.findViewById(R.id.city_left);

            // Configure the digital clock or analog clock depending on the user preference.
            final TextClock digitalClock = (TextClock) clock.findViewById(R.id.digital_clock);
            final AnalogClock analogClock = (AnalogClock) clock.findViewById(R.id.analog_clock);
            if (DataModel.getDataModel().getClockStyle() == DataModel.ClockStyle.ANALOG) {
                digitalClock.setVisibility(View.GONE);
                analogClock.setVisibility(View.VISIBLE);
                analogClock.setTimeZone(city.getTimeZoneId());
                analogClock.enableSeconds(false);
            } else {
                digitalClock.setVisibility(View.VISIBLE);
                analogClock.setVisibility(View.GONE);
                digitalClock.setTimeZone(city.getTimeZoneId());
                final int size = mContext.getResources().getDimensionPixelSize(R.dimen.label_font_size);
                Utils.setTimeFormat(mContext, digitalClock, size);
            }

            // Bind the city name.
            final TextView name = (TextView) clock.findViewById(R.id.city_name);
            name.setText(city.getName());

            // Compute if the city week day matches the weekday of the current timezone.
            final Calendar localCal = Calendar.getInstance(TimeZone.getDefault());
            final Calendar cityCal = Calendar.getInstance(city.getTimeZone());
            final boolean displayDayOfWeek = localCal.get(DAY_OF_WEEK) != cityCal.get(DAY_OF_WEEK);

            // Bind the week day display.
            final TextView dayOfWeek = (TextView) clock.findViewById(R.id.city_day);
            dayOfWeek.setVisibility(displayDayOfWeek ? View.VISIBLE : View.GONE);
            if (displayDayOfWeek) {
                final Locale locale = Locale.getDefault();
                final String weekday = cityCal.getDisplayName(DAY_OF_WEEK, Calendar.SHORT, locale);
                dayOfWeek.setText(mContext.getString(R.string.world_day_of_week_label, weekday));
            }

            return view;
        }

        @Override
        public boolean isEnabled(int position) {
            return false;
        }

        private City getHomeCity() {
            return DataModel.getDataModel().getHomeCity();
        }

        private List<City> getCities() {
            return DataModel.getDataModel().getSelectedCities();
        }

        private boolean getShowHomeClock() {
            return DataModel.getDataModel().getShowHomeClock();
        }
    }
}