Home | History | Annotate | Download | only in deskclock
      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 
     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
     54     protected void onDialogClosed(boolean positiveResult) {
     55         if (positiveResult) {
     56             mDaysOfWeek.set(mNewDaysOfWeek);
     57             setSummary(mDaysOfWeek.toString(getContext(), true));
     58             callChangeListener(mDaysOfWeek);
     59         } else {
     60             mNewDaysOfWeek.set(mDaysOfWeek);
     61         }
     62     }
     63 
     64     @Override
     65     protected void onPrepareDialogBuilder(Builder builder) {
     66         CharSequence[] entries = getEntries();
     67         CharSequence[] entryValues = getEntryValues();
     68 
     69         builder.setMultiChoiceItems(
     70                 entries, mDaysOfWeek.getBooleanArray(),
     71                 new DialogInterface.OnMultiChoiceClickListener() {
     72                     public void onClick(DialogInterface dialog, int which,
     73                             boolean isChecked) {
     74                         mNewDaysOfWeek.set(which, isChecked);
     75                     }
     76                 });
     77     }
     78 
     79     public void setDaysOfWeek(Alarm.DaysOfWeek dow) {
     80         mDaysOfWeek.set(dow);
     81         mNewDaysOfWeek.set(dow);
     82         setSummary(dow.toString(getContext(), true));
     83     }
     84 
     85     public Alarm.DaysOfWeek getDaysOfWeek() {
     86         return mDaysOfWeek;
     87     }
     88 }
     89