diff options
-rw-r--r-- | app/build.gradle | 1 | ||||
-rw-r--r-- | app/src/main/AndroidManifest.xml | 3 | ||||
-rw-r--r-- | app/src/main/java/me/texx/Texx/CameraActivity.kt | 65 |
3 files changed, 57 insertions, 12 deletions
diff --git a/app/build.gradle b/app/build.gradle index 37d058f..27e2c87 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,6 +27,7 @@ dependencies { implementation 'com.otaliastudios:cameraview:1.5.1' implementation 'com.github.mukeshsolanki:easypreferences:1.0.6' implementation 'com.kazakago.cryptore:cryptore:1.3.0' + implementation 'com.google.android.gms:play-services-location:15.0.1' implementation 'com.github.kittinunf.fuel:fuel-android:1.6.0' implementation 'com.github.Daio-io:dresscode:v0.0.2' implementation 'com.github.rtugeek:colorseekbar:1.7.2' 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 } } + |