|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2010-2024 Eric Kok et al. |
|
|
|
* Copyright 2010-2018 Eric Kok et al. |
|
|
|
* |
|
|
|
* |
|
|
|
* Transdroid is free software: you can redistribute it and/or modify |
|
|
|
* Transdroid is free software: you can redistribute it and/or modify |
|
|
|
* it under the terms of the GNU General Public License as published by |
|
|
|
* it under the terms of the GNU General Public License as published by |
|
|
@ -16,11 +16,13 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.transdroid.core.gui.settings; |
|
|
|
package org.transdroid.core.gui.settings; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.TargetApi; |
|
|
|
import android.app.Dialog; |
|
|
|
import android.app.Dialog; |
|
|
|
import android.content.DialogInterface; |
|
|
|
import android.content.DialogInterface; |
|
|
|
import android.content.DialogInterface.OnClickListener; |
|
|
|
import android.content.DialogInterface.OnClickListener; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.SharedPreferences; |
|
|
|
import android.content.SharedPreferences; |
|
|
|
|
|
|
|
import android.os.Build; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.text.TextUtils; |
|
|
|
|
|
|
|
|
|
|
@ -46,6 +48,7 @@ import org.transdroid.core.gui.search.BarcodeHelper; |
|
|
|
import org.transdroid.core.gui.search.SearchHistoryProvider; |
|
|
|
import org.transdroid.core.gui.search.SearchHistoryProvider; |
|
|
|
import org.transdroid.core.service.AppUpdateJob; |
|
|
|
import org.transdroid.core.service.AppUpdateJob; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.io.OutputStream; |
|
|
@ -65,7 +68,6 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
protected ErrorLogSender errorLogSender; |
|
|
|
protected ErrorLogSender errorLogSender; |
|
|
|
@Bean |
|
|
|
@Bean |
|
|
|
protected SettingsPersistence settingsPersistence; |
|
|
|
protected SettingsPersistence settingsPersistence; |
|
|
|
|
|
|
|
|
|
|
|
private OnPreferenceClickListener onImportSettingsClick = preference -> { |
|
|
|
private OnPreferenceClickListener onImportSettingsClick = preference -> { |
|
|
|
showDialog(DIALOG_IMPORTSETTINGS); |
|
|
|
showDialog(DIALOG_IMPORTSETTINGS); |
|
|
|
return true; |
|
|
|
return true; |
|
|
@ -75,10 +77,7 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
private OnPreferenceClickListener onCheckUpdatesClick = preference -> { |
|
|
|
private OnPreferenceClickListener onCheckUpdatesClick = preference -> { |
|
|
|
if (!navigationHelper.checkOrRequestNotificationPermission(this)) { |
|
|
|
AppUpdateJob.schedule(getApplicationContext()); |
|
|
|
// Already have permission: continue
|
|
|
|
|
|
|
|
AppUpdateJob.schedule(getApplicationContext()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
private OnPreferenceClickListener onClearSearchClick = preference -> { |
|
|
|
private OnPreferenceClickListener onClearSearchClick = preference -> { |
|
|
@ -89,6 +88,9 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
private OnClickListener importSettingsFromFile = new OnClickListener() { |
|
|
|
private OnClickListener importSettingsFromFile = new OnClickListener() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT |
|
|
|
|
|
|
|
&& !navigationHelper.checkSettingsReadPermission(SystemSettingsActivity.this)) |
|
|
|
|
|
|
|
return; // We are requesting permission to access file storage
|
|
|
|
importSettingsFromFile(); |
|
|
|
importSettingsFromFile(); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -99,6 +101,9 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
private OnClickListener exportSettingsToFile = new OnClickListener() { |
|
|
|
private OnClickListener exportSettingsToFile = new OnClickListener() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT |
|
|
|
|
|
|
|
&& !navigationHelper.checkSettingsWritePermission(SystemSettingsActivity.this)) |
|
|
|
|
|
|
|
return; // We are requesting permission to access file storage
|
|
|
|
exportSettingsToFile(); |
|
|
|
exportSettingsToFile(); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -137,6 +142,7 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
findPreference("system_exportsettings").setOnPreferenceClickListener(onExportSettingsClick); |
|
|
|
findPreference("system_exportsettings").setOnPreferenceClickListener(onExportSettingsClick); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB) |
|
|
|
@OptionsItem(android.R.id.home) |
|
|
|
@OptionsItem(android.R.id.home) |
|
|
|
protected void navigateUp() { |
|
|
|
protected void navigateUp() { |
|
|
|
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start(); |
|
|
|
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start(); |
|
|
@ -144,18 +150,32 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
|
|
|
if (Boolean.TRUE.equals(navigationHelper.handleSettingsReadPermissionResult(requestCode, grantResults))) { |
|
|
|
if (navigationHelper.handleNotificationPermissionResult(requestCode, grantResults)) { |
|
|
|
importSettingsFromFile(); |
|
|
|
// Now that we have permission, schedule the job
|
|
|
|
} else if (Boolean.TRUE.equals(navigationHelper.handleSettingsWritePermissionResult(requestCode, grantResults))) { |
|
|
|
AppUpdateJob.schedule(getApplicationContext()); |
|
|
|
exportSettingsToFile(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void importSettingsFromFile() { |
|
|
|
private void importSettingsFromFile() { |
|
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
|
|
|
try { |
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { |
|
|
|
intent.setType("application/json"); |
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SystemSettingsActivity.this); |
|
|
|
startActivityForResult(intent, ACTIVITY_IMPORT_SETTINGS); |
|
|
|
settingsPersistence.importSettingsFromFile(prefs, SettingsPersistence.DEFAULT_SETTINGS_FILE); |
|
|
|
|
|
|
|
SnackbarManager.show(Snackbar.with(SystemSettingsActivity.this).text(R.string.pref_import_success)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
|
|
|
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
|
|
|
|
|
|
intent.setType("application/json"); |
|
|
|
|
|
|
|
startActivityForResult(intent, ACTIVITY_IMPORT_SETTINGS); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
|
|
|
SnackbarManager |
|
|
|
|
|
|
|
.show(Snackbar.with(SystemSettingsActivity.this).text(R.string.error_file_not_found).colorResource(R.color.red)); |
|
|
|
|
|
|
|
} catch (JSONException e) { |
|
|
|
|
|
|
|
SnackbarManager.show(Snackbar.with(SystemSettingsActivity.this) |
|
|
|
|
|
|
|
.text(getString(R.string.error_no_valid_settings_file, getString(R.string.app_name))).colorResource(R.color.red)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@OnActivityResult(ACTIVITY_IMPORT_SETTINGS) |
|
|
|
@OnActivityResult(ACTIVITY_IMPORT_SETTINGS) |
|
|
@ -176,11 +196,22 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void exportSettingsToFile() { |
|
|
|
private void exportSettingsToFile() { |
|
|
|
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); |
|
|
|
try { |
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { |
|
|
|
intent.setType("application/json"); |
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SystemSettingsActivity.this); |
|
|
|
intent.putExtra(Intent.EXTRA_TITLE, SettingsPersistence.DEFAULT_SETTINGS_FILENAME); |
|
|
|
settingsPersistence.exportSettingsToFile(prefs, SettingsPersistence.DEFAULT_SETTINGS_FILE); |
|
|
|
startActivityForResult(intent, ACTIVITY_EXPORT_SETTINGS); |
|
|
|
SnackbarManager.show(Snackbar.with(SystemSettingsActivity.this).text(R.string.pref_export_success)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); |
|
|
|
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
|
|
|
|
|
|
intent.setType("application/json"); |
|
|
|
|
|
|
|
intent.putExtra(Intent.EXTRA_TITLE, SettingsPersistence.DEFAULT_SETTINGS_FILENAME); |
|
|
|
|
|
|
|
startActivityForResult(intent, ACTIVITY_EXPORT_SETTINGS); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (JSONException | IOException e) { |
|
|
|
|
|
|
|
SnackbarManager.show(Snackbar.with(SystemSettingsActivity.this).text(R.string.error_cant_write_settings_file) |
|
|
|
|
|
|
|
.colorResource(R.color.red)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@OnActivityResult(ACTIVITY_EXPORT_SETTINGS) |
|
|
|
@OnActivityResult(ACTIVITY_EXPORT_SETTINGS) |
|
|
@ -224,7 +255,12 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
case DIALOG_IMPORTSETTINGS: |
|
|
|
case DIALOG_IMPORTSETTINGS: |
|
|
|
// @formatter:off
|
|
|
|
// @formatter:off
|
|
|
|
return new AlertDialog.Builder(this) |
|
|
|
return new AlertDialog.Builder(this) |
|
|
|
.setMessage(getString(R.string.pref_import_dialog_android10)) |
|
|
|
.setMessage( |
|
|
|
|
|
|
|
getString( |
|
|
|
|
|
|
|
Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT |
|
|
|
|
|
|
|
? R.string.pref_import_dialog : R.string.pref_import_dialog_android10, |
|
|
|
|
|
|
|
getString(R.string.app_name), |
|
|
|
|
|
|
|
SettingsPersistence.DEFAULT_SETTINGS_FILE.toString())) |
|
|
|
.setPositiveButton(R.string.pref_import_fromfile, importSettingsFromFile) |
|
|
|
.setPositiveButton(R.string.pref_import_fromfile, importSettingsFromFile) |
|
|
|
.setNeutralButton(R.string.pref_import_fromqr, importSettingsFromQr) |
|
|
|
.setNeutralButton(R.string.pref_import_fromqr, importSettingsFromQr) |
|
|
|
.setNegativeButton(android.R.string.cancel, null) |
|
|
|
.setNegativeButton(android.R.string.cancel, null) |
|
|
@ -233,7 +269,12 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
case DIALOG_EXPORTSETTINGS: |
|
|
|
case DIALOG_EXPORTSETTINGS: |
|
|
|
// @formatter:off
|
|
|
|
// @formatter:off
|
|
|
|
return new AlertDialog.Builder(this) |
|
|
|
return new AlertDialog.Builder(this) |
|
|
|
.setMessage(getString(R.string.pref_export_dialog_android10)) |
|
|
|
.setMessage( |
|
|
|
|
|
|
|
getString( |
|
|
|
|
|
|
|
Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT |
|
|
|
|
|
|
|
? R.string.pref_export_dialog : R.string.pref_export_dialog_android10, |
|
|
|
|
|
|
|
getString(R.string.app_name), |
|
|
|
|
|
|
|
SettingsPersistence.DEFAULT_SETTINGS_FILE.toString())) |
|
|
|
.setPositiveButton(R.string.pref_export_tofile, exportSettingsToFile) |
|
|
|
.setPositiveButton(R.string.pref_export_tofile, exportSettingsToFile) |
|
|
|
.setNeutralButton(R.string.pref_export_toqr, exportSettingsToQr) |
|
|
|
.setNeutralButton(R.string.pref_export_toqr, exportSettingsToQr) |
|
|
|
.setNegativeButton(android.R.string.cancel, null) |
|
|
|
.setNegativeButton(android.R.string.cancel, null) |
|
|
|