OSDN Git Service

Settings: Make downtime days scrollable.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ZenModeDowntimeDaysSelection.java
1 /*
2  * Copyright (C) 2014 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;
18
19 import android.content.Context;
20 import android.service.notification.ZenModeConfig;
21 import android.util.SparseBooleanArray;
22 import android.view.LayoutInflater;
23 import android.widget.CheckBox;
24 import android.widget.CompoundButton;
25 import android.widget.ScrollView;
26 import android.widget.CompoundButton.OnCheckedChangeListener;
27 import android.widget.LinearLayout;
28
29 import com.android.settings.R;
30
31 import java.text.SimpleDateFormat;
32 import java.util.Calendar;
33
34 public class ZenModeDowntimeDaysSelection extends ScrollView {
35     private static final int[] DAYS = {
36         Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY,
37         Calendar.SATURDAY, Calendar.SUNDAY
38     };
39     private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("EEEE");
40
41     private final SparseBooleanArray mDays = new SparseBooleanArray();
42     private final LinearLayout mLayout;
43
44     public ZenModeDowntimeDaysSelection(Context context, String mode) {
45         super(context);
46         mLayout = new LinearLayout(mContext);
47         final int hPad = context.getResources().getDimensionPixelSize(R.dimen.zen_downtime_margin);
48         mLayout.setPadding(hPad, 0, hPad, 0);
49         addView(mLayout);
50         final int[] days = ZenModeConfig.tryParseDays(mode);
51         if (days != null) {
52             for (int i = 0; i < days.length; i++) {
53                 mDays.put(days[i], true);
54             }
55         }
56         mLayout.setOrientation(LinearLayout.VERTICAL);
57         final Calendar c = Calendar.getInstance();
58         final LayoutInflater inflater = LayoutInflater.from(context);
59         for (int i = 0; i < DAYS.length; i++) {
60             final int day = DAYS[i];
61             final CheckBox checkBox = (CheckBox) inflater.inflate(R.layout.zen_downtime_day,
62                     this, false);
63             c.set(Calendar.DAY_OF_WEEK, day);
64             checkBox.setText(DAY_FORMAT.format(c.getTime()));
65             checkBox.setChecked(mDays.get(day));
66             checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
67                 @Override
68                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
69                     mDays.put(day, isChecked);
70                     onChanged(getMode());
71                 }
72             });
73             mLayout.addView(checkBox);
74         }
75     }
76
77     private String getMode() {
78         final StringBuilder sb = new StringBuilder(ZenModeConfig.SLEEP_MODE_DAYS_PREFIX);
79         boolean empty = true;
80         for (int i = 0; i < mDays.size(); i++) {
81             final int day = mDays.keyAt(i);
82             if (!mDays.valueAt(i)) continue;
83             if (empty) {
84                 empty = false;
85             } else {
86                 sb.append(',');
87             }
88             sb.append(day);
89         }
90         return empty ? null : sb.toString();
91     }
92
93     protected void onChanged(String mode) {
94         // event hook for subclasses
95     }
96 }