2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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;
29 import com.android.settings.R;
31 import java.text.SimpleDateFormat;
32 import java.util.Calendar;
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
39 private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("EEEE");
41 private final SparseBooleanArray mDays = new SparseBooleanArray();
42 private final LinearLayout mLayout;
44 public ZenModeDowntimeDaysSelection(Context context, String mode) {
46 mLayout = new LinearLayout(mContext);
47 final int hPad = context.getResources().getDimensionPixelSize(R.dimen.zen_downtime_margin);
48 mLayout.setPadding(hPad, 0, hPad, 0);
50 final int[] days = ZenModeConfig.tryParseDays(mode);
52 for (int i = 0; i < days.length; i++) {
53 mDays.put(days[i], true);
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,
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() {
68 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
69 mDays.put(day, isChecked);
73 mLayout.addView(checkBox);
77 private String getMode() {
78 final StringBuilder sb = new StringBuilder(ZenModeConfig.SLEEP_MODE_DAYS_PREFIX);
80 for (int i = 0; i < mDays.size(); i++) {
81 final int day = mDays.keyAt(i);
82 if (!mDays.valueAt(i)) continue;
90 return empty ? null : sb.toString();
93 protected void onChanged(String mode) {
94 // event hook for subclasses