Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,18 @@ android {
}
}

apply plugin: 'com.getkeepsafe.dexcount'

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
// View annotation bindings
compile 'com.jakewharton:butterknife:7.0.1'
// Dependency injection tool
compile 'com.google.dagger:dagger:2.0.1'
// Charts
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
// Advanced logging tool
compile 'com.jakewharton.timber:timber:4.1.2'

compile 'com.jakewharton:butterknife:7.0.1' // View annotation bindings
compile 'com.google.dagger:dagger:2.0.1' // Dependency injection tool
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4' // Charts
compile 'com.jakewharton.timber:timber:4.1.2' // Advanced logging tool

apt 'com.google.dagger:dagger-compiler:2.0.1'
provided 'org.glassfish:javax.annotation:10.0-b28'
Expand Down
Binary file added app/libs/dropbox-android-sdk-1.6.3.jar
Binary file not shown.
Binary file added app/libs/json_simple-1.1.jar
Binary file not shown.
30 changes: 23 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blogspot.e_kanivets.moneytracker">

<!-- Used only for Dropbox backup -->
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MtApp"
android:allowBackup="true"
Expand Down Expand Up @@ -58,11 +61,6 @@
android:label="@string/title_exchange_rates"
android:screenOrientation="portrait"
android:theme="@style/Theme.Default" />
<activity
android:name=".activity.external.ExportActivity"
android:label="@string/title_export"
android:screenOrientation="portrait"
android:theme="@style/Theme.Default" />
<activity
android:name=".activity.SettingsActivity"
android:label="@string/title_settings"
Expand All @@ -74,10 +72,28 @@
android:screenOrientation="portrait"
android:theme="@style/Theme.Default" />
<activity
android:name=".activity.external.ImportActivity"
android:label="@string/title_import"
android:name=".activity.external.ImportExportActivity"
android:label="@string/title_import_export"
android:screenOrientation="portrait"
android:theme="@style/Theme.Default" />
<activity
android:name=".activity.external.BackupActivity"
android:label="@string/backup_data"
android:screenOrientation="portrait"
android:theme="@style/Theme.Default" />
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<data android:scheme="db-5lqugcckdy9y6lj" />
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<provider
android:name="android.support.v4.content.FileProvider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* @author Evgenii Kanivets
*/
public class MtApp extends Application {

private static MtApp mtApp;

public static MtApp get() {
Expand All @@ -30,7 +29,7 @@ public void onCreate() {
super.onCreate();

mtApp = this;
component = buildComponent();
buildAppComponent();

if (BuildConfig.DEBUG) Timber.plant(new Timber.DebugTree());
else Timber.plant(new ReleaseTree());
Expand All @@ -40,6 +39,10 @@ public AppComponent getAppComponent() {
return component;
}

public void buildAppComponent() {
component = buildComponent();
}

private AppComponent buildComponent() {
return DaggerAppComponent.builder()
.cachedRepoModule(new CachedRepoModule(get()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
Expand Down Expand Up @@ -77,6 +78,18 @@ public void stopProgress() {
getProgressBar().dismiss();
}

public void showAlert(@Nullable String title, @Nullable String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, null);
builder.setCancelable(false);

AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}

private ProgressDialog getProgressBar() {
if (progressDialog == null) progressDialog = new ProgressDialog(this);
return progressDialog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.activity.ChartsActivity;
import com.blogspot.e_kanivets.moneytracker.activity.external.ExportActivity;
import com.blogspot.e_kanivets.moneytracker.activity.external.ImportActivity;
import com.blogspot.e_kanivets.moneytracker.activity.external.BackupActivity;
import com.blogspot.e_kanivets.moneytracker.activity.external.ImportExportActivity;
import com.blogspot.e_kanivets.moneytracker.activity.SettingsActivity;
import com.blogspot.e_kanivets.moneytracker.activity.account.AccountsActivity;
import com.blogspot.e_kanivets.moneytracker.activity.exchange_rate.ExchangeRatesActivity;
Expand All @@ -30,7 +30,8 @@ public abstract class BaseDrawerActivity extends BaseActivity
private static final int REQUEST_ACCOUNTS = 1;
private static final int REQUEST_RATES = 2;
private static final int REQUEST_SETTINGS = 3;
private static final int REQUEST_IMPORT = 4;
private static final int REQUEST_IMPORT_EXPORT = 4;
protected static final int REQUEST_BACKUP = 5;

@Bind(R.id.drawer_layout)
DrawerLayout drawer;
Expand Down Expand Up @@ -84,13 +85,14 @@ public boolean onNavigationItemSelected(MenuItem item) {
startActivity(new Intent(BaseDrawerActivity.this, ChartsActivity.class));
break;

case R.id.nav_import:
startActivityForResult(new Intent(BaseDrawerActivity.this, ImportActivity.class),
REQUEST_IMPORT);
case R.id.nav_backup:
startActivityForResult(new Intent(BaseDrawerActivity.this, BackupActivity.class),
REQUEST_BACKUP);
break;

case R.id.nav_export:
startActivity(new Intent(BaseDrawerActivity.this, ExportActivity.class));
case R.id.nav_import_export:
startActivityForResult(new Intent(BaseDrawerActivity.this, ImportExportActivity.class),
REQUEST_IMPORT_EXPORT);
break;

case R.id.nav_settings:
Expand Down Expand Up @@ -124,7 +126,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
update();
break;

case REQUEST_IMPORT:
case REQUEST_IMPORT_EXPORT:
update();
break;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.blogspot.e_kanivets.moneytracker.activity.external;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.blogspot.e_kanivets.moneytracker.MtApp;
import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
import com.blogspot.e_kanivets.moneytracker.controller.BackupController;
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AppKeyPair;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnItemClick;
import timber.log.Timber;

public class BackupActivity extends BaseBackActivity {
private static final String APP_KEY = "5lqugcckdy9y6lj";
private static final String APP_SECRET = "psbu50k9713u68j";

@Inject
PreferenceController preferenceController;
@Inject
BackupController backupController;

private DropboxAPI<AndroidAuthSession> dbApi;

@Bind(R.id.btn_backup_now)
View btnBackupNow;
@Bind(R.id.list_view)
ListView listView;

@Override
protected int getContentViewId() {
return R.layout.activity_backup;
}

@Override
protected boolean initData() {
getAppComponent().inject(BackupActivity.this);

AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
String accessToken = preferenceController.readDropboxAccessToken();

AndroidAuthSession session = new AndroidAuthSession(appKeys);
dbApi = new DropboxAPI<>(session);
if (accessToken == null) dbApi.getSession().startOAuth2Authentication(BackupActivity.this);
else {
dbApi.getSession().setOAuth2AccessToken(accessToken);
fetchBackups();
}

return super.initData();
}

@Override
protected void initViews() {
super.initViews();
btnBackupNow.setEnabled(preferenceController.readDropboxAccessToken() != null);
}

@Override
protected void onResume() {
super.onResume();

if (dbApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the session
dbApi.getSession().finishAuthentication();
preferenceController.writeDropboxAccessToken(dbApi.getSession().getOAuth2AccessToken());
btnBackupNow.setEnabled(true);
fetchBackups();
} catch (IllegalStateException e) {
Timber.e("Error authenticating: %s", e.getMessage());
}
}
}

@OnClick(R.id.btn_backup_now)
public void backupNow() {
startProgress();
backupController.makeBackup(dbApi, new BackupController.OnBackupListener() {
@Override
public void onBackupSuccess() {
Timber.d("Backup success.");
stopProgress();
fetchBackups();
}

@Override
public void onBackupFailure(String reason) {
Timber.d("Backup failure.");
stopProgress();
showToast(R.string.failed_create_backup);

if (BackupController.OnBackupListener.ERROR_AUTHENTICATION.equals(reason)) logout();
}
});
}

@OnItemClick(R.id.list_view)
public void restoreBackupClicked(int position) {
final String backupName = listView.getAdapter().getItem(position).toString();

AlertDialog.Builder builder = new AlertDialog.Builder(BackupActivity.this);
builder.setTitle(getString(R.string.warning));
builder.setMessage(getString(R.string.want_erase_and_restore, backupName));
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
restoreBackup(backupName);
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}

private void restoreBackup(final String backupName) {
startProgress();
backupController.restoreBackup(dbApi, backupName, new BackupController.OnRestoreBackupListener() {
@Override
public void onRestoreSuccess() {
Timber.d("Restore success.");
stopProgress();

AlertDialog.Builder builder = new AlertDialog.Builder(BackupActivity.this);
builder.setTitle(getString(R.string.backup_is_restored));
builder.setMessage(getString(R.string.backup_restored, backupName));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
MtApp.get().buildAppComponent();
setResult(RESULT_OK);
finish();
}
});
builder.setPositiveButton(android.R.string.ok, null);
builder.show();
}

@Override
public void onRestoreFailure(String reason) {
Timber.d("Restore failure.");
stopProgress();
showToast(R.string.failed_restore_backup);

if (BackupController.OnRestoreBackupListener.ERROR_AUTHENTICATION.equals(reason))
logout();
}
});
}

private void fetchBackups() {
startProgress();
backupController.fetchBackups(dbApi, new BackupController.OnFetchBackupListListener() {
@Override
public void onBackupsFetched(@NonNull List<String> backupList) {
stopProgress();
ArrayAdapter<String> adapter = new ArrayAdapter<>(BackupActivity.this,
android.R.layout.simple_list_item_1, backupList);
listView.setAdapter(adapter);
}
});
}

private void logout() {
preferenceController.writeDropboxAccessToken(null);
dbApi.getSession().startOAuth2Authentication(BackupActivity.this);
btnBackupNow.setEnabled(false);
}
}
Loading