Active IQ v6.7.1版本的 MD5 值为:5082594ca2b69fbfbe64e6d6ee2159a7

以下内容为反编译后的 TokenManager.java 源代码,内容仅作参考


package com.willblaschko.android.alexa;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import com.willblaschko.android.alexa.connection.ClientUtil;
import com.willblaschko.android.alexa.utility.Util;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class TokenManager {
    private static String ACCESS_TOKEN = null;
    private static final String ARG_CLIENT_ID = "client_id";
    private static final String ARG_CLIENT_SECRET = "client_secret";
    private static final String ARG_CODE = "code";
    private static final String ARG_CODE_VERIFIER = "code_verifier";
    private static final String ARG_GRANT_TYPE = "grant_type";
    private static final String ARG_REDIRECT_URI = "redirect_uri";
    private static final String ARG_REFRESH_TOKEN = "refresh_token";
    public static final String PREF_ACCESS_TOKEN = "access_token_042017";
    public static final String PREF_REFRESH_TOKEN = "refresh_token_042017";
    public static final String PREF_TOKEN_EXPIRES = "token_expires_042017";
    private static String REFRESH_TOKEN = null;
    private static final String TAG = "TokenManager";

    public interface TokenCallback {
        void onFailure(Throwable th);

        void onSuccess(String str);
    }

    public static class TokenResponse {
        public String access_token;
        public long expires_in;
        public String refresh_token;
        public String token_type;
    }

    public interface TokenResponseCallback {
        void onFailure(Exception exc);

        void onSuccess(TokenResponse tokenResponse);
    }

    public static void getAccessToken(final Context context, @NotNull String str, @NotNull String str2, @Nullable final TokenResponseCallback tokenResponseCallback) {
        FormBody.Builder add = new FormBody.Builder().add("grant_type", "authorization_code").add("code", str);
        add.add("redirect_uri", AlexaMacros.ALEXA_REDIRECT_URL);
        add.add("client_id", AlexaMacros.ALEXA_CLIENT_ID);
        add.add("client_secret", AlexaMacros.ALEXA_SECRET_KEY);
        OkHttpClient tLS12OkHttpClient = ClientUtil.getTLS12OkHttpClient();
        Request build = new Request.Builder().url("https://api.amazon.com/auth/O2/token").post(add.build()).build();
        final Handler handler = new Handler(Looper.getMainLooper());
        tLS12OkHttpClient.newCall(build).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException iOException) {
                iOException.printStackTrace();
                if (TokenResponseCallback.this != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            TokenResponseCallback.this.onFailure(iOException);
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final TokenResponse tokenResponse = (TokenResponse) new Gson().fromJson(response.body().string(), (Class<Object>) TokenResponse.class);
                TokenManager.saveTokens(context, tokenResponse);
                if (TokenResponseCallback.this != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            TokenResponseCallback.this.onSuccess(tokenResponse);
                        }
                    });
                }
            }
        });
    }

    public static void getAccessToken(@NotNull Context context, @NotNull TokenCallback tokenCallback, String str) {
        getRefreshToken(context, tokenCallback, str);
    }

    public static void getAccessToken(@NotNull Context context, @NotNull TokenCallback tokenCallback) {
        SharedPreferences preferences = Util.getPreferences(context.getApplicationContext());
        if (preferences.contains(PREF_ACCESS_TOKEN)) {
            if (preferences.getLong(PREF_TOKEN_EXPIRES, 0L) > System.currentTimeMillis()) {
                tokenCallback.onSuccess(preferences.getString(PREF_ACCESS_TOKEN, null));
                return;
            } else if (preferences.contains(PREF_REFRESH_TOKEN)) {
                getRefreshToken(context, tokenCallback, preferences.getString(PREF_REFRESH_TOKEN, ""));
                return;
            } else {
                return;
            }
        }
        getAccessToken(context, tokenCallback, AlexaMacros.ALEXA_REFRESH_HARD_CODED_TOKEN);
    }

    public static String getExistingAccessToken(@NotNull Context context) {
        SharedPreferences preferences = Util.getPreferences(context.getApplicationContext());
        if (!preferences.contains(PREF_ACCESS_TOKEN) || preferences.getLong(PREF_TOKEN_EXPIRES, 0L) <= System.currentTimeMillis()) {
            return null;
        }
        return preferences.getString(PREF_ACCESS_TOKEN, null);
    }

    public static String getExistingRefreshToken(@NotNull Context context) {
        SharedPreferences preferences = Util.getPreferences(context.getApplicationContext());
        if (!preferences.contains(PREF_ACCESS_TOKEN) || preferences.getLong(PREF_TOKEN_EXPIRES, 0L) <= System.currentTimeMillis()) {
            return null;
        }
        return preferences.getString(PREF_REFRESH_TOKEN, null);
    }

    public static boolean isRefreshTokenValid(@NotNull Context context) {
        SharedPreferences preferences = Util.getPreferences(context.getApplicationContext());
        return preferences.contains(PREF_ACCESS_TOKEN) && preferences.getLong(PREF_TOKEN_EXPIRES, 0L) > System.currentTimeMillis();
    }

    private static void getRefreshToken(@NotNull final Context context, @NotNull final TokenCallback tokenCallback, String str) {
        FormBody.Builder add = new FormBody.Builder().add("grant_type", "refresh_token").add("refresh_token", str);
        add.add("client_id", AlexaMacros.ALEXA_CLIENT_ID);
        OkHttpClient tLS12OkHttpClient = ClientUtil.getTLS12OkHttpClient();
        Request build = new Request.Builder().url("https://api.amazon.com/auth/O2/token").post(add.build()).build();
        final Handler handler = new Handler(Looper.getMainLooper());
        tLS12OkHttpClient.newCall(build).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException iOException) {
                iOException.printStackTrace();
                if (TokenCallback.this != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            TokenCallback.this.onFailure(iOException);
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final TokenResponse tokenResponse = (TokenResponse) new Gson().fromJson(response.body().string(), (Class<Object>) TokenResponse.class);
                TokenManager.saveTokens(context, tokenResponse);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        TokenCallback.this.onSuccess(tokenResponse.access_token);
                    }
                });
            }
        });
    }

    public static void saveTokens(Context context, TokenResponse tokenResponse) {
        REFRESH_TOKEN = tokenResponse.refresh_token;
        ACCESS_TOKEN = tokenResponse.access_token;
        SharedPreferences.Editor edit = Util.getPreferences(context.getApplicationContext()).edit();
        edit.putString(PREF_ACCESS_TOKEN, ACCESS_TOKEN);
        edit.putString(PREF_REFRESH_TOKEN, REFRESH_TOKEN);
        edit.putLong(PREF_TOKEN_EXPIRES, System.currentTimeMillis() + (tokenResponse.expires_in * 1000));
        edit.commit();
    }
}