use of com.codename1.background.BackgroundFetch in project CodenameOne by codenameone.
the class AndroidImplementation method performBackgroundFetch.
/**
* Calls the background fetch callback. If the app is in teh background, this will
* check to see if the lifecycle class implements the {@link com.codename1.background.BackgroundFetch}
* interface. If it does, it will execute its {@link com.codename1.background.BackgroundFetch#performBackgroundFetch(long, com.codename1.util.Callback) }
* method.
* @param blocking True if this should block until it is complete.
*/
public static void performBackgroundFetch(boolean blocking) {
if (Display.getInstance().isMinimized()) {
// By definition, background fetch should only occur if the app is minimized.
// This keeps it consistent with the iOS implementation that doesn't have a
// choice
final boolean[] complete = new boolean[1];
final Object lock = new Object();
final BackgroundFetch bgFetchListener = instance.getBackgroundFetchListener();
final long timeout = System.currentTimeMillis() + 25000;
if (bgFetchListener != null) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
bgFetchListener.performBackgroundFetch(timeout, new Callback<Boolean>() {
@Override
public void onSucess(Boolean value) {
// So we'll just consume this.
synchronized (lock) {
complete[0] = true;
lock.notify();
}
}
@Override
public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
com.codename1.io.Log.e(err);
synchronized (lock) {
complete[0] = true;
lock.notify();
}
}
});
}
});
}
while (blocking && !complete[0]) {
synchronized (lock) {
try {
lock.wait(1000);
} catch (Exception ex) {
}
}
if (!complete[0]) {
System.out.println("Waiting for background fetch to complete. Make sure your background fetch handler calls onSuccess() or onError() in the callback when complete");
}
if (System.currentTimeMillis() > timeout) {
System.out.println("Background fetch exceeded time alotted. Not waiting for its completion");
break;
}
}
}
}
use of com.codename1.background.BackgroundFetch in project CodenameOne by codenameone.
the class BackgroundFetchHandler method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
String[] params = intent.getDataString().split("[?]");
if (!Display.isInitialized()) {
shouldStopContext = true;
AndroidImplementation.startContext(this);
try {
Class cls = Class.forName(params[1]);
BackgroundFetch obj = (BackgroundFetch) cls.newInstance();
AndroidImplementation.backgroundFetchListener = obj;
} catch (Exception ex) {
Log.d("CN1", "Failed to instantiate background fetch class " + params[1]);
ex.printStackTrace();
return;
}
}
try {
AndroidImplementation.performBackgroundFetch(shouldStopContext);
} catch (Exception e) {
Log.e("Codename One", "background fetch error", e);
}
if (shouldStopContext) {
AndroidImplementation.stopContext(this);
}
}
use of com.codename1.background.BackgroundFetch in project CodenameOne by codenameone.
the class LocalNotificationPublisher method onReceive.
public void onReceive(Context context, Intent intent) {
// Fire the notification to the display
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Bundle extras = intent.getExtras();
PendingIntent content = extras.getParcelable(NOTIFICATION_INTENT);
Bundle b = extras.getParcelable(NOTIFICATION);
LocalNotification notif = AndroidImplementation.createNotificationFromBundle(b);
if (AndroidImplementation.BACKGROUND_FETCH_NOTIFICATION_ID.equals(notif.getId())) {
PendingIntent backgroundFetchIntent = extras.getParcelable(BACKGROUND_FETCH_INTENT);
if (backgroundFetchIntent != null) {
try {
backgroundFetchIntent.send();
} catch (Exception ex) {
Log.e("Codename One", "Failed to send BackgroundFetchHandler intent", ex);
}
} else {
Log.d("Codename One", "BackgroundFetch intent was null");
}
} else {
Notification notification = createAndroidNotification(context, notif, content);
notification.when = System.currentTimeMillis();
try {
int notifId = Integer.parseInt(notif.getId());
notificationManager.notify("CN1", notifId, notification);
} catch (Exception e) {
// that was a mistake, the first param is the tag not the id
notificationManager.notify(notif.getId(), 0, notification);
}
}
}
Aggregations