Android Open Source - android-002 Reminder Interval Dialog Preference






From Project

Back to project page android-002.

License

The source code is released under:

Apache License

If you think the Android project android-002 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.michaelfitzmaurice.dailyselfie.settings;
/*from  w w w.j av a 2s .  c o  m*/
import static com.michaelfitzmaurice.dailyselfie.Alarms.ALARM_INTERVAL_PREFERENCES_KEY;
import static com.michaelfitzmaurice.dailyselfie.SelfieListActivity.LOG_TAG;
import static java.lang.String.format;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

import com.michaelfitzmaurice.dailyselfie.AlarmTimeInterval;
import com.michaelfitzmaurice.dailyselfie.Alarms;
import com.michaelfitzmaurice.dailyselfie.R;

public class ReminderIntervalDialogPreference extends DialogPreference {

  public ReminderIntervalDialogPreference(Context context, 
                      AttributeSet attrs) {
    super(context, attrs);
    setDialogLayoutResource(R.layout.interval_picker_view);
    setSummary();
  }
  
  @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
            builder.setTitle(R.string.notifications_interval_picker_title);
            builder.setPositiveButton(null, null);
            builder.setNegativeButton(null, null);
            super.onPrepareDialogBuilder(builder);  
    }
  
    @Override
    public void onBindDialogView(View view) {

      final NumberPicker dayPicker = 
        (NumberPicker) view.findViewById(R.id.daysNumberPicker);
      dayPicker.setMinValue(0);
      dayPicker.setMaxValue(365);
        
      final NumberPicker hoursPicker = 
        (NumberPicker) view.findViewById(R.id.hoursNumberPicker);
      hoursPicker.setMinValue(0);
      hoursPicker.setMaxValue(23);

      final NumberPicker minutesPicker = 
        (NumberPicker) view.findViewById(R.id.minutesNumberPicker);
      minutesPicker.setMinValue(0);
      minutesPicker.setMaxValue(59);

      AlarmTimeInterval alarmInterval = getAlarmInterval();
      if (alarmInterval != null) {
        dayPicker.setValue( alarmInterval.getDays() );
        hoursPicker.setValue( alarmInterval.getHours() );
        minutesPicker.setValue( alarmInterval.getMinutes() );
      }
      
      Button cancelButton = 
        (Button) view.findViewById(R.id.cancelNotificationIntervalButton);
      cancelButton.setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
          setSummary();
          getDialog().dismiss();        
        }
      });

      Button setButton = 
        (Button) view.findViewById(R.id.setNotificationIntervalButton);
      setButton.setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
          AlarmTimeInterval newTimeInterval = new AlarmTimeInterval();
          newTimeInterval.setDays( dayPicker.getValue() );
          newTimeInterval.setHours( hoursPicker.getValue() );
          newTimeInterval.setMinutes( minutesPicker.getValue() );
          if ( newTimeInterval.isZero() ) {
            showToast(R.string.notifications_interval_zero_warning);
          } else {
            SharedPreferences prefs = 
              getContext().getSharedPreferences(LOG_TAG, 
                              Context.MODE_PRIVATE);
            prefs
              .edit()
              .putString(ALARM_INTERVAL_PREFERENCES_KEY, 
                  newTimeInterval.serialiseToString() )
                  .apply();
            setSummary();
            if ( remindersAreOn() ) {
              Alarms.getInstance().set(newTimeInterval);
              showToast(
                R.string.notifications_switched_on_confirmation,
                newTimeInterval.getDays(),
                newTimeInterval.getHours(),
                newTimeInterval.getMinutes() );
            }
            getDialog().dismiss();
          }
        }
      });

      super.onBindDialogView(view);
    }
    
    private AlarmTimeInterval getAlarmInterval() {
    
    AlarmTimeInterval alarmInterval = null;
    SharedPreferences prefs = 
      getContext().getSharedPreferences(LOG_TAG, Context.MODE_PRIVATE);
    String alarmIntervalString = 
      prefs.getString(ALARM_INTERVAL_PREFERENCES_KEY, 
                      null);
    if (alarmIntervalString != null) {
      alarmInterval = new AlarmTimeInterval(alarmIntervalString);
    }
    
    return alarmInterval;
  }
    
    private void setSummary() {
    
      AlarmTimeInterval alarmInterval = getAlarmInterval();
    if (alarmInterval != null) {
      String text = 
        format( getContext().getString(
              R.string.notifications_preferences_on_summary), 
            alarmInterval.getDays(), 
            alarmInterval.getHours(),
            alarmInterval.getMinutes() );
      setSummary(text);
    } else {
      setSummary( 
        getContext().getString(
            R.string.notifications_preferences_off_summary) );
    }
    }
    
    private boolean remindersAreOn() {
      
      SharedPreferences prefs = 
        PreferenceManager.getDefaultSharedPreferences( getContext() );
      String reminderSwitchKey = 
        getContext().getString(R.string.notifications_switch_key);
      
      return prefs.getBoolean(reminderSwitchKey, false);
    }
    
    private void showToast(int messageId, Object... formatArgs) {
      
      String message = getContext().getString(messageId);
      if (formatArgs != null) {
        message = format(message, formatArgs);
      }
      
      Toast.makeText(getContext(),
            message, 
            Toast.LENGTH_SHORT)
        .show();
    }

}




Java Source Code List

com.michaelfitzmaurice.android.modernart.MainActivity.java
com.michaelfitzmaurice.dailyselfie.AlarmReceiver.java
com.michaelfitzmaurice.dailyselfie.AlarmTimeInterval.java
com.michaelfitzmaurice.dailyselfie.Alarms.java
com.michaelfitzmaurice.dailyselfie.SelfieDetailActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListViewAdapter.java
com.michaelfitzmaurice.dailyselfie.SelfieRecord.java
com.michaelfitzmaurice.dailyselfie.settings.ReminderIntervalDialogPreference.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsActivity.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsFragment.java
course.labs.contentproviderlab.MockLocationProvider.java
course.labs.contentproviderlab.PlaceDownloaderTask.java
course.labs.contentproviderlab.PlaceRecord.java
course.labs.contentproviderlab.PlaceViewActivity.java
course.labs.contentproviderlab.PlaceViewAdapter.java
course.labs.contentproviderlab.provider.PlaceBadgeContentProvider.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.graphicslab.BubbleActivity.java
course.labs.locationlab.MockLocationProvider.java
course.labs.locationlab.PlaceDownloaderTask.java
course.labs.locationlab.PlaceRecord.java
course.labs.locationlab.PlaceViewActivity.java
course.labs.locationlab.PlaceViewAdapter.java
course.labs.notificationslab.DownloaderTask.java
course.labs.notificationslab.FeedFragment.java
course.labs.notificationslab.FriendsFragment.java
course.labs.notificationslab.MainActivity.java
course.labs.notificationslab.SelectionListener.java
course.labs.notificationslab.TestFrontEndActivity.java