Search in sources :

Example 1 with OnFailureListener

use of com.google.android.gms.tasks.OnFailureListener in project FirebaseUI-Android by firebase.

the class WelcomeBackIdpPrompt method onSuccess.

@Override
public void onSuccess(final IdpResponse idpResponse) {
    if (idpResponse == null) {
        // do nothing
        return;
    }
    AuthCredential newCredential = AuthCredentialHelper.getAuthCredential(idpResponse);
    if (newCredential == null) {
        Log.e(TAG, "No credential returned");
        finish(ResultCodes.CANCELED, IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR));
        return;
    }
    FirebaseUser currentUser = mActivityHelper.getCurrentUser();
    if (currentUser == null) {
        mActivityHelper.getFirebaseAuth().signInWithCredential(newCredential).addOnSuccessListener(new OnSuccessListener<AuthResult>() {

            @Override
            public void onSuccess(AuthResult result) {
                if (mPrevCredential != null) {
                    result.getUser().linkWithCredential(mPrevCredential).addOnFailureListener(new TaskFailureLogger(TAG, "Error signing in with previous credential " + idpResponse.getProviderType())).addOnCompleteListener(new FinishListener(idpResponse));
                } else {
                    finish(ResultCodes.OK, IdpResponse.getIntent(idpResponse));
                }
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception e) {
                finishWithError();
            }
        }).addOnFailureListener(new TaskFailureLogger(TAG, "Error signing in with new credential " + idpResponse.getProviderType()));
    } else {
        currentUser.linkWithCredential(newCredential).addOnFailureListener(new TaskFailureLogger(TAG, "Error linking with credential " + idpResponse.getProviderType())).addOnCompleteListener(new FinishListener(idpResponse));
    }
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) TaskFailureLogger(com.firebase.ui.auth.ui.TaskFailureLogger) NonNull(android.support.annotation.NonNull) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 2 with OnFailureListener

use of com.google.android.gms.tasks.OnFailureListener in project FirebaseUI-Android by firebase.

the class CredentialSignInHandler method onComplete.

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
    if (task.isSuccessful()) {
        FirebaseUser firebaseUser = task.getResult().getUser();
        mHelper.saveCredentialsOrFinish(mSmartLock, mActivity, firebaseUser, mResponse);
    } else {
        if (task.getException() instanceof FirebaseAuthUserCollisionException) {
            final String email = mResponse.getEmail();
            if (email != null) {
                mHelper.getFirebaseAuth().fetchProvidersForEmail(email).addOnFailureListener(new TaskFailureLogger(TAG, "Error fetching providers for email")).addOnSuccessListener(new StartWelcomeBackFlow()).addOnFailureListener(new OnFailureListener() {

                    @Override
                    public void onFailure(@NonNull Exception e) {
                    // TODO: What to do when signing in with Credential fails
                    // and we can't continue to Welcome back flow without
                    // knowing providers?
                    }
                });
                return;
            }
        } else {
            Log.e(TAG, "Unexpected exception when signing in with credential " + mResponse.getProviderType() + " unsuccessful. Visit https://console.firebase.google.com to enable it.", task.getException());
        }
        mHelper.dismissDialog();
    }
}
Also used : TaskFailureLogger(com.firebase.ui.auth.ui.TaskFailureLogger) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUser(com.google.firebase.auth.FirebaseUser) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException)

Example 3 with OnFailureListener

use of com.google.android.gms.tasks.OnFailureListener in project quickstart-android by firebase.

the class MainActivity method onStart.

// [END handle_intent]
// [START app_indexing_view]
@Override
public void onStart() {
    super.onStart();
    if (articleId != null) {
        final Uri BASE_URL = Uri.parse("https://www.example.com/articles/");
        final String APP_URI = BASE_URL.buildUpon().appendPath(articleId).build().toString();
        Indexable articleToIndex = new Indexable.Builder().setName(TITLE).setUrl(APP_URI).build();
        Task<Void> task = FirebaseAppIndex.getInstance().update(articleToIndex);
        // If the Task is already complete, a call to the listener will be immediately
        // scheduled
        task.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Void>() {

            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "App Indexing API: Successfully added " + TITLE + " to index");
            }
        });
        task.addOnFailureListener(MainActivity.this, new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Log.e(TAG, "App Indexing API: Failed to add " + TITLE + " to index. " + exception.getMessage());
            }
        });
        // log the view action
        Task<Void> actionTask = FirebaseUserActions.getInstance().start(Actions.newView(TITLE, APP_URI));
        actionTask.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Void>() {

            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "App Indexing API: Successfully started view action on " + TITLE);
            }
        });
        actionTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Log.e(TAG, "App Indexing API: Failed to start view action on " + TITLE + ". " + exception.getMessage());
            }
        });
    }
}
Also used : Indexable(com.google.firebase.appindexing.Indexable) Uri(android.net.Uri) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 4 with OnFailureListener

use of com.google.android.gms.tasks.OnFailureListener in project quickstart-android by firebase.

the class MainActivity method onStop.

@Override
public void onStop() {
    super.onStop();
    if (articleId != null) {
        final Uri BASE_URL = Uri.parse("https://www.example.com/articles/");
        final String APP_URI = BASE_URL.buildUpon().appendPath(articleId).build().toString();
        Task<Void> actionTask = FirebaseUserActions.getInstance().end(Actions.newView(TITLE, APP_URI));
        actionTask.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Void>() {

            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "App Indexing API: Successfully ended view action on " + TITLE);
            }
        });
        actionTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Log.e(TAG, "App Indexing API: Failed to end view action on " + TITLE + ". " + exception.getMessage());
            }
        });
    }
}
Also used : Uri(android.net.Uri) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 5 with OnFailureListener

use of com.google.android.gms.tasks.OnFailureListener in project quickstart-android by firebase.

the class MyDownloadService method downloadFromPath.

private void downloadFromPath(final String downloadPath) {
    Log.d(TAG, "downloadFromPath:" + downloadPath);
    // Mark task started
    taskStarted();
    showProgressNotification(getString(R.string.progress_downloading), 0, 0);
    // Download and get total bytes
    mStorageRef.child(downloadPath).getStream(new StreamDownloadTask.StreamProcessor() {

        @Override
        public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot, InputStream inputStream) throws IOException {
            long totalBytes = taskSnapshot.getTotalByteCount();
            long bytesDownloaded = 0;
            byte[] buffer = new byte[1024];
            int size;
            while ((size = inputStream.read(buffer)) != -1) {
                bytesDownloaded += size;
                showProgressNotification(getString(R.string.progress_downloading), bytesDownloaded, totalBytes);
            }
            // Close the stream at the end of the Task
            inputStream.close();
        }
    }).addOnSuccessListener(new OnSuccessListener<StreamDownloadTask.TaskSnapshot>() {

        @Override
        public void onSuccess(StreamDownloadTask.TaskSnapshot taskSnapshot) {
            Log.d(TAG, "download:SUCCESS");
            // Send success broadcast with number of bytes downloaded
            broadcastDownloadFinished(downloadPath, taskSnapshot.getTotalByteCount());
            showDownloadFinishedNotification(downloadPath, (int) taskSnapshot.getTotalByteCount());
            // Mark task completed
            taskCompleted();
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.w(TAG, "download:FAILURE", exception);
            // Send failure broadcast
            broadcastDownloadFinished(downloadPath, -1);
            showDownloadFinishedNotification(downloadPath, -1);
            // Mark task completed
            taskCompleted();
        }
    });
}
Also used : InputStream(java.io.InputStream) StreamDownloadTask(com.google.firebase.storage.StreamDownloadTask) IOException(java.io.IOException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) IOException(java.io.IOException)

Aggregations

OnFailureListener (com.google.android.gms.tasks.OnFailureListener)23 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)14 StorageReference (com.google.firebase.storage.StorageReference)8 UploadTask (com.google.firebase.storage.UploadTask)6 NonNull (android.support.annotation.NonNull)5 TaskFailureLogger (com.firebase.ui.auth.ui.TaskFailureLogger)5 FirebaseUser (com.google.firebase.auth.FirebaseUser)5 File (java.io.File)5 Bitmap (android.graphics.Bitmap)4 AuthResult (com.google.firebase.auth.AuthResult)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Uri (android.net.Uri)3 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)3 FileDownloadTask (com.google.firebase.storage.FileDownloadTask)3 StorageMetadata (com.google.firebase.storage.StorageMetadata)3 HashMap (java.util.HashMap)3 IdpResponse (com.firebase.ui.auth.IdpResponse)2 AuthCredential (com.google.firebase.auth.AuthCredential)2 FirebaseAuthWeakPasswordException (com.google.firebase.auth.FirebaseAuthWeakPasswordException)2 IOException (java.io.IOException)2