Start change database file creation

This commit is contained in:
J-Jamet
2019-08-16 11:24:00 +02:00
parent 3989ca3ad1
commit b737501d4d
11 changed files with 55 additions and 259 deletions

View File

@@ -98,7 +98,6 @@ dependencies {
// Time
implementation 'joda-time:joda-time:2.9.9'
implementation 'org.sufficientlysecure:html-textview:3.5'
implementation 'com.nononsenseapps:filepicker:4.1.0'
implementation 'com.getkeepsafe.taptargetview:taptargetview:1.12.0'
// Permissions
implementation("com.github.hotchemi:permissionsdispatcher:$permissionDispatcherVersion") {

View File

@@ -78,24 +78,6 @@
<data android:mimeType="application/octet-stream"/>
</intent-filter>
</activity>
<!-- Folder picker -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/nnf_provider_paths" />
</provider>
<activity
android:name=".activities.stylish.FilePickerStylishActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Main Activity -->
<activity
android:name="com.kunzisoft.keepass.activities.GroupActivity"

View File

@@ -26,39 +26,26 @@ import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.Spinner
import android.widget.TextView
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.stylish.FilePickerStylishActivity
import com.kunzisoft.keepass.utils.UriUtil
import com.nononsenseapps.filepicker.FilePickerActivity
import com.nononsenseapps.filepicker.Utils
class CreateFileDialogFragment : DialogFragment(), AdapterView.OnItemSelectedListener {
private val FILE_CODE = 3853
class CreateFileDialogFragment : DialogFragment() {
private var folderPathView: EditText? = null
private var fileNameView: EditText? = null
private var pathFileNameView: EditText? = null
private var positiveButton: Button? = null
private var negativeButton: Button? = null
private var mDefinePathDialogListener: DefinePathDialogListener? = null
private var mDatabaseFileExtension: String? = null
private var mUriPath: Uri? = null
interface DefinePathDialogListener {
@@ -114,48 +101,14 @@ class CreateFileDialogFragment : DialogFragment(), AdapterView.OnItemSelectedLis
// Folder selection
val browseView = rootView.findViewById<View>(R.id.browse_button)
folderPathView = rootView.findViewById(R.id.folder_path)
folderPathView?.customSelectionActionModeCallback = actionCopyBarCallback
fileNameView = rootView.findViewById(R.id.filename)
fileNameView?.customSelectionActionModeCallback = actionCopyBarCallback
val defaultPath = Environment.getExternalStorageDirectory().path + getString(R.string.database_file_path_default)
folderPathView?.setText(defaultPath)
browseView.setOnClickListener { _ ->
Intent(context, FilePickerStylishActivity::class.java).apply {
putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)
putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true)
putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR)
putExtra(FilePickerActivity.EXTRA_START_PATH,
Environment.getExternalStorageDirectory().path)
startActivityForResult(this, FILE_CODE)
}
}
pathFileNameView = rootView.findViewById(R.id.folder_path)
pathFileNameView?.customSelectionActionModeCallback = actionCopyBarCallback
pathFileNameView?.setText("/document/primary:keepass/keepass.kdbx") // TODO
browseView.setOnClickListener { createNewFile() }
// Init path
mUriPath = null
// Extension
mDatabaseFileExtension = getString(R.string.database_file_extension_default)
val spinner = rootView.findViewById<Spinner>(R.id.file_types)
spinner.onItemSelectedListener = this
// Spinner Drop down elements
val fileTypes = resources.getStringArray(R.array.file_types)
val dataAdapter = ArrayAdapter(activity, android.R.layout.simple_spinner_item, fileTypes)
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = dataAdapter
// Or text if only one item https://github.com/Kunzisoft/KeePassDX/issues/105
if (fileTypes.size == 1) {
val params = spinner.layoutParams
spinner.visibility = View.GONE
val extensionTextView = TextView(context)
extensionTextView.text = mDatabaseFileExtension
extensionTextView.layoutParams = params
val parentView = spinner.parent as ViewGroup
parentView.addView(extensionTextView)
}
val dialog = builder.create()
dialog.setOnShowListener { _ ->
@@ -181,10 +134,8 @@ class CreateFileDialogFragment : DialogFragment(), AdapterView.OnItemSelectedLis
}
private fun buildPath(): Uri? {
if (folderPathView != null && fileNameView != null && mDatabaseFileExtension != null) {
var path = Uri.Builder().path(folderPathView!!.text.toString())
.appendPath(fileNameView!!.text.toString() + mDatabaseFileExtension!!)
.build()
if (pathFileNameView != null) {
var path = Uri.Builder().path(pathFileNameView!!.text.toString()).build()
context?.let { context ->
path = UriUtil.translateUri(context, path)
}
@@ -193,21 +144,33 @@ class CreateFileDialogFragment : DialogFragment(), AdapterView.OnItemSelectedLis
return null
}
/**
* Create a new file by calling the content provider
*/
private fun createNewFile() {
startActivityForResult(Intent(
Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/x-keepass"
putExtra(Intent.EXTRA_TITLE, getString(R.string.database_file_name_default) +
getString(R.string.database_file_extension_default))
},
CREATE_FILE_REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == FILE_CODE && resultCode == Activity.RESULT_OK) {
if (requestCode == CREATE_FILE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
mUriPath = data?.data
mUriPath?.let {
val file = Utils.getFileForUri(it)
folderPathView?.setText(file.path)
val file = data?.data
if (file != null) {
pathFileNameView?.setText(file.path)
}
}
}
}
override fun onItemSelected(adapterView: AdapterView<*>, view: View, position: Int, id: Long) {
mDatabaseFileExtension = adapterView.getItemAtPosition(position).toString()
}
override fun onNothingSelected(adapterView: AdapterView<*>) {
// Do nothing
companion object {
private const val CREATE_FILE_REQUEST_CODE = 3853
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright 2019 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.activities.stylish
import android.os.Bundle
import android.support.annotation.StyleRes
import android.util.Log
import com.nononsenseapps.filepicker.FilePickerActivity
/**
* FilePickerActivity class with a style compatibility
*/
class FilePickerStylishActivity : FilePickerActivity() {
@StyleRes
private var themeId: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
this.themeId = Stylish.getFilePickerThemeId(this)
setTheme(themeId)
super.onCreate(savedInstanceState)
}
override fun onResume() {
super.onResume()
if (Stylish.getFilePickerThemeId(this) != this.themeId) {
Log.d(this.javaClass.name, "Theme change detected, restarting activity")
this.recreate()
}
}
}

View File

@@ -68,15 +68,4 @@ object Stylish {
else -> R.style.KeepassDXStyle_Light
}
}
@StyleRes
fun getFilePickerThemeId(context: Context): Int {
return when {
themeString.equals(context.getString(R.string.list_style_name_dark)) -> R.style.KeepassDXStyle_FilePickerStyle_Dark
themeString.equals(context.getString(R.string.list_style_name_blue)) -> R.style.KeepassDXStyle_FilePickerStyle_Blue
themeString.equals(context.getString(R.string.list_style_name_red)) -> R.style.KeepassDXStyle_FilePickerStyle_Red
themeString.equals(context.getString(R.string.list_style_name_purple)) -> R.style.KeepassDXStyle_FilePickerStyle_Purple
else -> R.style.KeepassDXStyle_FilePickerStyle
}
}
}

View File

@@ -1,88 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/default_margin"
android:importantForAutofill="noExcludeDescendants"
tools:targetApi="o">
<RelativeLayout
<android.support.v7.widget.AppCompatImageView
android:id="@+id/browse_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/folder_path_input_layout"
android:src="@drawable/ic_folder_white_24dp"
android:tint="?attr/colorAccent" />
<android.support.design.widget.TextInputLayout
android:id="@+id/folder_path_input_layout"
android:layout_toLeftOf="@id/browse_button"
android:layout_toStartOf="@id/browse_button"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/browse_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/folder_path_input_layout"
android:src="@drawable/ic_folder_white_24dp"
android:tint="?attr/colorAccent" />
<android.support.design.widget.TextInputLayout
android:id="@+id/folder_path_input_layout"
android:layout_toLeftOf="@id/browse_button"
android:layout_toStartOf="@id/browse_button"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/folder_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/path"
android:inputType="textUri"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="@+id/filename_input_layout"
<android.support.design.widget.TextInputEditText
android:id="@+id/folder_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/container_file_type"
android:layout_toStartOf="@+id/container_file_type">
android:hint="@string/path"
android:inputType="textUri"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputEditText
android:id="@+id/filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/file_name"
android:text="@string/database_file_name_default"
android:inputType="textUri"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<!-- To add text view for one item -->
<FrameLayout
android:id="@+id/container_file_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/filename_input_layout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/file_types"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"/>
</FrameLayout>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>

View File

@@ -48,10 +48,4 @@
<style name="KeepassDXStyle.ActionMode.Blue" parent="@style/Widget.AppCompat.ActionMode">
<item name="background">@color/blue_dark</item>
</style>
<!-- File Picker Theme -->
<style name="KeepassDXStyle.FilePickerStyle.Blue" parent="KeepassDXStyle.FilePickerStyle">
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryDark">@color/blue_dark</item>
<item name="colorAccent">@color/blue</item>
</style>
</resources>

View File

@@ -49,10 +49,4 @@
<item name="colorAccent">#fefefe</item>
<item name="android:textColorPrimary">#80cbc4</item>
</style>
<!-- File Picker Theme -->
<style name="KeepassDXStyle.FilePickerStyle.Dark" parent="KeepassDXStyle.FilePickerStyle">
<item name="colorPrimary">#212121</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#59585a</item>
</style>
</resources>

View File

@@ -50,10 +50,4 @@
<style name="KeepassDXStyle.ActionMode.Purple" parent="@style/Widget.AppCompat.ActionMode">
<item name="background">@color/purple_dark</item>
</style>
<!-- File Picker Theme -->
<style name="KeepassDXStyle.FilePickerStyle.Purple" parent="KeepassDXStyle.FilePickerStyle">
<item name="colorPrimary">@color/purple</item>
<item name="colorPrimaryDark">@color/purple_dark</item>
<item name="colorAccent">@color/red</item>
</style>
</resources>

View File

@@ -45,10 +45,4 @@
<style name="KeepassDXStyle.ActionMode.Red" parent="@style/Widget.AppCompat.ActionMode">
<item name="background">@color/red_dark</item>
</style>
<!-- File Picker Theme -->
<style name="KeepassDXStyle.FilePickerStyle.Red" parent="KeepassDXStyle.FilePickerStyle">
<item name="colorPrimary">@color/red</item>
<item name="colorPrimaryDark">@color/red_dark</item>
<item name="colorAccent">@color/red</item>
</style>
</resources>

View File

@@ -319,19 +319,4 @@
<item name="android:textSize">16sp</item>
</style>
<!-- File Picker Theme -->
<style name="KeepassDXStyle.FilePickerStyle" parent="Theme.AppCompat.DialogWhenLarge">
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryDark">@color/green_dark</item>
<item name="colorAccent">@color/orange</item>
<item name="nnf_toolbarTheme">@style/ThemeOverlay.AppCompat.ActionBar</item>
<item name="nnf_separator_color">@color/nnf_dark_separator_color</item>
<item name="nnf_save_icon_color">?attr/colorAccent</item>
<item name="nnf_dir_icon_color">?attr/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>