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.notification.zen;
18 
19 import android.content.Context;
20 import android.icu.text.MessageFormat;
21 
22 import com.android.settings.R;
23 import com.android.settings.core.PreferenceControllerMixin;
24 import com.android.settingslib.core.lifecycle.Lifecycle;
25 
26 import java.util.HashMap;
27 import java.util.Locale;
28 import java.util.Map;
29 
30 public class ZenModeDurationPreferenceController extends AbstractZenModePreferenceController
31         implements PreferenceControllerMixin {
32 
33     protected static final String KEY = "zen_mode_duration_settings";
34 
ZenModeDurationPreferenceController(Context context, Lifecycle lifecycle)35     public ZenModeDurationPreferenceController(Context context, Lifecycle lifecycle) {
36         super(context, KEY, lifecycle);
37     }
38 
39     @Override
isAvailable()40     public boolean isAvailable() {
41         return true;
42     }
43 
44     @Override
getPreferenceKey()45     public String getPreferenceKey() {
46         return KEY;
47     }
48 
49     @Override
getSummary()50     public CharSequence getSummary() {
51         String summary;
52         int zenDuration = getZenDuration();
53         if (zenDuration < 0) {
54             summary = mContext.getString(R.string.zen_mode_duration_summary_always_prompt);
55         } else if (zenDuration == 0) {
56             summary = mContext.getString(R.string.zen_mode_duration_summary_forever);
57         } else {
58             if (zenDuration >= 60) {
59                 MessageFormat msgFormat = new MessageFormat(
60                         mContext.getString(R.string.zen_mode_duration_summary_time_hours),
61                         Locale.getDefault());
62                 Map<String, Object> msgArgs = new HashMap<>();
63                 msgArgs.put("count", zenDuration / 60);
64                 summary = msgFormat.format(msgArgs);
65             } else {
66                 MessageFormat msgFormat = new MessageFormat(
67                         mContext.getString(R.string.zen_mode_duration_summary_time_minutes),
68                         Locale.getDefault());
69                 Map<String, Object> msgArgs = new HashMap<>();
70                 msgArgs.put("count", zenDuration);
71                 summary = msgFormat.format(msgArgs);
72             }
73         }
74 
75         return summary;
76     }
77 }
78