Home | History | Annotate | Download | only in display
      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 com.android.internal.app.ColorDisplayController;
     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 
     33     NightDisplayTimeFormatter(Context context) {
     34         mTimeFormatter = android.text.format.DateFormat.getTimeFormat(context);
     35         mTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
     36     }
     37 
     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 
     48     public String getAutoModeTimeSummary(Context context, ColorDisplayController controller) {
     49         final int summaryFormatResId = controller.isActivated() ? R.string.night_display_summary_on
     50                 : R.string.night_display_summary_off;
     51         return context.getString(summaryFormatResId, getAutoModeSummary(context, controller));
     52     }
     53 
     54     private String getAutoModeSummary(Context context, ColorDisplayController controller) {
     55         final boolean isActivated = controller.isActivated();
     56         final int autoMode = controller.getAutoMode();
     57         if (autoMode == ColorDisplayController.AUTO_MODE_CUSTOM) {
     58             if (isActivated) {
     59                 return context.getString(R.string.night_display_summary_on_auto_mode_custom,
     60                         getFormattedTimeString(controller.getCustomEndTime()));
     61             } else {
     62                 return context.getString(R.string.night_display_summary_off_auto_mode_custom,
     63                         getFormattedTimeString(controller.getCustomStartTime()));
     64             }
     65         } else if (autoMode == ColorDisplayController.AUTO_MODE_TWILIGHT) {
     66             return context.getString(isActivated
     67                     ? R.string.night_display_summary_on_auto_mode_twilight
     68                     : R.string.night_display_summary_off_auto_mode_twilight);
     69         } else {
     70             return context.getString(isActivated
     71                     ? R.string.night_display_summary_on_auto_mode_never
     72                     : R.string.night_display_summary_off_auto_mode_never);
     73         }
     74     }
     75 }
     76