Skip to content

Commit

Permalink
Lock screen orientation when sending data.
Browse files Browse the repository at this point in the history
Fixes issue #20.
  • Loading branch information
residuum committed Mar 30, 2018
1 parent c42687a commit 22a3942
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions app/src/main/java/org/sensors2/osc/activities/StartUpActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.graphics.drawable.GradientDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
Expand All @@ -22,9 +25,11 @@
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
Expand Down Expand Up @@ -391,9 +396,11 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
this.wakeLock.acquire();
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this.setRequestedOrientation(this.getCurrentOrientation());
} else {
this.wakeLock.release();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
active = isChecked;
}
Expand Down Expand Up @@ -422,4 +429,50 @@ public boolean onTouch(View v, MotionEvent event) {

return false;
}

public int getCurrentOrientation() {

final Display display = this.getWindowManager().getDefaultDisplay();
final int width, height;
if (Build.VERSION.SDK_INT >= 13) {
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
else {
width = display.getWidth();
height = display.getHeight();
}
switch(display.getRotation()){
case Surface.ROTATION_90:
if (width > height) {
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
else {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
case Surface.ROTATION_180:
if (height > width) {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
else {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
case Surface.ROTATION_270:
if (width > height) {
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
else {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
default:
if (height > width) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
else {
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
}
}
}

0 comments on commit 22a3942

Please sign in to comment.