import/export preferences

This commit is contained in:
Milos Kozak 2016-07-03 20:37:42 +02:00
parent f8301360dc
commit afa4332d28
11 changed files with 177 additions and 58 deletions

View file

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View file

@ -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);

View file

@ -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;

View file

@ -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();
}

View file

@ -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) {

View file

@ -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

View file

@ -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();
}

View file

@ -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<String, ?> prefsMap = prefs.getAll();
for (Map.Entry<String, ?> 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();
}
}

View file

@ -8,8 +8,11 @@
android:id="@+id/nav_resetdb"
android:title="@string/nav_resetdb" />
<item
android:id="@+id/nav_backup"
android:title="@string/nav_backup" />
android:id="@+id/nav_export"
android:title="@string/nav_export" />
<item
android:id="@+id/nav_import"
android:title="@string/nav_import" />
<item
android:id="@+id/nav_test_alarm"
android:title="@string/nav_test_alarm" />

View file

@ -199,4 +199,11 @@
<string name="enabled">Povolený</string>
<string name="visible">Viditelný</string>
<string name="up">Nahoru</string>
<string name="exported">Nastavení exportováno</string>
<string name="setting_imported">Nastavení importováno</string>
<string name="import_from">Importovat nastavení z</string>
<string name="filenotfound">Soubor nenalezen</string>
<string name="export_to">Exportovat nastavení do</string>
<string name="nav_export">Exportovat nastavení</string>
<string name="nav_import">Importovat nastavení</string>
</resources>

View file

@ -207,5 +207,12 @@
<string name="enabled">Enabled</string>
<string name="visible">Visible</string>
<string name="up">Up</string>
<string name="exported">Preferences exported</string>
<string name="export_to">Export settings to</string>
<string name="import_from">Import settings from</string>
<string name="setting_imported">Settings imported</string>
<string name="filenotfound">File not found</string>
<string name="nav_export">Export settings</string>
<string name="nav_import">Import settings</string>
</resources>