From de6fc36e6cf3d852f5549eff7ba43ae36d09e479 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Mon, 9 Jul 2018 23:47:30 +0200 Subject: [PATCH] Import data implementation (begining) - Adds an interface for the data import feature (not final) - Can read data from a manually created backup file --- .idea/caches/build_file_checksums.ser | Bin 540 -> 540 bytes app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 1 + .../HomeActivityFragments/Summary.java | 4 +- .../moodl/Activities/SettingsActivity.java | 148 +++++++++++++++++- .../main/res/layout/dialog_import_data.xml | 51 ++++++ app/src/main/res/values/strings.xml | 7 + app/src/main/res/xml/pref_main.xml | 21 ++- build.gradle | 6 +- 9 files changed, 224 insertions(+), 16 deletions(-) create mode 100644 app/src/main/res/layout/dialog_import_data.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index b7963731466993ffdba52e8c194b033943dba50f..a417c2edec9045634ecfca38ff5e0ef8074e0eeb 100644 GIT binary patch delta 56 zcmV-80LTBF1e^qrm;`$%Z>o`;cMt$)jc?23tB98PP*SZN{*03&0gVtz_pjNg;80i% O@z(KQ1#udazyVy@`x;gN delta 56 zcmV-80LTBF1e^qrm;`21j-8R5cM#HkuuSXBmOMW&n2hWo=F5{K0gVte@b%ZFLZVMu Or?>)nT}_^ozyVz9jv8?Q diff --git a/app/build.gradle b/app/build.gradle index d7c78b3..8979578 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,7 +15,7 @@ android { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' lintOptions { - disable 'MissingTranslation' + //disable 'MissingTranslation' } } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fabd8f6..db8bb8b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + balance = balanceManager.getTotalBalance(); - if(balanceManager.getTotalBalance().size() > 0) + if(balance.size() > 0) { - for(int i = 0; i < balanceManager.getTotalBalance().size(); i++) + for(int i = 0; i < balance.size(); i++) { balance.get(i).updatePrice(getActivity(), defaultCurrency, new Currency.CurrencyCallBack() { @Override diff --git a/app/src/main/java/com/herbron/moodl/Activities/SettingsActivity.java b/app/src/main/java/com/herbron/moodl/Activities/SettingsActivity.java index 0bc4912..529b854 100644 --- a/app/src/main/java/com/herbron/moodl/Activities/SettingsActivity.java +++ b/app/src/main/java/com/herbron/moodl/Activities/SettingsActivity.java @@ -2,41 +2,58 @@ package com.herbron.moodl.Activities; import android.Manifest; import android.annotation.TargetApi; +import android.app.AlertDialog; +import android.app.Dialog; import android.app.KeyguardManager; import android.content.Context; +import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.hardware.fingerprint.FingerprintManager; -import android.media.Ringtone; -import android.media.RingtoneManager; -import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Environment; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; -import android.preference.PreferenceScreen; -import android.preference.RingtonePreference; import android.preference.SwitchPreference; import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyPermanentlyInvalidatedException; import android.security.keystore.KeyProperties; +import android.support.design.widget.TextInputLayout; import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; -import android.text.TextUtils; +import android.support.v7.view.ContextThemeWrapper; +import android.util.Log; +import android.view.Gravity; +import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.TextView; import com.herbron.moodl.BuildConfig; import com.herbron.moodl.FingerprintToolkit.FingerprintDialogFragment; import com.herbron.moodl.FingerprintToolkit.FingerprintHandler; +import com.herbron.moodl.MoodlBox; import com.herbron.moodl.R; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; +import java.io.PrintWriter; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.KeyStore; @@ -45,7 +62,12 @@ import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; import java.util.List; +import java.util.Locale; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; @@ -147,11 +169,125 @@ public class SettingsActivity extends AppCompatPreferenceActivity { bindPreferenceSummaryToValue(findPreference("default_currency")); bindPreferenceSummaryToValue(findPreference("minimum_value_displayed")); + findPreference("import").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + + Context context = SettingsActivity.this; + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); + View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_import_data, null, true); + dialogBuilder.setView(dialogView); + + CheckBox enterPasswordCheckbox = dialogView.findViewById(R.id.checkboxEnterPassword); + final TextInputLayout textInputLayoutPassword = dialogView.findViewById(R.id.textInputLayoutPassword); + + enterPasswordCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean b) { + if(b && textInputLayoutPassword.getVisibility() == View.GONE) + { + MoodlBox.expandH(textInputLayoutPassword); + } + + if(!b && textInputLayoutPassword.getVisibility() == View.VISIBLE) + { + MoodlBox.collapseH(textInputLayoutPassword); + } + } + }); + + dialogBuilder.setTitle("Restore backup"); + dialogBuilder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + + checkPermissions(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault()); + Date currentDate = new Date(); + //String fileName = getString(R.string.app_name) + "_" + formatter.format(currentDate) + ".backup"; + String fileName = "backup.moodl"; + + File dir = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name)); + + if (!dir.exists()) { + if (!dir.mkdirs()) { + Log.d("moodl", "Error while creating directory"); + } + } + + File backupFile = new File(dir + "/" + fileName); + + if(backupFile.exists()) + { + try (FileReader fileReader = new FileReader(backupFile)) { + BufferedReader bufferedReader = new BufferedReader(fileReader); + + String line; + + while((line = bufferedReader.readLine()) != null) + { + Log.d("moodl", line); + } + } catch (IOException e) { + Log.d("moodl", "Error > " + e); + } + } + else + { + Log.d("moodl", "Not backup file found"); + + try (PrintWriter printWriter = new PrintWriter(new FileWriter(backupFile, true))) { + + printWriter.write("Some data"); + + printWriter.close(); + } catch (IOException e) { + Log.d("moodl", "Error > " + e); + } + } + + dialog.dismiss(); + } + }); + dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + dialog.dismiss(); + } + }); + + AlertDialog alertDialog = dialogBuilder.create(); + alertDialog.show(); + + return false; + } + }); + EditTextPreference editTextPreference = (EditTextPreference) findPreference("minimum_value_displayed"); editTextPreference.setPositiveButtonText("Save"); editTextPreference.setNegativeButtonText("Cancel"); } + private boolean checkPermissions() { + + String[] permissions = new String[]{ + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE, + }; + + int result; + List listPermissionsNeeded = new ArrayList<>(); + for (String p : permissions) { + result = ContextCompat.checkSelfPermission(this, p); + if (result != PackageManager.PERMISSION_GRANTED) { + listPermissionsNeeded.add(p); + } + } + if (!listPermissionsNeeded.isEmpty()) { + ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 100); + return false; + } + return true; + } + /** * Set up the {@link android.app.ActionBar}, if the API is available. */ diff --git a/app/src/main/res/layout/dialog_import_data.xml b/app/src/main/res/layout/dialog_import_data.xml new file mode 100644 index 0000000..203fc43 --- /dev/null +++ b/app/src/main/res/layout/dialog_import_data.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 558ee8d..43eb975 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,7 @@ Display Data backup + Synchronization Other Minimum amount displayed @@ -89,6 +90,7 @@ Exchanges settings + Wallets settings Touch ID @@ -198,5 +200,10 @@ This field cannot be blank This field must be a number This field must be positive + Conserve current data + Restore manual entries + Restore API keys + Enter password + Password diff --git a/app/src/main/res/xml/pref_main.xml b/app/src/main/res/xml/pref_main.xml index 43563a8..219646c 100644 --- a/app/src/main/res/xml/pref_main.xml +++ b/app/src/main/res/xml/pref_main.xml @@ -28,19 +28,30 @@ android:title="@string/pref_title_category_data_backup"> + android:key="export" + android:enabled="false"/> - + android:title="@string/pref_title_category_synchronization"> - + android:title="@string/pref_header_exchange" + android:enabled="false"/> + + + + + + diff --git a/build.gradle b/build.gradle index 8fd7939..c46926d 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.2' + classpath 'com.android.tools.build:gradle:3.1.3' // NOTE: Do not place your application dependencies here; they belong @@ -19,7 +19,9 @@ allprojects { repositories { google() jcenter() - maven { url "https://jitpack.io" } + maven { + url "https://jitpack.io" + } } }