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.receiver; /*from w ww . j av a2s . c om*/ import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.os.Build; import android.provider.CalendarContract; import android.util.Log; import uk.co.bensproule.silentmeeting.constants.Actions; import uk.co.bensproule.silentmeeting.constants.Extra; import uk.co.bensproule.silentmeeting.constants.Setting; import uk.co.bensproule.silentmeeting.dao.InstanceDao; import uk.co.bensproule.silentmeeting.domain.Instance; import java.util.Calendar; public class MutePhoneBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { boolean active = Setting.getActive(context); if (active) { InstanceDao instanceDao = new InstanceDao(context); int instanceId = intent.getIntExtra(CalendarContract.Instances._ID, 0); Instance instance = instanceDao.get(instanceId); if (instance != null && instance.isRunning()) { return; } AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); Log.d("SilencePhone", "Muting phone"); int currentRingerMode = audioManager.getRingerMode(); audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); long endDate = intent.getLongExtra(CalendarContract.Instances.DTEND, 0); long activeAfterEvents = Setting.getActiveAfterEvents(context); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(endDate + activeAfterEvents); Intent unmuteIntent = new Intent(context, UnmutePhoneBroadcastReceiver.class); unmuteIntent.setAction(Actions.UNMUTE); unmuteIntent.putExtra(Extra.PREVIOUS_RINGER_MODE, currentRingerMode); unmuteIntent.putExtra(Extra.INSTANCE_ID, instanceId); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, instanceId, unmuteIntent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } instanceDao.setRunning(instanceId, true); } } }