Back to project page SoundScheduler.
The source code is released under:
GNU General Public License
If you think the Android project SoundScheduler listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Sound Scheduler//from w ww .j a v a 2 s . c om * Copyright (C) 2013 Victor Kifer */ package com.victorkifer.SoundScheduler.receivers; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import com.victorkifer.SoundScheduler.MainActivity; import com.victorkifer.SoundScheduler.database.RulesDataSource; import com.victorkifer.SoundScheduler.entities.Rule; import com.victorkifer.SoundScheduler.managers.SoundManager; import java.util.Calendar; import java.util.Date; import java.util.List; public class TimeChangeReceiver extends BroadcastReceiver { private static final String TAG = TimeChangeReceiver.class.getSimpleName(); public static final String ACTION_ALARM_CALL = "com.victorkifer.SoundScheduler.ALARM"; public static boolean SERVICE_STARTED = false; private RulesDataSource rulesDS; @Override public void onReceive(Context context, Intent intent) { SERVICE_STARTED = true; final String action = intent.getAction(); Log.d(TAG, "Action received"); if (action.equals(ACTION_ALARM_CALL)) { rulesDS = new RulesDataSource(context); rulesDS.open(); List<Rule> rules = rulesDS.getActiveRules(); Calendar c = Calendar.getInstance(); Date date = new Date(); c.setTime(date); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); long time = date.getTime() - c.getTimeInMillis(); for(Rule rule : rules) { int day = c.get(Calendar.DAY_OF_WEEK) - 2; day = day == -1 ? 6 : day; boolean applyOnDay = rule.isDaysRepeatEmpty() || rule.isActiveOnDay(day); if(rule.getStartTime() < rule.getEndTime()) { if(rule.getStartTime() <= time && rule.getEndTime() > time && applyOnDay && !rule.isApplied()) { applyRule(rule, context); } else if(rule.isApplied() && (rule.getStartTime() > time || rule.getEndTime() < time) ) { rulesDS.updateRule(resetRule(rule, context)); } } else { if((rule.getStartTime() <= time || rule.getEndTime() > time) && applyOnDay && !rule.isApplied()) { applyRule(rule, context); } else if(rule.isApplied() && (rule.getStartTime() > time && rule.getEndTime() <= time) ) { rulesDS.updateRule(resetRule(rule, context)); } } } rulesDS.close(); } } private void applyRule(Rule rule, Context context) { Log.d(TAG, "Activating rule"); if(rulesDS.hasAppliedRule()) { return; } rule.setApplied(true); SoundManager sm = new SoundManager(context); if(sm.isNormalModeEnabled()) { rule.setStateBefore(Rule.STATE_NORMAL); } if(sm.isVibrateModeEnabled()) { rule.setStateBefore(Rule.STATE_VIBRATE); } if(sm.isSilentModeEnabled()) { rule.setStateBefore(Rule.STATE_SILENT); } switch (rule.getStateToApply()) { case Rule.STATE_SILENT: sm.setSilentMode(); break; case Rule.STATE_VIBRATE: sm.setVibrateMode(); break; default: break; } rulesDS.updateRule(rule); } public static Rule resetRule(Rule rule, Context context) { Log.d(TAG, "Deactivating rule"); rule.setApplied(false); SoundManager sm = new SoundManager(context); switch (rule.getStateBefore()) { case Rule.STATE_SILENT: sm.setSilentMode(); break; case Rule.STATE_VIBRATE: sm.setVibrateMode(); break; case Rule.STATE_NORMAL: sm.setNormalMode(); break; default: break; } if (rule.isDaysRepeatEmpty()) { rule.setActive(false); context.getApplicationContext().sendBroadcast(new Intent(MainActivity.ACTION_RULELIST_UPDATE)); } return rule; } }