use the orientation event listener for checking device orientation

This commit is contained in:
tibbi 2017-04-07 21:16:37 +02:00
parent 3239d37bab
commit b45b4cf4a3
1 changed files with 29 additions and 31 deletions

View File

@ -3,12 +3,12 @@ package com.simplemobiletools.camera.activities
import android.Manifest
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources
import android.database.Cursor
import android.hardware.*
import android.hardware.Camera
import android.hardware.SensorManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
@ -29,13 +29,12 @@ import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, PhotoProcessor.MediaSavedListener {
class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSavedListener {
companion object {
private val CAMERA_STORAGE_PERMISSION = 1
private val RECORD_AUDIO_PERMISSION = 2
private val FADE_DELAY = 5000
lateinit var mSensorManager: SensorManager
lateinit var mFocusRectView: FocusRectView
lateinit var mTimerHandler: Handler
lateinit var mFadeHandler: Handler
@ -55,6 +54,8 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
var mLastHandledOrientation = 0
}
lateinit var mOrientationEventListener: OrientationEventListener
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
@ -64,6 +65,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
supportActionBar?.hide()
storeStoragePaths()
checkWhatsNewDialog()
setupOrientationEventListener()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
@ -134,7 +136,6 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
mFocusRectView = FocusRectView(applicationContext)
view_holder.addView(mFocusRectView)
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
mIsInPhotoMode = true
mTimerHandler = Handler()
mFadeHandler = Handler()
@ -449,6 +450,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
}
}
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
mOrientationEventListener.enable()
}
private fun resumeCameraItems() {
@ -457,9 +459,6 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
hideNavigationBarIcons()
checkFlash()
val accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL)
if (!mIsInPhotoMode) {
initVideoButtons()
}
@ -482,22 +481,22 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
hideTimer()
mPreview?.releaseCamera()
mSensorManager.unregisterListener(this)
mOrientationEventListener.disable()
}
override fun onSensorChanged(event: SensorEvent) {
val orientation = if (event.values[0] < 6.5 && event.values[0] > -6.5) {
ORIENT_PORTRAIT
} else {
if (event.values[0] > 0) {
private fun setupOrientationEventListener() {
mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
override fun onOrientationChanged(orientation: Int) {
val currOrient = if (orientation in 45..134) {
ORIENT_LANDSCAPE_RIGHT
} else if (orientation in 225..314) {
ORIENT_LANDSCAPE_LEFT
} else {
ORIENT_LANDSCAPE_RIGHT
}
ORIENT_PORTRAIT
}
if (orientation != mLastHandledOrientation) {
val degrees = when (orientation) {
if (currOrient != mLastHandledOrientation) {
val degrees = when (currOrient) {
ORIENT_LANDSCAPE_LEFT -> 90
ORIENT_LANDSCAPE_RIGHT -> -90
else -> 0
@ -505,7 +504,9 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
mPreview!!.deviceOrientationChanged()
animateViews(degrees)
mLastHandledOrientation = orientation
mLastHandledOrientation = currOrient
}
}
}
}
@ -518,9 +519,6 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
private fun rotate(view: View, degrees: Int) = view.animate().rotation(degrees.toFloat()).start()
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {
}
private fun checkCameraAvailable(): Boolean {
if (!mIsCameraAvailable) {
toast(R.string.camera_unavailable)