aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/src/main
diff options
context:
space:
mode:
authorMarvin Borner2018-09-02 17:04:51 +0200
committerMarvin Borner2018-09-02 17:04:51 +0200
commit54f8d30ef2e689bf8d923e85ada7b8aa110ebd52 (patch)
tree2bccf6393519f50548b971ea781337632b6d036b /app/src/main
parent2b38ecee603a661cf2c56f99bcc3197717bcfc4c (diff)
Added location tracking in camera and fixed permissions
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/AndroidManifest.xml3
-rw-r--r--app/src/main/java/me/texx/Texx/CameraActivity.kt65
2 files changed, 56 insertions, 12 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index f2882db..87001df 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -11,7 +11,8 @@
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".Application"
diff --git a/app/src/main/java/me/texx/Texx/CameraActivity.kt b/app/src/main/java/me/texx/Texx/CameraActivity.kt
index 8964a8d..8072074 100644
--- a/app/src/main/java/me/texx/Texx/CameraActivity.kt
+++ b/app/src/main/java/me/texx/Texx/CameraActivity.kt
@@ -1,15 +1,21 @@
package me.texx.Texx
+import android.Manifest
+import android.content.pm.PackageManager
import android.graphics.Color.RED
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.Drawable
+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
+import com.google.android.gms.location.FusedLocationProviderClient
+import com.google.android.gms.location.LocationServices
import com.otaliastudios.cameraview.CameraListener
import com.otaliastudios.cameraview.Gesture
import com.otaliastudios.cameraview.GestureAction
@@ -18,8 +24,10 @@ import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import kotlinx.android.synthetic.main.activity_camera.*
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
@@ -31,6 +39,8 @@ import java.util.*
* Output will be saved and redirected to corresponding preview activity
*/
class CameraActivity : AppCompatActivity() {
+ private lateinit var locationCLient: FusedLocationProviderClient
+
/**
* Set initial configuration
*/
@@ -43,11 +53,12 @@ class CameraActivity : AppCompatActivity() {
WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_camera)
- initCameraLayout()
+ initLocation()
+ initCamera()
}
- private fun initCameraLayout() {
- camera.sessionType = SessionType.PICTURE
+ private fun initCamera() {
+ initLocation()
setGestures()
setListeners()
}
@@ -59,26 +70,28 @@ class CameraActivity : AppCompatActivity() {
camera.mapGesture(Gesture.SCROLL_HORIZONTAL, GestureAction.EXPOSURE_CORRECTION)
}
- private val takingPicture = camera.sessionType == SessionType.PICTURE
-
private fun setListeners() {
camera.addCameraListener(object : CameraListener() {
override fun onPictureTaken(jpeg: ByteArray?) {
- val file: File? = createFile()
- SaveFileTask(file).execute(jpeg)
- startActivity(intentFor<PhotoEditorActivity>("filepath" to file.toString()))
+ if (ContextCompat.checkSelfPermission(this@CameraActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
+ val file: File? = createFile()
+ SaveFileTask(file).execute(jpeg)
+ startActivity(intentFor<PhotoEditorActivity>("filepath" to file.toString()))
+ } else {
+ triggerPermissionError("Storage")
+ }
}
})
camera_button.setOnClickListener {
- if (takingPicture)
+ if (camera.sessionType == SessionType.PICTURE)
camera.capturePicture()
else
camera.startCapturingVideo()
}
camera_button.setOnLongClickListener {
- if (takingPicture) {
+ if (camera.sessionType == SessionType.PICTURE) {
camera.sessionType = SessionType.VIDEO
val videoButtonDrawable: Drawable = this.resources.getDrawable(R.drawable.focus_marker_outline)
videoButtonDrawable.colorFilter = PorterDuffColorFilter(RED, PorterDuff.Mode.SRC_IN)
@@ -110,7 +123,7 @@ class CameraActivity : AppCompatActivity() {
}
/**
- * Creates empty [File] to write the file on
+ * Creates empty [File] to write the file on if permission is granted
*/
private fun createFile(): File? {
val mediaStorageDir = File(Environment.getExternalStorageDirectory().toString()
@@ -137,6 +150,35 @@ class CameraActivity : AppCompatActivity() {
}
/**
+ * Initializes location service
+ */
+ private fun initLocation() {
+ if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
+ locationCLient = LocationServices.getFusedLocationProviderClient(this)
+ locationCLient.lastLocation
+ .addOnSuccessListener { location: Location? ->
+ location?.let {
+ camera.location = location
+ }
+ }
+ }
+ }
+
+ /**
+ * 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() {
@@ -160,3 +202,4 @@ class CameraActivity : AppCompatActivity() {
camera.destroy() // doesn't really destroys your camera lol
}
}
+