If you think the Android project 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
/*
* Copyright (C) 2014. BaasBox//www.java2s.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/package com.baasbox.android;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
/**
* A handle to an asynchronous request.
*
* @author Andrea Tortorella
* @since 0.7.3
*/publicfinalclass RequestToken implements Parcelable, Comparable<RequestToken> {
// ------------------------------ FIELDS ------------------------------
publicstatic Creator<RequestToken> CREATOR =
new Creator<RequestToken>() {
@Override
public RequestToken createFromParcel(Parcel source) {
returnnew RequestToken(source.readInt());
}
@Override
public RequestToken[] newArray(int size) {
returnnew RequestToken[size];
}
};
finalint requestId;
// --------------------------- CONSTRUCTORS ---------------------------
RequestToken(int requestId) {
this.requestId = requestId;
}
// -------------------------- STATIC METHODS --------------------------
/**
* Loads a request token from the bundle
* and immediately tries to resume the request with handler
*
* @param bundle a non null bundle
* @param name the key of the saved token
* @param handler a handler to resume the request with.
* @return the token if resumed, null otherwise
*/publicstatic RequestToken loadAndResume(Bundle bundle, String name, BaasHandler<?> handler) {
if (bundle == null)
thrownew IllegalArgumentException("bunlde cannot be null");
if (name == null)
thrownew IllegalArgumentException("name cannot be null");
RequestToken token = bundle.getParcelable(name);
if (token != null && token.resume(handler)) {
return token;
}
return null;
}
/**
* Resumes a suspended asynchronous request, with the new
* provided handler
*
* @param handler a handler to resume the request with.
* @return
*/publicboolean resume(BaasHandler<?> handler) {
return BaasBox.getDefaultChecked().resume(this, handler==null?BaasHandler.NOOP:handler);
}
// ------------------------ CANONICAL METHODS ------------------------
@Override
publicboolean equals(Object o) {
if (o == this) return true;
if (o instanceof RequestToken) {
return ((RequestToken) o).requestId == requestId;
}
return false;
}
@Override
publicint hashCode() {
return requestId;
}
// ------------------------ INTERFACE METHODS ------------------------
// --------------------- Interface Comparable ---------------------
@Override
publicint compareTo(RequestToken another) {
return this.requestId - another.requestId;
}
// --------------------- Interface Parcelable ---------------------
@Override
publicint describeContents() {
return 0;
}
@Override
publicvoid writeToParcel(Parcel dest, int flags) {
dest.writeInt(requestId);
}
// -------------------------- OTHER METHODS --------------------------
publicboolean abort() {
return BaasBox.getDefaultChecked().abort(this);
}
public <R> BaasResult<R> await() {
return BaasBox.getDefaultChecked().await(this);
}
publicboolean cancel() {
return BaasBox.getDefaultChecked().cancel(this);
}
/**
* Suspends a request and immediately save it in a bundle
*
* @param bundle a non null bundle
* @param name the key to save the token with
* @return true if the token was suspended
*/publicboolean suspendAndSave(Bundle bundle, String name) {
if (bundle == null)
thrownew IllegalArgumentException("bunlde cannot be null");
if (name == null)
thrownew IllegalArgumentException("name cannot be null");
if (suspend()) {
bundle.putParcelable(name, this);
return true;
} else {
return false;
}
}
/**
* Tries to suspend the asynchronous request identified
* by this token.
* Suspended requests are executed normally but their associated
* callback is cleared.
*
* @return true if the attempt was successful
*/publicboolean suspend() {
return BaasBox.getDefaultChecked().suspend(this);
}
}