com.acer.batterycapacitydemo.CognitoSyncClientManager.java Source code

Java tutorial

Introduction

Here is the source code for com.acer.batterycapacitydemo.CognitoSyncClientManager.java

Source

/**
 * Copyright 2010-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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.acer.batterycapacitydemo;

import android.content.Context;
import android.util.Log;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSAbstractCognitoIdentityProvider;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.auth.CognitoCredentialsProvider;
import com.amazonaws.mobileconnectors.cognito.CognitoSyncManager;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;

import java.util.HashMap;
import java.util.Map;

public class CognitoSyncClientManager {

    private static final String TAG = "CognitoClientManager";
    private static final String LOG_TAG = "AmazonClientManager";
    public static final String IDENTITY_POOL_ID = "us-west-2:4c463cde-1528-4573-90b3-83a5749a971b";
    public static final String TEST_TABLE_NAME = "BatteryTBL";
    public static final Regions myAWS_REGION = Regions.US_WEST_2;
    private static Context context;

    private static CognitoSyncManager syncClient = null;
    protected static CognitoCachingCredentialsProvider credentialsProvider = null;
    private static AmazonDynamoDBClient ddb = null;

    public CognitoSyncClientManager(Context context) {
        this.context = context;
    }

    public AmazonDynamoDBClient ddb() {
        validateCredentials();
        return ddb;
    }

    public void validateCredentials() {

        if (ddb == null) {
            initClients();
        }
    }

    public static void initClients() {

        if (syncClient != null)
            return;

        credentialsProvider = new CognitoCachingCredentialsProvider(context, IDENTITY_POOL_ID, myAWS_REGION);

        syncClient = new CognitoSyncManager(context, myAWS_REGION, credentialsProvider);
        ddb = new AmazonDynamoDBClient(credentialsProvider);
        ddb.setRegion(Region.getRegion(myAWS_REGION));

    }

    public static void addLogins(String providerName, String token) {
        if (syncClient == null) {
            throw new IllegalStateException("CognitoSyncClientManager not initialized yet");
        }

        Map<String, String> logins = credentialsProvider.getLogins();
        if (logins == null) {
            logins = new HashMap<String, String>();
        }
        logins.put(providerName, token);
        credentialsProvider.setLogins(logins);

    }

    public static CognitoSyncManager getInstance() {
        if (syncClient == null) {
            throw new IllegalStateException("CognitoSyncClientManager not initialized yet");
        }
        return syncClient;
    }

    public static CognitoCredentialsProvider getCredentialsProvider() {
        return credentialsProvider;
    }

    public boolean wipeCredentialsOnAuthError(AmazonServiceException ex) {
        Log.e(LOG_TAG, "Error, wipeCredentialsOnAuthError called" + ex);
        if (
        // STS
        // http://docs.amazonwebservices.com/STS/latest/APIReference/CommonErrors.html
        ex.getErrorCode().equals("IncompleteSignature") || ex.getErrorCode().equals("InternalFailure")
                || ex.getErrorCode().equals("InvalidClientTokenId") || ex.getErrorCode().equals("OptInRequired")
                || ex.getErrorCode().equals("RequestExpired") || ex.getErrorCode().equals("ServiceUnavailable")

                // DynamoDB
                // http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/ErrorHandling.html#APIErrorTypes
                || ex.getErrorCode().equals("AccessDeniedException")
                || ex.getErrorCode().equals("IncompleteSignatureException")
                || ex.getErrorCode().equals("MissingAuthenticationTokenException")
                || ex.getErrorCode().equals("ValidationException") || ex.getErrorCode().equals("InternalFailure")
                || ex.getErrorCode().equals("InternalServerError")) {

            return true;
        }

        return false;
    }
}