Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent NPE on log line. Use monotonically increasing time for alarm … #325

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.os.Build;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.util.Log;

import org.eclipse.paho.client.mqttv3.IMqttActionListener;
Expand Down Expand Up @@ -101,19 +103,20 @@ public void stop() {

@Override
public void schedule(long delayInMilliseconds) {
long nextAlarmInMilliseconds = System.currentTimeMillis() + delayInMilliseconds;

long nextAlarmInMilliseconds = SystemClock.elapsedRealtime() + delayInMilliseconds;
Log.d(TAG, "Schedule next alarm at " + nextAlarmInMilliseconds);
AlarmManager alarmManager = (AlarmManager) service.getSystemService(Service.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 23) {
// In SDK 23 and above, dosing will prevent setExact, setExactAndAllowWhileIdle will force
// the device to run this task whilst dosing.
Log.d(TAG, "Alarm scheule using setExactAndAllowWhileIdle, next: " + delayInMilliseconds);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextAlarmInMilliseconds, pendingIntent);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
Log.d(TAG, "Alarm scheule using setExact, delay: " + delayInMilliseconds);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextAlarmInMilliseconds, pendingIntent);
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, nextAlarmInMilliseconds, pendingIntent);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextAlarmInMilliseconds, pendingIntent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.annotation.Nullable;
import android.util.Log;

import org.eclipse.paho.android.service.MessageStore.StoredMessage;
Expand Down Expand Up @@ -657,8 +658,12 @@ public IMqttDeliveryToken[] getPendingDeliveryTokens() {
* @param why the exeception causing the break in communications
*/
@Override
public void connectionLost(Throwable why) {
service.traceDebug(TAG, "connectionLost(" + why.getMessage() + ")");
public void connectionLost(@Nullable Throwable why) {
if (why != null) {
service.traceDebug(TAG, "connectionLost(" + why.getMessage() + ")");
} else {
service.traceDebug(TAG, "connectionLost(NO_REASON)");
}
disconnected = true;
try {
if (!this.connectOptions.isAutomaticReconnect()) {
Expand Down