• 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 import android.hardware.display.ColorDisplayManager;
21 
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 
getAutoModeSummary(Context context, ColorDisplayManager manager)48     public String getAutoModeSummary(Context context, ColorDisplayManager manager) {
49         final boolean isActivated = manager.isNightDisplayActivated();
50         final int autoMode = manager.getNightDisplayAutoMode();
51         if (autoMode == ColorDisplayManager.AUTO_MODE_CUSTOM_TIME) {
52             if (isActivated) {
53                 return context.getString(R.string.night_display_summary_on_auto_mode_custom,
54                         getFormattedTimeString(manager.getNightDisplayCustomEndTime()));
55             } else {
56                 return context.getString(R.string.night_display_summary_off_auto_mode_custom,
57                         getFormattedTimeString(manager.getNightDisplayCustomStartTime()));
58             }
59         } else if (autoMode == ColorDisplayManager.AUTO_MODE_TWILIGHT) {
60             return context.getString(isActivated
61                     ? R.string.night_display_summary_on_auto_mode_twilight
62                     : R.string.night_display_summary_off_auto_mode_twilight);
63         } else {
64             return context.getString(isActivated
65                     ? R.string.night_display_summary_on_auto_mode_never
66                     : R.string.night_display_summary_off_auto_mode_never);
67         }
68     }
69 }
70