AutoResolveHelper

public class AutoResolveHelper extends Object

Helper to auto resolve Tasks that may throw ResolvableApiException to request UI being shown before returning the result.

By calling resolveTask(Task, Activity, int) with your task, this helper will make sure that it shows any UI if necessary and at the end will return the result back to your activity's onActivityResult(int, int, Intent).

This is useful for handling Google Play Services APIs that may have to show UI to the user before returning their results back. Note that only APIs whose results implement the AutoResolvableResult are compatible with this helper.

Below is an example of an hypothetical loadFoo API that returns a FooResult but that may require the user to consent to sharing the resultData in FooResult the first time before returning it to you.

   public static class SampleActivity extends Activity {
     private static final int REQUEST_CODE_FOO = 1234;

     @Override
     public onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       if (savedInstanceState == null) {
         // Loading Foo
         FooClient fooClient = Foo.getFooClient(...);
         Task fooTask = fooClient.loadFoo();
         // Asking AutoResolveHelper to take care of resolving
         // any ResolvableApiExceptions, showing UI if necessary
         // and just piping FooResult to onActivityResult.
         AutoResolveHelper.resolveTask(fooTask, this, REQUEST_CODE_FOO);
       }
     }

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
       switch (requestCode) {
         case FOO_REQUEST_CODE:
           if (resultCode == Activity.RESULT_OK) {
             FooResult fooResult = FooResult.getFromIntent(resultData);
             // Result loaded, use it ...
           } else if (resultCode == Activity.RESULT_CANCELLED) {
             // user cancelled ...
           } else if (resultCode == AutoResolveHelper.RESULT_ERROR) {
             // there was an error, handle it or ignore ...
             int errorCode = AutoResolvableHelper.getStatusFromIntent(resultData);
             // handle the error, log, ...
           }
           break;
         default:
           super.onActivityResult(requestCode, resultCode, resultData);
       }
     }
   }
 
    Notes from the example above:
  • You will receive a response back with requestCode matching the one you originally passed in the call to resolveTask(Task, Activity, int).
  • You will receive resultCode equal to RESULT_OK when the call has been successful and you should be able to retrieve the actual result object from the resultData param. By convention result classes that can be used with this helper should have a getFromIntent(Intent) method that you can use to retrieve the result object.
  • You will receive resultCode equal to RESULT_CANCELED when the user has cancelled the UI that this helper has shown.
  • You will receive resultCode equal to RESULT_ERROR when an error occurs resolving the result of the given Task. In that case, you may call getStatusFromIntent(Intent) passing resultData param to get the Status with the details about the error.

Constant Summary

int RESULT_ERROR The result code that an Activity passed to resolveTask(Task, Activity, int) will receive in case an error happend.

Public Method Summary

static Status
static void
putStatusIntoIntent(Intent data, Status status)
Saves the given Status as an extra in the given Intent.
static <TResult extends AutoResolvableResult> void
resolveTask(Task<TResult> task, Activity activity, int requestCode)
Resolves the given task result showing UI if necessary and pipes back the final result back to the given activity's onActivityResult(int, int, Intent) callback.

Inherited Method Summary

Constants

public static final int RESULT_ERROR

The result code that an Activity passed to resolveTask(Task, Activity, int) will receive in case an error happend.

You can use getStatusFromIntent(Intent) to retrieve the actual Status for the error.

Constant Value: 1

Public Methods

public static Status getStatusFromIntent (Intent data)

Returns the Status from the Intent received in onActivityResult(int, int, Intent).

This method is expected to be called from onActivityResult(int, int, Intent) of activities passed to resolveTask(Task, Activity, int).

Note this method will return null if resultCode was not set to RESULT_ERROR.

public static void putStatusIntoIntent (Intent data, Status status)

Saves the given Status as an extra in the given Intent.

The saved Status can be read back by calling getStatusFromIntent(Intent).

public static void resolveTask (Task<TResult> task, Activity activity, int requestCode)

Resolves the given task result showing UI if necessary and pipes back the final result back to the given activity's onActivityResult(int, int, Intent) callback.

Note that this method adds a Fragment to your activity, so only call this method if FragmentManager is in a state where it can execute fragment transactions without state loss. The added fragment will be auto removed after the task result is delivered to your activity.