OSDN Git Service

Initial Contribution
[android-x86/packages-apps-DeskClock.git] / src / com / android / alarmclock / RepeatPreference.java
1 /*
2  * Copyright (C) 2008 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.alarmclock;
18
19 import android.app.AlertDialog.Builder;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.preference.ListPreference;
23 import android.util.AttributeSet;
24
25 public class RepeatPreference extends ListPreference {
26     private Alarms.DaysOfWeek mDaysOfWeek = new Alarms.DaysOfWeek();
27     private OnRepeatChangeListener mOnRepeatChangeListener;
28
29     public interface OnRepeatChangeListener {
30         /** Called when this preference has changed */
31         public void onRepeatChanged(Alarms.DaysOfWeek daysOfWeek);
32     }
33
34     public RepeatPreference(Context context, AttributeSet attrs) {
35         super(context, attrs);
36     }
37
38     void setDaysOfWeek(Alarms.DaysOfWeek daysOfWeek) {
39         /* we keep a local copy, so the host can set itself on a
40            positive result and ignore on a negative */
41         mDaysOfWeek.set(daysOfWeek);
42     }
43
44     void setOnRepeatChangeListener(OnRepeatChangeListener listener) {
45         mOnRepeatChangeListener = listener;
46     }
47
48     @Override
49     protected void onDialogClosed(boolean positiveResult) {
50         if (positiveResult) {
51             mOnRepeatChangeListener.onRepeatChanged(mDaysOfWeek);
52         }
53     }
54
55     @Override
56     protected void onPrepareDialogBuilder(Builder builder) {
57         CharSequence[] entries = getEntries();
58         CharSequence[] entryValues = getEntryValues();
59
60         if (entries == null || entryValues == null) {
61             throw new IllegalStateException(
62                     "RepeatPreference requires an entries array and an entryValues array.");
63         }
64         builder.setMultiChoiceItems(
65                 entries, mDaysOfWeek.getBooleanArray(),
66                 new DialogInterface.OnMultiChoiceClickListener() {
67                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
68                         mDaysOfWeek.set(which, isChecked);
69                     }
70                 });
71     }
72 }