aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/src/main
diff options
context:
space:
mode:
authorMarvin Borner2018-09-03 20:02:45 +0200
committerMarvin Borner2018-09-03 20:02:45 +0200
commitaecf0b00f315847459f7ff100e050bacff604b47 (patch)
tree6de00fc00bf70a1e0ee991c63037959244498aae /app/src/main
parent8d524cbbed9f3f1ab98e3e2abc239d23b866980b (diff)
Added better permission requesting and managing :lock:
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/me/texx/Texx/BugReportActivity.kt1
-rw-r--r--app/src/main/java/me/texx/Texx/CameraActivity.kt35
-rw-r--r--app/src/main/java/me/texx/Texx/LoginActivity.kt46
-rw-r--r--app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt16
-rw-r--r--app/src/main/java/me/texx/Texx/RoutingActivity.kt1
-rw-r--r--app/src/main/java/me/texx/Texx/SecureStorage.kt71
-rw-r--r--app/src/main/java/me/texx/Texx/Util/PermissionUtil.kt62
7 files changed, 87 insertions, 145 deletions
diff --git a/app/src/main/java/me/texx/Texx/BugReportActivity.kt b/app/src/main/java/me/texx/Texx/BugReportActivity.kt
index c5249cd..d1c443e 100644
--- a/app/src/main/java/me/texx/Texx/BugReportActivity.kt
+++ b/app/src/main/java/me/texx/Texx/BugReportActivity.kt
@@ -11,6 +11,7 @@ import com.madapps.prefrences.EasyPrefrences
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import kotlinx.android.synthetic.main.activity_bug_report.*
+import me.texx.Texx.Util.SecureStorage
import me.texx.Texx.util.ThemeUtil
import org.jetbrains.anko.longToast
import org.json.JSONObject
diff --git a/app/src/main/java/me/texx/Texx/CameraActivity.kt b/app/src/main/java/me/texx/Texx/CameraActivity.kt
index 8072074..4098c9c 100644
--- a/app/src/main/java/me/texx/Texx/CameraActivity.kt
+++ b/app/src/main/java/me/texx/Texx/CameraActivity.kt
@@ -1,7 +1,8 @@
package me.texx.Texx
-import android.Manifest
-import android.content.pm.PackageManager
+import android.Manifest.permission.ACCESS_FINE_LOCATION
+import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
+import android.annotation.SuppressLint
import android.graphics.Color.RED
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
@@ -10,7 +11,6 @@ import android.location.Location
import android.os.AsyncTask
import android.os.Bundle
import android.os.Environment
-import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.view.Window
import android.view.WindowManager
@@ -23,11 +23,11 @@ import com.otaliastudios.cameraview.SessionType
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import kotlinx.android.synthetic.main.activity_camera.*
+import me.texx.Texx.Util.PermissionUtil.askForPermission
+import me.texx.Texx.Util.PermissionUtil.permissionGranted
import me.texx.Texx.util.ThemeUtil.getThemeName
-import org.jetbrains.anko.alert
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.longToast
-import org.jetbrains.anko.startActivity
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
@@ -52,8 +52,6 @@ class CameraActivity : AppCompatActivity() {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_camera)
-
- initLocation()
initCamera()
}
@@ -73,12 +71,11 @@ class CameraActivity : AppCompatActivity() {
private fun setListeners() {
camera.addCameraListener(object : CameraListener() {
override fun onPictureTaken(jpeg: ByteArray?) {
- if (ContextCompat.checkSelfPermission(this@CameraActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
+ askForPermission(WRITE_EXTERNAL_STORAGE, this@CameraActivity)
+ if (permissionGranted(WRITE_EXTERNAL_STORAGE, this@CameraActivity)) {
val file: File? = createFile()
SaveFileTask(file).execute(jpeg)
startActivity(intentFor<PhotoEditorActivity>("filepath" to file.toString()))
- } else {
- triggerPermissionError("Storage")
}
}
})
@@ -152,8 +149,10 @@ class CameraActivity : AppCompatActivity() {
/**
* Initializes location service
*/
+ @SuppressLint("MissingPermission") // as this is handled but not recognized
private fun initLocation() {
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
+ askForPermission(ACCESS_FINE_LOCATION, this@CameraActivity)
+ if (permissionGranted(ACCESS_FINE_LOCATION, this@CameraActivity)) {
locationCLient = LocationServices.getFusedLocationProviderClient(this)
locationCLient.lastLocation
.addOnSuccessListener { location: Location? ->
@@ -165,20 +164,6 @@ class CameraActivity : AppCompatActivity() {
}
/**
- * Triggers permission error if permission wasn't granted
- */
- private fun triggerPermissionError(permission: String) {
- alert("You have to give this app the $permission permission to work properly.",
- "Error") {
- isCancelable = false
- positiveButton("Okay") {
- finishAffinity()
- startActivity<MainActivity>()
- }
- }.show()
- }
-
- /**
* Start components on activity resume (called at start)
*/
override fun onResume() {
diff --git a/app/src/main/java/me/texx/Texx/LoginActivity.kt b/app/src/main/java/me/texx/Texx/LoginActivity.kt
index d177196..ee3889f 100644
--- a/app/src/main/java/me/texx/Texx/LoginActivity.kt
+++ b/app/src/main/java/me/texx/Texx/LoginActivity.kt
@@ -1,20 +1,17 @@
package me.texx.Texx
-import android.Manifest.permission.READ_CONTACTS
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.TargetApi
import android.app.LoaderManager.LoaderCallbacks
import android.content.CursorLoader
import android.content.Loader
-import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import android.os.AsyncTask
import android.os.Build
import android.os.Bundle
import android.provider.ContactsContract
-import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
@@ -27,6 +24,8 @@ import com.madapps.prefrences.EasyPrefrences
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import kotlinx.android.synthetic.main.activity_login.*
+import me.texx.Texx.Util.PermissionUtil.askForPermission
+import me.texx.Texx.Util.SecureStorage
import me.texx.Texx.util.ThemeUtil.getThemeName
import org.jetbrains.anko.longToast
import org.jetbrains.anko.startActivity
@@ -64,42 +63,10 @@ class LoginActivity : AppCompatActivity(), LoaderCallbacks<Cursor> {
}
private fun populateAutoComplete() {
- if (!mayRequestContacts()) {
- return
- }
-
+ askForPermission(android.Manifest.permission.READ_CONTACTS, this@LoginActivity)
loaderManager.initLoader(0, null, this)
}
- private fun mayRequestContacts(): Boolean {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
- return true
- }
- if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
- return true
- }
- if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
- Snackbar.make(email, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
- .setAction(android.R.string.ok,
- { requestPermissions(arrayOf(READ_CONTACTS), REQUEST_READ_CONTACTS) })
- } else {
- requestPermissions(arrayOf(READ_CONTACTS), REQUEST_READ_CONTACTS)
- }
- return false
- }
-
- /**
- * Callback received when a permissions request has been completed.
- */
- override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
- grantResults: IntArray) {
- if (requestCode == REQUEST_READ_CONTACTS) {
- if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- populateAutoComplete()
- }
- }
- }
-
/**
* Attempts to sign in or register the account specified by the login form.
* If there are form errors (invalid email, missing fields, etc.), the
@@ -316,11 +283,4 @@ class LoginActivity : AppCompatActivity(), LoaderCallbacks<Cursor> {
showProgress(false)
}
}
-
- companion object {
- /**
- * Id to identity READ_CONTACTS permission request.
- */
- private val REQUEST_READ_CONTACTS = 0
- }
}
diff --git a/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt b/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt
index 7fff34a..18f0626 100644
--- a/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt
+++ b/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt
@@ -52,21 +52,25 @@ class PhotoEditorActivity : AppCompatActivity() {
undoButton.setOnClickListener { photoEditor.undo() }
redoButton.setOnClickListener { photoEditor.redo() }
- photoDrawButton.setOnClickListener {
+ drawButton.setOnClickListener {
currentlyDrawing = !currentlyDrawing
photoEditor.setBrushDrawingMode(currentlyDrawing)
- if (currentlyDrawing) drawColorSeekbar.visibility = View.VISIBLE
+ if (currentlyDrawing) colorSeekbar.visibility = View.VISIBLE
else {
- drawColorSeekbar.visibility = View.GONE
- photoDrawButton.background = ContextCompat.getDrawable(this, R.drawable.ic_mode_edit_white_24dp)
+ colorSeekbar.visibility = View.GONE
+ drawButton.background = ContextCompat.getDrawable(this, R.drawable.ic_mode_edit_white_24dp)
}
}
- drawColorSeekbar.setOnColorChangeListener { _, _, color ->
+ typeButton.setOnClickListener {
+ photoEditor.addText("", 123)
+ }
+
+ colorSeekbar.setOnColorChangeListener { _, _, color ->
if (currentlyDrawing) {
photoEditor.brushColor = color
- photoDrawButton.setBackgroundColor(color)
+ drawButton.setBackgroundColor(color)
}
}
}
diff --git a/app/src/main/java/me/texx/Texx/RoutingActivity.kt b/app/src/main/java/me/texx/Texx/RoutingActivity.kt
index 01e407e..805bf5f 100644
--- a/app/src/main/java/me/texx/Texx/RoutingActivity.kt
+++ b/app/src/main/java/me/texx/Texx/RoutingActivity.kt
@@ -9,6 +9,7 @@ import com.github.kittinunf.fuel.httpGet
import com.madapps.prefrences.EasyPrefrences
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
+import me.texx.Texx.Util.SecureStorage
import me.texx.Texx.util.ThemeUtil.getThemeName
import org.jetbrains.anko.*
import java.io.IOException
diff --git a/app/src/main/java/me/texx/Texx/SecureStorage.kt b/app/src/main/java/me/texx/Texx/SecureStorage.kt
deleted file mode 100644
index 780f418..0000000
--- a/app/src/main/java/me/texx/Texx/SecureStorage.kt
+++ /dev/null
@@ -1,71 +0,0 @@
-package me.texx.Texx
-
-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/PermissionUtil.kt b/app/src/main/java/me/texx/Texx/Util/PermissionUtil.kt
new file mode 100644
index 0000000..637d377
--- /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