• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.deskclock;
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 import java.text.DateFormatSymbols;
26 import java.util.Calendar;
27 
28 public class RepeatPreference extends ListPreference {
29 
30     // Initial value that can be set with the values saved in the database.
31     private Alarm.DaysOfWeek mDaysOfWeek = new Alarm.DaysOfWeek(0);
32     // New value that will be set if a positive result comes back from the
33     // dialog.
34     private Alarm.DaysOfWeek mNewDaysOfWeek = new Alarm.DaysOfWeek(0);
35 
RepeatPreference(Context context, AttributeSet attrs)36     public RepeatPreference(Context context, AttributeSet attrs) {
37         super(context, attrs);
38 
39         String[] weekdays = new DateFormatSymbols().getWeekdays();
40         String[] values = new String[] {
41             weekdays[Calendar.MONDAY],
42             weekdays[Calendar.TUESDAY],
43             weekdays[Calendar.WEDNESDAY],
44             weekdays[Calendar.THURSDAY],
45             weekdays[Calendar.FRIDAY],
46             weekdays[Calendar.SATURDAY],
47             weekdays[Calendar.SUNDAY],
48         };
49         setEntries(values);
50         setEntryValues(values);
51     }
52 
53     @Override
onDialogClosed(boolean positiveResult)54     protected void onDialogClosed(boolean positiveResult) {
55         if (positiveResult) {
56             mDaysOfWeek.set(mNewDaysOfWeek);
57             setSummary(mDaysOfWeek.toString(getContext(), true));
58             callChangeListener(mDaysOfWeek);
59         }
60     }
61 
62     @Override
onPrepareDialogBuilder(Builder builder)63     protected void onPrepareDialogBuilder(Builder builder) {
64         CharSequence[] entries = getEntries();
65         CharSequence[] entryValues = getEntryValues();
66 
67         builder.setMultiChoiceItems(
68                 entries, mDaysOfWeek.getBooleanArray(),
69                 new DialogInterface.OnMultiChoiceClickListener() {
70                     public void onClick(DialogInterface dialog, int which,
71                             boolean isChecked) {
72                         mNewDaysOfWeek.set(which, isChecked);
73                     }
74                 });
75     }
76 
setDaysOfWeek(Alarm.DaysOfWeek dow)77     public void setDaysOfWeek(Alarm.DaysOfWeek dow) {
78         mDaysOfWeek.set(dow);
79         mNewDaysOfWeek.set(dow);
80         setSummary(dow.toString(getContext(), true));
81     }
82 
getDaysOfWeek()83     public Alarm.DaysOfWeek getDaysOfWeek() {
84         return mDaysOfWeek;
85     }
86 }
87