|
5 | 5 | import android.app.PendingIntent; |
6 | 6 | import android.content.Intent; |
7 | 7 | import android.content.SharedPreferences; |
| 8 | +import android.content.pm.ActivityInfo; |
| 9 | +import android.graphics.Point; |
| 10 | +import android.graphics.drawable.GradientDrawable; |
8 | 11 | import android.hardware.Sensor; |
9 | 12 | import android.hardware.SensorEvent; |
10 | 13 | import android.hardware.SensorManager; |
|
22 | 25 | import android.support.v4.app.FragmentActivity; |
23 | 26 | import android.support.v4.app.FragmentManager; |
24 | 27 | import android.support.v4.app.FragmentTransaction; |
| 28 | +import android.view.Display; |
25 | 29 | import android.view.Menu; |
26 | 30 | import android.view.MenuItem; |
27 | 31 | import android.view.MotionEvent; |
| 32 | +import android.view.Surface; |
28 | 33 | import android.view.View; |
29 | 34 | import android.view.WindowManager; |
30 | 35 | import android.widget.CompoundButton; |
@@ -391,9 +396,11 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { |
391 | 396 | this.wakeLock.acquire(); |
392 | 397 | } |
393 | 398 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 399 | + this.setRequestedOrientation(this.getCurrentOrientation()); |
394 | 400 | } else { |
395 | 401 | this.wakeLock.release(); |
396 | 402 | getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 403 | + this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
397 | 404 | } |
398 | 405 | active = isChecked; |
399 | 406 | } |
@@ -422,4 +429,50 @@ public boolean onTouch(View v, MotionEvent event) { |
422 | 429 |
|
423 | 430 | return false; |
424 | 431 | } |
| 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 | + } |
425 | 478 | } |
0 commit comments