107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock/*
207f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * Copyright (C) 2014 The Android Open Source Project
307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock *
407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * you may not use this file except in compliance with the License.
607f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * You may obtain a copy of the License at
707f7efba67a5f261fca31890a1b60f23021325f5John Spurlock *
807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
907f7efba67a5f261fca31890a1b60f23021325f5John Spurlock *
1007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * Unless required by applicable law or agreed to in writing, software
1107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
1207f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * See the License for the specific language governing permissions and
1407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock * limitations under the License.
1507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock */
1607f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
1707f7efba67a5f261fca31890a1b60f23021325f5John Spurlockpackage com.android.settings.notification;
1807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
1907f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.content.Context;
2007f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.service.notification.ZenModeConfig;
2107f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.util.SparseBooleanArray;
2207f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.view.LayoutInflater;
2307f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.widget.CheckBox;
2407f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.widget.CompoundButton;
25cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlockimport android.widget.ScrollView;
2607f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.widget.CompoundButton.OnCheckedChangeListener;
2707f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport android.widget.LinearLayout;
2807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
2907f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport com.android.settings.R;
3007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
3107f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport java.text.SimpleDateFormat;
3207f7efba67a5f261fca31890a1b60f23021325f5John Spurlockimport java.util.Calendar;
3307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
34cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlockpublic class ZenModeDowntimeDaysSelection extends ScrollView {
351e7dbd455eade6e6d8f1abd7969dd096972ed84fJohn Spurlock    public static final int[] DAYS = {
3607f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY,
3707f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        Calendar.SATURDAY, Calendar.SUNDAY
3807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    };
3907f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("EEEE");
4007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
4107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    private final SparseBooleanArray mDays = new SparseBooleanArray();
42cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlock    private final LinearLayout mLayout;
4307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
4407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    public ZenModeDowntimeDaysSelection(Context context, String mode) {
4507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        super(context);
46cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlock        mLayout = new LinearLayout(mContext);
47cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlock        final int hPad = context.getResources().getDimensionPixelSize(R.dimen.zen_downtime_margin);
48cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlock        mLayout.setPadding(hPad, 0, hPad, 0);
49cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlock        addView(mLayout);
5007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        final int[] days = ZenModeConfig.tryParseDays(mode);
5107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        if (days != null) {
5207f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            for (int i = 0; i < days.length; i++) {
5307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                mDays.put(days[i], true);
5407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            }
5507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        }
56cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlock        mLayout.setOrientation(LinearLayout.VERTICAL);
5707f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        final Calendar c = Calendar.getInstance();
5807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        final LayoutInflater inflater = LayoutInflater.from(context);
5907f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        for (int i = 0; i < DAYS.length; i++) {
6007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            final int day = DAYS[i];
6107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            final CheckBox checkBox = (CheckBox) inflater.inflate(R.layout.zen_downtime_day,
6207f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                    this, false);
6307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            c.set(Calendar.DAY_OF_WEEK, day);
6407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            checkBox.setText(DAY_FORMAT.format(c.getTime()));
6507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            checkBox.setChecked(mDays.get(day));
6607f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
6707f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                @Override
6807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
6907f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                    mDays.put(day, isChecked);
7007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                    onChanged(getMode());
7107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                }
7207f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            });
73cddb29544ae4b7c67c3885eda7d2a485cceb53e6John Spurlock            mLayout.addView(checkBox);
7407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        }
7507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    }
7607f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
7707f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    private String getMode() {
7807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        final StringBuilder sb = new StringBuilder(ZenModeConfig.SLEEP_MODE_DAYS_PREFIX);
7907f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        boolean empty = true;
8007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        for (int i = 0; i < mDays.size(); i++) {
8107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            final int day = mDays.keyAt(i);
8207f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            if (!mDays.valueAt(i)) continue;
8307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            if (empty) {
8407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                empty = false;
8507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            } else {
8607f7efba67a5f261fca31890a1b60f23021325f5John Spurlock                sb.append(',');
8707f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            }
8807f7efba67a5f261fca31890a1b60f23021325f5John Spurlock            sb.append(day);
8907f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        }
9007f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        return empty ? null : sb.toString();
9107f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    }
9207f7efba67a5f261fca31890a1b60f23021325f5John Spurlock
9307f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    protected void onChanged(String mode) {
9407f7efba67a5f261fca31890a1b60f23021325f5John Spurlock        // event hook for subclasses
9507f7efba67a5f261fca31890a1b60f23021325f5John Spurlock    }
9607f7efba67a5f261fca31890a1b60f23021325f5John Spurlock}
97