use fragment insteaad of activity, move pref im/export to maintenance plugin

This commit is contained in:
Markus M. May 2018-07-30 22:56:00 +02:00
parent 61dba0633a
commit 883bbf46e1
5 changed files with 62 additions and 22 deletions

View file

@ -188,6 +188,7 @@ dependencies {
libs "MilosKozak:danars-support-lib:master@zip"
implementation "com.android.support:appcompat-v7:${supportLibraryVersion}"
implementation "com.android.support:support-v13:${supportLibraryVersion}"
implementation "com.android.support:support-v4:${supportLibraryVersion}"
implementation "com.android.support:cardview-v7:${supportLibraryVersion}"
implementation "com.android.support:recyclerview-v7:${supportLibraryVersion}"

View file

@ -11,6 +11,7 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Food.FoodPlugin;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.ImportExportPrefs;
/**
*
@ -22,6 +23,8 @@ public class MaintenanceFragment extends Fragment {
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.maintenance_fragment, container, false);
final Fragment f = this;
view.findViewById(R.id.log_send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -39,7 +42,7 @@ public class MaintenanceFragment extends Fragment {
view.findViewById(R.id.nav_resetdb).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(getContext())
new AlertDialog.Builder(f.getContext())
.setTitle(R.string.nav_resetdb)
.setMessage(R.string.reset_db_confirm)
.setNegativeButton(android.R.string.cancel, null)
@ -55,6 +58,23 @@ public class MaintenanceFragment extends Fragment {
}
});
view.findViewById(R.id.nav_export).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// start activity for checking permissions...
ImportExportPrefs.verifyStoragePermissions(f);
ImportExportPrefs.exportSharedPreferences(f);
}
});
view.findViewById(R.id.nav_import).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// start activity for checking permissions...
ImportExportPrefs.verifyStoragePermissions(f);
ImportExportPrefs.importSharedPreferences(f);
}
});
return view;

View file

@ -146,17 +146,6 @@ public class MaintenancePlugin extends PluginBase {
}
}
public void exportSettings() {
// ImportExportPrefs.verifyStoragePermissions(this);
// ImportExportPrefs.exportSharedPreferences(this);
}
public void importSettings() {
// ImportExportPrefs.verifyStoragePermissions(this);
// ImportExportPrefs.importSharedPreferences(this);
}
/**
* returns a list of log files. The number of returned logs is given via the amount
* parameter. The log files are sorted by the name descending.

View file

@ -10,6 +10,7 @@ import android.content.pm.PackageManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import org.slf4j.Logger;
@ -47,7 +48,6 @@ public class ImportExportPrefs {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
@ -58,7 +58,22 @@ public class ImportExportPrefs {
}
}
public static void exportSharedPreferences(final Activity c) {
public static void verifyStoragePermissions(Fragment fragment) {
int permission = ContextCompat.checkSelfPermission(fragment.getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
fragment.requestPermissions(PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
}
public static void exportSharedPreferences(final Fragment f) {
exportSharedPreferences(f.getContext());
}
public static void exportSharedPreferences(final Context c) {
new AlertDialog.Builder(c)
.setMessage(MainApp.gs(R.string.export_to) + " " + file + " ?")
@ -88,13 +103,17 @@ public class ImportExportPrefs {
.show();
}
public static void importSharedPreferences(final Activity c) {
new AlertDialog.Builder(c)
public static void importSharedPreferences(final Fragment fragment) {
importSharedPreferences(fragment.getContext());
}
public static void importSharedPreferences(final Context context) {
new AlertDialog.Builder(context)
.setMessage(MainApp.gs(R.string.import_from) + " " + file + " ?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
String line;
String[] lineParts;
@ -115,20 +134,20 @@ public class ImportExportPrefs {
}
reader.close();
editor.commit();
OKDialog.show(c, MainApp.gs(R.string.setting_imported), MainApp.gs(R.string.restartingapp), new Runnable() {
OKDialog.show(context, MainApp.gs(R.string.setting_imported), MainApp.gs(R.string.restartingapp), new Runnable() {
@Override
public void run() {
log.debug("Exiting");
MainApp.instance().stopKeepAliveService();
MainApp.bus().post(new EventAppExit());
MainApp.closeDbHelper();
c.finish();
// context.finish();
System.runFinalization();
System.exit(0);
}
});
} catch (FileNotFoundException e) {
ToastUtils.showToastInUiThread(c, MainApp.gs(R.string.filenotfound) + " " + file);
ToastUtils.showToastInUiThread(context, MainApp.gs(R.string.filenotfound) + " " + file);
log.error("Unhandled exception", e);
} catch (IOException e) {
log.error("Unhandled exception", e);

View file

@ -1,7 +1,9 @@
package info.nightscout.utils;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
@ -21,8 +23,12 @@ public class OKDialog {
private static Logger log = LoggerFactory.getLogger(OKDialog.class);
public static void show(final Activity activity, String title, String message, final Runnable runnable) {
show(activity, title, message, runnable);
}
public static void show(final Context context, String title, String message, final Runnable runnable) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme));
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AppTheme));
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() {
@ -30,7 +36,7 @@ public class OKDialog {
dialog.dismiss();
if (runnable != null) {
SystemClock.sleep(100);
activity.runOnUiThread(runnable);
runOnUiThread(runnable);
}
}
});
@ -41,6 +47,11 @@ public class OKDialog {
}
}
public static boolean runOnUiThread(Runnable theRunnable) {
final Handler mainHandler = new Handler(MainApp.instance().getApplicationContext().getMainLooper());
return mainHandler.post(theRunnable);
}
public static void show(final Activity activity, String title, Spanned message, final Runnable runnable) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme));