Developer Console

SDK for Android Best Practices

Best practices for using the Android SDK

AmazonCloudDriveClient Instance

The AmazonCloudDriveClient caches endpoint information that is specific to the user who is logged in. Reusing an instance results in faster responses, because the client is able to send fewer requests to Amazon Drive. If your application requires access an AmazonCloudDriveClient instance more than once during the lifespan of the process, then make the instance global.

/**
 * Holds global user state. State should be cleared on log out.
 */
public class UserState {

    // The LWA authorization manager
    private static AmazonAuthorizationManager sAmazonAuthorizationManager;

    // The Amazon Drive client. We prefer to use the same instance for
    // all places in the application because the client caches the endpoints.
    // Creating a new instance causes the cache to be cleared.
    private static AmazonCloudDriveClient sAmazonCloudDriveClient;

    /**
     * Get the global instance of the AmazonAuthorizationManager.
     * @param context an application Context
     * @return the auth manager
     */
    public static synchronized AmazonAuthorizationManager getAmazonAuthorizationManagerInstance(Context context) {
        if (sAmazonAuthorizationManager == null) {
            sAmazonAuthorizationManager = new AmazonAuthorizationManager(context.getApplicationContext(), Bundle.EMPTY);
        }

        return sAmazonAuthorizationManager;
    }

    /**
     * Gets the global instance of the AmazonCloudDriveClient
     * @param context an application Context
     * @return the client
     */
    public static synchronized AmazonCloudDriveClient getAmazonCloudDriveClientInstance(Context context) {
        if (sAmazonCloudDriveClient == null) {
            sAmazonCloudDriveClient = new AmazonCloudDriveClient(
                new AccountConfiguration(
                        new AmazonAuthorizationConnectionFactory(
                                getAmazonAuthorizationManagerInstance(context),
                                Constants.APP_AUTHORIZATION_SCOPES)),
                new ClientConfiguration(Constants.USER_AGENT));
        }

        return sAmazonCloudDriveClient;
    }

    /**
     * Clears the global user state.
     */
    public static synchronized void reset() {
        sAmazonAuthorizationManager = null;
        sAmazonCloudDriveClient = null;
    }
}

Protecting the User's Content

Customers trust Amazon Drive to protect the metadata and binary content that is stored. Similarly, applications that connect to Amazon Drive must maintain that trust. Metadata and binary content from the Amazon Drive should be stored in the internal application partition unless explicit permission has been provided by the user to store it in another location.

Dos

The following is a list of practices to follow in your app.

  • Write all of the metadata and binary data to the internal storage directory assigned to your app.
  • Clear persisted memory and the memory state that is related to the user when the user signs out of your app.
  • Use the same instance of AmazonCloudDriveClient object while the user is signed in your app.

Do Nots

The following is a list of practices to avoid in your app.

  • Do not make ContentProviders exported if they provide Amazon Drive metadata or binary data.

Support

If you have any questions, see the Developer Forum.