Migrate SettingsManager to Kotlin

This commit is contained in:
Torsten Grote 2019-07-04 08:25:37 +02:00
parent 3d5911d41d
commit c801502e81
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
7 changed files with 62 additions and 70 deletions

View file

@ -11,7 +11,7 @@ import com.stevesoltys.backup.R;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.stevesoltys.backup.settings.SettingsManager.areBackupsScheduled;
import static com.stevesoltys.backup.settings.SettingsManagerKt.areBackupsScheduled;
public class MainActivity extends Activity implements View.OnClickListener {

View file

@ -24,10 +24,10 @@ import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
import static com.stevesoltys.backup.Backup.JOB_ID_BACKGROUND_BACKUP;
import static com.stevesoltys.backup.activity.MainActivity.OPEN_DOCUMENT_TREE_BACKUP_REQUEST_CODE;
import static com.stevesoltys.backup.activity.MainActivity.OPEN_DOCUMENT_TREE_REQUEST_CODE;
import static com.stevesoltys.backup.settings.SettingsManager.getBackupFolderUri;
import static com.stevesoltys.backup.settings.SettingsManager.getBackupPassword;
import static com.stevesoltys.backup.settings.SettingsManager.setBackupFolderUri;
import static com.stevesoltys.backup.settings.SettingsManager.setBackupsScheduled;
import static com.stevesoltys.backup.settings.SettingsManagerKt.getBackupFolderUri;
import static com.stevesoltys.backup.settings.SettingsManagerKt.getBackupPassword;
import static com.stevesoltys.backup.settings.SettingsManagerKt.setBackupFolderUri;
import static com.stevesoltys.backup.settings.SettingsManagerKt.setBackupsScheduled;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.DAYS;

View file

@ -17,11 +17,13 @@ import com.stevesoltys.backup.R;
import com.stevesoltys.backup.activity.PopupWindowUtil;
import com.stevesoltys.backup.service.PackageService;
import com.stevesoltys.backup.service.backup.BackupService;
import com.stevesoltys.backup.settings.SettingsManager;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import static com.stevesoltys.backup.settings.SettingsManagerKt.getBackupPassword;
import static com.stevesoltys.backup.settings.SettingsManagerKt.setBackupPassword;
/**
* @author Steve Soltys
*/
@ -69,7 +71,7 @@ class CreateBackupActivityController {
}
void onCreateBackupButtonClicked(Set<String> selectedPackages, Activity parent) {
String password = SettingsManager.getBackupPassword(parent);
String password = getBackupPassword(parent);
if (password == null) {
showEnterPasswordAlert(selectedPackages, parent);
} else {
@ -114,7 +116,7 @@ class CreateBackupActivityController {
String password = passwordTextView.getText().toString();
if (originalPassword.equals(password)) {
SettingsManager.setBackupPassword(parent, password);
setBackupPassword(parent, password);
backupService.backupPackageData(selectedPackages, parent);
} else {

View file

@ -1,57 +0,0 @@
package com.stevesoltys.backup.settings;
import android.annotation.Nullable;
import android.content.Context;
import android.net.Uri;
import static android.preference.PreferenceManager.getDefaultSharedPreferences;
public class SettingsManager {
private static final String PREF_KEY_BACKUP_URI = "backupUri";
private static final String PREF_KEY_BACKUP_PASSWORD = "backupLegacyPassword";
private static final String PREF_KEY_BACKUPS_SCHEDULED = "backupsScheduled";
public static void setBackupFolderUri(Context context, Uri uri) {
getDefaultSharedPreferences(context)
.edit()
.putString(PREF_KEY_BACKUP_URI, uri.toString())
.apply();
}
@Nullable
public static Uri getBackupFolderUri(Context context) {
String uriStr = getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_URI, null);
if (uriStr == null) return null;
return Uri.parse(uriStr);
}
/**
* This is insecure and not supposed to be part of a release,
* but rather an intermediate step towards a generated passphrase.
*/
public static void setBackupPassword(Context context, String password) {
getDefaultSharedPreferences(context)
.edit()
.putString(PREF_KEY_BACKUP_PASSWORD, password)
.apply();
}
@Nullable
public static String getBackupPassword(Context context) {
return getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_PASSWORD, null);
}
public static void setBackupsScheduled(Context context) {
getDefaultSharedPreferences(context)
.edit()
.putBoolean(PREF_KEY_BACKUPS_SCHEDULED, true)
.apply();
}
@Nullable
public static Boolean areBackupsScheduled(Context context) {
return getDefaultSharedPreferences(context).getBoolean(PREF_KEY_BACKUPS_SCHEDULED, false);
}
}

View file

@ -0,0 +1,48 @@
package com.stevesoltys.backup.settings
import android.content.Context
import android.net.Uri
import android.preference.PreferenceManager.getDefaultSharedPreferences
private const val PREF_KEY_BACKUP_URI = "backupUri"
private const val PREF_KEY_BACKUP_PASSWORD = "backupLegacyPassword"
private const val PREF_KEY_BACKUPS_SCHEDULED = "backupsScheduled"
fun setBackupFolderUri(context: Context, uri: Uri) {
getDefaultSharedPreferences(context)
.edit()
.putString(PREF_KEY_BACKUP_URI, uri.toString())
.apply()
}
fun getBackupFolderUri(context: Context): Uri? {
val uriStr = getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_URI, null)
?: return null
return Uri.parse(uriStr)
}
/**
* This is insecure and not supposed to be part of a release,
* but rather an intermediate step towards a generated passphrase.
*/
fun setBackupPassword(context: Context, password: String) {
getDefaultSharedPreferences(context)
.edit()
.putString(PREF_KEY_BACKUP_PASSWORD, password)
.apply()
}
fun getBackupPassword(context: Context): String? {
return getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_PASSWORD, null)
}
fun setBackupsScheduled(context: Context) {
getDefaultSharedPreferences(context)
.edit()
.putBoolean(PREF_KEY_BACKUPS_SCHEDULED, true)
.apply()
}
fun areBackupsScheduled(context: Context): Boolean {
return getDefaultSharedPreferences(context).getBoolean(PREF_KEY_BACKUPS_SCHEDULED, false)
}

View file

@ -5,8 +5,6 @@ import android.content.Intent
import android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION
import android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
import androidx.lifecycle.AndroidViewModel
import com.stevesoltys.backup.settings.SettingsManager.getBackupFolderUri
import com.stevesoltys.backup.settings.SettingsManager.setBackupFolderUri
class SettingsViewModel(application: Application) : AndroidViewModel(application) {

View file

@ -10,7 +10,6 @@ import android.util.Log;
import com.stevesoltys.backup.security.CipherUtil;
import com.stevesoltys.backup.security.KeyGenerator;
import com.stevesoltys.backup.settings.SettingsManager;
import com.stevesoltys.backup.transport.component.BackupComponent;
import org.apache.commons.io.IOUtils;
@ -42,6 +41,8 @@ import static android.provider.DocumentsContract.buildDocumentUriUsingTree;
import static android.provider.DocumentsContract.createDocument;
import static android.provider.DocumentsContract.getTreeDocumentId;
import static com.stevesoltys.backup.activity.MainActivityController.DOCUMENT_MIME_TYPE;
import static com.stevesoltys.backup.settings.SettingsManagerKt.getBackupFolderUri;
import static com.stevesoltys.backup.settings.SettingsManagerKt.getBackupPassword;
import static com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConstants.DEFAULT_BACKUP_QUOTA;
import static com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConstants.DEFAULT_FULL_BACKUP_DIRECTORY;
import static com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConstants.DEFAULT_INCREMENTAL_BACKUP_DIRECTORY;
@ -286,13 +287,13 @@ public class ContentProviderBackupComponent implements BackupComponent {
backupState.getOutputStream().write(backupState.getSalt());
backupState.getOutputStream().closeEntry();
String password = requireNonNull(SettingsManager.getBackupPassword(context));
String password = requireNonNull(getBackupPassword(context));
backupState.setSecretKey(KeyGenerator.generate(password, backupState.getSalt()));
}
}
private void initializeOutputStream() throws IOException {
Uri folderUri = SettingsManager.getBackupFolderUri(context);
Uri folderUri = getBackupFolderUri(context);
// TODO notify about failure with notification
Uri fileUri = createBackupFile(folderUri);