1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.settings.display;
16
17import android.content.Context;
18import android.support.v14.preference.SwitchPreference;
19import android.util.AttributeSet;
20
21import com.android.internal.app.NightDisplayController;
22import com.android.settings.R;
23
24import java.text.DateFormat;
25import java.util.Calendar;
26import java.util.TimeZone;
27
28public class NightDisplayPreference extends SwitchPreference
29        implements NightDisplayController.Callback {
30
31    private NightDisplayController mController;
32    private DateFormat mTimeFormatter;
33
34    public NightDisplayPreference(Context context, AttributeSet attrs) {
35        super(context, attrs);
36
37        mController = new NightDisplayController(context);
38        mTimeFormatter = android.text.format.DateFormat.getTimeFormat(context);
39        mTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
40    }
41
42    @Override
43    public void onAttached() {
44        super.onAttached();
45
46        // Listen for changes only while attached.
47        mController.setListener(this);
48
49        // Update the summary since the state may have changed while not attached.
50        updateSummary();
51    }
52
53    @Override
54    public void onDetached() {
55        super.onDetached();
56
57        // Stop listening for state changes.
58        mController.setListener(null);
59    }
60
61    private String getFormattedTimeString(NightDisplayController.LocalTime localTime) {
62        final Calendar c = Calendar.getInstance();
63        c.setTimeZone(mTimeFormatter.getTimeZone());
64        c.set(Calendar.HOUR_OF_DAY, localTime.hourOfDay);
65        c.set(Calendar.MINUTE, localTime.minute);
66        c.set(Calendar.SECOND, 0);
67        c.set(Calendar.MILLISECOND, 0);
68        return mTimeFormatter.format(c.getTime());
69    }
70
71    private void updateSummary() {
72        final Context context = getContext();
73
74        final boolean isActivated = mController.isActivated();
75        final int autoMode = mController.getAutoMode();
76
77        final String autoModeSummary;
78        switch (autoMode) {
79            default:
80            case NightDisplayController.AUTO_MODE_DISABLED:
81                autoModeSummary = context.getString(isActivated
82                        ? R.string.night_display_summary_on_auto_mode_never
83                        : R.string.night_display_summary_off_auto_mode_never);
84                break;
85            case NightDisplayController.AUTO_MODE_CUSTOM:
86                if (isActivated) {
87                    autoModeSummary = context.getString(
88                            R.string.night_display_summary_on_auto_mode_custom,
89                            getFormattedTimeString(mController.getCustomEndTime()));
90                } else {
91                    autoModeSummary = context.getString(
92                            R.string.night_display_summary_off_auto_mode_custom,
93                            getFormattedTimeString(mController.getCustomStartTime()));
94                }
95                break;
96            case NightDisplayController.AUTO_MODE_TWILIGHT:
97                autoModeSummary = context.getString(isActivated
98                        ? R.string.night_display_summary_on_auto_mode_twilight
99                        : R.string.night_display_summary_off_auto_mode_twilight);
100                break;
101        }
102
103        final int summaryFormatResId = isActivated ? R.string.night_display_summary_on
104                : R.string.night_display_summary_off;
105        setSummary(context.getString(summaryFormatResId, autoModeSummary));
106    }
107
108    @Override
109    public void onActivated(boolean activated) {
110        updateSummary();
111    }
112
113    @Override
114    public void onAutoModeChanged(int autoMode) {
115        updateSummary();
116    }
117
118    @Override
119    public void onCustomStartTimeChanged(NightDisplayController.LocalTime startTime) {
120        updateSummary();
121    }
122
123    @Override
124    public void onCustomEndTimeChanged(NightDisplayController.LocalTime endTime) {
125        updateSummary();
126    }
127}
128