Skip to content

Commit

Permalink
Fix Google drive issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Barashkov committed May 26, 2020
1 parent 9e41c04 commit 4a78a6e
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 119 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// noinspection GradleCompatible
repositories {
google()
jcenter()
Expand All @@ -21,10 +20,11 @@ android {
applicationId "com.handydev.financier"
minSdkVersion 21
targetSdkVersion 29
versionCode 204
versionName "2.0.4"
versionCode 205
versionName "2.0.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
setProperty("archivesBaseName", "Financier_Full_$versionName")
javaCompileOptions {
annotationProcessorOptions {
arguments = [
Expand Down Expand Up @@ -97,14 +97,14 @@ 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 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
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'
implementation 'com.google.apis:google-api-services-drive:v3-rev136-1.25.0'
//dependent by google drive sample
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0-alpha03'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation ('androidx.work:work-runtime-ktx:2.3.4'){
//Needed due to a transitive dependency clash with the google api client dep
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/assets/whatsnew.htm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@
</head>
<body>

<p><b>2.0.5</b></p>
<p>
[+] Google Drive folder selector is back.<br/>
[*] Google Drive folder selector is back.<br/>
[*] Fixed some filter UI's white on white.<br/>
</p>

<p><b>2.0.4</b></p>
<p>
[+] Google Drive works again.<br/>
[*] UI fixes.<br/>
</p>

<p><b>2.0.3</b></p>
<p>
[+] Bugfixes.<br/>
[*] Bugfixes.<br/>
</p>

<p><b>2.0.0</b></p>
Expand All @@ -28,7 +41,7 @@
[+] Material UI redesign.<br/>
[+] Updated third-party libraries.<br/>
[+] Added an option to color each other row in lists.<br/>
[+] Currency rate update now uses https and works again.<br/>
[*] Currency rate update now uses https and works again.<br/>
</p>

<p><b>1.8.2</b></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,16 @@ public View addNodeUnsplit(LinearLayout layout) {
return v;
}

public View addSplitNodeMinus(LinearLayout layout, int id, int minusId, int labelId, String defaultValue) {
View addSplitNodeMinus(LinearLayout layout, int id, int minusId, int labelId, String defaultValue) {
ListBuilder b = inflater.new ListBuilder(layout, R.layout.select_entry_minus);
return b.withButtonId(minusId, listener).withoutMoreButton().withId(id, listener).withLabel(labelId).withData(defaultValue).create();
}

public TextView addFilterNodeMinus(LinearLayout layout, int id, int minusId, int labelId, int defaultValueResId) {
TextView addFilterNodeMinus(LinearLayout layout, int id, int minusId, int labelId, int defaultValueResId) {
return addFilterNodeMinus(layout, id, minusId, labelId, defaultValueResId, null);
}

public TextView addFilterNodeMinus(LinearLayout layout, int id, int minusId, int labelId, int defaultValueResId, String defaultValue) {
TextView addFilterNodeMinus(LinearLayout layout, int id, int minusId, int labelId, int defaultValueResId, String defaultValue) {
Builder b = inflater.new ListBuilder(layout, R.layout.select_entry_minus).withButtonId(minusId, listener).withId(id, listener).withLabel(labelId);
if (defaultValue != null) {
b.withData(defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,24 @@
******************************************************************************/
package com.handydev.financier.activity;

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
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.android.gms.common.api.Scope;
import com.google.api.services.drive.DriveScopes;
import com.handydev.financier.R;
import com.handydev.financier.app.FinancierApp;
Expand All @@ -55,22 +41,22 @@

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;
import static com.handydev.financier.utils.FingerprintUtils.fingerprintUnavailable;
import static com.handydev.financier.utils.FingerprintUtils.reasonWhyFingerprintUnavailable;

public class PreferencesActivity extends PreferenceActivity {
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

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

Preference pOpenExchangeRatesAppId;

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
setGDriveBackupFolder();
}


@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(MyPreferences.switchLocale(base));
Expand All @@ -80,8 +66,8 @@ protected void attachBaseContext(Context base) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);

PreferenceScreen preferenceScreen = getPreferenceScreen();
setGDriveBackupFolder();
Preference pLocale = preferenceScreen.findPreference("ui_language");
pLocale.setOnPreferenceChangeListener((preference, newValue) -> {
String locale = (String) newValue;
Expand All @@ -99,13 +85,13 @@ protected void onCreate(Bundle savedInstanceState) {
return true;
});
Preference pDatabaseBackupFolder = preferenceScreen.findPreference("database_backup_folder");
pDatabaseBackupFolder.setOnPreferenceClickListener(arg0 -> {
if (isRequestingPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
return false;
}
selectDatabaseBackupFolder();

Preference pGDriveBackupFolder = preferenceScreen.findPreference("backup_folder");
pGDriveBackupFolder.setOnPreferenceChangeListener((preference, o) -> {
setGDriveBackupFolder();
return true;
});

Preference pAuthDropbox = preferenceScreen.findPreference("dropbox_authorize");
pAuthDropbox.setOnPreferenceClickListener(arg0 -> {
authDropbox();
Expand Down Expand Up @@ -199,6 +185,11 @@ private void setCurrentDatabaseBackupFolder() {
pDatabaseBackupFolder.setSummary(summary);
}

private void setGDriveBackupFolder() {
Preference pGDriveBackupFolder = getPreferenceScreen().findPreference("backup_folder");
pGDriveBackupFolder.setSummary(MyPreferences.getBackupFolder(this));
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down Expand Up @@ -265,15 +256,16 @@ private void deAuthDropbox() {
@Override
protected void onPause() {
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
PinProtection.lock(this);
}

@Override
protected void onResume() {
super.onResume();
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
PinProtection.unlock(this);
dropbox.completeAuth();
linkToDropbox();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,39 +114,39 @@ class MenuListFragment: ListFragment() {
*/

//@OnActivityResult(MenuListItem.ACTIVITY_CSV_EXPORT)
fun onCsvExportResult(resultCode: Int, data: Intent?) {
private fun onCsvExportResult(resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && activity != null) {
val options = CsvExportOptions.fromIntent(data)
MenuListItem.doCsvExport(activity!!, options)
}
}

// @OnActivityResult(MenuListItem.ACTIVITY_QIF_EXPORT)
fun onQifExportResult(resultCode: Int, data: Intent?) {
private fun onQifExportResult(resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && activity != null) {
val options = QifExportOptions.fromIntent(data)
MenuListItem.doQifExport(activity!!, options)
}
}

//@OnActivityResult(MenuListItem.ACTIVITY_CSV_IMPORT)
fun onCsvImportResult(resultCode: Int, data: Intent?) {
private fun onCsvImportResult(resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && activity != null) {
val options = CsvImportOptions.fromIntent(data)
MenuListItem.doCsvImport(activity!!, options)
}
}

//@OnActivityResult(MenuListItem.ACTIVITY_QIF_IMPORT)
fun onQifImportResult(resultCode: Int, data: Intent?) {
private fun onQifImportResult(resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && activity != null) {
val options = QifImportOptions.fromIntent(data)
MenuListItem.doQifImport(activity!!, options)
}
}

//@OnActivityResult(MenuListItem.ACTIVITY_CHANGE_PREFERENCES)
fun onChangePreferences() {
private fun onChangePreferences() {
if(activity != null) {
DailyAutoBackupScheduler.scheduleNextAutoBackup(activity!!)
}
Expand Down Expand Up @@ -196,8 +196,8 @@ class MenuListFragment: ListFragment() {
bus!!.post(DoDriveBackup())
}

var resumeGoogleDriveBackupAction = false
var resumeGoogleDriveRestoreAction = false
private var resumeGoogleDriveBackupAction = false
private var resumeGoogleDriveRestoreAction = false

@Subscribe(threadMode = ThreadMode.MAIN)
fun resumeDriveAction(e: ResumeDriveAction?) {
Expand All @@ -210,7 +210,7 @@ class MenuListFragment: ListFragment() {
return
}
if(resumeGoogleDriveBackupAction) {
bus!!.post(DoDriveBackup())
bus!!.post(StartDriveBackup())
} else if(resumeGoogleDriveRestoreAction) {
bus!!.post(StartDriveRestore())
}
Expand Down Expand Up @@ -342,7 +342,7 @@ class MenuListFragment: ListFragment() {
}

//@OnActivityResult(RESOLVE_CONNECTION_REQUEST_CODE)
fun onConnectionRequest(resultCode: Int) {
private fun onConnectionRequest(resultCode: Int) {
if (resultCode == Activity.RESULT_OK && activity != null) {
Toast.makeText(activity!!, R.string.google_drive_connection_resolved, Toast.LENGTH_LONG).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ suspend fun <T> DriveRequest<T>.executeWithCoroutines(): T {
fun Drive.fetchOrCreateAppFolder(folderName: String): String {
val folderList = getAppFolder()

return if (folderList.files.isEmpty()) {
var folderExists = false
if(folderList.files.isNotEmpty()) {
for(folder in folderList) {
if((folder as? File)?.name == folderName) {
folderExists = true
break
}
}
}

return if (!folderExists) {
val fileMetadata = File().apply {
name = folderName
mimeType = DriveContentType.DRIVE_FOLDER.mimeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import com.handydev.financier.backup.DatabaseExport
import com.handydev.financier.backup.DatabaseImport
import com.handydev.financier.db.DatabaseAdapter
import com.handydev.financier.db.DatabaseAdapter_
import com.handydev.financier.export.ImportExportException
import com.handydev.financier.export.drive.*
import com.handydev.financier.utils.MyPreferences
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
Expand All @@ -25,10 +27,9 @@ class GoogleDriveClient(var context: Context) {
private var driveService: Drive? = null
private var appFolderId: String? = null

var db: DatabaseAdapter
var db: DatabaseAdapter = DatabaseAdapter_.getInstance_(context)

init {
db = DatabaseAdapter_.getInstance_(context)
EventBus.getDefault().register(this)
}

Expand All @@ -51,31 +52,20 @@ class GoogleDriveClient(var context: Context) {
if(driveService == null) {
return false
}
appFolderId = driveService!!.fetchOrCreateAppFolder(
context.getString(R.string.application_folder)
)

/*
Collection<String> scopes = new ArrayList<String>() {{
add(DriveScopes.DRIVE_FILE);
add(DriveScopes.DRIVE_APPDATA);
}};
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scopes);
googleApiClient = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),
credential).setApplicationName("Financier").build();
googleApiClient = new GoogleApiClient.Builder(context)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.setAccountName(googleDriveAccount)
//.addConnectionCallbacks(this)
//.addOnConnectionFailedListener(this)
.build();*/
appFolderId = driveService!!.fetchOrCreateAppFolder(getDriveFolderName())
}
return driveService != null;
//return googleApiClient.blockingConnect(1, TimeUnit.MINUTES);
}

private fun getDriveFolderName(): String {
val folder = MyPreferences.getBackupFolder(context)
if(!(folder != null && folder.isNotEmpty())) {
throw ImportExportException(R.string.gdocs_folder_not_configured)
}
return folder
}


@Subscribe(threadMode = ThreadMode.BACKGROUND)
fun doBackup(event: DoDriveBackup) {
val export = DatabaseExport(context, db.db(), true)
Expand Down
Loading

0 comments on commit 4a78a6e

Please sign in to comment.