mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Refactor util
This commit is contained in:
@@ -51,6 +51,7 @@ import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.timeout.TimeoutHelper
|
||||
import com.kunzisoft.keepass.utils.MenuUtil
|
||||
import com.kunzisoft.keepass.utils.Util
|
||||
import com.kunzisoft.keepass.utils.applyFontVisibility
|
||||
import com.kunzisoft.keepass.view.EntryEditCustomField
|
||||
|
||||
class EntryEditActivity : LockingHideActivity(), IconPickerDialogFragment.IconPickerListener, GeneratePasswordDialogFragment.GeneratePasswordListener {
|
||||
@@ -310,7 +311,7 @@ class EntryEditActivity : LockingHideActivity(), IconPickerDialogFragment.IconPi
|
||||
for (i in 0 until it.childCount) {
|
||||
val entryEditCustomField = it.getChildAt(i) as EntryEditCustomField
|
||||
val key = entryEditCustomField.label
|
||||
if (key == null || key.isEmpty()) {
|
||||
if (key.isEmpty()) {
|
||||
validationErrorMessageId = R.string.error_string_key
|
||||
return false
|
||||
}
|
||||
@@ -418,10 +419,10 @@ class EntryEditActivity : LockingHideActivity(), IconPickerDialogFragment.IconPi
|
||||
|
||||
val visibilityFontActivated = PreferencesUtil.fieldFontIsInVisibility(this)
|
||||
if (visibilityFontActivated) {
|
||||
Util.applyFontVisibilityTo(this, entryUserNameView)
|
||||
Util.applyFontVisibilityTo(this, entryPasswordView)
|
||||
Util.applyFontVisibilityTo(this, entryConfirmationPasswordView)
|
||||
Util.applyFontVisibilityTo(this, entryCommentView)
|
||||
entryUserNameView?.applyFontVisibility()
|
||||
entryPasswordView?.applyFontVisibility()
|
||||
entryConfirmationPasswordView?.applyFontVisibility()
|
||||
entryCommentView?.applyFontVisibility()
|
||||
}
|
||||
|
||||
if (entry.allowExtraFields()) {
|
||||
|
||||
@@ -39,13 +39,13 @@ class BrowserDialogFragment : DialogFragment() {
|
||||
|
||||
val market = root.findViewById<Button>(R.id.install_market)
|
||||
market.setOnClickListener {
|
||||
Util.gotoUrl(context, R.string.filemanager_play_store)
|
||||
Util.gotoUrl(context!!, R.string.filemanager_play_store)
|
||||
dismiss()
|
||||
}
|
||||
|
||||
val web = root.findViewById<Button>(R.id.install_web)
|
||||
web.setOnClickListener {
|
||||
Util.gotoUrl(context, R.string.filemanager_f_droid)
|
||||
Util.gotoUrl(context!!, R.string.filemanager_f_droid)
|
||||
dismiss()
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.password.PasswordGenerator
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.Util
|
||||
import com.kunzisoft.keepass.utils.applyFontVisibility
|
||||
|
||||
class GeneratePasswordDialogFragment : DialogFragment() {
|
||||
|
||||
@@ -66,7 +67,7 @@ class GeneratePasswordDialogFragment : DialogFragment() {
|
||||
root = inflater.inflate(R.layout.generate_password, null)
|
||||
|
||||
passwordView = root?.findViewById(R.id.password)
|
||||
Util.applyFontVisibilityTo(context, passwordView)
|
||||
passwordView?.applyFontVisibility()
|
||||
|
||||
lengthTextView = root?.findViewById(R.id.length)
|
||||
|
||||
|
||||
@@ -47,9 +47,9 @@ class KeyboardExplanationDialogFragment : DialogFragment() {
|
||||
|
||||
val containerKeyboardSwitcher = rootView.findViewById<View>(R.id.container_keyboard_switcher)
|
||||
if (BuildConfig.CLOSED_STORE) {
|
||||
containerKeyboardSwitcher.setOnClickListener { Util.gotoUrl(context, R.string.keyboard_switcher_play_store) }
|
||||
containerKeyboardSwitcher.setOnClickListener { Util.gotoUrl(context!!, R.string.keyboard_switcher_play_store) }
|
||||
} else {
|
||||
containerKeyboardSwitcher.setOnClickListener { Util.gotoUrl(context, R.string.keyboard_switcher_f_droid) }
|
||||
containerKeyboardSwitcher.setOnClickListener { Util.gotoUrl(context!!, R.string.keyboard_switcher_f_droid) }
|
||||
}
|
||||
|
||||
builder.setView(rootView)
|
||||
|
||||
@@ -49,7 +49,7 @@ class ProFeatureDialogFragment : DialogFragment() {
|
||||
stringBuilder.append(Html.fromHtml(getString(R.string.html_text_buy_pro)))
|
||||
builder.setPositiveButton(R.string.download) { _, _ ->
|
||||
try {
|
||||
Util.gotoUrl(context, R.string.app_pro_url)
|
||||
Util.gotoUrl(context!!, R.string.app_pro_url)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
Toast.makeText(context, R.string.error_failed_to_launch_link, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
@@ -59,7 +59,7 @@ class ProFeatureDialogFragment : DialogFragment() {
|
||||
stringBuilder.append(Html.fromHtml(getString(R.string.html_text_donation)))
|
||||
builder.setPositiveButton(R.string.contribute) { _, _ ->
|
||||
try {
|
||||
Util.gotoUrl(context, R.string.contribution_url)
|
||||
Util.gotoUrl(context!!, R.string.contribution_url)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
Toast.makeText(context, R.string.error_failed_to_launch_link, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class UnderDevelopmentFeatureDialogFragment : DialogFragment() {
|
||||
.append(Html.fromHtml(getString(R.string.html_text_dev_feature_encourage)))
|
||||
builder.setPositiveButton(R.string.download) { _, _ ->
|
||||
try {
|
||||
Util.gotoUrl(context, R.string.app_pro_url)
|
||||
Util.gotoUrl(context!!, R.string.app_pro_url)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
Toast.makeText(context, R.string.error_failed_to_launch_link, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
@@ -69,7 +69,7 @@ class UnderDevelopmentFeatureDialogFragment : DialogFragment() {
|
||||
.append(Html.fromHtml(getString(R.string.html_text_dev_feature_encourage)))
|
||||
builder.setPositiveButton(R.string.contribute) { _, _ ->
|
||||
try {
|
||||
Util.gotoUrl(context, R.string.contribution_url)
|
||||
Util.gotoUrl(context!!, R.string.contribution_url)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
Toast.makeText(context, R.string.error_failed_to_launch_link, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ class NodeAdapter
|
||||
}
|
||||
|
||||
private fun assignPreferences() {
|
||||
val textSizeDefault = Util.getListTextDefaultSize(context)
|
||||
val textSizeDefault = java.lang.Float.parseFloat(context.getString(R.string.list_size_default))
|
||||
this.textSize = PreferencesUtil.getListTextSize(context)
|
||||
this.subtextSize = context.resources.getInteger(R.integer.list_small_size_default) * textSize / textSizeDefault
|
||||
// Retrieve the icon size
|
||||
|
||||
@@ -32,6 +32,8 @@ import android.widget.ProgressBar
|
||||
import android.widget.TextView
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.utils.Util
|
||||
import com.kunzisoft.keepass.utils.lockScreenOrientation
|
||||
import com.kunzisoft.keepass.utils.unlockScreenOrientation
|
||||
|
||||
open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater {
|
||||
|
||||
@@ -77,7 +79,7 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater {
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface?) {
|
||||
Util.unlockScreenOrientation(activity)
|
||||
activity?.unlockScreenOrientation()
|
||||
super.onDismiss(dialog)
|
||||
}
|
||||
|
||||
@@ -134,7 +136,7 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater {
|
||||
|
||||
fun start(activity: FragmentActivity,
|
||||
dialog: ProgressTaskDialogFragment) {
|
||||
Util.lockScreenOrientation(activity)
|
||||
activity.lockScreenOrientation()
|
||||
dialog.show(activity.supportFragmentManager, PROGRESS_TASK_DIALOG_TAG)
|
||||
}
|
||||
|
||||
@@ -143,7 +145,7 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater {
|
||||
if (fragmentTask != null) {
|
||||
val loadingDatabaseDialog = fragmentTask as ProgressTaskDialogFragment
|
||||
loadingDatabaseDialog.dismissAllowingStateLoss()
|
||||
Util.unlockScreenOrientation(activity)
|
||||
activity.unlockScreenOrientation()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017 Brian Pellin, 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.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Typeface;
|
||||
import android.net.Uri;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.kunzisoft.keepass.R;
|
||||
|
||||
public class Util {
|
||||
|
||||
public static void gotoUrl(Context context, String url) throws ActivityNotFoundException {
|
||||
if ( url != null && url.length() > 0 ) {
|
||||
Uri uri = Uri.parse(url);
|
||||
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
|
||||
}
|
||||
}
|
||||
|
||||
public static void gotoUrl(Context context, int resId) throws ActivityNotFoundException {
|
||||
gotoUrl(context, context.getString(resId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace font by monospace, must be called after seText()
|
||||
*/
|
||||
public static void applyFontVisibilityTo(final Context context, final TextView textView) {
|
||||
Typeface typeFace=Typeface.createFromAsset(context.getAssets(),"fonts/DroidSansMonoSlashed.ttf");
|
||||
textView.setTypeface(typeFace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace font by monospace, must be called after seText()
|
||||
*/
|
||||
public static void applyFontVisibilityTo(final Context context, final EditText editText) {
|
||||
applyFontVisibilityTo(context, (TextView) editText);
|
||||
}
|
||||
|
||||
public static float getListTextDefaultSize(Context context) {
|
||||
return Float.parseFloat(context.getString(R.string.list_size_default));
|
||||
}
|
||||
|
||||
public static void lockScreenOrientation(Activity activity) {
|
||||
if (activity != null) {
|
||||
int currentOrientation = activity.getResources().getConfiguration().orientation;
|
||||
if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
} else {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void unlockScreenOrientation(Activity activity) {
|
||||
if (activity != null) {
|
||||
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
|
||||
}
|
||||
}
|
||||
}
|
||||
66
app/src/main/java/com/kunzisoft/keepass/utils/Util.kt
Normal file
66
app/src/main/java/com/kunzisoft/keepass/utils/Util.kt
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.utils
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.content.res.Configuration
|
||||
import android.graphics.Typeface
|
||||
import android.net.Uri
|
||||
import android.widget.TextView
|
||||
|
||||
object Util {
|
||||
|
||||
@Throws(ActivityNotFoundException::class)
|
||||
fun gotoUrl(context: Context, url: String?) {
|
||||
if (url != null && url.isNotEmpty()) {
|
||||
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(ActivityNotFoundException::class)
|
||||
fun gotoUrl(context: Context, resId: Int) {
|
||||
gotoUrl(context, context.getString(resId))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace font by monospace, must be called after seText()
|
||||
*/
|
||||
fun TextView.applyFontVisibility() {
|
||||
val typeFace = Typeface.createFromAsset(context.assets, "fonts/DroidSansMonoSlashed.ttf")
|
||||
typeface = typeFace
|
||||
}
|
||||
|
||||
fun Activity.lockScreenOrientation() {
|
||||
val currentOrientation = resources.configuration.orientation
|
||||
requestedOrientation = if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
|
||||
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
||||
} else {
|
||||
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
|
||||
}
|
||||
}
|
||||
|
||||
fun Activity.unlockScreenOrientation() {
|
||||
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import android.widget.TextView
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.utils.Util
|
||||
import com.kunzisoft.keepass.utils.applyFontVisibility
|
||||
import java.text.DateFormat
|
||||
import java.util.*
|
||||
|
||||
@@ -121,7 +122,7 @@ class EntryContentsView @JvmOverloads constructor(context: Context, attrs: Attri
|
||||
userNameView?.apply {
|
||||
text = userName
|
||||
if (fontInVisibility)
|
||||
Util.applyFontVisibilityTo(context, this)
|
||||
applyFontVisibility()
|
||||
}
|
||||
} else {
|
||||
userNameContainerView?.visibility = View.GONE
|
||||
@@ -138,7 +139,7 @@ class EntryContentsView @JvmOverloads constructor(context: Context, attrs: Attri
|
||||
passwordView?.apply {
|
||||
text = password
|
||||
if (fontInVisibility)
|
||||
Util.applyFontVisibilityTo(context, this)
|
||||
applyFontVisibility()
|
||||
}
|
||||
if (!allowCopyPassword) {
|
||||
passwordActionView?.setColorFilter(ContextCompat.getColor(context, R.color.grey_dark))
|
||||
@@ -198,7 +199,7 @@ class EntryContentsView @JvmOverloads constructor(context: Context, attrs: Attri
|
||||
commentView?.apply {
|
||||
text = comment
|
||||
if (fontInVisibility)
|
||||
Util.applyFontVisibilityTo(context, this)
|
||||
applyFontVisibility()
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
@@ -30,6 +30,7 @@ import android.widget.TextView
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.utils.Util
|
||||
import com.kunzisoft.keepass.utils.applyFontVisibility
|
||||
|
||||
open class EntryCustomField(context: Context,
|
||||
attrs: AttributeSet?,
|
||||
@@ -70,7 +71,7 @@ open class EntryCustomField(context: Context,
|
||||
|
||||
fun applyFontVisibility(fontInVisibility: Boolean) {
|
||||
if (fontInVisibility)
|
||||
Util.applyFontVisibilityTo(context, valueView)
|
||||
valueView.applyFontVisibility()
|
||||
}
|
||||
|
||||
fun setLabel(label: String?) {
|
||||
|
||||
@@ -33,6 +33,7 @@ import android.widget.TextView
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.utils.Util
|
||||
import com.kunzisoft.keepass.utils.applyFontVisibility
|
||||
|
||||
class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
@@ -76,7 +77,7 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
|
||||
fun setFontVisibility(applyFontVisibility: Boolean) {
|
||||
if (applyFontVisibility)
|
||||
Util.applyFontVisibilityTo(context, valueView)
|
||||
valueView.applyFontVisibility()
|
||||
}
|
||||
|
||||
private fun deleteViewFromParent() {
|
||||
|
||||
Reference in New Issue
Block a user