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