Skip to content

Commit

Permalink
更新旋转同步功能
Browse files Browse the repository at this point in the history
  • Loading branch information
mingzhixian committed Apr 28, 2023
1 parent 6846369 commit 1c28cf8
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ scrcpy_android/.idea/navEditor.xml
scrcpy_android/.idea/assetWizardSettings.xml
scrcpy_android/.DS_Store
scrcpy_android/build
scrcpy_android/app/release
scrcpy_android/captures
scrcpy_android/.externalNativeBuild
scrcpy_android/.cxx
Expand All @@ -25,6 +26,7 @@ scrcpy_back/.idea/navEditor.xml
scrcpy_back/.idea/assetWizardSettings.xml
scrcpy_back/.DS_Store
scrcpy_back/build
scrcpy_back/app/release
scrcpy_back/captures
scrcpy_back/.externalNativeBuild
scrcpy_back/.cxx
Expand Down
4 changes: 2 additions & 2 deletions scrcpy_android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "top.saymzx.scrcpy_android"
minSdk 23
targetSdk 33
versionCode 75
versionName "7.5"
versionCode 76
versionName "7.6"
resConfigs "zh"
ndk {
abiFilter "arm64-v8a"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package top.saymzx.scrcpy_android

import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.graphics.PixelFormat
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioTrack
Expand Down Expand Up @@ -81,7 +86,13 @@ class Configs : ViewModel() {
private lateinit var loudnessEnhancer: LoudnessEnhancer

// 目前屏幕方向
var nowOrientation=-1
var nowOrientation = -1

// 传感器管理
lateinit var sensorManager: SensorManager

// 重力传感器处理
val orientationListener = OrientationListener()

// context
@SuppressLint("StaticFieldLeak")
Expand Down Expand Up @@ -309,4 +320,61 @@ class Configs : ViewModel() {
audioTrack.play()
}

// 重力感应处理
inner class OrientationListener : SensorEventListener {
override fun onSensorChanged(event: SensorEvent?) {
if (event == null || Sensor.TYPE_ACCELEROMETER != event.sensor.type) {
return
}
val values = event.values
val x = values[0]
val y = values[1]

val newOrientation = if (x < 4 && x > -4 && y > 5) {
//纵向显示
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
} else if (x > 5 && y < 4 && y > -4) {
//横向显示
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
} else if (x < -5 && y < 4 && y > -4) {
//与正常的横向方向相反显示
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
} else if (x < 4 && x > -4 && y < -5) {
//与正常的纵向方向相反显示
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
} else {
return
}
// 发送旋转请求
if (status == 1 && nowOrientation != newOrientation) {
// 如果是横屏变相反横屏
if (remoteWidth > remoteHeight && (newOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE || newOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)) {
main.requestedOrientation = newOrientation
}
// 如果是竖屏变相反竖屏
else if (remoteWidth < remoteHeight && (newOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT || newOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)) {
main.requestedOrientation = newOrientation
}
// 如果是横竖屏切换
else {
nowOrientation = newOrientation
setRotation()
}
}
}

override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
}

}

// 被控端旋转
private fun setRotation() {
val byteBuffer = ByteBuffer.allocate(1)
byteBuffer.clear()
byteBuffer.put(11)
byteBuffer.flip()
controls.offer(byteBuffer.array())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.recyclerview.widget.RecyclerView
class DeviceAdapter(private val main: MainActivity) :
RecyclerView.Adapter<DeviceAdapter.ViewHolder>() {
private var dbHelper = DbHelper(main, "scrcpy_android.db", 2)
var devices = initData()
private var devices = initData()

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textViewName: TextView = view.findViewById(R.id.device_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import android.content.*
import android.content.Intent.*
import android.content.pm.ActivityInfo
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.media.*
import android.media.MediaCodec.BufferInfo
Expand Down Expand Up @@ -70,13 +68,7 @@ class MainActivity : AppCompatActivity() {
configs.isInit = true
configs.main = this
configs.init()
// 注册重力感应用以旋转屏幕
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(
OrientationListener(),
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL
)
configs.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
}
// 读取数据库并展示设备列表
setDevicesList()
Expand Down Expand Up @@ -200,6 +192,12 @@ class MainActivity : AppCompatActivity() {
windowManager.addView(configs.navView, configs.navLayoutParams)
// 设置常驻通知栏用以关闭
setNotification()
// 注册重力传感器
configs.sensorManager.registerListener(
configs.orientationListener,
configs.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL
)
}

// 发送server
Expand Down Expand Up @@ -531,6 +529,8 @@ class MainActivity : AppCompatActivity() {
while (configs.status != -6) Thread.sleep(10)
configs.adbStream.write(" ps -ef | grep scrcpy | grep -v grep | awk '{print $2}' | xargs kill -9 \n")
configs.adbStream.close()
// 关闭重力传感器监听
configs.sensorManager.unregisterListener(configs.orientationListener)
configs.status--
}
}
Expand All @@ -545,16 +545,6 @@ class MainActivity : AppCompatActivity() {
configs.controls.offer(byteBuffer.array())
}

// 被控端旋转
private fun setRotation(rotation: Int) {
val byteBuffer = ByteBuffer.allocate(2)
byteBuffer.clear()
byteBuffer.put(11)
byteBuffer.put(rotation.toByte())
byteBuffer.flip()
configs.controls.offer(byteBuffer.array())
}

// 判断是否旋转
private fun ifRotation(format: MediaFormat) {
configs.remoteWidth = format.getInteger("width")
Expand Down Expand Up @@ -587,12 +577,12 @@ class MainActivity : AppCompatActivity() {
windowManager.updateViewLayout(configs.navView, configs.navLayoutParams)
}
// 旋转方向
if (configs.remoteWidth > configs.remoteHeight && (configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE))
requestedOrientation = configs.nowOrientation
else if (configs.remoteWidth < configs.remoteHeight && (configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT))
requestedOrientation = configs.nowOrientation
else
requestedOrientation =
requestedOrientation =
if (configs.remoteWidth > configs.remoteHeight && (configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE))
configs.nowOrientation
else if (configs.remoteWidth < configs.remoteHeight && (configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || configs.nowOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT))
configs.nowOrientation
else
if (configs.remoteWidth > configs.remoteHeight) ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE else ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
Expand Down Expand Up @@ -649,38 +639,4 @@ class MainActivity : AppCompatActivity() {
}
}

// 重力感应处理
inner class OrientationListener : SensorEventListener {
override fun onSensorChanged(event: SensorEvent?) {
if (event == null || Sensor.TYPE_ACCELEROMETER != event.sensor.type) {
return
}
val values = event.values
val x = values[0]
val y = values[1]

val newOrientation = if (x < 4.5 && x >= -4.5 && y >= 4.5) {
//纵向显示
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
} else if (x >= 4.5 && y < 4.5 && y >= -4.5) {
//横向显示
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
} else if (x <= -4.5 && y < 4.5 && y >= -4.5) {
//与正常的横向方向相反显示
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
} else {
//与正常的纵向方向相反显示
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
}
// 发送旋转请求
if (configs.status == 1 && configs.nowOrientation != newOrientation) {
configs.nowOrientation = newOrientation
setRotation(if (newOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || newOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) 0 else 1)
}
}

override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ public static ControlMessage createSetScreenPowerMode(int mode) {
return msg;
}

public static ControlMessage createSetRotate(int rotate) {
ControlMessage msg = new ControlMessage();
msg.type = TYPE_ROTATE_DEVICE;
msg.action = rotate;
return msg;
}

public static ControlMessage createEmpty(int type) {
ControlMessage msg = new ControlMessage();
msg.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ControlMessage next() {
case ControlMessage.TYPE_EXPAND_SETTINGS_PANEL:
case ControlMessage.TYPE_COLLAPSE_PANELS:
case ControlMessage.TYPE_ROTATE_DEVICE:
msg = parseSetRotate();
msg = ControlMessage.createEmpty(type);
break;
default:
Ln.w("Unknown event type: " + type);
Expand Down Expand Up @@ -194,14 +194,6 @@ private ControlMessage parseSetScreenPowerMode() {
return ControlMessage.createSetScreenPowerMode(mode);
}

private ControlMessage parseSetRotate() {
if (buffer.remaining() < 1) {
return null;
}
int rotate = buffer.get();
return ControlMessage.createSetRotate(rotate);
}

private static Position readPosition(ByteBuffer buffer) {
int x = buffer.getInt();
int y = buffer.getInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void handleEvent() throws IOException {
}
break;
case ControlMessage.TYPE_ROTATE_DEVICE:
Device.rotateDevice(msg.getAction());
Device.rotateDevice();
break;
default:
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,18 @@ public static boolean powerOffScreen(int displayId) {
/**
* Disable auto-rotation (if enabled), set the screen rotation and re-enable auto-rotation (if it was enabled).
*/
public static void rotateDevice(int rorate) {
public static void rotateDevice() {
WindowManager wm = ServiceManager.getWindowManager();

boolean accelerometerRotation = !wm.isRotationFrozen();

int currentRotation = wm.getRotation();
// int newRotation = (currentRotation & 1) ^ 1; // 0->1, 1->0, 2->1, 3->0
// String newRotationString = newRotation == 0 ? "portrait" : "landscape";
//
// Ln.i("Device rotation requested: " + newRotationString);
// 防止上下旋转未识别
if (currentRotation == rorate) {
wm.freezeRotation(rorate ^ 1);
wm.freezeRotation(rorate);
} else {
wm.freezeRotation(rorate);
}
int newRotation = (currentRotation & 1) ^ 1; // 0->1, 1->0, 2->1, 3->0
String newRotationString = newRotation == 0 ? "portrait" : "landscape";

Ln.i("Device rotation requested: " + newRotationString);

wm.freezeRotation(newRotation);

// restore auto-rotate if necessary
if (accelerometerRotation) {
Expand Down

0 comments on commit 1c28cf8

Please sign in to comment.