2016-11-21 22:30:29 -05:00
|
|
|
package keepass2android.javafilestorage;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2016-11-22 05:11:44 -05:00
|
|
|
import android.os.AsyncTask;
|
2016-11-21 22:30:29 -05:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.onedrive.sdk.concurrency.ICallback;
|
|
|
|
import com.onedrive.sdk.core.ClientException;
|
|
|
|
import com.onedrive.sdk.core.DefaultClientConfig;
|
|
|
|
import com.onedrive.sdk.core.IClientConfig;
|
2016-11-22 05:37:56 -05:00
|
|
|
import com.onedrive.sdk.core.OneDriveErrorCodes;
|
2016-11-21 22:30:29 -05:00
|
|
|
import com.onedrive.sdk.extensions.IItemCollectionPage;
|
2016-11-22 05:11:44 -05:00
|
|
|
import com.onedrive.sdk.extensions.IItemCollectionRequestBuilder;
|
2016-11-21 22:30:29 -05:00
|
|
|
import com.onedrive.sdk.extensions.IOneDriveClient;
|
|
|
|
import com.onedrive.sdk.extensions.Item;
|
|
|
|
import com.onedrive.sdk.extensions.OneDriveClient;
|
2016-11-22 05:37:56 -05:00
|
|
|
import com.onedrive.sdk.http.OneDriveServiceException;
|
2016-11-21 22:30:29 -05:00
|
|
|
|
2016-11-22 05:37:56 -05:00
|
|
|
import java.io.FileNotFoundException;
|
2016-11-21 22:30:29 -05:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Philipp on 20.11.2016.
|
|
|
|
*/
|
|
|
|
public class OneDriveStorage extends JavaFileStorageBase
|
|
|
|
{
|
2016-11-22 05:11:44 -05:00
|
|
|
final IClientConfig oneDriveConfig;
|
|
|
|
final keepass2android.javafilestorage.onedrive.MyMSAAuthenticator msaAuthenticator;
|
2016-11-21 22:30:29 -05:00
|
|
|
|
|
|
|
IOneDriveClient oneDriveClient;
|
|
|
|
|
2016-11-22 05:11:44 -05:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:30:29 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean requiresSetup(String path) {
|
2016-11-22 05:11:44 -05:00
|
|
|
return !isConnected(null);
|
2016-11-21 22:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startSelectFile(FileStorageSetupInitiatorActivity activity, boolean isForSave, int requestCode) {
|
2016-11-22 05:11:44 -05:00
|
|
|
|
|
|
|
initAuthenticator((Activity)activity);
|
|
|
|
|
2016-11-21 22:30:29 -05:00
|
|
|
String path = getProtocolId()+":///";
|
|
|
|
Log.d("KP2AJ", "startSelectFile "+path+", connected: "+path);
|
2016-11-22 05:11:44 -05:00
|
|
|
if (isConnected(null))
|
2016-11-21 22:30:29 -05:00
|
|
|
{
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.putExtra(EXTRA_IS_FOR_SAVE, isForSave);
|
|
|
|
intent.putExtra(EXTRA_PATH, path);
|
|
|
|
activity.onImmediateResult(requestCode, RESULT_FILECHOOSER_PREPARED, intent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
activity.startSelectFileProcess(path, isForSave, requestCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 05:11:44 -05:00
|
|
|
private boolean isConnected(Activity activity) {
|
|
|
|
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());
|
2016-11-21 22:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void prepareFileUsage(FileStorageSetupInitiatorActivity activity, String path, int requestCode, boolean alwaysReturnSuccess) {
|
2016-11-22 05:11:44 -05:00
|
|
|
initAuthenticator((Activity)activity);
|
|
|
|
if (isConnected((Activity)activity))
|
2016-11-21 22:30:29 -05:00
|
|
|
{
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.putExtra(EXTRA_PATH, path);
|
|
|
|
activity.onImmediateResult(requestCode, RESULT_FILEUSAGE_PREPARED, intent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
activity.startFileUsageProcess(path, requestCode, alwaysReturnSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getProtocolId() {
|
|
|
|
return "onedrive";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void prepareFileUsage(Context appContext, String path) throws UserInteractionRequiredException {
|
2016-11-22 05:11:44 -05:00
|
|
|
if (!isConnected(null))
|
2016-11-21 22:30:29 -05:00
|
|
|
{
|
|
|
|
throw new UserInteractionRequiredException();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(FileStorageSetupActivity activity, Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
Log.d("KP2AJ", "OnCreate");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-11-22 05:11:44 -05:00
|
|
|
public void onResume(final FileStorageSetupActivity activity) {
|
2016-11-21 22:30:29 -05:00
|
|
|
|
2016-11-22 05:11:44 -05:00
|
|
|
Log.d("KP2AJ", "onResume");
|
2016-11-21 22:30:29 -05:00
|
|
|
if (activity.getProcessName().equals(PROCESS_NAME_SELECTFILE))
|
|
|
|
activity.getState().putString(EXTRA_PATH, activity.getPath());
|
|
|
|
|
|
|
|
JavaFileStorage.FileStorageSetupActivity storageSetupAct = activity;
|
|
|
|
|
|
|
|
if (storageSetupAct.getState().containsKey("hasStartedAuth"))
|
|
|
|
{
|
|
|
|
Log.d("KP2AJ", "auth started");
|
|
|
|
|
|
|
|
|
|
|
|
if (oneDriveClient != null) {
|
|
|
|
Log.d("KP2AJ", "auth successful");
|
|
|
|
try {
|
|
|
|
|
|
|
|
finishActivityWithSuccess(activity);
|
|
|
|
return;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d("KP2AJ", "finish with error: " + e.toString());
|
|
|
|
finishWithError(activity, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Log.i(TAG, "authenticating not succesful");
|
|
|
|
Intent data = new Intent();
|
|
|
|
data.putExtra(EXTRA_ERROR_MESSAGE, "authenticating not succesful");
|
|
|
|
((Activity)activity).setResult(Activity.RESULT_CANCELED, data);
|
|
|
|
((Activity)activity).finish();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.d("KP2AJ", "Starting auth");
|
2016-11-22 05:11:44 -05:00
|
|
|
new AsyncTask<Object, Object, Object>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Object doInBackground(Object... params) {
|
|
|
|
return buildClient((Activity)activity);
|
|
|
|
}
|
2016-11-21 22:30:29 -05:00
|
|
|
|
2016-11-22 05:11:44 -05:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Object o) {
|
|
|
|
oneDriveClient = (IOneDriveClient) o;
|
|
|
|
finishActivityWithSuccess(activity);
|
2016-11-21 22:30:29 -05:00
|
|
|
|
2016-11-22 05:11:44 -05:00
|
|
|
}
|
|
|
|
}.execute();
|
2016-11-21 22:30:29 -05:00
|
|
|
storageSetupAct.getState().putBoolean("hasStartedAuth", true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-22 05:11:44 -05:00
|
|
|
private IOneDriveClient buildClient(Activity activity) {
|
|
|
|
|
|
|
|
return new OneDriveClient.Builder()
|
|
|
|
.fromConfig(oneDriveConfig)
|
|
|
|
.loginAndBuildClient(activity);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:30:29 -05:00
|
|
|
|
|
|
|
String removeProtocol(String path)
|
|
|
|
{
|
|
|
|
if (path == null)
|
|
|
|
return null;
|
|
|
|
return path.substring(getProtocolId().length()+3);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDisplayName(String path) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFilename(String path) throws Exception {
|
|
|
|
return path.substring(path.lastIndexOf("/")+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean checkForFileChangeFast(String path, String previousFileVersion) throws Exception {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getCurrentFileVersionFast(String path) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InputStream openFileForRead(String path) throws Exception {
|
2016-11-22 05:37:56 -05:00
|
|
|
try {
|
|
|
|
path = removeProtocol(path);
|
|
|
|
return oneDriveClient.getDrive()
|
|
|
|
.getRoot()
|
|
|
|
.getItemWithPath(path)
|
|
|
|
.getContent()
|
|
|
|
.buildRequest()
|
|
|
|
.get();
|
|
|
|
}
|
|
|
|
catch (OneDriveServiceException e)
|
|
|
|
{
|
|
|
|
throw convertException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private Exception convertException(OneDriveServiceException e) {
|
|
|
|
if (e.isError(OneDriveErrorCodes.ItemNotFound))
|
|
|
|
return new FileNotFoundException(e.getMessage());
|
|
|
|
return e;
|
2016-11-21 22:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception {
|
2016-11-22 05:37:56 -05:00
|
|
|
try {
|
|
|
|
path = removeProtocol(path);
|
|
|
|
oneDriveClient.getDrive()
|
|
|
|
.getRoot()
|
|
|
|
.getItemWithPath(path)
|
|
|
|
.getContent()
|
|
|
|
.buildRequest()
|
|
|
|
.put(data);
|
|
|
|
} catch (OneDriveServiceException e) {
|
|
|
|
throw convertException(e);
|
|
|
|
}
|
2016-11-21 22:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String createFolder(String parentPath, String newDirName) throws Exception {
|
|
|
|
throw new Exception("not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String createFilePath(String parentPath, String newFileName) throws Exception {
|
|
|
|
String path = parentPath;
|
|
|
|
if (!path.endsWith("/"))
|
|
|
|
path = path + "/";
|
|
|
|
path = path + newFileName;
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<FileEntry> listFiles(String parentPath) throws Exception {
|
2016-11-22 05:37:56 -05:00
|
|
|
try {
|
|
|
|
ArrayList<FileEntry> result = new ArrayList<FileEntry>();
|
|
|
|
parentPath = removeProtocol(parentPath);
|
|
|
|
IItemCollectionPage itemsPage = oneDriveClient.getDrive()
|
|
|
|
.getRoot()
|
|
|
|
.getItemWithPath(parentPath)
|
|
|
|
.getChildren()
|
|
|
|
.buildRequest()
|
|
|
|
.get();
|
|
|
|
if (parentPath.endsWith("/"))
|
|
|
|
parentPath = parentPath.substring(0,parentPath.length()-1);
|
|
|
|
while (true)
|
2016-11-21 22:30:29 -05:00
|
|
|
{
|
2016-11-22 05:37:56 -05:00
|
|
|
List<Item> items = itemsPage.getCurrentPage();
|
|
|
|
if (items.isEmpty())
|
|
|
|
return result;
|
2016-11-22 05:11:44 -05:00
|
|
|
|
2016-11-22 05:37:56 -05:00
|
|
|
for (Item i: items)
|
|
|
|
{
|
|
|
|
FileEntry e = getFileEntry(parentPath + "/" + i.name, i);
|
|
|
|
Log.d("KP2AJ", e.path);
|
|
|
|
result.add(e);
|
|
|
|
}
|
|
|
|
IItemCollectionRequestBuilder nextPageReqBuilder = itemsPage.getNextPage();
|
|
|
|
if (nextPageReqBuilder == null)
|
|
|
|
return result;
|
|
|
|
itemsPage = nextPageReqBuilder.buildRequest().get();
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (OneDriveServiceException e) {
|
|
|
|
throw convertException(e);
|
2016-11-21 22:30:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
private FileEntry getFileEntry(String path, Item i) {
|
|
|
|
FileEntry e = new FileEntry();
|
|
|
|
e.sizeInBytes = i.size;
|
|
|
|
e.displayName = i.name;
|
|
|
|
e.canRead = e.canWrite = true;
|
2016-11-22 05:37:56 -05:00
|
|
|
e.path = getProtocolId() +"://"+path;
|
2016-11-22 05:11:44 -05:00
|
|
|
e.lastModifiedTime = i.lastModifiedDateTime.getTimeInMillis();
|
2016-11-21 22:30:29 -05:00
|
|
|
e.isDirectory = i.folder != null;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileEntry getFileEntry(String filename) throws Exception {
|
2016-11-22 05:37:56 -05:00
|
|
|
try {
|
|
|
|
filename = removeProtocol(filename);
|
|
|
|
Item item = oneDriveClient.getDrive()
|
|
|
|
.getRoot()
|
|
|
|
.getItemWithPath(filename)
|
|
|
|
.buildRequest()
|
|
|
|
.get();
|
|
|
|
return getFileEntry(filename, item);
|
|
|
|
} catch (OneDriveServiceException e) {
|
|
|
|
throw convertException(e);
|
|
|
|
}
|
2016-11-21 22:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void delete(String path) throws Exception {
|
2016-11-22 05:37:56 -05:00
|
|
|
try {
|
|
|
|
path = removeProtocol(path);
|
|
|
|
oneDriveClient.getDrive()
|
|
|
|
.getRoot()
|
|
|
|
.getItemWithPath(path)
|
|
|
|
.buildRequest()
|
|
|
|
.delete();
|
|
|
|
} catch (OneDriveServiceException e) {
|
|
|
|
throw convertException(e);
|
|
|
|
}
|
2016-11-21 22:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart(FileStorageSetupActivity activity) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(FileStorageSetupActivity activity, int requestCode, int resultCode, Intent data) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|