Closed Cornholio2108 closed 4 years ago
AlarmHelper.setAlarm:
intent.putExtra(ALARM_TRAFFIC_UPDATE, ALARM_TRAFFIC_UPDATE);
Intent serviceIntent = new Intent(context, TrafficUpdateReceiver.class);
serviceIntent.putExtra(ALARM_TRAFFIC_UPDATE, ALARM_TRAFFIC_UPDATE);
PendingIntent pendingIntentService = PendingIntent.getBroadcast(context, alarmID, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManagerService = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManagerService.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime - TimeUnit.MINUTES.toMillis(20), pendingIntentService);
TrafficUpdateReceiver:
public class TrafficUpdateReceive extends BroadcastReceiver implements BingTrafficHandler.OnBingTrafficHandlerUpdate {
public static final String TAG = TrafficUpdateReceiver.class.getName();
int alarmID = 0;
boolean isTrafficUpdate=false;
Context context;
AwakeAlarm alarm;
///TODO: include Location update???
@Override
public void onReceive(Context context, Intent intent) {
this.context=context;
Globals.initPrefs(context);
if(intent.hasExtra(ALARM_ID))
{
alarmID=intent.getIntExtra(ALARM_ID,0);
}
isTrafficUpdate=intent.hasExtra(Globals.ALARM_TRAFFIC_UPDATE);
if(AlarmMap!=null)
{
if(AlarmMap.size()>0)
{
alarm=AlarmMap.get(alarmID);
initTrafficUpdate();
}
else
RoomsHandler.getOneAlarm(context,alarmID);
}
else
RoomsHandler.getOneAlarm(context,alarmID);
///Check again 10 min before alarm
Intent serviceIntent = new Intent(context, TrafficUpdateReceiver.class);
serviceIntent.putExtra(ALARM_TRAFFIC_UPDATE, ALARM_TRAFFIC_UPDATE);
PendingIntent pendingIntentService = PendingIntent.getBroadcast(context, alarmID, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManagerService = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManagerService.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime - TimeUnit.MINUTES.toMillis(10), pendingIntentService);
}
private void initTrafficUpdate()
{
if(isTrafficUpdate)
{
//Check traffic and update alarm time
double destLat = destinationList.get(alarm.destination).lat;
double destLon = destinationList.get(alarm.destination).lon;
BingTrafficHandler.getBingTraffic(context, myPrefs.getString(SP_USERPOS_LAT,"") + "," + myPrefs.getString(SP_USERPOS_LON,"") , (destLat + "," + destLon));
}
else
{
//Cancel traffic update
}
}
public Double getPrefDouble(String prefCode)
{
return (Double.parseDouble(myPrefs.getString(prefCode, "0")));
}
@Override
public void onBingTrafficReceived(Double distance, int duration, int durationTraffic) {
Log.d(TAG, "onBingTrafficReceived: "+" distance: "+distance+", duration: "+duration+", durationTraffic: "+durationTraffic);
int delay=durationTraffic-duration-116;
if(delay>0) {
long alarmTime = alarm.alarmMillis - TimeUnit.SECONDS.toMillis(delay);
Intent intent = new Intent(context, SmartReceiver.class);
intent.putExtra(ALARM_ID, alarm.id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
}
else {
//No delay - do nothing
}
}
@Override
public void onBingIncidentsReceived(int count, List<BingIncidentsData.Resources> incidents) {
}
@Override
public void onBingTrafficFailed(String errorMsg) {
Toast.makeText(this,"Traffic request failed: "+errorMsg, Toast.LENGTH_LONG).show();
}
@Override
public void onLoadingFinished() {
}
@Override
public void onGetOneAlarm(AwakeAlarm alarm) {
if(alarm!=null) {
initTrafficUpdate();
}
}
@Override
public void onAlarmSaved(AwakeAlarm alarm) {
}
@Override
public void onAlarmUpdated(final AwakeAlarm alarm) {
}
}
Added.
Needs to be implemented