• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.zen;
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     // per-instance to ensure we're always using the current locale
36     private final SimpleDateFormat mDayFormat = new SimpleDateFormat("EEEE");
37     private final SparseBooleanArray mDays = new SparseBooleanArray();
38     private final LinearLayout mLayout;
39 
ZenModeScheduleDaysSelection(Context context, int[] days)40     public ZenModeScheduleDaysSelection(Context context, int[] days) {
41         super(context);
42         mLayout = new LinearLayout(mContext);
43         final int hPad = context.getResources()
44                 .getDimensionPixelSize(R.dimen.zen_schedule_day_margin);
45         mLayout.setPadding(hPad, 0, hPad, 0);
46         addView(mLayout);
47         if (days != null) {
48             for (int i = 0; i < days.length; i++) {
49                 mDays.put(days[i], true);
50             }
51         }
52         mLayout.setOrientation(LinearLayout.VERTICAL);
53         final Calendar c = Calendar.getInstance();
54         int[] daysOfWeek = getDaysOfWeekForLocale(c);
55         final LayoutInflater inflater = LayoutInflater.from(context);
56         for (int i = 0; i < daysOfWeek.length; i++) {
57             final int day = daysOfWeek[i];
58             final CheckBox checkBox = (CheckBox) inflater.inflate(R.layout.zen_schedule_rule_day,
59                     this, false);
60             c.set(Calendar.DAY_OF_WEEK, day);
61             checkBox.setText(mDayFormat.format(c.getTime()));
62             checkBox.setChecked(mDays.get(day));
63             checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
64                 @Override
65                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
66                     mDays.put(day, isChecked);
67                     onChanged(getDays());
68                 }
69             });
70             mLayout.addView(checkBox);
71         }
72     }
73 
getDays()74     private int[] getDays() {
75         final SparseBooleanArray rt = new SparseBooleanArray(mDays.size());
76         for (int i = 0; i < mDays.size(); i++) {
77             final int day = mDays.keyAt(i);
78             if (!mDays.valueAt(i)) continue;
79             rt.put(day, true);
80         }
81         final int[] rta = new int[rt.size()];
82         for (int i = 0; i < rta.length; i++) {
83             rta[i] = rt.keyAt(i);
84         }
85         Arrays.sort(rta);
86         return rta;
87     }
88 
getDaysOfWeekForLocale(Calendar c)89     protected static int[] getDaysOfWeekForLocale(Calendar c) {
90         int[] daysOfWeek = new int[7];
91         int currentDay = c.getFirstDayOfWeek();
92         for (int i = 0; i < daysOfWeek.length; i++) {
93             if (currentDay > 7) currentDay = 1;
94             daysOfWeek[i] = currentDay;
95             currentDay++;
96         }
97         return daysOfWeek;
98     }
99 
onChanged(int[] days)100     protected void onChanged(int[] days) {
101         // event hook for subclasses
102     }
103 }
104