Back to project page silent-meeting.
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.
package uk.co.bensproule.silentmeeting.activity; /*from w w w. jav a2s . c o m*/ import android.app.Activity; import android.app.AlarmManager; import android.app.DialogFragment; import android.app.PendingIntent; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CompoundButton; import android.widget.ListView; import android.widget.Switch; import uk.co.bensproule.silentmeeting.R; 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.fragment.TimePickerFragment; import uk.co.bensproule.silentmeeting.receiver.CalendarEventsReceiver; import java.util.Calendar; public class PreferencesActivity extends Activity implements DialogCloseListener { public static final String TIME_PICKER = "timePicker"; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new Thread(new Runnable() { @Override public void run() { createActiveSwitch(); } }).start(); new Thread(new Runnable() { @Override public void run() { createOptionsList(); } }).start(); new Thread(new Runnable() { @Override public void run() { setServiceTime(); } }).start(); } private void createActiveSwitch() { final boolean active = Setting.getActive(this); Switch activeSwitch = (Switch) findViewById(R.id.activeSwitch); activeSwitch.setChecked(active); activeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) { new Thread(new Runnable() { @Override public void run() { Setting.setActive(getApplicationContext(), isChecked); } }).start(); } }); } private void createOptionsList() { String[] options = getResources().getStringArray(R.array.options); ListView optionsList = (ListView) findViewById(R.id.optionsList); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, options); optionsList.setAdapter(adapter); optionsList.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Bundle arguments = new Bundle(); arguments.putString(TimePickerFragment.SETTING, getStrippedOption((int) id)); DialogFragment timePicker = new TimePickerFragment(); timePicker.setArguments(arguments); timePicker.show(getFragmentManager(), TIME_PICKER); } }); } private String getStrippedOption(int index) { String[] options = getResources().getStringArray(R.array.options); return options[index].replaceAll(" ", "").toLowerCase(); } private void setServiceTime() { Intent intent = new Intent(getApplicationContext(), CalendarEventsReceiver.class); intent.setAction(Actions.SET_SILENCE_ALARMS); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); long interval = Setting.getInterval(getApplicationContext()); AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE); alarm.cancel(pendingIntent); alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent); } @Override public void handleDialogClose(DialogInterface dialog) { setServiceTime(); } }