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.util.SparseBooleanArray;
21 import android.view.LayoutInflater;
22 import android.widget.CheckBox;
23 import android.widget.CompoundButton;
24 import android.widget.CompoundButton.OnCheckedChangeListener;
25 import android.widget.LinearLayout;
26 import android.widget.ScrollView;
27 
28 import com.android.settings.R;
29 
30 import java.text.SimpleDateFormat;
31 import java.util.Arrays;
32 import java.util.Calendar;
33 
34 public class ZenModeScheduleDaysSelection extends ScrollView {
35     public static final int[] DAYS = {
36         Calendar.SUNDAY,
37         Calendar.MONDAY,
38         Calendar.TUESDAY,
39         Calendar.WEDNESDAY,
40         Calendar.THURSDAY,
41         Calendar.FRIDAY,
42         Calendar.SATURDAY,
43     };
44 
45     // per-instance to ensure we're always using the current locale
46     private final SimpleDateFormat mDayFormat = new SimpleDateFormat("EEEE");
47     private final SparseBooleanArray mDays = new SparseBooleanArray();
48     private final LinearLayout mLayout;
49 
ZenModeScheduleDaysSelection(Context context, int[] days)50     public ZenModeScheduleDaysSelection(Context context, int[] days) {
51         super(context);
52         mLayout = new LinearLayout(mContext);
53         final int hPad = context.getResources()
54                 .getDimensionPixelSize(R.dimen.zen_schedule_day_margin);
55         mLayout.setPadding(hPad, 0, hPad, 0);
56         addView(mLayout);
57         if (days != null) {
58             for (int i = 0; i < days.length; i++) {
59                 mDays.put(days[i], true);
60             }
61         }
62         mLayout.setOrientation(LinearLayout.VERTICAL);
63         final Calendar c = Calendar.getInstance();
64         final LayoutInflater inflater = LayoutInflater.from(context);
65         for (int i = 0; i < DAYS.length; i++) {
66             final int day = DAYS[i];
67             final CheckBox checkBox = (CheckBox) inflater.inflate(R.layout.zen_schedule_rule_day,
68                     this, false);
69             c.set(Calendar.DAY_OF_WEEK, day);
70             checkBox.setText(mDayFormat.format(c.getTime()));
71             checkBox.setChecked(mDays.get(day));
72             checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
73                 @Override
74                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
75                     mDays.put(day, isChecked);
76                     onChanged(getDays());
77                 }
78             });
79             mLayout.addView(checkBox);
80         }
81     }
82 
getDays()83     private int[] getDays() {
84         final SparseBooleanArray rt = new SparseBooleanArray(mDays.size());
85         for (int i = 0; i < mDays.size(); i++) {
86             final int day = mDays.keyAt(i);
87             if (!mDays.valueAt(i)) continue;
88             rt.put(day, true);
89         }
90         final int[] rta = new int[rt.size()];
91         for (int i = 0; i < rta.length; i++) {
92             rta[i] = rt.keyAt(i);
93         }
94         Arrays.sort(rta);
95         return rta;
96     }
97 
onChanged(int[] days)98     protected void onChanged(int[] days) {
99         // event hook for subclasses
100     }
101 }
102