Java tutorial
//package com.java2s; /* * Copyright 2013-2016 Amazon.com, * Inc. or its affiliates. All Rights Reserved. * * Licensed under the Amazon Software License (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/asl/ * * or in the "license" file accompanying this file. This file is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, express or implied. See the License * for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.content.SharedPreferences; import android.util.Log; public class Main { final static private String TAG = "CognitoDeviceHelper"; final static private String COGNITO_DEVICE_CACHE = "CognitoIdentityProviderDeviceCache"; final static private String COGNITO_DEVICE_SECRET = "DeviceSecret"; /** * Returns the cached device secret for this device. Device secret is generated when the device * is confirmed and is used for device identification. * * @param username REQUIRED: The current user. * @param userPoolId REQUIRED: Client ID of the application. * @param context REQUIRED: Application context. * @return device secret as String, null if the device-key is not available. */ public static String getDeviceSecret(String username, String userPoolId, Context context) { try { SharedPreferences cipCachedDeviceDetails = context .getSharedPreferences(getDeviceDetailsCacheForUser(username, userPoolId), 0); if (cipCachedDeviceDetails != null && cipCachedDeviceDetails.contains(COGNITO_DEVICE_SECRET)) { return cipCachedDeviceDetails.getString(COGNITO_DEVICE_SECRET, null); } } catch (Exception e) { Log.e(TAG, "Error accessing SharedPreferences" + e.getMessage()); } return null; } /** * Generates and returns the key to access device details from shared preferences. * * @param username REQUIRED: The current user. * @param userPoolId REQUIRED: Client ID of the device. * @return a string which is a key to access the device key from SharedPreferences. */ private static String getDeviceDetailsCacheForUser(String username, String userPoolId) { return COGNITO_DEVICE_CACHE + "." + userPoolId + "." + username; } }