Android Open Source - wonderpush-android-sdk Wonder Push Request Vault






From Project

Back to project page wonderpush-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project wonderpush-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wonderpush.sdk;
/*from w w w  .j a  v  a 2 s .co m*/
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.http.NoHttpResponseException;
import org.json.JSONException;

import android.util.Log;

import com.wonderpush.sdk.WonderPush.Response;

/**
 * This class will make sure important {@link WonderPushRestClient.Request} objects are run eventually, even if the user
 * is currently offline and the app terminated.
 */
class WonderPushRequestVault {

    private static final String TAG = WonderPush.TAG;

    private static WonderPushRequestVault sDefaultVault;

    protected static WonderPushRequestVault getDefaultVault() {
        return sDefaultVault;
    }

    /**
     * Start the default vault.
     */
    protected static void initialize() {
        if (null == sDefaultVault) {
            sDefaultVault = new WonderPushRequestVault(WonderPushJobQueue.getDefaultQueue());
        }
    }

    private WonderPushJobQueue mJobQueue;
    private Thread mThread;

    WonderPushRequestVault(WonderPushJobQueue jobQueue) {
        mJobQueue = jobQueue;
        mThread = new Thread(getRunnable());
        mThread.start();
    }

    /**
     * Save a request in the vault for future retry
     *
     * @param request
     * @throws JSONException
     */
    protected void put(WonderPushRestClient.Request request) throws JSONException {
        mJobQueue.postJobWithDescription(request.toJSON());
    }

    private Runnable getRunnable() {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        // Sleep for 10 seconds
                        Thread.sleep(1000 * 10);

                        // Blocks
                        final WonderPushJobQueue.Job job = mJobQueue.nextJob();

                        final WonderPushRestClient.Request request;
                        try {
                            request = new WonderPushRestClient.Request(job.getJobDescription());
                        } catch (JSONException e) {
                            WonderPush.logError("Could not restore request", e);
                            continue;
                        }

                        request.setHandler(new WonderPush.ResponseHandler() {
                            @Override
                            public void onFailure(Throwable e, Response errorResponse) {
                                // Post back to job queue if this is a network error
                                if (e instanceof NoHttpResponseException
                                        || e instanceof UnknownHostException
                                        || e instanceof SocketException) {
                                    job.repost();
                                    return;
                                }
                            }

                            @Override
                            public void onSuccess(Response response) {
                            }
                        });
                        WonderPushRestClient.requestAuthenticated(request);
                    }
                } catch (InterruptedException e) {
                    Log.i(TAG, "Vault interrupted", e);
                }

            }
        };
    }

}




Java Source Code List

com.wonderpush.sdk.QueryStringParser.java
com.wonderpush.sdk.WonderPushBroadcastReceiver.java
com.wonderpush.sdk.WonderPushCompatibilityHelper.java
com.wonderpush.sdk.WonderPushConfiguration.java
com.wonderpush.sdk.WonderPushDialogBuilder.java
com.wonderpush.sdk.WonderPushGcmClient.java
com.wonderpush.sdk.WonderPushInitializer.java
com.wonderpush.sdk.WonderPushJobQueue.java
com.wonderpush.sdk.WonderPushOnUpgradeReceiver.java
com.wonderpush.sdk.WonderPushRequestParamsDecorator.java
com.wonderpush.sdk.WonderPushRequestVault.java
com.wonderpush.sdk.WonderPushRestClient.java
com.wonderpush.sdk.WonderPushService.java
com.wonderpush.sdk.WonderPushUriHelper.java
com.wonderpush.sdk.WonderPushView.java
com.wonderpush.sdk.WonderPush.java
com.wonderpush.sdk.package-info.java