Skip to content

Commit

Permalink
Fix settings import/export to use document provider
Browse files Browse the repository at this point in the history
  • Loading branch information
erickok committed Jan 24, 2024
1 parent c07dc9e commit 25ba61e
Show file tree
Hide file tree
Showing 25 changed files with 21 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
@EBean(scope = Scope.Singleton)
public class SettingsPersistence {

public static final String DEFAULT_SETTINGS_DIR = Environment.getExternalStorageDirectory().toString()
+ "/Transdroid/";
public static final String DEFAULT_SETTINGS_FILENAME = "settings.json";
public static final File DEFAULT_SETTINGS_FILE = new File(DEFAULT_SETTINGS_DIR + DEFAULT_SETTINGS_FILENAME);
@Bean
protected ApplicationSettings applicationSettings;
@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ protected void init() {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
TorrentsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ protected void onDetailsScreenResult(Intent result) {
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void startAutoRefresh() {
// Check if already running
if (autoRefreshTask != null || stopRefresh || systemSettings.getRefreshIntervalMilliseconds() == 0) {
Expand Down Expand Up @@ -1026,12 +1025,13 @@ public void addTorrentByMagnetUrl(String url, String title) {

@Background
protected void addTorrentByFile(String localFile, String title) {
if (!navigationHelper.checkTorrentReadPermission(this)) {
// No read permission yet (which we get the result of in onRequestPermissionsResult)
awaitingAddLocalFile = localFile;
awaitingAddTitle = title;
return;
}
// TODO EKO
// if (!navigationHelper.checkTorrentReadPermission(this)) {
// // No read permission yet (which we get the result of in onRequestPermissionsResult)
// awaitingAddLocalFile = localFile;
// awaitingAddTitle = title;
// return;
// }
DaemonTaskResult result = AddByFileTask.create(currentConnection, localFile).execute(log);
if (result instanceof DaemonTaskSuccessResult) {
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_added, title));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,6 @@ private static String getQueryParameter(Uri uri, String parameter) {
return null;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public boolean checkTorrentReadPermission(final Activity activity) {
return Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ||
checkPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE, REQUEST_TORRENT_READ_PERMISSION);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public boolean checkSettingsReadPermission(final Activity activity) {
return Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ||
checkPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE, REQUEST_SETTINGS_READ_PERMISSION);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public boolean checkSettingsWritePermission(final Activity activity) {
return Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ||
checkPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_SETTINGS_WRITE_PERMISSION);
}

private boolean checkPermission(final Activity activity, final String permission, final int requestCode) {
if (hasPermission(permission))
// Permission already granted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ protected void init() {
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
TorrentsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ protected void init() {
fragmentItems.update(rssfeed, false, requiresExternalAuthentication);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
TorrentsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ private String parseQuery(Intent intent) {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
TorrentsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
package org.transdroid.core.gui.settings;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.preference.Preference;
Expand Down Expand Up @@ -88,7 +86,6 @@ protected void onCreate(Bundle savedInstanceState) {
findPreference("system_about").setOnPreferenceClickListener(onAboutClick);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
*/
package org.transdroid.core.gui.settings;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
Expand Down Expand Up @@ -256,7 +254,6 @@ protected void onResume() {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
TorrentsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
package org.transdroid.core.gui.settings;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -71,7 +69,6 @@ protected void onPause() {
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
*/
package org.transdroid.core.gui.settings;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
Expand Down Expand Up @@ -60,7 +58,6 @@ public void onCreate(Bundle savedInstanceState) {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
package org.transdroid.core.gui.settings;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -95,7 +93,6 @@ public void onCreate(Bundle savedInstanceState) {
});
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down Expand Up @@ -123,6 +120,7 @@ protected Dialog onCreateDialog(int id) {

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (connectivityHelper.requestedPermissionWasGranted(requestCode, permissions, grantResults)) {
localNetworkPreference.getOnPreferenceClickListener().onPreferenceClick(localNetworkPreference);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
*/
package org.transdroid.core.gui.settings;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceManager;
Expand All @@ -48,7 +45,6 @@
import org.transdroid.core.gui.search.SearchHistoryProvider;
import org.transdroid.core.service.AppUpdateJob;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -88,9 +84,6 @@ public class SystemSettingsActivity extends PreferenceCompatActivity {
private OnClickListener importSettingsFromFile = new OnClickListener() {
@Override
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();
}
};
Expand All @@ -101,9 +94,6 @@ public void onClick(DialogInterface dialog, int which) {
private OnClickListener exportSettingsToFile = new OnClickListener() {
@Override
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();
}
};
Expand Down Expand Up @@ -142,40 +132,16 @@ protected void onCreate(Bundle savedInstanceState) {
findPreference("system_exportsettings").setOnPreferenceClickListener(onExportSettingsClick);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (Boolean.TRUE.equals(navigationHelper.handleSettingsReadPermissionResult(requestCode, grantResults))) {
importSettingsFromFile();
} else if (Boolean.TRUE.equals(navigationHelper.handleSettingsWritePermissionResult(requestCode, grantResults))) {
exportSettingsToFile();
}
}

private void importSettingsFromFile() {
try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SystemSettingsActivity.this);
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));
}
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/json");
startActivityForResult(intent, ACTIVITY_IMPORT_SETTINGS);
}

@OnActivityResult(ACTIVITY_IMPORT_SETTINGS)
Expand All @@ -196,22 +162,11 @@ public void importSettingsFilePicked(int resultCode, Intent data) {
}

private void exportSettingsToFile() {
try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SystemSettingsActivity.this);
settingsPersistence.exportSettingsToFile(prefs, SettingsPersistence.DEFAULT_SETTINGS_FILE);
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));
}
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);
}

@OnActivityResult(ACTIVITY_EXPORT_SETTINGS)
Expand Down Expand Up @@ -255,12 +210,7 @@ protected Dialog onCreateDialog(int id) {
case DIALOG_IMPORTSETTINGS:
// @formatter:off
return new AlertDialog.Builder(this)
.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()))
.setMessage(getString(R.string.pref_import_dialog_android10))
.setPositiveButton(R.string.pref_import_fromfile, importSettingsFromFile)
.setNeutralButton(R.string.pref_import_fromqr, importSettingsFromQr)
.setNegativeButton(android.R.string.cancel, null)
Expand All @@ -269,12 +219,7 @@ protected Dialog onCreateDialog(int id) {
case DIALOG_EXPORTSETTINGS:
// @formatter:off
return new AlertDialog.Builder(this)
.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()))
.setMessage(getString(R.string.pref_export_dialog_android10))
.setPositiveButton(R.string.pref_export_tofile, exportSettingsToFile)
.setNeutralButton(R.string.pref_export_toqr, exportSettingsToQr)
.setNegativeButton(android.R.string.cancel, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
*/
package org.transdroid.core.gui.settings;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
Expand Down Expand Up @@ -56,7 +54,6 @@ public void onCreate(Bundle savedInstanceState) {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public void onCreate(Bundle savedInstanceState) {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public void onCreate(Bundle savedInstanceState) {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public void onCreate(Bundle savedInstanceState) {

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@OptionsItem(android.R.id.home)
protected void navigateUp() {
MainSettingsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
Expand Down
Loading

0 comments on commit 25ba61e

Please sign in to comment.