• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.display;
18 
19 import android.content.Context;
20 
21 import android.hardware.display.ColorDisplayManager;
22 import com.android.settings.R;
23 
24 import java.text.DateFormat;
25 import java.time.LocalTime;
26 import java.util.Calendar;
27 import java.util.TimeZone;
28 
29 public class NightDisplayTimeFormatter {
30 
31     private DateFormat mTimeFormatter;
32 
NightDisplayTimeFormatter(Context context)33     NightDisplayTimeFormatter(Context context) {
34         mTimeFormatter = android.text.format.DateFormat.getTimeFormat(context);
35         mTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
36     }
37 
getFormattedTimeString(LocalTime localTime)38     public String getFormattedTimeString(LocalTime localTime) {
39         final Calendar c = Calendar.getInstance();
40         c.setTimeZone(mTimeFormatter.getTimeZone());
41         c.set(Calendar.HOUR_OF_DAY, localTime.getHour());
42         c.set(Calendar.MINUTE, localTime.getMinute());
43         c.set(Calendar.SECOND, 0);
44         c.set(Calendar.MILLISECOND, 0);
45         return mTimeFormatter.format(c.getTime());
46     }
47 
getAutoModeTimeSummary(Context context, ColorDisplayManager manager)48     public String getAutoModeTimeSummary(Context context, ColorDisplayManager manager) {
49         final int summaryFormatResId =
50                 manager.isNightDisplayActivated() ? R.string.night_display_summary_on
51                         : R.string.night_display_summary_off;
52         return context.getString(summaryFormatResId, getAutoModeSummary(context, manager));
53     }
54 
getAutoModeSummary(Context context, ColorDisplayManager manager)55     public String getAutoModeSummary(Context context, ColorDisplayManager manager) {
56         final boolean isActivated = manager.isNightDisplayActivated();
57         final int autoMode = manager.getNightDisplayAutoMode();
58         if (autoMode == ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) {
59             if (isActivated) {
60                 return context.getString(R.string.night_display_summary_on_auto_mode_custom,
61                         getFormattedTimeString(manager.getNightDisplayCustomEndTime()));
62             } else {
63                 return context.getString(R.string.night_display_summary_off_auto_mode_custom,
64                         getFormattedTimeString(manager.getNightDisplayCustomStartTime()));
65             }
66         } else if (autoMode == ColorDisplayManager.AUTO_MODE_TWILIGHT) {
67             return context.getString(isActivated
68                     ? R.string.night_display_summary_on_auto_mode_twilight
69                     : R.string.night_display_summary_off_auto_mode_twilight);
70         } else {
71             return context.getString(isActivated
72                     ? R.string.night_display_summary_on_auto_mode_never
73                     : R.string.night_display_summary_off_auto_mode_never);
74         }
75     }
76 }
77