Skip to content

Commit

Permalink
replaced orientationListener with thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpkarras committed Sep 28, 2023
1 parent 7fb0623 commit 9638ecb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdk 30
targetSdk 31
versionCode 1
versionName "1.0.4.2-2"
versionName "1.0.4.2-3"
signingConfig signingConfigs.debug
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
33 changes: 18 additions & 15 deletions app/src/main/java/com/tpkarras/mirror2rearultra/Mirror.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.os.SystemClock;
import android.provider.Settings;
import android.view.IWindowManager;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.TextureView;
import android.view.Window;
Expand All @@ -44,7 +43,6 @@ public class Mirror extends Activity {
private static Binder sBinder;
private static Method serviceMethod;
private IBinder windowBinder;
private OrientationEventListener orientationEventListener;

public static void rearScreenSwitch(boolean z){
Parcel parcel = Parcel.obtain();
Expand Down Expand Up @@ -93,24 +91,30 @@ protected void onCreate(Bundle savedInstanceState){
} catch (IllegalAccessException e) {
e.printStackTrace();
}
orientationEventListener = new OrientationEventListener(getApplicationContext(), SensorManager.SENSOR_DELAY_NORMAL) {
Runnable rotationListener = new Runnable() {
@Override
public void onOrientationChanged(int i) {
public void run() {
try {
if(wm.getDefaultDisplayRotation() == 3 && screenRotation.get() != 3){
screenRotation.set(3);
} else if(wm.getDefaultDisplayRotation() == 2 && screenRotation.get() != 2) {
screenRotation.set(2);
} else if(wm.getDefaultDisplayRotation() == 1 && screenRotation.get() != 1) {
screenRotation.set(1);
} else if(wm.getDefaultDisplayRotation() == 0 && screenRotation.get() != 0) {
screenRotation.set(Surface.ROTATION_0);
while (mirrorSwitch.get() == 1) {
if (wm.getDefaultDisplayRotation() == 3 && screenRotation.get() != 3) {
screenRotation.set(3);
} else if (wm.getDefaultDisplayRotation() == 2 && screenRotation.get() != 2) {
screenRotation.set(2);
} else if (wm.getDefaultDisplayRotation() == 1 && screenRotation.get() != 1) {
screenRotation.set(1);
} else if (wm.getDefaultDisplayRotation() == 0 && screenRotation.get() != 0) {
screenRotation.set(Surface.ROTATION_0);
}
Thread.sleep(250);
}
} catch (RemoteException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};
Thread rotationThread = new Thread(rotationListener);
try {
subscreenSwitch = Settings.System.getInt(getContentResolver(), "subscreen_switch");
} catch (Settings.SettingNotFoundException e) {
Expand Down Expand Up @@ -212,7 +216,6 @@ public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture,
@Override
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
sensorManager.unregisterListener(sensorEventListener);
orientationEventListener.disable();
virtualDisplay.release();
if(subscreenSwitch == 1) {
Intent intent = new Intent();
Expand All @@ -230,7 +233,7 @@ public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {
};
textureView.setSurfaceTextureListener(surfaceTextureListener);
rearScreenSwitch(true);
orientationEventListener.enable();
rotationThread.start();
mirrorSwitch.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
@Override
public void onPropertyChanged(Observable sender, int propertyId) {
Expand Down

0 comments on commit 9638ecb

Please sign in to comment.