implemented OneDrive to work with file browser

This commit is contained in:
Philipp Crocoll 2016-11-22 11:11:44 +01:00
parent a63663c30e
commit d483f840de
2 changed files with 102 additions and 32 deletions

View File

@ -3,6 +3,7 @@ package keepass2android.javafilestorage;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
@ -13,6 +14,7 @@ import com.onedrive.sdk.core.ClientException;
import com.onedrive.sdk.core.DefaultClientConfig; import com.onedrive.sdk.core.DefaultClientConfig;
import com.onedrive.sdk.core.IClientConfig; import com.onedrive.sdk.core.IClientConfig;
import com.onedrive.sdk.extensions.IItemCollectionPage; import com.onedrive.sdk.extensions.IItemCollectionPage;
import com.onedrive.sdk.extensions.IItemCollectionRequestBuilder;
import com.onedrive.sdk.extensions.IOneDriveClient; import com.onedrive.sdk.extensions.IOneDriveClient;
import com.onedrive.sdk.extensions.Item; import com.onedrive.sdk.extensions.Item;
import com.onedrive.sdk.extensions.OneDriveClient; import com.onedrive.sdk.extensions.OneDriveClient;
@ -26,21 +28,30 @@ import java.util.List;
*/ */
public class OneDriveStorage extends JavaFileStorageBase public class OneDriveStorage extends JavaFileStorageBase
{ {
final keepass2android.javafilestorage.onedrive.MyMSAAuthenticator msaAuthenticator = new keepass2android.javafilestorage.onedrive.MyMSAAuthenticator() { final IClientConfig oneDriveConfig;
@Override final keepass2android.javafilestorage.onedrive.MyMSAAuthenticator msaAuthenticator;
public String getClientId() {
return "000000004010C234";
}
@Override
public String[] getScopes() {
return new String[] { "offline_access", "onedrive.readwrite" };
}
};
IOneDriveClient oneDriveClient; IOneDriveClient oneDriveClient;
public void bla(final Activity activity) { public OneDriveStorage(final Context context, final String clientId) {
msaAuthenticator = new keepass2android.javafilestorage.onedrive.MyMSAAuthenticator(context) {
@Override
public String getClientId() {
return clientId;
}
@Override
public String[] getScopes() {
return new String[] { "offline_access", "onedrive.readwrite" };
}
};
oneDriveConfig = DefaultClientConfig.createWithAuthenticator(msaAuthenticator);
initAuthenticator(null);
}
public void bla(final Activity activity) {
android.util.Log.d("KP2A", "0"); android.util.Log.d("KP2A", "0");
android.util.Log.d("KP2A", "1"); android.util.Log.d("KP2A", "1");
@ -70,14 +81,17 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public boolean requiresSetup(String path) { public boolean requiresSetup(String path) {
return !isConnected(); return !isConnected(null);
} }
@Override @Override
public void startSelectFile(FileStorageSetupInitiatorActivity activity, boolean isForSave, int requestCode) { public void startSelectFile(FileStorageSetupInitiatorActivity activity, boolean isForSave, int requestCode) {
initAuthenticator((Activity)activity);
String path = getProtocolId()+":///"; String path = getProtocolId()+":///";
Log.d("KP2AJ", "startSelectFile "+path+", connected: "+path); Log.d("KP2AJ", "startSelectFile "+path+", connected: "+path);
if (isConnected()) if (isConnected(null))
{ {
Intent intent = new Intent(); Intent intent = new Intent();
intent.putExtra(EXTRA_IS_FOR_SAVE, isForSave); intent.putExtra(EXTRA_IS_FOR_SAVE, isForSave);
@ -90,14 +104,41 @@ public class OneDriveStorage extends JavaFileStorageBase
} }
} }
private boolean isConnected() { private boolean isConnected(Activity activity) {
return msaAuthenticator.loginSilent() != null; if (oneDriveClient == null)
{
Log.d("KP2AJ", "trying silent login");
if (msaAuthenticator.loginSilent() != null)
{
Log.d("KP2AJ", "ok: silent login");
try
{
oneDriveClient = buildClient(activity);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else Log.d("KP2AJ", "trying silent login failed.");
}
return oneDriveClient != null;
}
private void initAuthenticator(Activity activity) {
msaAuthenticator.init(
oneDriveConfig.getExecutors(),
oneDriveConfig.getHttpProvider(),
activity,
oneDriveConfig.getLogger());
} }
@Override @Override
public void prepareFileUsage(FileStorageSetupInitiatorActivity activity, String path, int requestCode, boolean alwaysReturnSuccess) { public void prepareFileUsage(FileStorageSetupInitiatorActivity activity, String path, int requestCode, boolean alwaysReturnSuccess) {
if (isConnected()) initAuthenticator((Activity)activity);
if (isConnected((Activity)activity))
{ {
Intent intent = new Intent(); Intent intent = new Intent();
intent.putExtra(EXTRA_PATH, path); intent.putExtra(EXTRA_PATH, path);
@ -117,7 +158,7 @@ public class OneDriveStorage extends JavaFileStorageBase
@Override @Override
public void prepareFileUsage(Context appContext, String path) throws UserInteractionRequiredException { public void prepareFileUsage(Context appContext, String path) throws UserInteractionRequiredException {
if (!isConnected()) if (!isConnected(null))
{ {
throw new UserInteractionRequiredException(); throw new UserInteractionRequiredException();
} }
@ -132,8 +173,9 @@ public class OneDriveStorage extends JavaFileStorageBase
} }
@Override @Override
public void onResume(FileStorageSetupActivity activity) { public void onResume(final FileStorageSetupActivity activity) {
Log.d("KP2AJ", "onResume");
if (activity.getProcessName().equals(PROCESS_NAME_SELECTFILE)) if (activity.getProcessName().equals(PROCESS_NAME_SELECTFILE))
activity.getState().putString(EXTRA_PATH, activity.getPath()); activity.getState().putString(EXTRA_PATH, activity.getPath());
@ -168,16 +210,20 @@ public class OneDriveStorage extends JavaFileStorageBase
else else
{ {
Log.d("KP2AJ", "Starting auth"); Log.d("KP2AJ", "Starting auth");
final IClientConfig oneDriveConfig = new DefaultClientConfig() { }; new AsyncTask<Object, Object, Object>() {
oneDriveClient = new OneDriveClient.Builder() @Override
//.fromConfig(oneDriveConfig) protected Object doInBackground(Object... params) {
.authenticator(msaAuthenticator) return buildClient((Activity)activity);
.executors(oneDriveConfig.getExecutors()) }
.httpProvider(oneDriveConfig.getHttpProvider())
.serializer(oneDriveConfig.getSerializer())
.loginAndBuildClient((Activity)activity);
@Override
protected void onPostExecute(Object o) {
oneDriveClient = (IOneDriveClient) o;
finishActivityWithSuccess(activity);
}
}.execute();
storageSetupAct.getState().putBoolean("hasStartedAuth", true); storageSetupAct.getState().putBoolean("hasStartedAuth", true);
} }
@ -185,6 +231,14 @@ public class OneDriveStorage extends JavaFileStorageBase
} }
private IOneDriveClient buildClient(Activity activity) {
return new OneDriveClient.Builder()
.fromConfig(oneDriveConfig)
.loginAndBuildClient(activity);
}
String removeProtocol(String path) String removeProtocol(String path)
{ {
@ -260,19 +314,25 @@ public class OneDriveStorage extends JavaFileStorageBase
.getChildren() .getChildren()
.buildRequest() .buildRequest()
.get(); .get();
if (parentPath.endsWith("/"))
parentPath = parentPath.substring(0,parentPath.length()-1);
while (true) while (true)
{ {
List<Item> items = itemsPage.getCurrentPage(); List<Item> items = itemsPage.getCurrentPage();
if (items.isEmpty()) if (items.isEmpty())
return result; return result;
itemsPage = itemsPage.getNextPage().buildRequest().get();
for (Item i: items) for (Item i: items)
{ {
FileEntry e = getFileEntry(getProtocolId() +"://"+ parentPath + "/" + i.name, i); FileEntry e = getFileEntry(getProtocolId() +"://"+ parentPath + "/" + i.name, i);
Log.d("KP2AJ", e.path);
result.add(e); result.add(e);
} }
IItemCollectionRequestBuilder nextPageReqBuilder = itemsPage.getNextPage();
if (nextPageReqBuilder == null)
return result;
itemsPage = nextPageReqBuilder.buildRequest().get();
} }
} }
@ -283,6 +343,7 @@ public class OneDriveStorage extends JavaFileStorageBase
e.displayName = i.name; e.displayName = i.name;
e.canRead = e.canWrite = true; e.canRead = e.canWrite = true;
e.path = path; e.path = path;
e.lastModifiedTime = i.lastModifiedDateTime.getTimeInMillis();
e.isDirectory = i.folder != null; e.isDirectory = i.folder != null;
return e; return e;
} }

View File

@ -6,6 +6,7 @@ package keepass2android.javafilestorage.onedrive;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Log;
import com.microsoft.onedrivesdk.BuildConfig; import com.microsoft.onedrivesdk.BuildConfig;
import com.microsoft.services.msa.LiveAuthClient; import com.microsoft.services.msa.LiveAuthClient;
@ -36,6 +37,12 @@ import java.util.concurrent.atomic.AtomicReference;
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public abstract class MyMSAAuthenticator implements IAuthenticator { public abstract class MyMSAAuthenticator implements IAuthenticator {
private final Context mContext;
public MyMSAAuthenticator(Context context)
{
mContext = context;
}
/** /**
* The sign in cancellation message. * The sign in cancellation message.
@ -118,15 +125,16 @@ public abstract class MyMSAAuthenticator implements IAuthenticator {
final IHttpProvider httpProvider, final IHttpProvider httpProvider,
final Activity activity, final Activity activity,
final ILogger logger) { final ILogger logger) {
mActivity = activity;
if (mInitialized) { if (mInitialized) {
return; return;
} }
mExecutors = executors; mExecutors = executors;
mActivity = activity;
mLogger = logger; mLogger = logger;
mInitialized = true; mInitialized = true;
mAuthClient = new LiveAuthClient(activity, getClientId(), Arrays.asList(getScopes())); mAuthClient = new LiveAuthClient(mContext, getClientId(), Arrays.asList(getScopes()));
final SharedPreferences prefs = getSharedPreferences(); final SharedPreferences prefs = getSharedPreferences();
mUserId.set(prefs.getString(USER_ID_KEY, null)); mUserId.set(prefs.getString(USER_ID_KEY, null));
@ -139,6 +147,7 @@ public abstract class MyMSAAuthenticator implements IAuthenticator {
*/ */
@Override @Override
public void login(final String emailAddressHint, final ICallback<IAccountInfo> loginCallback) { public void login(final String emailAddressHint, final ICallback<IAccountInfo> loginCallback) {
Log.d("KP2AJ", "login()");
if (!mInitialized) { if (!mInitialized) {
throw new IllegalStateException("init must be called"); throw new IllegalStateException("init must be called");
} }
@ -431,7 +440,7 @@ public abstract class MyMSAAuthenticator implements IAuthenticator {
* @return The shared preferences. * @return The shared preferences.
*/ */
private SharedPreferences getSharedPreferences() { private SharedPreferences getSharedPreferences() {
return mActivity.getSharedPreferences(MSA_AUTHENTICATOR_PREFS, Context.MODE_PRIVATE); return mContext.getSharedPreferences(MSA_AUTHENTICATOR_PREFS, Context.MODE_PRIVATE);
} }
} }