Add a SettingsViewModel in Kotlin including the Kotlin deps
This commit is contained in:
parent
b983414295
commit
3d5911d41d
4 changed files with 90 additions and 1 deletions
|
@ -1,6 +1,8 @@
|
|||
import groovy.xml.XmlUtil
|
||||
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
|
||||
|
@ -78,8 +80,11 @@ dependencies {
|
|||
'libcore.jar'
|
||||
], dir: 'libs')
|
||||
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
|
||||
implementation 'commons-io:commons-io:2.6'
|
||||
|
||||
implementation 'androidx.preference:preference:1.0.0'
|
||||
implementation "androidx.core:core-ktx:1.0.2"
|
||||
implementation 'androidx.preference:preference-ktx:1.0.0'
|
||||
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
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;
|
||||
|
@ -8,23 +12,44 @@ 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();
|
||||
|
@ -49,4 +74,30 @@ public class SettingsActivity extends AppCompatActivity {
|
|||
}
|
||||
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,28 @@
|
|||
package com.stevesoltys.backup.settings
|
||||
|
||||
import android.app.Application
|
||||
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) {
|
||||
|
||||
private val app = application
|
||||
|
||||
fun locationIsSet() = getBackupFolderUri(getApplication()) != null
|
||||
|
||||
fun handleChooseFolderResult(result: Intent?) {
|
||||
val folderUri = result?.data ?: return
|
||||
|
||||
// persist permission to access backup folder across reboots
|
||||
val takeFlags = result.flags and (FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
app.contentResolver.takePersistableUriPermission(folderUri, takeFlags)
|
||||
|
||||
// store backup folder location in settings
|
||||
setBackupFolderUri(app, folderUri)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
|
||||
ext.kotlin_version = '1.3.40'
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
google()
|
||||
|
@ -8,6 +11,7 @@ buildscript {
|
|||
dependencies {
|
||||
// newer versions require us to remove targetSdkVersion from Manifest
|
||||
classpath 'com.android.tools.build:gradle:3.1.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
|
@ -16,6 +20,7 @@ buildscript {
|
|||
|
||||
allprojects {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
google()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue