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.time.LocalTime;
26import java.util.Calendar;
27import java.util.TimeZone;
28
29public class NightDisplayPreference extends SwitchPreference
30        implements NightDisplayController.Callback {
31
32    private NightDisplayController mController;
33    private DateFormat mTimeFormatter;
34
35    public NightDisplayPreference(Context context, AttributeSet attrs) {
36        super(context, attrs);
37
38        mController = new NightDisplayController(context);
39        mTimeFormatter = android.text.format.DateFormat.getTimeFormat(context);
40        mTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
41    }
42
43    @Override
44    public void onAttached() {
45        super.onAttached();
46
47        // Listen for changes only while attached.
48        mController.setListener(this);
49
50        // Update the summary since the state may have changed while not attached.
51        updateSummary();
52    }
53
54    @Override
55    public void onDetached() {
56        super.onDetached();
57
58        // Stop listening for state changes.
59        mController.setListener(null);
60    }
61
62    private String getFormattedTimeString(LocalTime localTime) {
63        final Calendar c = Calendar.getInstance();
64        c.setTimeZone(mTimeFormatter.getTimeZone());
65        c.set(Calendar.HOUR_OF_DAY, localTime.getHour());
66        c.set(Calendar.MINUTE, localTime.getMinute());
67        c.set(Calendar.SECOND, 0);
68        c.set(Calendar.MILLISECOND, 0);
69        return mTimeFormatter.format(c.getTime());
70    }
71
72    private void updateSummary() {
73        final Context context = getContext();
74
75        final boolean isActivated = mController.isActivated();
76        final int autoMode = mController.getAutoMode();
77
78        final String autoModeSummary;
79        switch (autoMode) {
80            default:
81            case NightDisplayController.AUTO_MODE_DISABLED:
82                autoModeSummary = context.getString(isActivated
83                        ? R.string.night_display_summary_on_auto_mode_never
84                        : R.string.night_display_summary_off_auto_mode_never);
85                break;
86            case NightDisplayController.AUTO_MODE_CUSTOM:
87                if (isActivated) {
88                    autoModeSummary = context.getString(
89                            R.string.night_display_summary_on_auto_mode_custom,
90                            getFormattedTimeString(mController.getCustomEndTime()));
91                } else {
92                    autoModeSummary = context.getString(
93                            R.string.night_display_summary_off_auto_mode_custom,
94                            getFormattedTimeString(mController.getCustomStartTime()));
95                }
96                break;
97            case NightDisplayController.AUTO_MODE_TWILIGHT:
98                autoModeSummary = context.getString(isActivated
99                        ? R.string.night_display_summary_on_auto_mode_twilight
100                        : R.string.night_display_summary_off_auto_mode_twilight);
101                break;
102        }
103
104        final int summaryFormatResId = isActivated ? R.string.night_display_summary_on
105                : R.string.night_display_summary_off;
106        setSummary(context.getString(summaryFormatResId, autoModeSummary));
107    }
108
109    @Override
110    public void onActivated(boolean activated) {
111        updateSummary();
112    }
113
114    @Override
115    public void onAutoModeChanged(int autoMode) {
116        updateSummary();
117    }
118
119    @Override
120    public void onCustomStartTimeChanged(LocalTime startTime) {
121        updateSummary();
122    }
123
124    @Override
125    public void onCustomEndTimeChanged(LocalTime endTime) {
126        updateSummary();
127    }
128}
129