code cleanup & refactor

This commit is contained in:
zeapo 2017-10-30 22:37:07 +01:00
parent e85cd2297b
commit d697b6a46c
2 changed files with 38 additions and 36 deletions

View file

@ -272,7 +272,7 @@ class UserPreference : AppCompatActivity() {
* @param reason The text to be shown to the user to explain why we're requesting this permission * @param reason The text to be shown to the user to explain why we're requesting this permission
* @param body The function to run * @param body The function to run
*/ */
fun runWithPermissions(requestedPermission: String, requestCode: Int, reason: String, body: () -> Unit): Unit { private fun runWithPermissions(requestedPermission: String, requestCode: Int, reason: String, body: () -> Unit): Unit {
if (ContextCompat.checkSelfPermission(this, requestedPermission) != PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(this, requestedPermission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, requestedPermission)) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, requestedPermission)) {
val snack = Snackbar.make(prefsFragment.view, val snack = Snackbar.make(prefsFragment.view,
@ -283,7 +283,7 @@ class UserPreference : AppCompatActivity() {
} }
snack.show() snack.show()
val view = snack.view val view = snack.view
val tv = view.findViewById(android.support.design.R.id.snackbar_text) as TextView val tv = view.findViewById<TextView>(android.support.design.R.id.snackbar_text)
tv.setTextColor(Color.WHITE) tv.setTextColor(Color.WHITE)
tv.maxLines = 10 tv.maxLines = 10
} else { } else {
@ -310,7 +310,7 @@ class UserPreference : AppCompatActivity() {
/** /**
* Exports the passwords * Exports the passwords
*/ */
fun exportPasswords(): Unit { private fun exportPasswords(): Unit {
val i = Intent(applicationContext, FilePickerActivity::class.java) val i = Intent(applicationContext, FilePickerActivity::class.java)
// Set these depending on your use case. These are the defaults. // Set these depending on your use case. These are the defaults.

View file

@ -1,5 +1,6 @@
package com.zeapo.pwdstore.crypto package com.zeapo.pwdstore.crypto
import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.app.PendingIntent import android.app.PendingIntent
import android.content.* import android.content.*
@ -33,22 +34,22 @@ import java.io.File
import java.nio.charset.Charset import java.nio.charset.Charset
class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
val clipboard: ClipboardManager by lazy { private val clipboard: ClipboardManager by lazy {
getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
} }
var passwordEntry: PasswordEntry? = null private var passwordEntry: PasswordEntry? = null
var api: OpenPgpApi? = null private var api: OpenPgpApi? = null
val operation: String by lazy { intent.getStringExtra("OPERATION") } private val operation: String by lazy { intent.getStringExtra("OPERATION") }
val repoPath: String by lazy { intent.getStringExtra("REPO_PATH") } private val repoPath: String by lazy { intent.getStringExtra("REPO_PATH") }
val fullPath: String by lazy { intent.getStringExtra("FILE_PATH") } private val fullPath: String by lazy { intent.getStringExtra("FILE_PATH") }
val name: String by lazy { getName(fullPath, repoPath) } private val name: String by lazy { getName(fullPath, repoPath) }
val relativeParentPath: String by lazy { getParentPath(fullPath, repoPath) } private val relativeParentPath: String by lazy { getParentPath(fullPath, repoPath) }
val settings: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) } private val settings: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) }
val keyIDs: MutableSet<String> by lazy { settings.getStringSet("openpgp_key_ids_set", emptySet()) } private val keyIDs: MutableSet<String> by lazy { settings.getStringSet("openpgp_key_ids_set", emptySet()) }
var mServiceConnection: OpenPgpServiceConnection? = null private var mServiceConnection: OpenPgpServiceConnection? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -130,7 +131,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
/** /**
* Shows a simple toast message * Shows a simple toast message
*/ */
fun showToast(message: String) { private fun showToast(message: String) {
runOnUiThread({ Toast.makeText(this, message, Toast.LENGTH_SHORT).show() }) runOnUiThread({ Toast.makeText(this, message, Toast.LENGTH_SHORT).show() })
} }
@ -140,7 +141,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
* @param result The intent returned by OpenKeychain * @param result The intent returned by OpenKeychain
* @param requestCode The code we'd like to use to identify the behaviour * @param requestCode The code we'd like to use to identify the behaviour
*/ */
fun handleUserInteractionRequest(result: Intent, requestCode: Int) { private fun handleUserInteractionRequest(result: Intent, requestCode: Int) {
Log.i(TAG, "RESULT_CODE_USER_INTERACTION_REQUIRED") Log.i(TAG, "RESULT_CODE_USER_INTERACTION_REQUIRED")
val pi: PendingIntent = result.getParcelableExtra(RESULT_INTENT) val pi: PendingIntent = result.getParcelableExtra(RESULT_INTENT)
@ -158,7 +159,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
* *
* @param result The intent returned by OpenKeychain * @param result The intent returned by OpenKeychain
*/ */
fun handleError(result: Intent) { private fun handleError(result: Intent) {
// TODO show what kind of error it is // TODO show what kind of error it is
/* For example: /* For example:
* No suitable key found -> no key in OpenKeyChain * No suitable key found -> no key in OpenKeyChain
@ -171,7 +172,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
Log.e(TAG, "onError getMessage:" + error.message) Log.e(TAG, "onError getMessage:" + error.message)
} }
fun initOpenPgpApi() { private fun initOpenPgpApi() {
api = api ?: OpenPgpApi(this, mServiceConnection?.service) api = api ?: OpenPgpApi(this, mServiceConnection?.service)
} }
@ -252,7 +253,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
/** /**
* Encrypts the password and the extra content * Encrypts the password and the extra content
*/ */
private fun encrypt(): Unit { private fun encrypt() {
val name = crypto_password_file_edit.text.toString().trim() val name = crypto_password_file_edit.text.toString().trim()
val pass = crypto_password_edit.text.toString() val pass = crypto_password_edit.text.toString()
val extra = crypto_extra_edit.text.toString() val extra = crypto_extra_edit.text.toString()
@ -315,7 +316,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
/** /**
* Opens EncryptActivity with the information for this file to be edited * Opens EncryptActivity with the information for this file to be edited
*/ */
fun editPassword() { private fun editPassword() {
setContentView(R.layout.encrypt_layout) setContentView(R.layout.encrypt_layout)
title = getString(R.string.edit_password_title) title = getString(R.string.edit_password_title)
@ -342,7 +343,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
/** /**
* Get the Key ids from OpenKeychain * Get the Key ids from OpenKeychain
*/ */
fun getKeyIds(receivedIntent: Intent? = null) { private fun getKeyIds(receivedIntent: Intent? = null) {
val data = receivedIntent ?: Intent() val data = receivedIntent ?: Intent()
data.action = OpenPgpApi.ACTION_GET_KEY_IDS data.action = OpenPgpApi.ACTION_GET_KEY_IDS
api?.executeApiAsync(data, null, null, { result: Intent? -> api?.executeApiAsync(data, null, null, { result: Intent? ->
@ -420,6 +421,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
return if (shown) charSequence else super.getTransformation("12345", view) return if (shown) charSequence else super.getTransformation("12345", view)
} }
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean { override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
when (motionEvent.action) { when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> { MotionEvent.ACTION_DOWN -> {
@ -435,9 +437,8 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
} }
} }
fun copyPasswordToClipBoard() { private fun copyPasswordToClipBoard() {
if (findViewById<TextView>(R.id.crypto_password_show) == null)
if (findViewById(R.id.crypto_password_show) == null)
return return
setTimer() setTimer()
@ -455,15 +456,15 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
showToast(this.resources.getString(R.string.clipboard_password_toast_text, clearAfter)) showToast(this.resources.getString(R.string.clipboard_password_toast_text, clearAfter))
} }
fun copyUsernameToClipBoard(username: String) { private fun copyUsernameToClipBoard(username: String) {
val clip = ClipData.newPlainText("pgp_handler_result_pm", username) val clip = ClipData.newPlainText("pgp_handler_result_pm", username)
clipboard.primaryClip = clip clipboard.primaryClip = clip
showToast(resources.getString(R.string.clipboard_username_toast_text)) showToast(resources.getString(R.string.clipboard_username_toast_text))
} }
fun shareAsPlaintext() { private fun shareAsPlaintext() {
if (findViewById(R.id.share_password_as_plaintext) == null) if (findViewById<View>(R.id.share_password_as_plaintext) == null)
return return
val sendIntent = Intent() val sendIntent = Intent()
@ -473,7 +474,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
startActivity(Intent.createChooser(sendIntent, resources.getText(R.string.send_plaintext_password_to)))//Always show a picker to give the user a chance to cancel startActivity(Intent.createChooser(sendIntent, resources.getText(R.string.send_plaintext_password_to)))//Always show a picker to give the user a chance to cancel
} }
fun setTimer() { private fun setTimer() {
delayTask?.skip = true delayTask?.skip = true
// launch a new one // launch a new one
@ -481,29 +482,30 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
delayTask?.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) delayTask?.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
} }
@SuppressLint("StaticFieldLeak")
inner class DelayShow(val activity: PgpActivity) : AsyncTask<Void, Int, Boolean>() { inner class DelayShow(val activity: PgpActivity) : AsyncTask<Void, Int, Boolean>() {
internal val pb: ProgressBar by lazy { pbLoading } private val pb: ProgressBar by lazy { pbLoading }
internal var skip = false internal var skip = false
internal var showTime: Int = 0 private var showTime: Int = 0
val settings: SharedPreferences by lazy { val settings: SharedPreferences by lazy {
PreferenceManager.getDefaultSharedPreferences(activity) PreferenceManager.getDefaultSharedPreferences(activity)
} }
override fun onPreExecute() { override fun onPreExecute() {
try { showTime = try {
showTime = Integer.parseInt(settings.getString("general_show_time", "45")) Integer.parseInt(settings.getString("general_show_time", "45"))
} catch (e: NumberFormatException) { } catch (e: NumberFormatException) {
showTime = 45 45
} }
val container = findViewById(R.id.crypto_container_decrypt) as LinearLayout val container = findViewById<LinearLayout>(R.id.crypto_container_decrypt)
container.visibility = View.VISIBLE container.visibility = View.VISIBLE
val extraText = findViewById(R.id.crypto_extra_show) as TextView val extraText = findViewById<TextView>(R.id.crypto_extra_show)
if (extraText.text.isNotEmpty()) if (extraText.text.isNotEmpty())
findViewById(R.id.crypto_extra_show_layout).visibility = View.VISIBLE findViewById<View>(R.id.crypto_extra_show_layout).visibility = View.VISIBLE
if (showTime == 0) { if (showTime == 0) {
// treat 0 as forever, and the user must exit and/or clear clipboard on their own // treat 0 as forever, and the user must exit and/or clear clipboard on their own