DropboxV2Adapter.java 2.0 KB

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