Skip to content

Commit

Permalink
Google Drive works again. Other fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Barashkov committed May 23, 2020
1 parent f0dd2ce commit 9e41c04
Show file tree
Hide file tree
Showing 17 changed files with 412 additions and 110 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ dependencies {
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

//googledrive
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
implementation 'com.google.android.gms:play-services-auth:18.0.0'
implementation 'com.google.http-client:google-http-client-gson:1.26.0'
implementation 'com.google.api-client:google-api-client-android:1.26.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,38 @@
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.common.api.Scope;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.AccountPicker;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
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.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.handydev.financier.R;
import com.handydev.financier.app.FinancierApp;
import com.handydev.financier.dialog.FolderBrowser;
import com.handydev.financier.export.Export;
import com.handydev.financier.export.drive.DriveBackupError;
import com.handydev.financier.export.dropbox.Dropbox;
import com.handydev.financier.rates.ExchangeRateProviderFactory;
import com.handydev.financier.utils.MyPreferences;
import com.handydev.financier.utils.PinProtection;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collection;

import static android.Manifest.permission.GET_ACCOUNTS;
import static com.handydev.financier.activity.RequestPermission.isRequestingPermission;
import static com.handydev.financier.activity.RequestPermission.isRequestingPermissions;
Expand All @@ -45,7 +67,7 @@
public class PreferencesActivity extends PreferenceActivity {

private static final int SELECT_DATABASE_FOLDER = 100;
private static final int CHOOSE_ACCOUNT = 101;
public static final int CHOOSE_ACCOUNT = 101;

Preference pOpenExchangeRatesAppId;

Expand Down Expand Up @@ -124,17 +146,15 @@ private boolean isOpenExchangeRatesProvider(String provider) {
}

private void chooseAccount() {
try {
if (isRequestingPermissions(this, GET_ACCOUNTS, "android.permission.USE_CREDENTIALS")) {
return;
}
Account selectedAccount = getSelectedAccount();
Intent intent = AccountPicker.newChooseAccountIntent(selectedAccount, null,
new String[]{"com.google"}, true, null, null, null, null);
startActivityForResult(intent, CHOOSE_ACCOUNT);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.google_drive_account_select_error, Toast.LENGTH_LONG).show();
}
GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(DriveScopes.DRIVE_FILE), new Scope(DriveScopes.DRIVE_APPDATA))
.build();
GoogleSignInClient client = GoogleSignIn.getClient(this, signInOptions);
client.signOut().addOnSuccessListener(aVoid -> {
MyPreferences.setGoogleDriveAccount(PreferencesActivity.this, "");
startActivityForResult(client.getSignInIntent(), CHOOSE_ACCOUNT);
});
}

private Account getSelectedAccount() {
Expand Down Expand Up @@ -190,20 +210,20 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
setCurrentDatabaseBackupFolder();
break;
case CHOOSE_ACCOUNT:
if (data != null) {
Bundle b = data.getExtras();
String accountName = b.getString(AccountManager.KEY_ACCOUNT_NAME);
Log.d("Preferences", "Selected account: " + accountName);
if (accountName != null && accountName.length() > 0) {
MyPreferences.setGoogleDriveAccount(this, accountName);
selectAccount();
}
}
handleSignInResult(data);
break;
}
}
}

private void handleSignInResult(Intent intent) {
GoogleSignIn.getSignedInAccountFromIntent(intent).addOnSuccessListener(googleSignInAccount -> {
MyPreferences.setGoogleDriveAccount(this, googleSignInAccount.getEmail());
FinancierApp.driveClient.setAccount(googleSignInAccount.getAccount());
selectAccount();
}).addOnFailureListener(e -> EventBus.getDefault().post(new DriveBackupError(e.getMessage())));
}

private void selectAccount() {
Preference pDriveAccount = getPreferenceScreen().findPreference("google_drive_backup_account");
Account account = getSelectedAccount();
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/handydev/financier/app/FinancierApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import androidx.multidex.MultiDexApplication;

import com.handydev.financier.export.drive.GoogleDriveClient;
import com.handydev.financier.utils.MyPreferences;
import com.handydev.main.googledrive.GoogleDriveClient;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EApplication;
import org.greenrobot.eventbus.EventBus;

Expand All @@ -18,18 +17,21 @@ public class FinancierApp extends MultiDexApplication {

public EventBus bus;

@Bean
public GoogleDriveClient driveClient;
//@Bean

public static GoogleDriveClient driveClient;

@AfterInject
public void init() {
bus = EventBus.getDefault();
driveClient = new GoogleDriveClient(getApplicationContext());
//bus.register(driveClient);
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(MyPreferences.switchLocale(base));

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static DatabaseImport createFromDropboxBackup(Context context, DatabaseAd
return new DatabaseImport(context, dbAdapter, in);
}

private DatabaseImport(Context context, DatabaseAdapter dbAdapter, InputStream backupStream) {
public DatabaseImport(Context context, DatabaseAdapter dbAdapter, InputStream backupStream) {
super(context, dbAdapter);
this.schemaEvolution = new DatabaseSchemaEvolution(context, Database.DATABASE_NAME, null, Database.DATABASE_VERSION);
this.backupStream = backupStream;
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/handydev/financier/export/Export.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

import com.handydev.financier.R;
import com.handydev.financier.activity.RequestPermission;
import com.handydev.financier.export.drive.GoogleDriveClient;
import com.handydev.financier.export.drive.GoogleDriveClient_;
import com.handydev.financier.app.FinancierApp;
import com.handydev.financier.export.dropbox.Dropbox;
import com.handydev.financier.utils.MyPreferences;
import com.handydev.main.googledrive.GoogleDriveClient;

public abstract class Export {

Expand Down Expand Up @@ -126,8 +126,7 @@ public static void uploadBackupFileToDropbox(Context context, String backupFileN

public static void uploadBackupFileToGoogleDrive(Context context, String backupFileName) throws Exception {
File file = getBackupFile(context, backupFileName);
GoogleDriveClient driveClient = GoogleDriveClient_.getInstance_(context);
driveClient.uploadFile(file);
//GoogleDriveClient client = new GoogleDriveClient(context);
FinancierApp.driveClient.uploadFile(file);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.handydev.financier.export.drive;

import com.google.api.services.drive.model.File;

public class DoDriveRestore {

public final DriveFileInfo selectedDriveFile;
public final File file;

public DoDriveRestore(DriveFileInfo selectedDriveFile) {
this.selectedDriveFile = selectedDriveFile;
public DoDriveRestore(File file) {
this.file = file;
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.handydev.financier.export.drive;

import com.google.android.gms.common.api.Status;

public class DriveBackupFailure {

public final Status status;

public DriveBackupFailure(Status status) {
this.status = status;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.handydev.financier.export.drive;

import java.util.List;
import com.google.api.services.drive.model.FileList;

public class DriveFileList {

public final List<DriveFileInfo> files;
public final FileList files;

public DriveFileList(List<DriveFileInfo> files) {
public DriveFileList(FileList files) {
this.files = files;
}

Expand Down
Loading

0 comments on commit 9e41c04

Please sign in to comment.