aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarvin Borner2018-09-13 16:25:25 +0200
committerMarvin Borner2018-09-13 16:25:25 +0200
commit743f2f35afbd510d428d3d5982a1dd5da67fe0c2 (patch)
tree1e0b12c1376910bc3edac581d05dc61540bf3f52
parented1e15a75dec67bc47d393a227c6db870df6bc3f (diff)
Applied naming convention for util package :art:
-rw-r--r--app/src/main/java/me/texx/Texx/util/PermissionUtil.kt62
-rw-r--r--app/src/main/java/me/texx/Texx/util/SecureStorage.kt71
-rw-r--r--app/src/main/java/me/texx/Texx/util/ThemeUtil.kt34
3 files changed, 167 insertions, 0 deletions
diff --git a/app/src/main/java/me/texx/Texx/util/PermissionUtil.kt b/app/src/main/java/me/texx/Texx/util/PermissionUtil.kt
new file mode 100644
index 0000000..e31a10b
--- /dev/null
+++ b/app/src/main/java/me/texx/Texx/util/PermissionUtil.kt
@@ -0,0 +1,62 @@
+package me.texx.Texx.util
+
+import android.Manifest.permission.ACCESS_FINE_LOCATION
+import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
+import android.app.Activity
+import android.content.pm.PackageManager
+import android.support.v4.app.ActivityCompat
+import android.support.v4.app.ActivityCompat.finishAffinity
+import android.support.v4.content.ContextCompat
+import org.jetbrains.anko.alert
+
+
+/**
+ * Object with function for asking for permissions
+ */
+object PermissionUtil {
+ /**
+ * Asks for specified permission and starts actions depending on users choose
+ */
+ fun askForPermission(permissionString: String, activity: Activity) {
+ if (ContextCompat.checkSelfPermission(activity, permissionString) != PackageManager.PERMISSION_GRANTED) {
+ if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionString)) {
+ activity.alert(getPermissionText(permissionString), "${getPermissionName(permissionString, activity)}-Permission is missing") {
+ isCancelable = false
+ negativeButton("Never!") {
+ finishAffinity(activity)
+ }
+ positiveButton("Okay") {
+ ActivityCompat.requestPermissions(activity, arrayOf(permissionString), 1)
+ }
+ }.show()
+ } else {
+ ActivityCompat.requestPermissions(activity, arrayOf(permissionString), 1)
+ }
+ }
+ }
+
+ /**
+ * Returns true if permission is granted
+ */
+ fun permissionGranted(permissionString: String, activity: Activity): Boolean {
+ return ContextCompat.checkSelfPermission(activity, permissionString) == PackageManager.PERMISSION_GRANTED
+ }
+
+ private fun getPermissionName(permissionString: String, activity: Activity): String {
+ val packageManager = activity.packageManager
+ val permissionInfo = packageManager.getPermissionInfo(permissionString, 0)
+ val permissionGroupInfo = packageManager.getPermissionGroupInfo(permissionInfo.group, 0)
+ return permissionGroupInfo.loadLabel(packageManager).toString()
+ }
+
+ /**
+ * Gets the text for permission requests
+ */
+ private fun getPermissionText(permissionString: String): String {
+ return when (permissionString) {
+ ACCESS_FINE_LOCATION -> "Optional permission to classify your image by location - deny if you don't want to share the photo's location."
+ WRITE_EXTERNAL_STORAGE -> "We need this permission in order to save your image on you device."
+ else -> "Please allow if you want to use the apps whole potential."
+ }
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/me/texx/Texx/util/SecureStorage.kt b/app/src/main/java/me/texx/Texx/util/SecureStorage.kt
new file mode 100644
index 0000000..4c812ad
--- /dev/null
+++ b/app/src/main/java/me/texx/Texx/util/SecureStorage.kt
@@ -0,0 +1,71 @@
+package me.texx.Texx.util
+
+import android.content.Context
+import android.preference.PreferenceManager
+import android.util.Base64
+import com.kazakago.cryptore.CipherAlgorithm
+import com.kazakago.cryptore.Cryptore
+import com.madapps.prefrences.EasyPrefrences
+
+/**
+ * Class for saving data securely in SharedPreferences
+ */
+class SecureStorage(private val context: Context) {
+ /**
+ * Encrypts and saves the [value] with [key] as index
+ */
+ fun set(key: String, value: String) {
+ sharedPrefs.putString(key, encryptAES(value))
+ }
+
+ /**
+ * Finds the encrypted value by [key], decrypts it and returns the value as string
+ */
+ fun get(key: String): String? {
+ return try {
+ decryptAES(sharedPrefs.getString(key))
+ } catch (e: Exception) {
+ null
+ }
+ }
+
+ private val sharedPrefs = EasyPrefrences(context)
+
+ private enum class Alias(val value: String) {
+ RSA("CIPHER_RSA"),
+ AES("CIPHER_AES")
+ }
+
+ private val cryptoreAES: Cryptore by lazy {
+ val builder = Cryptore.Builder(alias = Alias.AES.value, type = CipherAlgorithm.AES)
+ builder.build()
+ }
+
+ private fun encryptAES(plainStr: String): String {
+ val plainByte = plainStr.toByteArray()
+ val result = cryptoreAES.encrypt(plainByte = plainByte)
+ cipherIV = result.cipherIV
+ return Base64.encodeToString(result.bytes, Base64.DEFAULT)
+ }
+
+ private fun decryptAES(encryptedStr: String): String {
+ val encryptedByte = Base64.decode(encryptedStr, Base64.DEFAULT)
+ val result = cryptoreAES.decrypt(encryptedByte = encryptedByte, cipherIV = cipherIV)
+ return String(result.bytes)
+ }
+
+ private var cipherIV: ByteArray?
+ get() {
+ val preferences = PreferenceManager.getDefaultSharedPreferences(this.context)
+ preferences.getString("cipher_iv", null)?.let {
+ return Base64.decode(it, Base64.DEFAULT)
+ }
+ return null
+ }
+ set(value) {
+ val preferences = PreferenceManager.getDefaultSharedPreferences(this.context)
+ val editor = preferences.edit()
+ editor.putString("cipher_iv", Base64.encodeToString(value, Base64.DEFAULT))
+ editor.apply()
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/me/texx/Texx/util/ThemeUtil.kt b/app/src/main/java/me/texx/Texx/util/ThemeUtil.kt
new file mode 100644
index 0000000..c8b00f1
--- /dev/null
+++ b/app/src/main/java/me/texx/Texx/util/ThemeUtil.kt
@@ -0,0 +1,34 @@
+package me.texx.Texx.util
+
+import android.content.Context
+import com.madapps.prefrences.EasyPrefrences
+import me.texx.Texx.util.ThemeUtil.isDarkTheme
+
+/**
+ * Get the name of the theme depending on [actionBar] and [isDarkTheme]
+ */
+object ThemeUtil {
+ /**
+ * Checks if the theme saved in sharedPreferences is dark/light
+ */
+ private fun isDarkTheme(context: Context): Boolean {
+ val sharedPrefs = EasyPrefrences(context)
+ val darkTheme: Boolean? = sharedPrefs.getBoolean("dark_theme_switch")
+ darkTheme?.let {
+ return darkTheme
+ } ?: run {
+ return false
+ }
+ }
+
+ /**
+ * Get the name of the theme depending on [actionBar] and [isDarkTheme]
+ */
+ fun getThemeName(context: Context): String {
+ return if (isDarkTheme(context)) {
+ "dark"
+ } else {
+ "light"
+ }
+ }
+} \ No newline at end of file