DropboxV2Adapter.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.epson.iprint.storage.dropbox;
  2. import android.content.Context;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import com.dropbox.core.android.Auth;
  6. import com.dropbox.core.p005v2.DbxClientV2;
  7. import com.epson.iprint.storage.SecureKeyStore;
  8. import com.epson.iprint.storage.StorageSecureStore;
  9. import epson.server.utils.Define;
  10. public class DropboxV2Adapter {
  11. public static void init(@NonNull Context context) {
  12. String savedToken = getSavedToken(context);
  13. if (savedToken != null) {
  14. DropboxClientFactory.init(savedToken);
  15. }
  16. }
  17. public static void getTokenAndInitClient(@NonNull Context context) {
  18. String oAuth2Token = Auth.getOAuth2Token();
  19. if (oAuth2Token != null) {
  20. StorageSecureStore.getSharedSecureStore().put(Define.DROPBOX_V2_TOKEN, oAuth2Token, StorageSecureStore.EXEC_MODE.OVERWRITE_IF_EXIST);
  21. eraseOldData();
  22. DropboxClientFactory.init(oAuth2Token);
  23. }
  24. }
  25. private static void eraseOldData() {
  26. StorageSecureStore sharedSecureStore = StorageSecureStore.getSharedSecureStore();
  27. for (String revoke : new String[]{Define.DROPBOX_ACCESS_KEY, Define.DROPBOX_ACCESS_SECRET, Define.DROPBOX_PASSWORD, Define.DROPBOX_USERNAME}) {
  28. sharedSecureStore.revoke(revoke);
  29. }
  30. }
  31. public static boolean hasToken(@NonNull Context context) {
  32. return getSavedToken(context) != null;
  33. }
  34. private static String getSavedToken(@NonNull Context context) {
  35. return StorageSecureStore.getSharedSecureStore().fetch(Define.DROPBOX_V2_TOKEN);
  36. }
  37. public static void startAuthentication(@NonNull Context context) {
  38. Auth.startOAuth2Authentication(context, new SecureKeyStore().getApiKeyA(context));
  39. }
  40. @Nullable
  41. public static DbxClientV2 getClient() {
  42. return DropboxClientFactory.getClient();
  43. }
  44. public static void clearToken(@NonNull Context context) {
  45. StorageSecureStore.getSharedSecureStore().revoke(Define.DROPBOX_V2_TOKEN);
  46. DropboxClientFactory.deleteClient();
  47. }
  48. }