mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-02-02 21:17:32 +01:00
Clean preferences code
This commit is contained in:
parent
58e504e299
commit
1477235fa2
@ -18,6 +18,7 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge.activities;
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
|
import android.content.ContentUris;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
@ -28,14 +29,16 @@ import android.location.Location;
|
|||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Environment;
|
||||||
import android.preference.EditTextPreference;
|
import android.preference.EditTextPreference;
|
||||||
import android.preference.ListPreference;
|
import android.preference.ListPreference;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.PreferenceCategory;
|
import android.preference.PreferenceCategory;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.provider.DocumentsContract;
|
import android.provider.DocumentsContract;
|
||||||
import android.provider.OpenableColumns;
|
import android.provider.MediaStore;
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -44,6 +47,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@ -283,33 +287,43 @@ public class SettingsActivity extends AbstractSettingsActivity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
pref = findPreference("export_location");
|
pref = findPreference(GBPrefs.AUTO_EXPORT_LOCATION);
|
||||||
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
Intent i = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||||
i.setType("application/x-sqlite3");
|
i.setType("application/x-sqlite3");
|
||||||
i.addCategory(Intent.CATEGORY_OPENABLE);
|
i.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
startActivityForResult(Intent.createChooser(i, "Choose directory"), FILE_REQUEST_CODE);
|
String title = getApplicationContext().getString(R.string.choose_auto_export_location);
|
||||||
|
startActivityForResult(Intent.createChooser(i, title), FILE_REQUEST_CODE);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
pref.setSummary(getAutoExportLocationSummary());
|
||||||
|
|
||||||
pref = findPreference("auto_export_period");
|
pref = findPreference(GBPrefs.AUTO_EXPORT_INTERVAL);
|
||||||
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(Preference preference, Object auto_export_period) {
|
public boolean onPreferenceChange(Preference preference, Object autoExportInterval) {
|
||||||
preference.setSummary(auto_export_period.toString());
|
String summary = String.format(
|
||||||
boolean auto_export_enabled = GBApplication.getPrefs().getBoolean("auto_export_enabled", false);
|
getApplicationContext().getString(R.string.pref_summary_auto_export_interval),
|
||||||
PeriodicExporter.sheduleAlarm(getApplicationContext(), (int) auto_export_period, auto_export_enabled);
|
(int) autoExportInterval);
|
||||||
|
preference.setSummary(summary);
|
||||||
|
boolean auto_export_enabled = GBApplication.getPrefs().getBoolean(GBPrefs.AUTO_EXPORT_ENABLED, false);
|
||||||
|
PeriodicExporter.sheduleAlarm(getApplicationContext(), (int) autoExportInterval, auto_export_enabled);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
int autoExportInterval = GBApplication.getPrefs().getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0);
|
||||||
|
String summary = String.format(
|
||||||
|
getApplicationContext().getString(R.string.pref_summary_auto_export_interval),
|
||||||
|
(int) autoExportInterval);
|
||||||
|
pref.setSummary(summary);
|
||||||
|
|
||||||
findPreference("auto_export_enabled").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
findPreference(GBPrefs.AUTO_EXPORT_ENABLED).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(Preference preference, Object autoExportEnabled) {
|
public boolean onPreferenceChange(Preference preference, Object autoExportEnabled) {
|
||||||
int auto_export_period = GBApplication.getPrefs().getInt("auto_export_period", 0);
|
int autoExportInterval = GBApplication.getPrefs().getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0);
|
||||||
PeriodicExporter.sheduleAlarm(getApplicationContext(), auto_export_period, (boolean) autoExportEnabled);
|
PeriodicExporter.sheduleAlarm(getApplicationContext(), autoExportInterval, (boolean) autoExportEnabled);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -345,30 +359,103 @@ public class SettingsActivity extends AbstractSettingsActivity {
|
|||||||
if (requestCode == FILE_REQUEST_CODE && data != null) {
|
if (requestCode == FILE_REQUEST_CODE && data != null) {
|
||||||
Uri uri = data.getData();
|
Uri uri = data.getData();
|
||||||
PreferenceManager
|
PreferenceManager
|
||||||
.getDefaultSharedPreferences(this)
|
.getDefaultSharedPreferences(this)
|
||||||
.edit()
|
.edit()
|
||||||
.putString("export_location", uri.toString())
|
.putString(GBPrefs.AUTO_EXPORT_LOCATION, uri.toString())
|
||||||
.apply();
|
.apply();
|
||||||
|
String summary = getAutoExportLocationSummary();
|
||||||
Cursor cursor = getContentResolver().query(
|
findPreference(GBPrefs.AUTO_EXPORT_LOCATION).setSummary(summary);
|
||||||
uri,
|
|
||||||
new String[] { DocumentsContract.Document.COLUMN_DISPLAY_NAME },
|
|
||||||
null, null, null, null
|
|
||||||
);
|
|
||||||
if (cursor == null || ! cursor.moveToFirst()) {
|
|
||||||
LOG.warn("Unable to fetch information on URI " + uri.toString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
|
|
||||||
findPreference("export_location").setSummary(displayName);
|
|
||||||
boolean autoExportEnabled = GBApplication
|
boolean autoExportEnabled = GBApplication
|
||||||
.getPrefs().getBoolean("auto_export_enabled", false);
|
.getPrefs().getBoolean(GBPrefs.AUTO_EXPORT_ENABLED, false);
|
||||||
int autoExportPeriod = GBApplication
|
int autoExportPeriod = GBApplication
|
||||||
.getPrefs().getInt("auto_export_period", 0);
|
.getPrefs().getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0);
|
||||||
PeriodicExporter.sheduleAlarm(getApplicationContext(), autoExportPeriod, autoExportEnabled);
|
PeriodicExporter.sheduleAlarm(getApplicationContext(), autoExportPeriod, autoExportEnabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Either returns the file path of the selected document, or the display name
|
||||||
|
*/
|
||||||
|
private String getAutoExportLocationSummary() {
|
||||||
|
String autoExportLocation = GBApplication.getPrefs().getString(GBPrefs.AUTO_EXPORT_LOCATION, null);
|
||||||
|
if (autoExportLocation == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
Uri uri = Uri.parse(autoExportLocation);
|
||||||
|
try {
|
||||||
|
String filePath = getFilePath(getApplicationContext(), uri);
|
||||||
|
if (filePath != null) {
|
||||||
|
return filePath;
|
||||||
|
}
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
Cursor cursor = getContentResolver().query(
|
||||||
|
uri,
|
||||||
|
new String[] { DocumentsContract.Document.COLUMN_DISPLAY_NAME },
|
||||||
|
null, null, null, null
|
||||||
|
);
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
return cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
As seen on stackoverflow https://stackoverflow.com/a/36714242/1207186
|
||||||
|
Try to find the file path of a document uri
|
||||||
|
*/
|
||||||
|
private String getFilePath(Context context, Uri uri) throws URISyntaxException {
|
||||||
|
String selection = null;
|
||||||
|
String[] selectionArgs = null;
|
||||||
|
// Uri is different in versions after KITKAT (Android 4.4), we need to
|
||||||
|
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
|
||||||
|
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
|
||||||
|
final String docId = DocumentsContract.getDocumentId(uri);
|
||||||
|
final String[] split = docId.split(":");
|
||||||
|
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
||||||
|
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
|
||||||
|
final String id = DocumentsContract.getDocumentId(uri);
|
||||||
|
uri = ContentUris.withAppendedId(
|
||||||
|
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
|
||||||
|
} else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
|
||||||
|
final String docId = DocumentsContract.getDocumentId(uri);
|
||||||
|
final String[] split = docId.split(":");
|
||||||
|
final String type = split[0];
|
||||||
|
if ("image".equals(type)) {
|
||||||
|
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
} else if ("video".equals(type)) {
|
||||||
|
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
} else if ("audio".equals(type)) {
|
||||||
|
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
}
|
||||||
|
selection = "_id=?";
|
||||||
|
selectionArgs = new String[]{
|
||||||
|
split[1]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ("content".equalsIgnoreCase(uri.getScheme())) {
|
||||||
|
String[] projection = {
|
||||||
|
MediaStore.Images.Media.DATA
|
||||||
|
};
|
||||||
|
Cursor cursor = null;
|
||||||
|
try {
|
||||||
|
cursor = context.getContentResolver()
|
||||||
|
.query(uri, projection, selection, selectionArgs, null);
|
||||||
|
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
return cursor.getString(column_index);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
||||||
|
return uri.getPath();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* delayed execution so that the preferences are applied first
|
* delayed execution so that the preferences are applied first
|
||||||
*/
|
*/
|
||||||
|
@ -16,6 +16,7 @@ import java.io.OutputStream;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by maufl on 1/4/18.
|
* Created by maufl on 1/4/18.
|
||||||
@ -51,7 +52,7 @@ public class PeriodicExporter extends BroadcastReceiver {
|
|||||||
LOG.info("Exporting DB");
|
LOG.info("Exporting DB");
|
||||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||||
DBHelper helper = new DBHelper(context);
|
DBHelper helper = new DBHelper(context);
|
||||||
String dst = GBApplication.getPrefs().getString("export_location", null);
|
String dst = GBApplication.getPrefs().getString(GBPrefs.AUTO_EXPORT_LOCATION, null);
|
||||||
if (dst == null) {
|
if (dst == null) {
|
||||||
LOG.info("Unable to export DB, export location not set");
|
LOG.info("Unable to export DB, export location not set");
|
||||||
return;
|
return;
|
||||||
|
@ -133,7 +133,6 @@ public class FileUtils {
|
|||||||
try (OutputStream bufOut = new BufferedOutputStream(out)) {
|
try (OutputStream bufOut = new BufferedOutputStream(out)) {
|
||||||
copyFileToStream(src, bufOut);
|
copyFileToStream(src, bufOut);
|
||||||
}
|
}
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,6 +24,9 @@ public class GBPrefs {
|
|||||||
public static final String CALENDAR_BLACKLIST = "calendar_blacklist";
|
public static final String CALENDAR_BLACKLIST = "calendar_blacklist";
|
||||||
public static final String AUTO_RECONNECT = "general_autocreconnect";
|
public static final String AUTO_RECONNECT = "general_autocreconnect";
|
||||||
private static final String AUTO_START = "general_autostartonboot";
|
private static final String AUTO_START = "general_autostartonboot";
|
||||||
|
public static final String AUTO_EXPORT_ENABLED = "auto_export_enabled";
|
||||||
|
public static final String AUTO_EXPORT_LOCATION = "auto_export_location";
|
||||||
|
public static final String AUTO_EXPORT_INTERVAL = "auto_export_interval";
|
||||||
private static final boolean AUTO_START_DEFAULT = true;
|
private static final boolean AUTO_START_DEFAULT = true;
|
||||||
public static boolean AUTO_RECONNECT_DEFAULT = true;
|
public static boolean AUTO_RECONNECT_DEFAULT = true;
|
||||||
|
|
||||||
|
@ -187,6 +187,13 @@
|
|||||||
<string name="prefs_title_all_day_heart_rate">All day heart rate measurement</string>
|
<string name="prefs_title_all_day_heart_rate">All day heart rate measurement</string>
|
||||||
<string name="preferences_hplus_settings">HPlus/Makibes settings</string>
|
<string name="preferences_hplus_settings">HPlus/Makibes settings</string>
|
||||||
|
|
||||||
|
<!-- Auto export preferences -->
|
||||||
|
<string name="pref_header_auto_export">Auto export</string>
|
||||||
|
<string name="pref_title_auto_export_enabled">Auto export enabled</string>
|
||||||
|
<string name="pref_title_auto_export_location">Export location</string>
|
||||||
|
<string name="pref_title_auto_export_interval">Export interval</string>
|
||||||
|
<string name="pref_summary_auto_export_interval">Export every %d hour</string>
|
||||||
|
|
||||||
<string name="not_connected">Not connected</string>
|
<string name="not_connected">Not connected</string>
|
||||||
<string name="connecting">Connecting</string>
|
<string name="connecting">Connecting</string>
|
||||||
<string name="connected">Connected</string>
|
<string name="connected">Connected</string>
|
||||||
@ -523,4 +530,6 @@
|
|||||||
<string name="devicetype_exrizu_k8">Exrizu K8</string>
|
<string name="devicetype_exrizu_k8">Exrizu K8</string>
|
||||||
<string name="devicetype_no1_f1">No.1 F1</string>
|
<string name="devicetype_no1_f1">No.1 F1</string>
|
||||||
<string name="devicetype_teclast_h30">Teclast H30</string>
|
<string name="devicetype_teclast_h30">Teclast H30</string>
|
||||||
|
|
||||||
|
<string name="choose_auto_export_location">Choose export location</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -504,23 +504,22 @@
|
|||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
<PreferenceCategory
|
<PreferenceCategory
|
||||||
android:key="database"
|
android:title="@string/pref_header_auto_export">
|
||||||
android:title="Database settings">
|
|
||||||
<Preference
|
|
||||||
android:key="export_location"
|
|
||||||
android:title="Database export location"
|
|
||||||
android:summary="%s" />
|
|
||||||
<CheckBoxPreference
|
<CheckBoxPreference
|
||||||
android:defaultValue="false"
|
android:defaultValue="false"
|
||||||
android:key="auto_export_enabled"
|
android:key="auto_export_enabled"
|
||||||
android:title="Enable periodic export" />
|
android:title="@string/pref_title_auto_export_enabled" />
|
||||||
|
<Preference
|
||||||
|
android:key="auto_export_location"
|
||||||
|
android:title="@string/pref_title_auto_export_location"
|
||||||
|
android:summary="%s" />
|
||||||
<EditTextPreference
|
<EditTextPreference
|
||||||
android:inputType="number"
|
android:inputType="number"
|
||||||
android:key="auto_export_period"
|
android:key="auto_export_interval"
|
||||||
android:defaultValue="3"
|
android:defaultValue="3"
|
||||||
android:maxLength="3"
|
android:maxLength="3"
|
||||||
android:title="Export every n hours"
|
android:title="@string/pref_title_auto_export_interval"
|
||||||
android:summary="%n"/>
|
android:summary="@string/pref_summary_auto_export_interval"/>
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
<PreferenceCategory
|
<PreferenceCategory
|
||||||
android:key="pref_key_development"
|
android:key="pref_key_development"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user