Migrate SettingsActivity and Fragment to Kotlin
This commit is contained in:
parent
c801502e81
commit
ee6cf38312
4 changed files with 168 additions and 180 deletions
|
@ -1,103 +0,0 @@
|
||||||
package com.stevesoltys.backup.settings;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.ActivityNotFoundException;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuInflater;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
|
||||||
import androidx.lifecycle.ViewModelProviders;
|
|
||||||
|
|
||||||
import com.stevesoltys.backup.R;
|
|
||||||
|
|
||||||
import static android.content.Intent.ACTION_OPEN_DOCUMENT_TREE;
|
|
||||||
import static android.content.Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
|
|
||||||
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
|
||||||
import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
|
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
|
||||||
import static android.widget.Toast.LENGTH_SHORT;
|
|
||||||
import static com.stevesoltys.backup.activity.MainActivity.OPEN_DOCUMENT_TREE_REQUEST_CODE;
|
|
||||||
import static java.util.Objects.requireNonNull;
|
|
||||||
|
|
||||||
public class SettingsActivity extends AppCompatActivity {
|
|
||||||
|
|
||||||
private final static String TAG = SettingsActivity.class.getName();
|
|
||||||
|
|
||||||
private SettingsViewModel viewModel;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
setContentView(R.layout.activity_settings);
|
|
||||||
|
|
||||||
viewModel = ViewModelProviders.of(this).get(SettingsViewModel.class);
|
|
||||||
|
|
||||||
requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
if (!viewModel.locationIsSet()) {
|
|
||||||
showChooseFolderActivity();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
|
||||||
MenuInflater inflater = getMenuInflater();
|
|
||||||
inflater.inflate(R.menu.settings_menu, menu);
|
|
||||||
if (getResources().getBoolean(R.bool.show_restore_in_settings)) {
|
|
||||||
menu.findItem(R.id.action_restore).setVisible(true);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
if (item.getItemId() == android.R.id.home) {
|
|
||||||
onBackPressed();
|
|
||||||
return true;
|
|
||||||
} else if (item.getItemId() == R.id.action_backup) {
|
|
||||||
Toast.makeText(this, "Not yet implemented", LENGTH_SHORT).show();
|
|
||||||
return true;
|
|
||||||
} else if (item.getItemId() == R.id.action_restore) {
|
|
||||||
Toast.makeText(this, "Not yet implemented", LENGTH_SHORT).show();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityResult(int requestCode, int resultCode, Intent result) {
|
|
||||||
if (resultCode != Activity.RESULT_OK) {
|
|
||||||
Log.w(TAG, "Error in activity result: " + requestCode);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (requestCode == OPEN_DOCUMENT_TREE_REQUEST_CODE) {
|
|
||||||
viewModel.handleChooseFolderResult(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showChooseFolderActivity() {
|
|
||||||
Intent openTreeIntent = new Intent(ACTION_OPEN_DOCUMENT_TREE);
|
|
||||||
openTreeIntent.addFlags(FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
|
|
||||||
FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
|
|
||||||
try {
|
|
||||||
Intent documentChooser = Intent.createChooser(openTreeIntent, "Select the backup location");
|
|
||||||
startActivityForResult(documentChooser, OPEN_DOCUMENT_TREE_REQUEST_CODE);
|
|
||||||
} catch (ActivityNotFoundException ex) {
|
|
||||||
Toast.makeText(this, "Please install a file manager.", LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
package com.stevesoltys.backup.settings
|
||||||
|
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.Intent.*
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.widget.Toast
|
||||||
|
import android.widget.Toast.LENGTH_LONG
|
||||||
|
import android.widget.Toast.LENGTH_SHORT
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.lifecycle.ViewModelProviders
|
||||||
|
import com.stevesoltys.backup.R
|
||||||
|
import com.stevesoltys.backup.activity.MainActivity.OPEN_DOCUMENT_TREE_REQUEST_CODE
|
||||||
|
|
||||||
|
private val TAG = SettingsActivity::class.java.name
|
||||||
|
|
||||||
|
class SettingsActivity : AppCompatActivity() {
|
||||||
|
|
||||||
|
private lateinit var viewModel: SettingsViewModel
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
setContentView(R.layout.activity_settings)
|
||||||
|
|
||||||
|
viewModel = ViewModelProviders.of(this).get(SettingsViewModel::class.java)
|
||||||
|
|
||||||
|
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStart() {
|
||||||
|
super.onStart()
|
||||||
|
if (!viewModel.locationIsSet()) {
|
||||||
|
showChooseFolderActivity()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
menuInflater.inflate(R.menu.settings_menu, menu)
|
||||||
|
if (resources.getBoolean(R.bool.show_restore_in_settings)) {
|
||||||
|
menu.findItem(R.id.action_restore).isVisible = true
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
return when {
|
||||||
|
item.itemId == android.R.id.home -> {
|
||||||
|
onBackPressed()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
item.itemId == R.id.action_backup -> {
|
||||||
|
Toast.makeText(this, "Not yet implemented", LENGTH_SHORT).show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
item.itemId == R.id.action_restore -> {
|
||||||
|
Toast.makeText(this, "Not yet implemented", LENGTH_SHORT).show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, result: Intent?) {
|
||||||
|
if (resultCode != RESULT_OK) {
|
||||||
|
Log.w(TAG, "Error in activity result: $requestCode")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requestCode == OPEN_DOCUMENT_TREE_REQUEST_CODE) {
|
||||||
|
viewModel.handleChooseFolderResult(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showChooseFolderActivity() {
|
||||||
|
val openTreeIntent = Intent(ACTION_OPEN_DOCUMENT_TREE)
|
||||||
|
openTreeIntent.addFlags(FLAG_GRANT_PERSISTABLE_URI_PERMISSION or
|
||||||
|
FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||||
|
// TODO StringRes
|
||||||
|
try {
|
||||||
|
val documentChooser = createChooser(openTreeIntent, "Select the backup location")
|
||||||
|
startActivityForResult(documentChooser, OPEN_DOCUMENT_TREE_REQUEST_CODE)
|
||||||
|
} catch (ex: ActivityNotFoundException) {
|
||||||
|
Toast.makeText(this, "Please install a file manager.", LENGTH_LONG).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,77 +0,0 @@
|
||||||
package com.stevesoltys.backup.settings;
|
|
||||||
|
|
||||||
import android.app.backup.IBackupManager;
|
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.RemoteException;
|
|
||||||
import android.provider.Settings;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import androidx.preference.PreferenceFragmentCompat;
|
|
||||||
import androidx.preference.TwoStatePreference;
|
|
||||||
|
|
||||||
import com.stevesoltys.backup.R;
|
|
||||||
|
|
||||||
import static android.content.Context.BACKUP_SERVICE;
|
|
||||||
import static android.os.ServiceManager.getService;
|
|
||||||
import static android.provider.Settings.Secure.BACKUP_AUTO_RESTORE;
|
|
||||||
|
|
||||||
public class SettingsFragment extends PreferenceFragmentCompat {
|
|
||||||
|
|
||||||
private final static String TAG = SettingsFragment.class.getSimpleName();
|
|
||||||
|
|
||||||
private IBackupManager backupManager;
|
|
||||||
|
|
||||||
private TwoStatePreference backup;
|
|
||||||
private TwoStatePreference autoRestore;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
|
||||||
setPreferencesFromResource(R.xml.settings, rootKey);
|
|
||||||
|
|
||||||
backupManager = IBackupManager.Stub.asInterface(getService(BACKUP_SERVICE));
|
|
||||||
|
|
||||||
backup = (TwoStatePreference) findPreference("backup");
|
|
||||||
backup.setOnPreferenceChangeListener((preference, newValue) -> {
|
|
||||||
boolean enabled = (boolean) newValue;
|
|
||||||
try {
|
|
||||||
backupManager.setBackupEnabled(enabled);
|
|
||||||
return true;
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
backup.setChecked(!enabled);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
autoRestore = (TwoStatePreference) findPreference("auto_restore");
|
|
||||||
autoRestore.setOnPreferenceChangeListener((preference, newValue) -> {
|
|
||||||
boolean enabled = (boolean) newValue;
|
|
||||||
try {
|
|
||||||
backupManager.setAutoRestore(enabled);
|
|
||||||
return true;
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
Log.e(TAG, "Error communicating with BackupManager", e);
|
|
||||||
autoRestore.setChecked(!enabled);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
|
|
||||||
try {
|
|
||||||
backup.setChecked(backupManager.isBackupEnabled());
|
|
||||||
backup.setEnabled(true);
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
Log.e(TAG, "Error communicating with BackupManager", e);
|
|
||||||
backup.setEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
ContentResolver resolver = requireContext().getContentResolver();
|
|
||||||
autoRestore.setChecked(Settings.Secure.getInt(resolver, BACKUP_AUTO_RESTORE, 1) == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.stevesoltys.backup.settings
|
||||||
|
|
||||||
|
import android.app.backup.IBackupManager
|
||||||
|
import android.content.ContentResolver
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.os.RemoteException
|
||||||
|
import android.provider.Settings
|
||||||
|
import android.util.Log
|
||||||
|
|
||||||
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
|
import androidx.preference.TwoStatePreference
|
||||||
|
|
||||||
|
import com.stevesoltys.backup.R
|
||||||
|
|
||||||
|
import android.content.Context.BACKUP_SERVICE
|
||||||
|
import android.os.ServiceManager.getService
|
||||||
|
import android.provider.Settings.Secure.BACKUP_AUTO_RESTORE
|
||||||
|
import androidx.preference.Preference
|
||||||
|
import androidx.preference.Preference.OnPreferenceChangeListener
|
||||||
|
|
||||||
|
private val TAG = SettingsFragment::class.java.name
|
||||||
|
|
||||||
|
class SettingsFragment : PreferenceFragmentCompat() {
|
||||||
|
|
||||||
|
private lateinit var backupManager: IBackupManager
|
||||||
|
|
||||||
|
private lateinit var backup: TwoStatePreference
|
||||||
|
private lateinit var autoRestore: TwoStatePreference
|
||||||
|
|
||||||
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
|
setPreferencesFromResource(R.xml.settings, rootKey)
|
||||||
|
|
||||||
|
backupManager = IBackupManager.Stub.asInterface(getService(BACKUP_SERVICE))
|
||||||
|
|
||||||
|
backup = findPreference("backup") as TwoStatePreference
|
||||||
|
backup.onPreferenceChangeListener = OnPreferenceChangeListener { _, newValue ->
|
||||||
|
val enabled = newValue as Boolean
|
||||||
|
try {
|
||||||
|
backupManager.isBackupEnabled = enabled
|
||||||
|
return@OnPreferenceChangeListener true
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
backup.isChecked = !enabled
|
||||||
|
return@OnPreferenceChangeListener false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
autoRestore = findPreference("auto_restore") as TwoStatePreference
|
||||||
|
autoRestore.onPreferenceChangeListener = OnPreferenceChangeListener { _, newValue ->
|
||||||
|
val enabled = newValue as Boolean
|
||||||
|
try {
|
||||||
|
backupManager.setAutoRestore(enabled)
|
||||||
|
return@OnPreferenceChangeListener true
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
Log.e(TAG, "Error communicating with BackupManager", e)
|
||||||
|
autoRestore.isChecked = !enabled
|
||||||
|
return@OnPreferenceChangeListener false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStart() {
|
||||||
|
super.onStart()
|
||||||
|
|
||||||
|
try {
|
||||||
|
backup.isChecked = backupManager.isBackupEnabled
|
||||||
|
backup.isEnabled = true
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
Log.e(TAG, "Error communicating with BackupManager", e)
|
||||||
|
backup.isEnabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
val resolver = requireContext().contentResolver
|
||||||
|
autoRestore.isChecked = Settings.Secure.getInt(resolver, BACKUP_AUTO_RESTORE, 1) == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue