OneDriveAuthenticator.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.epson.iprint.storage.onedrive;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import com.epson.iprint.storage.SecureKeyStore;
  5. import com.onedrive.sdk.authentication.IAuthenticator;
  6. import com.onedrive.sdk.authentication.MSAAuthenticator;
  7. import com.onedrive.sdk.concurrency.ICallback;
  8. import com.onedrive.sdk.core.ClientException;
  9. import com.onedrive.sdk.core.DefaultClientConfig;
  10. import com.onedrive.sdk.extensions.IOneDriveClient;
  11. public class OneDriveAuthenticator {
  12. protected static final String TAG = "OneDriveAuthenticator";
  13. public interface OnOneDriveAuthenticatorListener {
  14. void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient);
  15. }
  16. public static void getOneDriveClient(Activity activity, final OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener) {
  17. new OneDriveClient.Builder().fromConfig(DefaultClientConfig.createWithAuthenticator(getMSAAuthenticator(activity))).loginAndBuildClient(activity, new ICallback<IOneDriveClient>() {
  18. public void success(IOneDriveClient iOneDriveClient) {
  19. IAuthenticator authenticator;
  20. if (!(iOneDriveClient == null || (authenticator = iOneDriveClient.getAuthenticator()) == null)) {
  21. OneDriveClient.saveToken(authenticator.getAccountInfo());
  22. }
  23. OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener = onOneDriveAuthenticatorListener;
  24. if (onOneDriveAuthenticatorListener != null) {
  25. onOneDriveAuthenticatorListener.onNotifyOneDriveClient(iOneDriveClient);
  26. }
  27. }
  28. public void failure(ClientException clientException) {
  29. clientException.printStackTrace();
  30. OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener = onOneDriveAuthenticatorListener;
  31. if (onOneDriveAuthenticatorListener != null) {
  32. onOneDriveAuthenticatorListener.onNotifyOneDriveClient((IOneDriveClient) null);
  33. }
  34. }
  35. });
  36. }
  37. private static MSAAuthenticator getMSAAuthenticator(final Context context) {
  38. return new MSAAuthenticator() {
  39. public String getClientId() {
  40. return getMicrosoftAccountForOneDrive(context);
  41. }
  42. public String[] getScopes() {
  43. return new String[]{"onedrive.readwrite", "offline_access"};
  44. }
  45. private String getMicrosoftAccountForOneDrive(Context context) {
  46. return new SecureKeyStore().getApiKeyD(context);
  47. }
  48. };
  49. }
  50. }