From afa4332d28665a202b1227bdf29d1ac562018c51 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 3 Jul 2016 20:37:42 +0200 Subject: [PATCH] import/export preferences --- .idea/misc.xml | 2 +- .../nightscout/androidaps/MainActivity.java | 11 +- .../androidaps/Services/DataService.java | 3 - .../ConfigBuilder/ConfigBuilderFragment.java | 19 ++-- .../NSProfileViewerFragment.java | 6 +- .../Objectives/ObjectivesFragment.java | 30 +++--- .../SimpleProfile/SimpleProfileFragment.java | 43 ++++---- .../nightscout/utils/ImportExportPrefs.java | 100 ++++++++++++++++++ app/src/main/res/menu/menu_main.xml | 7 +- app/src/main/res/values-cs/strings.xml | 7 ++ app/src/main/res/values/strings.xml | 7 ++ 11 files changed, 177 insertions(+), 58 deletions(-) create mode 100644 app/src/main/java/info/nightscout/utils/ImportExportPrefs.java diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb68289f4..5d19981032 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 03bc99601e..36d08346d8 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -37,6 +37,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment; import info.nightscout.androidaps.tabs.*; import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment; +import info.nightscout.utils.ImportExportPrefs; import info.nightscout.utils.LocaleHelper; public class MainActivity extends AppCompatActivity { @@ -140,17 +141,23 @@ public class MainActivity extends AppCompatActivity { recreate(); break; } + case R.id.nav_export: { + ImportExportPrefs.exportSharedPreferences(this); + break; + } + case R.id.nav_import: { + ImportExportPrefs.importSharedPreferences(this); + break; + } case R.id.nav_exit: log.debug("Exiting"); //chancelAlarmManager(); //MainApp.bus().post(new StopEvent()); MainApp.closeDbHelper(); - finish(); System.runFinalization(); System.exit(0); - break; } return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index ed76971ae4..50ee51b5aa 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -2,10 +2,7 @@ package info.nightscout.androidaps.Services; import android.app.IntentService; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; -import android.os.Handler; -import android.os.HandlerThread; import android.support.annotation.Nullable; import com.j256.ormlite.dao.Dao; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java index f0f95348cf..a32b55fde6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java @@ -7,6 +7,7 @@ import android.content.SharedPreferences; import android.content.pm.ResolveInfo; import android.graphics.Color; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -57,8 +58,6 @@ import info.nightscout.utils.DateUtil; public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpInterface, ConstraintsInterface { private static Logger log = LoggerFactory.getLogger(ConfigBuilderFragment.class); - private static final String PREFS_NAME = "Settings"; - ListView bgsourceListView; ListView pumpListView; ListView loopListView; @@ -672,12 +671,12 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI private void storeSettings() { if (Config.logPrefsChange) log.debug("Storing settings"); - SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); for (PluginBase p : pluginList) { - editor.putBoolean(p.getName() + "Enabled", p.isEnabled()); - editor.putBoolean(p.getName() + "Visible", p.isVisibleInTabs()); + editor.putBoolean("ConfigBuilder" + p.getName() + "Enabled", p.isEnabled()); + editor.putBoolean("ConfigBuilder" + p.getName() + "Visible", p.isVisibleInTabs()); } editor.commit(); verifySelectionInCategories(); @@ -686,12 +685,12 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI private void loadSettings() { if (Config.logPrefsChange) log.debug("Loading stored settings"); - SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); for (PluginBase p : pluginList) { - if (settings.contains(p.getName() + "Enabled")) - p.setFragmentEnabled(settings.getBoolean(p.getName() + "Enabled", true)); - if (settings.contains(p.getName() + "Visible")) - p.setFragmentVisible(settings.getBoolean(p.getName() + "Visible", true)); + if (settings.contains("ConfigBuilder" + p.getName() + "Enabled")) + p.setFragmentEnabled(settings.getBoolean("ConfigBuilder" + p.getName() + "Enabled", true)); + if (settings.contains("ConfigBuilder" + p.getName() + "Visible")) + p.setFragmentVisible(settings.getBoolean("ConfigBuilder" + p.getName() + "Visible", true)); } verifySelectionInCategories(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java index 290c79e24d..e1633a8051 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -41,7 +42,6 @@ public class NSProfileViewerFragment extends Fragment implements PluginBase, Pro private static TextView basal; private static TextView target; - private static final String PREFS_NAME = "NightscoutProfile"; private static DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); boolean fragmentEnabled = true; @@ -160,7 +160,7 @@ public class NSProfileViewerFragment extends Fragment implements PluginBase, Pro } private void storeNSProfile() { - SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); editor.putString("profile", profile.getData().toString()); editor.putString("activeProfile", profile.getActiveProfile()); @@ -172,7 +172,7 @@ public class NSProfileViewerFragment extends Fragment implements PluginBase, Pro private void loadNSProfile() { if (Config.logPrefsChange) log.debug("Loading stored profile"); - SharedPreferences store = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); String activeProfile = store.getString("activeProfile", null); String profileString = store.getString("profile", null); if (profileString != null) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java index 1e4825e58f..b08795507f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v7.widget.CardView; import android.support.v7.widget.LinearLayoutManager; @@ -42,8 +43,6 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener boolean fragmentVisible = true; - String PREFS_NAME = "Objectives"; - @Override public int getType() { return PluginBase.CONSTRAINTS; @@ -193,31 +192,31 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener } public void saveProgress() { - SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); for (int num = 0; num < objectives.size(); num++) { Objective o = objectives.get(num); - editor.putLong(num + "started", o.started.getTime()); - editor.putLong(num + "accomplished", o.accomplished.getTime()); + editor.putLong("Objectives" + num + "started", o.started.getTime()); + editor.putLong("Objectives" + num + "accomplished", o.accomplished.getTime()); } - editor.putBoolean("bgIsAvailableInNS", bgIsAvailableInNS); - editor.putBoolean("pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS); - editor.putInt("manualEnacts", manualEnacts); + editor.putBoolean("Objectives" + "bgIsAvailableInNS", bgIsAvailableInNS); + editor.putBoolean("Objectives" + "pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS); + editor.putInt("Objectives" + "manualEnacts", manualEnacts); editor.commit(); if (Config.logPrefsChange) log.debug("Objectives stored"); } void loadProgress() { - SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); for (int num = 0; num < objectives.size(); num++) { Objective o = objectives.get(num); - o.started = new Date(settings.getLong(num + "started", 0)); - o.accomplished = new Date(settings.getLong(num + "accomplished", 0)); + o.started = new Date(settings.getLong("Objectives" + num + "started", 0)); + o.accomplished = new Date(settings.getLong("Objectives" + num + "accomplished", 0)); } - bgIsAvailableInNS = settings.getBoolean("bgIsAvailableInNS", false); - pumpStatusIsAvailableInNS = settings.getBoolean("pumpStatusIsAvailableInNS", false); - manualEnacts = settings.getInt("manualEnacts", 0); + bgIsAvailableInNS = settings.getBoolean("Objectives" + "bgIsAvailableInNS", false); + pumpStatusIsAvailableInNS = settings.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false); + manualEnacts = settings.getInt("Objectives" + "manualEnacts", 0); if (Config.logPrefsChange) log.debug("Objectives loaded"); } @@ -233,8 +232,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener @Override public ObjectiveViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.objectives_item, viewGroup, false); - ObjectiveViewHolder objectiveViewHolder = new ObjectiveViewHolder(v); - return objectiveViewHolder; + return new ObjectiveViewHolder(v); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index 5e523bc7a4..363c192f8b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.SimpleProfile; import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.NotificationCompat; import android.text.Editable; @@ -194,17 +195,17 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi private void storeSettings() { if (Config.logPrefsChange) log.debug("Storing settings"); - SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); - editor.putBoolean("mmol", mmol); - editor.putBoolean("mgdl", mgdl); - editor.putFloat("dia", new Float(dia)); - editor.putFloat("ic", new Float(ic)); - editor.putFloat("isf", new Float(isf)); - editor.putFloat("car", new Float(car)); - editor.putFloat("basal", new Float(basal)); - editor.putFloat("targetlow", new Float(targetLow)); - editor.putFloat("targethigh", new Float(targetHigh)); + editor.putBoolean("SimpleProfile" + "mmol", mmol); + editor.putBoolean("SimpleProfile" + "mgdl", mgdl); + editor.putFloat("SimpleProfile" + "dia", new Float(dia)); + editor.putFloat("SimpleProfile" + "ic", new Float(ic)); + editor.putFloat("SimpleProfile" + "isf", new Float(isf)); + editor.putFloat("SimpleProfile" + "car", new Float(car)); + editor.putFloat("SimpleProfile" + "basal", new Float(basal)); + editor.putFloat("SimpleProfile" + "targetlow", new Float(targetLow)); + editor.putFloat("SimpleProfile" + "targethigh", new Float(targetHigh)); editor.commit(); createConvertedProfile(); @@ -213,18 +214,18 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi private void loadSettings() { if (Config.logPrefsChange) log.debug("Loading stored settings"); - SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - if (settings.contains("mgdl")) mgdl = settings.getBoolean("mgdl", true); else mgdl = true; - if (settings.contains("mmol")) mmol = settings.getBoolean("mmol", false); else mmol = false; - if (settings.contains("dia")) dia = (double) settings.getFloat("dia", 3); else dia = 3d; - if (settings.contains("ic")) ic = (double) settings.getFloat("ic", 20); else ic = 20d; - if (settings.contains("isf")) isf = (double) settings.getFloat("isf", 200); else isf = 200d; - if (settings.contains("car")) car = (double) settings.getFloat("car", 20); else car = 20d; - if (settings.contains("basal")) basal = (double) settings.getFloat("basal", 1); else basal = 1d; - if (settings.contains("targetlow")) targetLow = (double) settings.getFloat("targetlow", 80); else targetLow = 80d; - if (settings.contains("targethigh")) - targetHigh = (double) settings.getFloat("targethigh", 120); else targetHigh = 120d; + if (settings.contains("SimpleProfile" + "mgdl")) mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true); else mgdl = true; + if (settings.contains("SimpleProfile" + "mmol")) mmol = settings.getBoolean("SimpleProfile" + "mmol", false); else mmol = false; + if (settings.contains("SimpleProfile" + "dia")) dia = (double) settings.getFloat("SimpleProfile" + "dia", 3); else dia = 3d; + if (settings.contains("SimpleProfile" + "ic")) ic = (double) settings.getFloat("SimpleProfile" + "ic", 20); else ic = 20d; + if (settings.contains("SimpleProfile" + "isf")) isf = (double) settings.getFloat("SimpleProfile" + "isf", 200); else isf = 200d; + if (settings.contains("SimpleProfile" + "car")) car = (double) settings.getFloat("SimpleProfile" + "car", 20); else car = 20d; + if (settings.contains("SimpleProfile" + "basal")) basal = (double) settings.getFloat("SimpleProfile" + "basal", 1); else basal = 1d; + if (settings.contains("SimpleProfile" + "targetlow")) targetLow = (double) settings.getFloat("SimpleProfile" + "targetlow", 80); else targetLow = 80d; + if (settings.contains("SimpleProfile" + "targethigh")) + targetHigh = (double) settings.getFloat("SimpleProfile" + "targethigh", 120); else targetHigh = 120d; createConvertedProfile(); } diff --git a/app/src/main/java/info/nightscout/utils/ImportExportPrefs.java b/app/src/main/java/info/nightscout/utils/ImportExportPrefs.java new file mode 100644 index 0000000000..00c49ede9e --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/ImportExportPrefs.java @@ -0,0 +1,100 @@ +package info.nightscout.utils; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.os.Environment; +import android.preference.PreferenceManager; +import android.widget.Toast; + +import com.crashlytics.android.Crashlytics; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Map; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; + +/** + * Created by mike on 03.07.2016. + */ +public class ImportExportPrefs { + static File path = new File(Environment.getExternalStorageDirectory().toString()); + static final File file = new File(path, MainApp.resources.getString(R.string.app_name) + "Preferences"); + + //exports shared Preferences + public static void exportSharedPreferences(final Context c) { + + new AlertDialog.Builder(c) + .setMessage(MainApp.resources.getString(R.string.export_to) + " " + path + "/" + file + "?") + .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + try { + FileWriter fw = new FileWriter(file); + PrintWriter pw = new PrintWriter(fw); + Map prefsMap = prefs.getAll(); + for (Map.Entry entry : prefsMap.entrySet()) { + pw.println(entry.getKey() + "::" + entry.getValue().toString()); + } + pw.close(); + fw.close(); + ToastUtils.showToastInUiThread(c, MainApp.resources.getString(R.string.exported)); + } catch (Exception e) { + e.printStackTrace(); + } + } + }) + .setNegativeButton(android.R.string.cancel, null) + .show(); + } + + public static void importSharedPreferences(final Context c) { + new AlertDialog.Builder(c) + .setMessage(MainApp.resources.getString(R.string.import_from) + " " + path + "/" + file + "?") + .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + SharedPreferences.Editor editor = prefs.edit(); + String line; + String[] lineParts; + try { + editor.clear(); + editor.commit(); + + BufferedReader reader = new BufferedReader(new FileReader(file)); + while ((line = reader.readLine()) != null) { + lineParts = line.split("::"); + if (lineParts.length == 2) { + if (lineParts[1].equals("true") || lineParts[1].equals("false")) { + editor.putBoolean(lineParts[0], Boolean.parseBoolean(lineParts[1])); + } else { + editor.putString(lineParts[0], lineParts[1]); + } + } + } + reader.close(); + editor.commit(); + ToastUtils.showToastInUiThread(c, MainApp.resources.getString(R.string.setting_imported)); + } catch (FileNotFoundException e) { + ToastUtils.showToastInUiThread(c, MainApp.resources.getString(R.string.filenotfound) + " " + file); + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }) + .setNegativeButton(android.R.string.cancel, null) + .show(); + } + +} diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index dff9c3cfdc..ef7a82ff3e 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -8,8 +8,11 @@ android:id="@+id/nav_resetdb" android:title="@string/nav_resetdb" /> + android:id="@+id/nav_export" + android:title="@string/nav_export" /> + diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 21d887649d..fd5b017aa3 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -199,4 +199,11 @@ Povolený Viditelný Nahoru + Nastavení exportováno + Nastavení importováno + Importovat nastavení z + Soubor nenalezen + Exportovat nastavení do + Exportovat nastavení + Importovat nastavení \ 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 7ccc4bab55..94989c73e0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -207,5 +207,12 @@ Enabled Visible Up + Preferences exported + Export settings to + Import settings from + Settings imported + File not found + Export settings + Import settings