|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2010-2018 Eric Kok et al. |
|
|
|
* Copyright 2010-2024 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,13 +16,11 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
@ -48,7 +46,6 @@ 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; |
|
|
@ -68,6 +65,7 @@ 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; |
|
|
@ -77,7 +75,10 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
private OnPreferenceClickListener onCheckUpdatesClick = preference -> { |
|
|
|
private OnPreferenceClickListener onCheckUpdatesClick = preference -> { |
|
|
|
AppUpdateJob.schedule(getApplicationContext()); |
|
|
|
if (!navigationHelper.checkOrRequestNotificationPermission(this)) { |
|
|
|
|
|
|
|
// Already have permission: continue
|
|
|
|
|
|
|
|
AppUpdateJob.schedule(getApplicationContext()); |
|
|
|
|
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
private OnPreferenceClickListener onClearSearchClick = preference -> { |
|
|
|
private OnPreferenceClickListener onClearSearchClick = preference -> { |
|
|
@ -88,9 +89,6 @@ 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(); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -101,9 +99,6 @@ 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(); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -142,7 +137,6 @@ 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(); |
|
|
@ -150,32 +144,18 @@ 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) { |
|
|
|
if (Boolean.TRUE.equals(navigationHelper.handleSettingsReadPermissionResult(requestCode, grantResults))) { |
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
|
|
|
importSettingsFromFile(); |
|
|
|
if (navigationHelper.handleNotificationPermissionResult(requestCode, grantResults)) { |
|
|
|
} else if (Boolean.TRUE.equals(navigationHelper.handleSettingsWritePermissionResult(requestCode, grantResults))) { |
|
|
|
// Now that we have permission, schedule the job
|
|
|
|
exportSettingsToFile(); |
|
|
|
AppUpdateJob.schedule(getApplicationContext()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void importSettingsFromFile() { |
|
|
|
private void importSettingsFromFile() { |
|
|
|
try { |
|
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { |
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SystemSettingsActivity.this); |
|
|
|
intent.setType("application/json"); |
|
|
|
settingsPersistence.importSettingsFromFile(prefs, SettingsPersistence.DEFAULT_SETTINGS_FILE); |
|
|
|
startActivityForResult(intent, ACTIVITY_IMPORT_SETTINGS); |
|
|
|
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) |
|
|
@ -196,22 +176,11 @@ public class SystemSettingsActivity extends PreferenceCompatActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void exportSettingsToFile() { |
|
|
|
private void exportSettingsToFile() { |
|
|
|
try { |
|
|
|
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); |
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { |
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SystemSettingsActivity.this); |
|
|
|
intent.setType("application/json"); |
|
|
|
settingsPersistence.exportSettingsToFile(prefs, SettingsPersistence.DEFAULT_SETTINGS_FILE); |
|
|
|
intent.putExtra(Intent.EXTRA_TITLE, SettingsPersistence.DEFAULT_SETTINGS_FILENAME); |
|
|
|
SnackbarManager.show(Snackbar.with(SystemSettingsActivity.this).text(R.string.pref_export_success)); |
|
|
|
startActivityForResult(intent, ACTIVITY_EXPORT_SETTINGS); |
|
|
|
} 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) |
|
|
@ -255,12 +224,7 @@ 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( |
|
|
|
.setMessage(getString(R.string.pref_import_dialog_android10)) |
|
|
|
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) |
|
|
@ -269,12 +233,7 @@ 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( |
|
|
|
.setMessage(getString(R.string.pref_export_dialog_android10)) |
|
|
|
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) |
|
|
|