Android Open Source - silent-meeting Time Picker Fragment






From Project

Back to project page silent-meeting.

License

The source code is released under:

GNU General Public License

If you think the Android project silent-meeting 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 uk.co.bensproule.silentmeeting.fragment;
//from ww  w .  ja v a2 s  .c  o  m
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.TextView;
import android.widget.TimePicker;
import uk.co.bensproule.silentmeeting.constants.Actions;
import uk.co.bensproule.silentmeeting.constants.Setting;
import uk.co.bensproule.silentmeeting.domain.DialogCloseListener;
import uk.co.bensproule.silentmeeting.receiver.CalendarEventsReceiver;

import java.util.Calendar;

import static uk.co.bensproule.silentmeeting.util.StringUtils.isBlank;

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    public static final String SETTING = "setting";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity().getApplicationContext());
        textView.setText("Set Time");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20f);
        textView.setPadding(20, 20, 20, 20);

        final TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), this, 0, 0, true);
        timePickerDialog.setCustomTitle(textView);

        new Thread(new Runnable() {
            @Override
            public void run() {
                String setting = getArguments().getString(SETTING);
                if (isBlank(setting)) {
                    throw new IllegalArgumentException("Setting must be provided");
                }

                long time = Setting.getLong(getActivity().getApplicationContext(), setting);
                time = time / 1000 / 60;
                int hour = (int) time / 60;
                int minute = (int) time % 60;
                timePickerDialog.updateTime(hour, minute);
            }
        }).start();

        return timePickerDialog;
    }

    @Override
    public void onTimeSet(TimePicker timePicker, final int hourOfDay, final int minute) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Context context = getActivity().getApplicationContext();
                String setting = getArguments().getString(SETTING);

                int hour = hourOfDay;
                if (hourOfDay == 0 && minute == 0 && setting.equals(Setting.POLLING_INTERVAL)) {
                    hour = 24;
                }

                long time = minute + (hour * 60l);
                time = time * 60l * 1000l;

                Setting.setLong(context, setting, time);

                Intent intent = new Intent(context, CalendarEventsReceiver.class);
                intent.setAction(Actions.SET_SILENCE_ALARMS);

                if (setting.equals(Setting.POLLING_INTERVAL)) {
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(calendar.getTimeInMillis() + time);

                    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                    alarm.cancel(pendingIntent);
                    alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), time, pendingIntent);
                } else {
                    context.sendBroadcast(intent);
                }
            }
        }).start();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        Activity activity = getActivity();
        if (activity instanceof DialogCloseListener)
            ((DialogCloseListener) activity).handleDialogClose(dialog);
    }
}




Java Source Code List

uk.co.bensproule.silentmeeting.ApplicationTest.java
uk.co.bensproule.silentmeeting.activity.PreferencesActivity.java
uk.co.bensproule.silentmeeting.constants.Actions.java
uk.co.bensproule.silentmeeting.constants.Extra.java
uk.co.bensproule.silentmeeting.constants.Setting.java
uk.co.bensproule.silentmeeting.dao.EventDao.java
uk.co.bensproule.silentmeeting.dao.InstanceDao.java
uk.co.bensproule.silentmeeting.dao.SQLiteHelper.java
uk.co.bensproule.silentmeeting.domain.Attendee.java
uk.co.bensproule.silentmeeting.domain.Calendar.java
uk.co.bensproule.silentmeeting.domain.DialogCloseListener.java
uk.co.bensproule.silentmeeting.domain.Event.java
uk.co.bensproule.silentmeeting.domain.Instance.java
uk.co.bensproule.silentmeeting.fragment.TimePickerFragment.java
uk.co.bensproule.silentmeeting.receiver.CalendarEventsReceiver.java
uk.co.bensproule.silentmeeting.receiver.MutePhoneBroadcastReceiver.java
uk.co.bensproule.silentmeeting.receiver.UnmutePhoneBroadcastReceiver.java
uk.co.bensproule.silentmeeting.util.StringUtils.java