|
| 1 | +/* |
| 2 | + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: GPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package com.zeapo.pwdstore.utils |
| 7 | + |
| 8 | +import android.app.KeyguardManager |
| 9 | +import android.content.ClipboardManager |
| 10 | +import android.content.Context |
| 11 | +import android.content.SharedPreferences |
| 12 | +import android.content.pm.PackageManager |
| 13 | +import android.os.Build |
| 14 | +import android.util.Base64 |
| 15 | +import android.util.TypedValue |
| 16 | +import android.view.View |
| 17 | +import android.view.autofill.AutofillManager |
| 18 | +import android.view.inputmethod.InputMethodManager |
| 19 | +import androidx.annotation.IdRes |
| 20 | +import androidx.annotation.RequiresApi |
| 21 | +import androidx.appcompat.app.AlertDialog |
| 22 | +import androidx.core.content.ContextCompat |
| 23 | +import androidx.core.content.getSystemService |
| 24 | +import androidx.fragment.app.FragmentActivity |
| 25 | +import androidx.preference.PreferenceManager |
| 26 | +import androidx.security.crypto.EncryptedSharedPreferences |
| 27 | +import androidx.security.crypto.MasterKey |
| 28 | +import com.github.ajalt.timberkt.d |
| 29 | +import com.github.michaelbull.result.Ok |
| 30 | +import com.github.michaelbull.result.Result |
| 31 | +import com.google.android.material.snackbar.Snackbar |
| 32 | +import com.zeapo.pwdstore.R |
| 33 | +import com.zeapo.pwdstore.git.operation.GitOperation |
| 34 | + |
| 35 | +/** |
| 36 | + * Extension function for [AlertDialog] that requests focus for the |
| 37 | + * view whose id is [id]. Solution based on a StackOverflow |
| 38 | + * answer: https://stackoverflow.com/a/13056259/297261 |
| 39 | + */ |
| 40 | +fun <T : View> AlertDialog.requestInputFocusOnView(@IdRes id: Int) { |
| 41 | + setOnShowListener { |
| 42 | + findViewById<T>(id)?.apply { |
| 43 | + setOnFocusChangeListener { v, _ -> |
| 44 | + v.post { |
| 45 | + context.getSystemService<InputMethodManager>() |
| 46 | + ?.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT) |
| 47 | + } |
| 48 | + } |
| 49 | + requestFocus() |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Get an instance of [AutofillManager]. Only |
| 56 | + * available on Android Oreo and above |
| 57 | + */ |
| 58 | +val Context.autofillManager: AutofillManager? |
| 59 | + @RequiresApi(Build.VERSION_CODES.O) |
| 60 | + get() = getSystemService() |
| 61 | + |
| 62 | +/** |
| 63 | + * Get an instance of [ClipboardManager] |
| 64 | + */ |
| 65 | +val Context.clipboard |
| 66 | + get() = getSystemService<ClipboardManager>() |
| 67 | + |
| 68 | +/** |
| 69 | + * Wrapper for [getEncryptedPrefs] to avoid open-coding the file name at |
| 70 | + * each call site |
| 71 | + */ |
| 72 | +fun Context.getEncryptedGitPrefs() = getEncryptedPrefs("git_operation") |
| 73 | + |
| 74 | +/** |
| 75 | + * Get an instance of [EncryptedSharedPreferences] with the given [fileName] |
| 76 | + */ |
| 77 | +private fun Context.getEncryptedPrefs(fileName: String): SharedPreferences { |
| 78 | + val masterKeyAlias = MasterKey.Builder(applicationContext) |
| 79 | + .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) |
| 80 | + .build() |
| 81 | + return EncryptedSharedPreferences.create( |
| 82 | + applicationContext, |
| 83 | + fileName, |
| 84 | + masterKeyAlias, |
| 85 | + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, |
| 86 | + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Get an instance of [KeyguardManager] |
| 92 | + */ |
| 93 | +val Context.keyguardManager: KeyguardManager |
| 94 | + get() = getSystemService()!! |
| 95 | + |
| 96 | +/** |
| 97 | + * Get the default [SharedPreferences] instance |
| 98 | + */ |
| 99 | +val Context.sharedPrefs: SharedPreferences |
| 100 | + get() = PreferenceManager.getDefaultSharedPreferences(applicationContext) |
| 101 | + |
| 102 | + |
| 103 | +/** |
| 104 | + * Resolve [attr] from the [Context]'s theme |
| 105 | + */ |
| 106 | +fun Context.resolveAttribute(attr: Int): Int { |
| 107 | + val typedValue = TypedValue() |
| 108 | + this.theme.resolveAttribute(attr, typedValue, true) |
| 109 | + return typedValue.data |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Commit changes to the store from a [FragmentActivity] using |
| 114 | + * a custom implementation of [GitOperation] |
| 115 | + */ |
| 116 | +suspend fun FragmentActivity.commitChange( |
| 117 | + message: String, |
| 118 | +): Result<Unit, Throwable> { |
| 119 | + if (!PasswordRepository.isGitRepo()) { |
| 120 | + return Ok(Unit) |
| 121 | + } |
| 122 | + return object : GitOperation(this@commitChange) { |
| 123 | + override val commands = arrayOf( |
| 124 | + // Stage all files |
| 125 | + git.add().addFilepattern("."), |
| 126 | + // Populate the changed files count |
| 127 | + git.status(), |
| 128 | + // Commit everything! If anything changed, that is. |
| 129 | + git.commit().setAll(true).setMessage(message), |
| 130 | + ) |
| 131 | + |
| 132 | + override fun preExecute(): Boolean { |
| 133 | + d { "Committing with message: '$message'" } |
| 134 | + return true |
| 135 | + } |
| 136 | + }.execute() |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Check if [permission] has been granted to the app. |
| 141 | + */ |
| 142 | +fun FragmentActivity.isPermissionGranted(permission: String): Boolean { |
| 143 | + return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Show a [Snackbar] in a [FragmentActivity] and correctly |
| 148 | + * anchor it to a [com.google.android.material.floatingactionbutton.FloatingActionButton] |
| 149 | + * if one exists in the [view] |
| 150 | + */ |
| 151 | +fun FragmentActivity.snackbar( |
| 152 | + view: View = findViewById(android.R.id.content), |
| 153 | + message: String, |
| 154 | + length: Int = Snackbar.LENGTH_SHORT, |
| 155 | +): Snackbar { |
| 156 | + val snackbar = Snackbar.make(view, message, length) |
| 157 | + snackbar.anchorView = findViewById(R.id.fab) |
| 158 | + snackbar.show() |
| 159 | + return snackbar |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * Simplifies the common `getString(key, null) ?: defaultValue` case slightly |
| 164 | + */ |
| 165 | +fun SharedPreferences.getString(key: String): String? = getString(key, null) |
| 166 | + |
| 167 | +/** |
| 168 | + * Convert this [String] to its [Base64] representation |
| 169 | + */ |
| 170 | +fun String.base64(): String { |
| 171 | + return Base64.encodeToString(encodeToByteArray(), Base64.NO_WRAP) |
| 172 | +} |
0 commit comments