* modified setup workflow for IFileStorage (to be compatible with Google Drive requirements)
* scheme (protocol) is always contained in path variables passed to JavaFileStorage implementors * file chooser improvements (internal browser displayed also in file chooser list e.g. when selecting an attachments, compatible with Solid Explorer content uris, removed OI stuff) * started GDrive support
This commit is contained in:
@@ -5,5 +5,7 @@
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
|
||||
<classpathentry kind="con" path="com.google.gdt.eclipse.managedapis.MANAGED_API_CONTAINER/drive-v2r102lv1.16.0-rc"/>
|
||||
<classpathentry exported="true" kind="lib" path="C:/Users/Philipp/AppData/Local/Android/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar"/>
|
||||
<classpathentry kind="output" path="bin/classes"/>
|
||||
</classpath>
|
||||
|
||||
@@ -18,5 +18,6 @@
|
||||
|
||||
|
||||
</application>
|
||||
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
# project structure.
|
||||
#
|
||||
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
|
||||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt:proguard-google-api-client.txt
|
||||
|
||||
# Project target.
|
||||
target=android-17
|
||||
android.library=true
|
||||
android.library.reference.1=../../../../../../../AppData/Local/Android/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib
|
||||
|
||||
@@ -15,6 +15,7 @@ import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.DropBoxManager.Entry;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
@@ -35,7 +36,7 @@ import com.dropbox.client2.session.Session.AccessType;
|
||||
public class DropboxFileStorage implements JavaFileStorage {
|
||||
|
||||
//NOTE: also adjust secret!
|
||||
final static private String APP_KEY = "i8shu7v1hgh7ynt"; //KP2A
|
||||
//final static private String APP_KEY = "i8shu7v1hgh7ynt"; //KP2A
|
||||
//final static private String APP_KEY = "4ybka4p4a1027n6"; //FileStorageTest
|
||||
|
||||
// If you'd like to change the access type to the full Dropbox instead of
|
||||
@@ -51,14 +52,36 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
DropboxAPI<AndroidAuthSession> mApi;
|
||||
private boolean mLoggedIn = false;
|
||||
private Context mContext;
|
||||
|
||||
private String appKey;
|
||||
private String appSecret;
|
||||
|
||||
public DropboxFileStorage(Context ctx)
|
||||
public DropboxFileStorage(Context ctx, String _appKey, String _appSecret)
|
||||
{
|
||||
appKey = _appKey;
|
||||
appSecret = _appSecret;
|
||||
mContext = ctx;
|
||||
// We create a new AuthSession so that we can use the Dropbox API.
|
||||
AndroidAuthSession session = buildSession();
|
||||
mApi = new DropboxAPI<AndroidAuthSession>(session);
|
||||
|
||||
checkAppKeySetup();
|
||||
}
|
||||
|
||||
public DropboxFileStorage(Context ctx, String _appKey, String _appSecret, boolean clearKeysOnStart)
|
||||
{
|
||||
appKey = _appKey;
|
||||
appSecret = _appSecret;
|
||||
mContext = ctx;
|
||||
|
||||
if (clearKeysOnStart)
|
||||
clearKeys();
|
||||
|
||||
|
||||
// We create a new AuthSession so that we can use the Dropbox API.
|
||||
AndroidAuthSession session = buildSession();
|
||||
mApi = new DropboxAPI<AndroidAuthSession>(session);
|
||||
|
||||
checkAppKeySetup();
|
||||
}
|
||||
|
||||
@@ -69,29 +92,6 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
return mLoggedIn;
|
||||
}
|
||||
|
||||
public void onResume()
|
||||
{
|
||||
AndroidAuthSession session = mApi.getSession();
|
||||
|
||||
// The next part must be inserted in the onResume() method of the
|
||||
// activity from which session.startAuthentication() was called, so
|
||||
// that Dropbox authentication completes properly.
|
||||
if (session.authenticationSuccessful()) {
|
||||
try {
|
||||
// Mandatory call to complete the auth
|
||||
session.finishAuthentication();
|
||||
|
||||
// Store it locally in our app for later use
|
||||
TokenPair tokens = session.getAccessTokenPair();
|
||||
storeKeys(tokens.key, tokens.secret);
|
||||
setLoggedIn(true);
|
||||
} catch (IllegalStateException e) {
|
||||
Log.i(TAG, "Error authenticating", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setLoggedIn(boolean b) {
|
||||
mLoggedIn = b;
|
||||
|
||||
@@ -101,7 +101,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
|
||||
// Check if the app has set up its manifest properly.
|
||||
Intent testIntent = new Intent(Intent.ACTION_VIEW);
|
||||
String scheme = "db-" + APP_KEY;
|
||||
String scheme = "db-" + appKey;
|
||||
String uri = scheme + "://" + AuthActivity.AUTH_VERSION + "/test";
|
||||
testIntent.setData(Uri.parse(uri));
|
||||
PackageManager pm = mContext.getPackageManager();
|
||||
@@ -123,9 +123,10 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
|
||||
public boolean checkForFileChangeFast(String path, String previousFileVersion) throws Exception
|
||||
{
|
||||
if ((previousFileVersion == null) || (previousFileVersion == ""))
|
||||
if ((previousFileVersion == null) || (previousFileVersion.equals("")))
|
||||
return false;
|
||||
try {
|
||||
path = removeProtocol(path);
|
||||
com.dropbox.client2.DropboxAPI.Entry entry = mApi.metadata(path, 1, null, false, null);
|
||||
return entry.hash != previousFileVersion;
|
||||
} catch (DropboxException e) {
|
||||
@@ -136,6 +137,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
public String getCurrentFileVersionFast(String path)
|
||||
{
|
||||
try {
|
||||
path = removeProtocol(path);
|
||||
com.dropbox.client2.DropboxAPI.Entry entry = mApi.metadata(path, 1, null, false, null);
|
||||
return entry.rev;
|
||||
} catch (DropboxException e) {
|
||||
@@ -147,6 +149,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
public InputStream openFileForRead(String path) throws Exception
|
||||
{
|
||||
try {
|
||||
path = removeProtocol(path);
|
||||
return mApi.getFileStream(path, null);
|
||||
} catch (DropboxException e) {
|
||||
//System.out.println("Something went wrong: " + e);
|
||||
@@ -158,6 +161,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
{
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(data);
|
||||
try {
|
||||
path = removeProtocol(path);
|
||||
//TODO: it would be nice to be able to use the parent version with putFile()
|
||||
mApi.putFileOverwrite(path, bis, data.length, null);
|
||||
} catch (DropboxException e) {
|
||||
@@ -231,7 +235,6 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
//TODO: call when Unlinked Exception
|
||||
private void clearKeys() {
|
||||
SharedPreferences prefs = mContext.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
|
||||
Editor edit = prefs.edit();
|
||||
@@ -240,8 +243,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
}
|
||||
|
||||
private AndroidAuthSession buildSession() {
|
||||
//note: the SecretKeys class is not public because the App-Secret must be secret!
|
||||
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, SecretKeys.DROPBOX_APP_SECRET);
|
||||
AppKeyPair appKeyPair = new AppKeyPair(appKey, appSecret);
|
||||
AndroidAuthSession session;
|
||||
|
||||
String[] stored = getKeys();
|
||||
@@ -263,6 +265,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
public void createFolder(String path) throws Exception {
|
||||
try
|
||||
{
|
||||
path = removeProtocol(path);
|
||||
mApi.createFolder(path);
|
||||
}
|
||||
catch (DropboxException e) {
|
||||
@@ -274,6 +277,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
public List<FileEntry> listFiles(String dirName) throws Exception {
|
||||
try
|
||||
{
|
||||
dirName = removeProtocol(dirName);
|
||||
com.dropbox.client2.DropboxAPI.Entry dirEntry = mApi.metadata(dirName, 0, null, true, null);
|
||||
|
||||
if (dirEntry.isDeleted)
|
||||
@@ -305,7 +309,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
fileEntry.canWrite = true;
|
||||
fileEntry.isDirectory = e.isDir;
|
||||
fileEntry.sizeInBytes = e.bytes;
|
||||
fileEntry.path = e.path;
|
||||
fileEntry.path = getProtocolId()+"://"+ e.path;
|
||||
//Log.d("JFS","fileEntry="+fileEntry);
|
||||
Date lastModifiedDate = null;
|
||||
if (e.modified != null)
|
||||
@@ -322,6 +326,7 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
public void delete(String path) throws Exception {
|
||||
try
|
||||
{
|
||||
path = removeProtocol(path);
|
||||
mApi.delete(path);
|
||||
} catch (DropboxException e) {
|
||||
throw convertException(e);
|
||||
@@ -334,8 +339,8 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
public FileEntry getFileEntry(String filename) throws Exception {
|
||||
try
|
||||
{
|
||||
Log.d("JFS", "Hi!");
|
||||
Log.d("JFS", "mApi = "+mApi);
|
||||
filename = removeProtocol(filename);
|
||||
Log.d("KP2AJ", "getFileEntry(), mApi = "+mApi+" filename="+filename);
|
||||
com.dropbox.client2.DropboxAPI.Entry dbEntry = mApi.metadata(filename, 0, null, false, null);
|
||||
Log.d("JFS", "dbEntry = "+dbEntry);
|
||||
|
||||
@@ -350,4 +355,164 @@ public class DropboxFileStorage implements JavaFileStorage {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startSelectFile(FileStorageSetupInitiatorActivity activity, boolean isForSave,
|
||||
int requestCode)
|
||||
{
|
||||
|
||||
String path = getProtocolId()+":///";
|
||||
Log.d("KP2AJ", "startSelectFile "+path+", connected: "+path);
|
||||
if (isConnected())
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProtocolId() {
|
||||
return "dropbox";
|
||||
}
|
||||
|
||||
public boolean requiresSetup(String path)
|
||||
{
|
||||
return !isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareFileUsage(FileStorageSetupInitiatorActivity activity, String path, int requestCode) {
|
||||
if (isConnected())
|
||||
{
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(EXTRA_PATH, path);
|
||||
activity.onImmediateResult(requestCode, RESULT_FILEUSAGE_PREPARED, intent);
|
||||
}
|
||||
else
|
||||
{
|
||||
activity.startFileUsageProcess(path, requestCode);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(FileStorageSetupActivity activity, Bundle savedInstanceState) {
|
||||
|
||||
Log.d("KP2AJ", "OnCreate");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(FileStorageSetupActivity activity) {
|
||||
|
||||
Log.d("KP2AJ", "OnResume. LoggedIn="+mLoggedIn);
|
||||
if (mLoggedIn)
|
||||
{
|
||||
finishActivityWithSuccess(activity);
|
||||
return;
|
||||
}
|
||||
|
||||
AndroidAuthSession session = mApi.getSession();
|
||||
|
||||
// The next part must be inserted in the onResume() method of the
|
||||
// activity from which session.startAuthentication() was called, so
|
||||
// that Dropbox authentication completes properly.
|
||||
if (session.authenticationSuccessful()) {
|
||||
try {
|
||||
// Mandatory call to complete the auth
|
||||
session.finishAuthentication();
|
||||
|
||||
// Store it locally in our app for later use
|
||||
TokenPair tokens = session.getAccessTokenPair();
|
||||
storeKeys(tokens.key, tokens.secret);
|
||||
setLoggedIn(true);
|
||||
|
||||
finishActivityWithSuccess(activity);
|
||||
return;
|
||||
|
||||
} catch (Throwable t) {
|
||||
Log.i(TAG, "Error authenticating", t);
|
||||
Intent data = new Intent();
|
||||
data.putExtra(EXTRA_ERROR_MESSAGE, t.getMessage());
|
||||
((Activity)activity).setResult(Activity.RESULT_CANCELED, data);
|
||||
((Activity)activity).finish();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
JavaFileStorage.FileStorageSetupActivity storageSetupAct = (JavaFileStorage.FileStorageSetupActivity)activity;
|
||||
|
||||
if (storageSetupAct.getState().containsKey("hasStartedAuth"))
|
||||
{
|
||||
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");
|
||||
mApi.getSession().startAuthentication(((Activity)activity));
|
||||
storageSetupAct.getState().putBoolean("hasStartedAuth", true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void finishActivityWithSuccess(FileStorageSetupActivity setupActivity) {
|
||||
Log.d("KP2AJ", "Success with authentcating!");
|
||||
Activity activity = (Activity)setupActivity;
|
||||
|
||||
if (setupActivity.getProcessName().equals(PROCESS_NAME_FILE_USAGE_SETUP))
|
||||
{
|
||||
Intent data = new Intent();
|
||||
data.putExtra(EXTRA_IS_FOR_SAVE, setupActivity.isForSave());
|
||||
data.putExtra(EXTRA_PATH, setupActivity.getPath());
|
||||
activity.setResult(RESULT_FILEUSAGE_PREPARED, data);
|
||||
activity.finish();
|
||||
return;
|
||||
}
|
||||
if (setupActivity.getProcessName().equals(PROCESS_NAME_SELECTFILE))
|
||||
{
|
||||
Intent data = new Intent();
|
||||
data.putExtra(EXTRA_PATH, setupActivity.getPath());
|
||||
activity.setResult(RESULT_FILECHOOSER_PREPARED, data);
|
||||
activity.finish();
|
||||
return;
|
||||
}
|
||||
|
||||
Log.w("KP2AJ", "Unknown process: " + setupActivity.getProcessName());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(FileStorageSetupActivity activity) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(FileStorageSetupActivity activity, int requestCode, int resultCode, Intent data) {
|
||||
//nothing to do here
|
||||
|
||||
}
|
||||
|
||||
String removeProtocol(String path)
|
||||
{
|
||||
if (path == null)
|
||||
return null;
|
||||
return path.substring(getProtocolId().length()+3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
package keepass2android.javafilestorage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.api.client.extensions.android.http.AndroidHttp;
|
||||
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
||||
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.services.drive.Drive;
|
||||
import com.google.api.services.drive.Drive.Children;
|
||||
import com.google.api.services.drive.DriveScopes;
|
||||
import com.google.api.services.drive.model.ChildList;
|
||||
import com.google.api.services.drive.model.ChildReference;
|
||||
|
||||
import android.accounts.AccountManager;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
public class GoogleDriveFileStorage
|
||||
{};
|
||||
/*/
|
||||
public class GoogleDriveFileStorage implements JavaFileStorage {
|
||||
|
||||
private static Drive service;
|
||||
//private GoogleAccountCredential credential;
|
||||
|
||||
static final int MAGIC_GDRIVE=2082334;
|
||||
static final int REQUEST_ACCOUNT_PICKER = MAGIC_GDRIVE+1;
|
||||
static final int REQUEST_AUTHORIZATION = MAGIC_GDRIVE+2;
|
||||
|
||||
class TestConnectionTask extends AsyncTask<Object, Void, Void>
|
||||
{
|
||||
@Override
|
||||
protected Void doInBackground(Object... params) {
|
||||
|
||||
Activity activity = (Activity) params[0];
|
||||
|
||||
//try to list files:
|
||||
//todo: is there a simpler way to test if the user is authorized?
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (UserRecoverableAuthIOException e) {
|
||||
activity.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
Intent data = new Intent();
|
||||
data.putExtra(EXTRA_ERROR_MESSAGE, t.getMessage());
|
||||
activity.setResult(Activity.RESULT_CANCELED, data);
|
||||
activity.finish();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void printFilesInFolder(Drive service, String folderId)
|
||||
throws IOException {
|
||||
Children.List request = service.files().list();
|
||||
|
||||
do {
|
||||
try {
|
||||
ChildList children = request.execute();
|
||||
|
||||
for (ChildReference child : children.getItems()) {
|
||||
System.out.println("File Id: " + child.getId());
|
||||
}
|
||||
request.setPageToken(children.getNextPageToken());
|
||||
} catch (IOException e) {
|
||||
System.out.println("An error occurred: " + e);
|
||||
request.setPageToken(null);
|
||||
}
|
||||
} while (request.getPageToken() != null &&
|
||||
request.getPageToken().length() > 0);
|
||||
}
|
||||
|
||||
public boolean tryConnect(Activity activity) {
|
||||
|
||||
List<String> scopes = new ArrayList<String>();
|
||||
scopes.add(DriveScopes.DRIVE);
|
||||
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(activity, scopes);
|
||||
|
||||
String storedAccountName = PreferenceManager.getDefaultSharedPreferences(activity).getString("GDRIVE_ACCOUNT_NAME", null);
|
||||
|
||||
if (storedAccountName != null)
|
||||
{
|
||||
credential.setSelectedAccountName(storedAccountName);
|
||||
//try to list files:
|
||||
//todo: is there a simpler way to test if the user is authorized?
|
||||
try
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (UserRecoverableAuthIOException e) {
|
||||
activity.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
|
||||
}
|
||||
|
||||
catch (Throwable t)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
activity.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean checkForFileChangeFast(String path,
|
||||
String previousFileVersion) throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentFileVersionFast(String path) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream openFileForRead(String path) throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadFile(String path, byte[] data, boolean writeTransactional)
|
||||
throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createFolder(String path) throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileEntry> listFiles(String dirName) throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileEntry getFileEntry(String filename) throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
private Drive getDriveService(GoogleAccountCredential credential) {
|
||||
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
|
||||
switch (requestCode) {
|
||||
case REQUEST_ACCOUNT_PICKER:
|
||||
|
||||
if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
|
||||
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
|
||||
if (accountName != null) {
|
||||
//credential.setSelectedAccountName(accountName);
|
||||
todo
|
||||
return;
|
||||
}
|
||||
}
|
||||
todo
|
||||
|
||||
case REQUEST_AUTHORIZATION:
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
// App is authorized
|
||||
todo
|
||||
} else {
|
||||
// User denied access, show him the account chooser again
|
||||
activity.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startSelectFile(Activity activity, boolean isForSave,
|
||||
int requestCode) {
|
||||
((JavaFileStorage.FileStorageSetupInitiatorActivity)(activity)).startSelectFileProcess(getProtocolId()+"://", isForSave, requestCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareFileUsage(Activity activity, String path, int requestCode) {
|
||||
((JavaFileStorage.FileStorageSetupInitiatorActivity)(activity)).startFileUsageProcess(path, requestCode);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProtocolId() {
|
||||
|
||||
return "gdrive";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onResume(Activity activity) {
|
||||
|
||||
JavaFileStorage.FileStorageSetupActivity setupAct = (FileStorageSetupActivity) activity;
|
||||
|
||||
if (activity.isFinishing())
|
||||
return;
|
||||
|
||||
if (!hasAccount(setupAct))
|
||||
{
|
||||
List<String> scopes = new ArrayList<String>();
|
||||
scopes.add(DriveScopes.DRIVE);
|
||||
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(activity, scopes);
|
||||
|
||||
activity.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(Activity activity) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -4,9 +4,37 @@ import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
public interface JavaFileStorage {
|
||||
|
||||
public static final String PROCESS_NAME_SELECTFILE = "SELECT_FILE";
|
||||
public static final String PROCESS_NAME_FILE_USAGE_SETUP = "FILE_USAGE_SETUP";
|
||||
|
||||
public static final String EXTRA_PROCESS_NAME = "EXTRA_PROCESS_NAME";
|
||||
public static final String EXTRA_PATH = "fileName"; //match KP2A PasswordActivity Ioc-Path Extra key
|
||||
public static final String EXTRA_IS_FOR_SAVE = "IS_FOR_SAVE";
|
||||
public static final String EXTRA_ERROR_MESSAGE = "EXTRA_ERROR_MESSAGE";
|
||||
|
||||
|
||||
public interface FileStorageSetupInitiatorActivity
|
||||
{
|
||||
void startSelectFileProcess(String path, boolean isForSave, int requestCode);
|
||||
void startFileUsageProcess(String path, int requestCode);
|
||||
void onImmediateResult(int requestCode, int result, Intent intent);
|
||||
Activity getActivity();
|
||||
}
|
||||
|
||||
public interface FileStorageSetupActivity
|
||||
{
|
||||
String getPath();
|
||||
String getProcessName();
|
||||
//int getRequestCode();
|
||||
boolean isForSave();
|
||||
Bundle getState();
|
||||
}
|
||||
|
||||
|
||||
public class FileEntry {
|
||||
public String path;
|
||||
@@ -66,11 +94,26 @@ public class FileEntry {
|
||||
|
||||
}
|
||||
|
||||
public boolean tryConnect(Activity activity);
|
||||
//public boolean tryConnect(Activity activity);
|
||||
|
||||
public void onResume();
|
||||
//public void onResume();
|
||||
|
||||
//public void onActivityResult(Activity activity, final int requestCode, final int resultCode, final Intent data);
|
||||
|
||||
public boolean isConnected();
|
||||
//public boolean isConnected();
|
||||
|
||||
public static int MAGIC_NUMBER_JFS = 874345;
|
||||
public static int RESULT_FULL_FILENAME_SELECTED = MAGIC_NUMBER_JFS+1;
|
||||
public static int RESULT_FILECHOOSER_PREPARED = MAGIC_NUMBER_JFS+2;
|
||||
public static int RESULT_FILEUSAGE_PREPARED = MAGIC_NUMBER_JFS+3;
|
||||
|
||||
public boolean requiresSetup(String path);
|
||||
|
||||
public void startSelectFile(FileStorageSetupInitiatorActivity activity, boolean isForSave, int requestCode);
|
||||
|
||||
public void prepareFileUsage(FileStorageSetupInitiatorActivity activity, String path, int requestCode);
|
||||
|
||||
public String getProtocolId();
|
||||
|
||||
public boolean checkForFileChangeFast(String path, String previousFileVersion) throws Exception;
|
||||
|
||||
@@ -88,4 +131,9 @@ public class FileEntry {
|
||||
|
||||
public void delete(String path) throws Exception;
|
||||
|
||||
public void onCreate(FileStorageSetupActivity activity, Bundle savedInstanceState);
|
||||
public void onResume(FileStorageSetupActivity activity);
|
||||
public void onStart(FileStorageSetupActivity activity);
|
||||
public void onActivityResult(FileStorageSetupActivity activity, int requestCode, int resultCode, Intent data);
|
||||
|
||||
}
|
||||
@@ -980,11 +980,14 @@ public class FragmentFiles extends Fragment implements
|
||||
|
||||
if (path == null
|
||||
|| !BaseFileProviderUtils.isDirectory(getActivity(), path))
|
||||
{
|
||||
Log.d(CLASSNAME, "load default path");
|
||||
path = BaseFileProviderUtils
|
||||
.getDefaultPath(
|
||||
getActivity(),
|
||||
path == null ? mFileProviderAuthority : path
|
||||
.getAuthority());
|
||||
}
|
||||
|
||||
if (path == null) {
|
||||
showCannotConnectToServiceAndFinish();
|
||||
|
||||
@@ -213,10 +213,7 @@ public class DisplayPrefs extends Prefs {
|
||||
* @since v4.7 beta
|
||||
*/
|
||||
public static boolean isRememberLastLocation(Context c) {
|
||||
return p(c).getBoolean(
|
||||
c.getString(R.string.afc_pkey_display_remember_last_location),
|
||||
c.getResources().getBoolean(
|
||||
R.bool.afc_pkey_display_remember_last_location_def));
|
||||
return false; //KP2A: don't allow to remember because of different protocols
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,7 +13,7 @@ public class Kp2aFileChooserBridge {
|
||||
Class<?> cls = FileChooserActivity.class;
|
||||
|
||||
Intent intent = new Intent(ctx, cls);
|
||||
|
||||
intent.putExtra(FileChooserActivity.EXTRA_FILE_PROVIDER_AUTHORITY, authority);
|
||||
intent.putExtra(FileChooserActivity.EXTRA_ROOTPATH,
|
||||
BaseFile.genContentIdUriBase(authority)
|
||||
.buildUpon()
|
||||
|
||||
@@ -230,7 +230,11 @@ public abstract class Kp2aFileProvider extends BaseFileProvider {
|
||||
private MatrixCursor doAnswerApiCommand(Uri uri) {
|
||||
MatrixCursor matrixCursor = null;
|
||||
|
||||
if (BaseFile.CMD_CANCEL.equals(uri.getLastPathSegment())) {
|
||||
String lastPathSegment = uri.getLastPathSegment();
|
||||
|
||||
//Log.d(CLASSNAME, "lastPathSegment:" + lastPathSegment);
|
||||
|
||||
if (BaseFile.CMD_CANCEL.equals(lastPathSegment)) {
|
||||
int taskId = ProviderUtils.getIntQueryParam(uri,
|
||||
BaseFile.PARAM_TASK_ID, 0);
|
||||
synchronized (mMapInterruption) {
|
||||
@@ -241,15 +245,14 @@ public abstract class Kp2aFileProvider extends BaseFileProvider {
|
||||
mMapInterruption.put(taskId, true);
|
||||
}
|
||||
return null;
|
||||
} else if (BaseFile.CMD_GET_DEFAULT_PATH.equals(uri
|
||||
.getLastPathSegment())) {
|
||||
} else if (BaseFile.CMD_GET_DEFAULT_PATH.equals(lastPathSegment)) {
|
||||
|
||||
return null;
|
||||
|
||||
}// get default path
|
||||
else if (BaseFile.CMD_IS_ANCESTOR_OF.equals(uri.getLastPathSegment())) {
|
||||
else if (BaseFile.CMD_IS_ANCESTOR_OF.equals(lastPathSegment)) {
|
||||
return doCheckAncestor(uri);
|
||||
} else if (BaseFile.CMD_GET_PARENT.equals(uri.getLastPathSegment())) {
|
||||
} else if (BaseFile.CMD_GET_PARENT.equals(lastPathSegment)) {
|
||||
|
||||
{
|
||||
String path = Uri.parse(
|
||||
@@ -292,7 +295,7 @@ public abstract class Kp2aFileProvider extends BaseFileProvider {
|
||||
return matrixCursor;
|
||||
}
|
||||
|
||||
} else if (BaseFile.CMD_SHUTDOWN.equals(uri.getLastPathSegment())) {
|
||||
} else if (BaseFile.CMD_SHUTDOWN.equals(lastPathSegment)) {
|
||||
/*
|
||||
* TODO Stop all tasks. If the activity call this command in
|
||||
* onDestroy(), it seems that this code block will be suspended and
|
||||
@@ -492,7 +495,8 @@ public abstract class Kp2aFileProvider extends BaseFileProvider {
|
||||
|
||||
//puts the file entry in the cache for later reuse with retrieveFileInfo
|
||||
private void updateFileEntryCache(FileEntry f) {
|
||||
fileEntryMap.put(f.path, f);
|
||||
if (f != null)
|
||||
fileEntryMap.put(f.path, f);
|
||||
}
|
||||
//removes the file entry from the cache (if cached). Should be called whenever the file changes
|
||||
private void removeFromCache(String filename, boolean recursive) {
|
||||
@@ -501,11 +505,17 @@ public abstract class Kp2aFileProvider extends BaseFileProvider {
|
||||
if (recursive)
|
||||
{
|
||||
Set<String> keys = fileEntryMap.keySet();
|
||||
Set<String> keysToRemove = new HashSet<String>();
|
||||
for (String key: keys)
|
||||
{
|
||||
if (key.startsWith(key))
|
||||
fileEntryMap.remove(key);
|
||||
keysToRemove.add(key);
|
||||
}
|
||||
for (String key: keysToRemove)
|
||||
{
|
||||
fileEntryMap.remove(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user