|
| 1 | +package com.blogspot.e_kanivets.moneytracker.activity.external; |
| 2 | + |
| 3 | +import android.content.DialogInterface; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | +import android.support.v7.app.AlertDialog; |
| 6 | +import android.view.View; |
| 7 | +import android.widget.ArrayAdapter; |
| 8 | +import android.widget.ListView; |
| 9 | + |
| 10 | +import com.blogspot.e_kanivets.moneytracker.MtApp; |
| 11 | +import com.blogspot.e_kanivets.moneytracker.R; |
| 12 | +import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity; |
| 13 | +import com.blogspot.e_kanivets.moneytracker.controller.BackupController; |
| 14 | +import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController; |
| 15 | +import com.dropbox.client2.DropboxAPI; |
| 16 | +import com.dropbox.client2.android.AndroidAuthSession; |
| 17 | +import com.dropbox.client2.session.AppKeyPair; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import javax.inject.Inject; |
| 22 | + |
| 23 | +import butterknife.Bind; |
| 24 | +import butterknife.OnClick; |
| 25 | +import butterknife.OnItemClick; |
| 26 | +import timber.log.Timber; |
| 27 | + |
| 28 | +public class BackupActivity extends BaseBackActivity { |
| 29 | + private static final String APP_KEY = "5lqugcckdy9y6lj"; |
| 30 | + private static final String APP_SECRET = "psbu50k9713u68j"; |
| 31 | + |
| 32 | + @Inject |
| 33 | + PreferenceController preferenceController; |
| 34 | + @Inject |
| 35 | + BackupController backupController; |
| 36 | + |
| 37 | + private DropboxAPI<AndroidAuthSession> dbApi; |
| 38 | + |
| 39 | + @Bind(R.id.btn_backup_now) |
| 40 | + View btnBackupNow; |
| 41 | + @Bind(R.id.list_view) |
| 42 | + ListView listView; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected int getContentViewId() { |
| 46 | + return R.layout.activity_backup; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected boolean initData() { |
| 51 | + getAppComponent().inject(BackupActivity.this); |
| 52 | + |
| 53 | + AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); |
| 54 | + String accessToken = preferenceController.readDropboxAccessToken(); |
| 55 | + |
| 56 | + AndroidAuthSession session = new AndroidAuthSession(appKeys); |
| 57 | + dbApi = new DropboxAPI<>(session); |
| 58 | + if (accessToken == null) dbApi.getSession().startOAuth2Authentication(BackupActivity.this); |
| 59 | + else { |
| 60 | + dbApi.getSession().setOAuth2AccessToken(accessToken); |
| 61 | + fetchBackups(); |
| 62 | + } |
| 63 | + |
| 64 | + return super.initData(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void initViews() { |
| 69 | + super.initViews(); |
| 70 | + btnBackupNow.setEnabled(preferenceController.readDropboxAccessToken() != null); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void onResume() { |
| 75 | + super.onResume(); |
| 76 | + |
| 77 | + if (dbApi.getSession().authenticationSuccessful()) { |
| 78 | + try { |
| 79 | + // Required to complete auth, sets the access token on the session |
| 80 | + dbApi.getSession().finishAuthentication(); |
| 81 | + preferenceController.writeDropboxAccessToken(dbApi.getSession().getOAuth2AccessToken()); |
| 82 | + btnBackupNow.setEnabled(true); |
| 83 | + fetchBackups(); |
| 84 | + } catch (IllegalStateException e) { |
| 85 | + Timber.e("Error authenticating: %s", e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @OnClick(R.id.btn_backup_now) |
| 91 | + public void backupNow() { |
| 92 | + startProgress(); |
| 93 | + backupController.makeBackup(dbApi, new BackupController.OnBackupListener() { |
| 94 | + @Override |
| 95 | + public void onBackupSuccess() { |
| 96 | + Timber.d("Backup success."); |
| 97 | + stopProgress(); |
| 98 | + fetchBackups(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onBackupFailure(String reason) { |
| 103 | + Timber.d("Backup failure."); |
| 104 | + stopProgress(); |
| 105 | + showToast(R.string.failed_create_backup); |
| 106 | + |
| 107 | + if (BackupController.OnBackupListener.ERROR_AUTHENTICATION.equals(reason)) logout(); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + @OnItemClick(R.id.list_view) |
| 113 | + public void restoreBackupClicked(int position) { |
| 114 | + final String backupName = listView.getAdapter().getItem(position).toString(); |
| 115 | + |
| 116 | + AlertDialog.Builder builder = new AlertDialog.Builder(BackupActivity.this); |
| 117 | + builder.setTitle(getString(R.string.warning)); |
| 118 | + builder.setMessage(getString(R.string.want_erase_and_restore, backupName)); |
| 119 | + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 120 | + @Override |
| 121 | + public void onClick(DialogInterface dialog, int which) { |
| 122 | + restoreBackup(backupName); |
| 123 | + } |
| 124 | + }); |
| 125 | + builder.setNegativeButton(android.R.string.cancel, null); |
| 126 | + builder.show(); |
| 127 | + } |
| 128 | + |
| 129 | + private void restoreBackup(final String backupName) { |
| 130 | + startProgress(); |
| 131 | + backupController.restoreBackup(dbApi, backupName, new BackupController.OnRestoreBackupListener() { |
| 132 | + @Override |
| 133 | + public void onRestoreSuccess() { |
| 134 | + Timber.d("Restore success."); |
| 135 | + stopProgress(); |
| 136 | + |
| 137 | + AlertDialog.Builder builder = new AlertDialog.Builder(BackupActivity.this); |
| 138 | + builder.setTitle(getString(R.string.backup_is_restored)); |
| 139 | + builder.setMessage(getString(R.string.backup_restored, backupName)); |
| 140 | + builder.setOnDismissListener(new DialogInterface.OnDismissListener() { |
| 141 | + @Override |
| 142 | + public void onDismiss(DialogInterface dialog) { |
| 143 | + MtApp.get().buildAppComponent(); |
| 144 | + setResult(RESULT_OK); |
| 145 | + finish(); |
| 146 | + } |
| 147 | + }); |
| 148 | + builder.setPositiveButton(android.R.string.ok, null); |
| 149 | + builder.show(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void onRestoreFailure(String reason) { |
| 154 | + Timber.d("Restore failure."); |
| 155 | + stopProgress(); |
| 156 | + showToast(R.string.failed_restore_backup); |
| 157 | + |
| 158 | + if (BackupController.OnRestoreBackupListener.ERROR_AUTHENTICATION.equals(reason)) |
| 159 | + logout(); |
| 160 | + } |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + private void fetchBackups() { |
| 165 | + startProgress(); |
| 166 | + backupController.fetchBackups(dbApi, new BackupController.OnFetchBackupListListener() { |
| 167 | + @Override |
| 168 | + public void onBackupsFetched(@NonNull List<String> backupList) { |
| 169 | + stopProgress(); |
| 170 | + ArrayAdapter<String> adapter = new ArrayAdapter<>(BackupActivity.this, |
| 171 | + android.R.layout.simple_list_item_1, backupList); |
| 172 | + listView.setAdapter(adapter); |
| 173 | + } |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + private void logout() { |
| 178 | + preferenceController.writeDropboxAccessToken(null); |
| 179 | + dbApi.getSession().startOAuth2Authentication(BackupActivity.this); |
| 180 | + btnBackupNow.setEnabled(false); |
| 181 | + } |
| 182 | +} |
0 commit comments