com.google.android.gms.gcm.TaskParams Java Examples

The following examples show how to use com.google.android.gms.gcm.TaskParams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskService.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onRunTask(TaskParams taskParams) {
    Log.d(TAG, "onRunTask: " + taskParams.getTag());

    String tag = taskParams.getTag();

    // Default result is success.
    int result = GcmNetworkManager.RESULT_SUCCESS;

    // Choose method based on the tag.
    if (GcmActivity.TASK_TAG_UNMETERED.equals(tag)) {
        result = doUnmeteredTask();
    } else if (GcmActivity.TASK_TAG_CHARGING.equals(tag)) {
        result = doChargingTask();
    }
    // Return one of RESULT_SUCCESS, RESULT_FAILURE, or RESULT_RESCHEDULE
    return result;
}
 
Example #2
Source File: TaskService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onRunTask(TaskParams taskParams) {
    Log.d(TAG, "onRunTask: " + taskParams.getTag());

    String tag = taskParams.getTag();

    // Default result is success.
    int result = GcmNetworkManager.RESULT_SUCCESS;

    // Choose method based on the tag.
    if (GcmActivity.TASK_TAG_UNMETERED.equals(tag)) {
        result = doUnmeteredTask();
    } else if (GcmActivity.TASK_TAG_CHARGING.equals(tag)) {
        result = doChargingTask();
    }
    // Return one of RESULT_SUCCESS, RESULT_FAILURE, or RESULT_RESCHEDULE
    return result;
}
 
Example #3
Source File: TaskSchedulerService.java    From gcm with Apache License 2.0 6 votes vote down vote up
@Override
public int onRunTask(TaskParams params) {
    String tag = params.getTag();
    TaskCollection tasks = TaskCollection.getInstance(this);
    TaskTracker task = tasks.getTask(tag);
    if (task != null) {
        task.execute(mLogger);
        tasks.updateTask(task);
    } else {
        mLogger.log(Log.ERROR, "Could not find task with tag " + tag);
        task = TaskTracker.emptyTaskWithTag(tag);
        task.execute(mLogger);
        tasks.updateTask(task);
    }
    return GcmNetworkManager.RESULT_SUCCESS;

}
 
Example #4
Source File: PlatformGcmService.java    From android-job with Apache License 2.0 6 votes vote down vote up
@Override
public int onRunTask(TaskParams taskParams) {
    int jobId = Integer.parseInt(taskParams.getTag());
    JobProxy.Common common = new JobProxy.Common(this, CAT, jobId);

    JobRequest request = common.getPendingRequest(true, true);
    if (request == null) {
        return GcmNetworkManager.RESULT_FAILURE;
    }

    Job.Result result = common.executeJobRequest(request, taskParams.getExtras());
    if (Job.Result.SUCCESS.equals(result)) {
        return GcmNetworkManager.RESULT_SUCCESS;
    } else {
        return GcmNetworkManager.RESULT_FAILURE;
    }
}
 
Example #5
Source File: BackgroundTaskSchedulerGcmNetworkManager.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the {@link TaskParameters} from the {@link TaskParams}, which are passed as
 * one of the keys. Only values valid for {@link android.os.BaseBundle} are supported, and other
 * values are stripped at the time when the task is scheduled.
 *
 * @param taskParams the {@link TaskParams} to extract the {@link TaskParameters} from.
 * @return the {@link TaskParameters} for the current job.
 */
static TaskParameters getTaskParametersFromTaskParams(@NonNull TaskParams taskParams) {
    int taskId;
    try {
        taskId = Integer.parseInt(taskParams.getTag());
    } catch (NumberFormatException e) {
        Log.e(TAG, "Cound not parse task ID from task tag: " + taskParams.getTag());
        return null;
    }

    TaskParameters.Builder builder = TaskParameters.create(taskId);

    Bundle extras = taskParams.getExtras();
    Bundle taskExtras = extras.getBundle(BACKGROUND_TASK_EXTRAS_KEY);
    builder.addExtras(taskExtras);

    return builder.build();
}
 
Example #6
Source File: MyTaskService.java    From android-gcmnetworkmanager with Apache License 2.0 5 votes vote down vote up
@Override
public int onRunTask(TaskParams taskParams) {
    Log.d(TAG, "onRunTask: " + taskParams.getTag());

    String tag = taskParams.getTag();

    // Default result is success.
    int result = GcmNetworkManager.RESULT_SUCCESS;

    // Choose method based on the tag.
    if (MainActivity.TASK_TAG_WIFI.equals(tag)) {
        result = doWifiTask();
    } else if (MainActivity.TASK_TAG_CHARGING.equals(tag)) {
        result = doChargingTask();
    } else if (MainActivity.TASK_TAG_PERIODIC.equals(tag)) {
        result = doPeriodicTask();
    }

    // Create Intent to broadcast the task information.
    Intent intent = new Intent();
    intent.setAction(ACTION_DONE);
    intent.putExtra(EXTRA_TAG, tag);
    intent.putExtra(EXTRA_RESULT, result);

    // Send local broadcast, running Activities will be notified about the task.
    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
    manager.sendBroadcast(intent);

    // Return one of RESULT_SUCCESS, RESULT_FAILURE, or RESULT_RESCHEDULE
    return result;
}
 
Example #7
Source File: NetworkGCMTaskService.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
@Override
public int onRunTask(TaskParams taskParams) {
    if (AppSession.ChatState.FOREGROUND == AppSession.getSession().getChatState()) {
        QBLoginChatCompositeCommand.start(this);
        return GcmNetworkManager.RESULT_SUCCESS;
    } else {
        return GcmNetworkManager.RESULT_RESCHEDULE;
    }
}
 
Example #8
Source File: ChromeBackgroundService.java    From delion with Apache License 2.0 4 votes vote down vote up
@Override
@VisibleForTesting
public int onRunTask(final TaskParams params) {
    final String taskTag = params.getTag();
    Log.i(TAG, "[" + taskTag + "] Woken up at " + new java.util.Date().toString());
    final ChromeBackgroundServiceWaiter waiter = getWaiterIfNeeded(params.getExtras());
    final Context context = this;
    ThreadUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            switch (taskTag) {
                case BackgroundSyncLauncher.TASK_TAG:
                    handleBackgroundSyncEvent(context, taskTag);
                    break;

                case OfflinePageUtils.TASK_TAG:
                    handleOfflinePageBackgroundLoad(
                            context, params.getExtras(), waiter, taskTag);
                    break;

                case SnippetsLauncher.TASK_TAG_WIFI_CHARGING:
                case SnippetsLauncher.TASK_TAG_WIFI:
                case SnippetsLauncher.TASK_TAG_FALLBACK:
                    handleFetchSnippets(context, taskTag);
                    break;

                case SnippetsLauncher.TASK_TAG_RESCHEDULE:
                    handleRescheduleSnippets(context, taskTag);
                    break;

                case PrecacheController.PERIODIC_TASK_TAG:
                case PrecacheController.CONTINUATION_TASK_TAG:
                    handlePrecache(context, taskTag);
                    break;

                case DownloadResumptionScheduler.TASK_TAG:
                    DownloadResumptionScheduler.getDownloadResumptionScheduler(
                            context.getApplicationContext()).handleDownloadResumption();
                    break;

                default:
                    Log.i(TAG, "Unknown task tag " + taskTag);
                    break;
            }
        }
    });
    // If needed, block the GcmNetworkManager thread until the UI thread has finished its work.
    waitForTaskIfNeeded(waiter);

    return GcmNetworkManager.RESULT_SUCCESS;
}
 
Example #9
Source File: ChromeBackgroundService.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
@VisibleForTesting
public int onRunTask(final TaskParams params) {
    final String taskTag = params.getTag();
    Log.i(TAG, "[" + taskTag + "] Woken up at " + new java.util.Date().toString());
    final ChromeBackgroundServiceWaiter waiter = getWaiterIfNeeded(params.getExtras());
    final Context context = this;
    ThreadUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            switch (taskTag) {
                case BackgroundSyncLauncher.TASK_TAG:
                    handleBackgroundSyncEvent(context, taskTag);
                    break;

                case OfflinePageUtils.TASK_TAG:
                    handleOfflinePageBackgroundLoad(
                            context, params.getExtras(), waiter, taskTag);
                    break;

                case SnippetsLauncher.TASK_TAG_WIFI:
                case SnippetsLauncher.TASK_TAG_FALLBACK:
                    handleFetchSnippets(context, taskTag);
                    break;

                case PrecacheController.PERIODIC_TASK_TAG:
                case PrecacheController.CONTINUATION_TASK_TAG:
                    handlePrecache(context, taskTag);
                    break;

                case DownloadResumptionScheduler.TASK_TAG:
                    DownloadResumptionScheduler.getDownloadResumptionScheduler(
                            context.getApplicationContext()).handleDownloadResumption();
                    break;

                default:
                    Log.i(TAG, "Unknown task tag " + taskTag);
                    break;
            }
        }
    });
    // If needed, block the GcmNetworkManager thread until the UI thread has finished its work.
    waitForTaskIfNeeded(waiter);

    return GcmNetworkManager.RESULT_SUCCESS;
}
 
Example #10
Source File: ChromeBackgroundService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
@VisibleForTesting
public int onRunTask(final TaskParams params) {
    final String taskTag = params.getTag();
    Log.i(TAG, "[" + taskTag + "] Woken up at " + new java.util.Date().toString());
    final ChromeBackgroundServiceWaiter waiter = getWaiterIfNeeded(params.getExtras());
    final Context context = this;
    ThreadUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            switch (taskTag) {
                case BackgroundSyncLauncher.TASK_TAG:
                    handleBackgroundSyncEvent(context, taskTag);
                    break;

                case OfflinePageUtils.TASK_TAG:
                    handleOfflinePageBackgroundLoad(
                            context, params.getExtras(), waiter, taskTag);
                    break;

                case SnippetsLauncher.TASK_TAG_WIFI:
                case SnippetsLauncher.TASK_TAG_FALLBACK:
                    handleFetchSnippets(context, taskTag);
                    break;

                case PrecacheController.PERIODIC_TASK_TAG:
                case PrecacheController.CONTINUATION_TASK_TAG:
                    handlePrecache(context, taskTag);
                    break;

                case DownloadResumptionScheduler.TASK_TAG:
                    DownloadResumptionScheduler.getDownloadResumptionScheduler(
                            context.getApplicationContext()).handleDownloadResumption();
                    break;

                default:
                    Log.i(TAG, "Unknown task tag " + taskTag);
                    break;
            }
        }
    });
    // If needed, block the GcmNetworkManager thread until the UI thread has finished its work.
    waitForTaskIfNeeded(waiter);

    return GcmNetworkManager.RESULT_SUCCESS;
}
 
Example #11
Source File: BackgroundTaskGcmTaskService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public int onRunTask(TaskParams params) {
    final BackgroundTask backgroundTask =
            BackgroundTaskSchedulerGcmNetworkManager.getBackgroundTaskFromTaskParams(params);
    if (backgroundTask == null) {
        Log.w(TAG, "Failed to start task. Could not instantiate class.");
        return GcmNetworkManager.RESULT_FAILURE;
    }

    final TaskParameters taskParams =
            BackgroundTaskSchedulerGcmNetworkManager.getTaskParametersFromTaskParams(params);
    final Waiter waiter = new Waiter(Waiter.MAX_TIMEOUT_SECONDS);

    final AtomicBoolean taskNeedsBackgroundProcessing = new AtomicBoolean();
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            taskNeedsBackgroundProcessing.set(
                    backgroundTask.onStartTask(ContextUtils.getApplicationContext(), taskParams,
                            new TaskFinishedCallbackGcmTaskService(waiter)));
        }
    });

    if (!taskNeedsBackgroundProcessing.get()) return GcmNetworkManager.RESULT_SUCCESS;

    waiter.startWaiting();

    if (waiter.isRescheduleNeeded()) return GcmNetworkManager.RESULT_RESCHEDULE;
    if (!waiter.hasTaskTimedOut()) return GcmNetworkManager.RESULT_SUCCESS;

    final AtomicBoolean taskNeedsRescheduling = new AtomicBoolean();
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            taskNeedsRescheduling.set(backgroundTask.onStopTask(
                    ContextUtils.getApplicationContext(), taskParams));
        }
    });

    if (taskNeedsRescheduling.get()) return GcmNetworkManager.RESULT_RESCHEDULE;

    return GcmNetworkManager.RESULT_SUCCESS;
}
 
Example #12
Source File: BackgroundTaskSchedulerGcmNetworkManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
static BackgroundTask getBackgroundTaskFromTaskParams(@NonNull TaskParams taskParams) {
    String backgroundTaskClassName = getBackgroundTaskClassFromTaskParams(taskParams);
    return BackgroundTaskScheduler.getBackgroundTaskFromClassName(backgroundTaskClassName);
}
 
Example #13
Source File: BackgroundTaskSchedulerGcmNetworkManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private static String getBackgroundTaskClassFromTaskParams(@NonNull TaskParams taskParams) {
    Bundle extras = taskParams.getExtras();
    if (extras == null) return null;
    return extras.getString(BACKGROUND_TASK_CLASS_KEY);
}