本文整理汇总了Java中com.google.firebase.tasks.Task类的典型用法代码示例。如果您正苦于以下问题:Java Task类的具体用法?Java Task怎么用?Java Task使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Task类属于com.google.firebase.tasks包,在下文中一共展示了Task类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deleteInstanceId
import com.google.firebase.tasks.Task; //导入依赖的package包/类
private Task<Void> deleteInstanceId(final String instanceId) {
checkArgument(!Strings.isNullOrEmpty(instanceId), "instance ID must not be null or empty");
return ImplFirebaseTrampolines.submitCallable(app, new Callable<Void>(){
@Override
public Void call() throws Exception {
String url = String.format(
"%s/project/%s/instanceId/%s", IID_SERVICE_URL, projectId, instanceId);
HttpRequest request = requestFactory.buildDeleteRequest(new GenericUrl(url));
request.setParser(new JsonObjectParser(jsonFactory));
request.setResponseInterceptor(interceptor);
HttpResponse response = null;
try {
response = request.execute();
ByteStreams.exhaust(response.getContent());
} catch (Exception e) {
handleError(instanceId, e);
} finally {
if (response != null) {
response.disconnect();
}
}
return null;
}
});
}
示例2: wrapOnComplete
import com.google.firebase.tasks.Task; //导入依赖的package包/类
public static Pair<Task<Void>, DatabaseReference.CompletionListener> wrapOnComplete(
DatabaseReference.CompletionListener optListener) {
if (optListener == null) {
final TaskCompletionSource<Void> source = new TaskCompletionSource<>();
DatabaseReference.CompletionListener listener =
new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference ref) {
if (error != null) {
source.setException(error.toException());
} else {
source.setResult(null);
}
}
};
return new Pair<>(source.getTask(), listener);
} else {
// If a listener is supplied we do not want to create a Task
return new Pair<>(null, optListener);
}
}
示例3: testAddListener
import com.google.firebase.tasks.Task; //导入依赖的package包/类
@Test
public void testAddListener() throws Exception {
Task<String> task = Tasks.forResult("test");
ApiFuture<String> future = new TaskToApiFuture<>(task);
final AtomicBoolean result = new AtomicBoolean(false);
future.addListener(new Runnable() {
@Override
public void run() {
result.set(true);
}
}, MoreExecutors.directExecutor());
assertEquals("test", future.get());
assertTrue(result.get());
assertFalse(future.isCancelled());
assertTrue(future.isDone());
}
示例4: setValueInternal
import com.google.firebase.tasks.Task; //导入依赖的package包/类
private Task<Void> setValueInternal(Object value, Node priority, CompletionListener optListener) {
Validation.validateWritablePath(getPath());
ValidationPath.validateWithObject(getPath(), value);
Object bouncedValue = CustomClassMapper.convertToPlainJavaTypes(value);
Validation.validateWritableObject(bouncedValue);
final Node node = NodeUtilities.NodeFromJSON(bouncedValue, priority);
final Pair<Task<Void>, CompletionListener> wrapped = Utilities.wrapOnComplete(optListener);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.setValue(getPath(), node, wrapped.getSecond());
}
});
return wrapped.getFirst();
}
示例5: updateChildrenInternal
import com.google.firebase.tasks.Task; //导入依赖的package包/类
private Task<Void> updateChildrenInternal(
final Map<String, Object> update, final CompletionListener optListener) {
if (update == null) {
throw new NullPointerException("Can't pass null for argument 'update' in updateChildren()");
}
final Map<String, Object> bouncedUpdate = CustomClassMapper.convertToPlainJavaTypes(update);
final Map<Path, Node> parsedUpdate =
Validation.parseAndValidateUpdate(getPath(), bouncedUpdate);
final CompoundWrite merge = CompoundWrite.fromPathMerge(parsedUpdate);
final Pair<Task<Void>, CompletionListener> wrapped = Utilities.wrapOnComplete(optListener);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.updateChildren(getPath(), merge, wrapped.getSecond(), bouncedUpdate);
}
});
return wrapped.getFirst();
}
示例6: onDisconnectSetInternal
import com.google.firebase.tasks.Task; //导入依赖的package包/类
private Task<Void> onDisconnectSetInternal(
Object value, Node priority, final CompletionListener optListener) {
Validation.validateWritablePath(path);
ValidationPath.validateWithObject(path, value);
Object bouncedValue = CustomClassMapper.convertToPlainJavaTypes(value);
Validation.validateWritableObject(bouncedValue);
final Node node = NodeUtilities.NodeFromJSON(bouncedValue, priority);
final Pair<Task<Void>, CompletionListener> wrapped = Utilities.wrapOnComplete(optListener);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.onDisconnectSetValue(path, node, wrapped.getSecond());
}
});
return wrapped.getFirst();
}
示例7: createCustomToken
import com.google.firebase.tasks.Task; //导入依赖的package包/类
/**
* Similar to {@link #createCustomTokenAsync(String, Map)}, but returns a {@link Task}.
*
* @param uid The UID to store in the token. This identifies the user to other Firebase services
* (Realtime Database, Storage, etc.). Should be less than 128 characters.
* @param developerClaims Additional claims to be stored in the token (and made available to
* security rules in Database, Storage, etc.). These must be able to be serialized to JSON
* (e.g. contain only Maps, Arrays, Strings, Booleans, Numbers, etc.)
* @return A {@link Task} which will complete successfully with the created Firebase Custom Token,
* or unsuccessfully with the failure Exception.
* @deprecated Use {@link #createCustomTokenAsync(String, Map)}
*/
public Task<String> createCustomToken(
final String uid, final Map<String, Object> developerClaims) {
checkNotDestroyed();
checkState(credentials instanceof ServiceAccountCredentials,
"Must initialize FirebaseApp with a service account credential to call "
+ "createCustomToken()");
final ServiceAccountCredentials serviceAccount = (ServiceAccountCredentials) credentials;
return call(new Callable<String>() {
@Override
public String call() throws Exception {
FirebaseTokenFactory tokenFactory = FirebaseTokenFactory.getInstance();
return tokenFactory.createSignedCustomAuthTokenForUser(
uid,
developerClaims,
serviceAccount.getClientEmail(),
serviceAccount.getPrivateKey());
}
});
}
示例8: verifyIdToken
import com.google.firebase.tasks.Task; //导入依赖的package包/类
/**
* Similar to {@link #verifyIdTokenAsync(String)}, but returns a {@link Task}.
*
* @param token A Firebase ID Token to verify and parse.
* @return A {@link Task} which will complete successfully with the parsed token, or
* unsuccessfully with the failure Exception.
* @deprecated Use {@link #verifyIdTokenAsync(String)}
*/
public Task<FirebaseToken> verifyIdToken(final String token) {
checkNotDestroyed();
checkState(!Strings.isNullOrEmpty(projectId),
"Must initialize FirebaseApp with a project ID to call verifyIdToken()");
return call(new Callable<FirebaseToken>() {
@Override
public FirebaseToken call() throws Exception {
FirebaseTokenVerifier firebaseTokenVerifier =
new FirebaseTokenVerifier.Builder()
.setProjectId(projectId)
.setPublicKeysManager(googlePublicKeysManager)
.setClock(clock)
.build();
FirebaseToken firebaseToken = FirebaseToken.parse(jsonFactory, token);
// This will throw a FirebaseAuthException with details on how the token is invalid.
firebaseTokenVerifier.verifyTokenAndSignature(firebaseToken.getToken());
return firebaseToken;
}
});
}
示例9: testWrapOnComplete
import com.google.firebase.tasks.Task; //导入依赖的package包/类
@Test
public void testWrapOnComplete() {
Pair<Task<Void>, DatabaseReference.CompletionListener> result =
Utilities.wrapOnComplete(null);
assertNotNull(result.getFirst());
assertNotNull(result.getSecond());
try {
result.getFirst().getResult();
fail("No error thrown for pending task");
} catch (Exception e) {
// expected
}
result.getSecond().onComplete(null, null);
result.getFirst().getResult();
}
示例10: testWrapOnCompleteErrorResult
import com.google.firebase.tasks.Task; //导入依赖的package包/类
@Test
public void testWrapOnCompleteErrorResult() {
Pair<Task<Void>, DatabaseReference.CompletionListener> result =
Utilities.wrapOnComplete(null);
assertNotNull(result.getFirst());
assertNotNull(result.getSecond());
try {
result.getFirst().getResult();
fail("No error thrown for pending task");
} catch (Exception e) {
// expected
}
result.getSecond().onComplete(DatabaseError.fromStatus("test error"), null);
assertNotNull(result.getFirst().getException());
}
示例11: createOptionsWithCustomFirebaseCredential
import com.google.firebase.tasks.Task; //导入依赖的package包/类
@Test
public void createOptionsWithCustomFirebaseCredential() throws IOException {
FirebaseOptions firebaseOptions =
new FirebaseOptions.Builder()
.setCredential(new FirebaseCredential() {
@Override
public Task<GoogleOAuthAccessToken> getAccessToken() {
return null;
}
})
.build();
assertNotNull(firebaseOptions.getJsonFactory());
assertNotNull(firebaseOptions.getHttpTransport());
assertNull(firebaseOptions.getDatabaseUrl());
assertNull(firebaseOptions.getStorageBucket());
GoogleCredentials credentials = firebaseOptions.getCredentials();
assertNotNull(credentials);
}
示例12: setPriorityInternal
import com.google.firebase.tasks.Task; //导入依赖的package包/类
private Task<Void> setPriorityInternal(final Node priority, CompletionListener optListener) {
Validation.validateWritablePath(getPath());
final Pair<Task<Void>, CompletionListener> wrapped = Utilities.wrapOnComplete(optListener);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.setValue(
getPath().child(ChildKey.getPriorityKey()), priority, wrapped.getSecond());
}
});
return wrapped.getFirst();
}
示例13: updateChildrenInternal
import com.google.firebase.tasks.Task; //导入依赖的package包/类
private Task<Void> updateChildrenInternal(
final Map<String, Object> update, final CompletionListener optListener) {
final Map<Path, Node> parsedUpdate = Validation.parseAndValidateUpdate(path, update);
final Pair<Task<Void>, CompletionListener> wrapped = Utilities.wrapOnComplete(optListener);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.onDisconnectUpdate(path, parsedUpdate, wrapped.getSecond(), update);
}
});
return wrapped.getFirst();
}
示例14: cancelInternal
import com.google.firebase.tasks.Task; //导入依赖的package包/类
private Task<Void> cancelInternal(final CompletionListener optListener) {
final Pair<Task<Void>, CompletionListener> wrapped = Utilities.wrapOnComplete(optListener);
repo.scheduleNow(
new Runnable() {
@Override
public void run() {
repo.onDisconnectCancel(path, wrapped.getSecond());
}
});
return wrapped.getFirst();
}
示例15: getAccessToken
import com.google.firebase.tasks.Task; //导入依赖的package包/类
@Override
public Task<GoogleOAuthAccessToken> getAccessToken() {
try {
AccessToken accessToken = googleCredentials.refreshAccessToken();
GoogleOAuthAccessToken googleToken = new GoogleOAuthAccessToken(accessToken.getTokenValue(),
accessToken.getExpirationTime().getTime());
return Tasks.forResult(googleToken);
} catch (Exception e) {
return Tasks.forException(e);
}
}