Skip to content
This repository was archived by the owner on Jun 12, 2025. It is now read-only.

Commit 22a3942

Browse files
committed
Lock screen orientation when sending data.
Fixes issue #20.
1 parent c42687a commit 22a3942

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

app/src/main/java/org/sensors2/osc/activities/StartUpActivity.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import android.app.PendingIntent;
66
import android.content.Intent;
77
import android.content.SharedPreferences;
8+
import android.content.pm.ActivityInfo;
9+
import android.graphics.Point;
10+
import android.graphics.drawable.GradientDrawable;
811
import android.hardware.Sensor;
912
import android.hardware.SensorEvent;
1013
import android.hardware.SensorManager;
@@ -22,9 +25,11 @@
2225
import android.support.v4.app.FragmentActivity;
2326
import android.support.v4.app.FragmentManager;
2427
import android.support.v4.app.FragmentTransaction;
28+
import android.view.Display;
2529
import android.view.Menu;
2630
import android.view.MenuItem;
2731
import android.view.MotionEvent;
32+
import android.view.Surface;
2833
import android.view.View;
2934
import android.view.WindowManager;
3035
import android.widget.CompoundButton;
@@ -391,9 +396,11 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
391396
this.wakeLock.acquire();
392397
}
393398
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
399+
this.setRequestedOrientation(this.getCurrentOrientation());
394400
} else {
395401
this.wakeLock.release();
396402
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
403+
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
397404
}
398405
active = isChecked;
399406
}
@@ -422,4 +429,50 @@ public boolean onTouch(View v, MotionEvent event) {
422429

423430
return false;
424431
}
432+
433+
public int getCurrentOrientation() {
434+
435+
final Display display = this.getWindowManager().getDefaultDisplay();
436+
final int width, height;
437+
if (Build.VERSION.SDK_INT >= 13) {
438+
Point size = new Point();
439+
display.getSize(size);
440+
width = size.x;
441+
height = size.y;
442+
}
443+
else {
444+
width = display.getWidth();
445+
height = display.getHeight();
446+
}
447+
switch(display.getRotation()){
448+
case Surface.ROTATION_90:
449+
if (width > height) {
450+
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
451+
}
452+
else {
453+
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
454+
}
455+
case Surface.ROTATION_180:
456+
if (height > width) {
457+
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
458+
}
459+
else {
460+
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
461+
}
462+
case Surface.ROTATION_270:
463+
if (width > height) {
464+
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
465+
}
466+
else {
467+
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
468+
}
469+
default:
470+
if (height > width) {
471+
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
472+
}
473+
else {
474+
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
475+
}
476+
}
477+
}
425478
}

0 commit comments

Comments
 (0)