Improve detection of window size.

This commit is contained in:
James Sanford 2012-10-03 15:32:11 -07:00
parent 3f106b9ce0
commit ee2814b689
2 changed files with 48 additions and 12 deletions

View File

@ -24,7 +24,12 @@ class BitmapSize {
}
public boolean showActionBar() {
if (mHeight < ((400 + 64) * mScaleFactorY)) {
if (mScaleFactorY != 1.0f && mScaleFactorX != mScaleFactorY) {
return false;
} else if (mHeight < getViewHeight()) {
// TODO: FIXME: This could possibly cause the action bar to turn on and off repeatedly, so be careful about when saying it's safe to turn on or off.
// One way would be if we have turned it off for a certain height,
// and a new height is within 20% of that height, keep it off.
return false;
} else {
return true;
@ -86,8 +91,7 @@ class BitmapSize {
// Force integer scaling on X axis.
scaleX = (float)Math.round((width * 0.9) / 640);
scaleX = Math.max(1, scaleX);
// TODO: Fix '48' hack being used for system buttons or soft buttons.
scaleY = Math.min(scaleX, (height - 48) / 400.0f);
scaleY = Math.min(scaleX, height / 400.0f);
// If Y would be compressed in a weird way, reduce the scale and use 1:1.
if ((scaleX - scaleY) > 0.5) {
@ -95,15 +99,16 @@ class BitmapSize {
scaleY = scaleX;
}
// TODO: Fix '32' and '64' for software buttons and window decorations.
if (height < ((400 + 32 + 64) * scaleY)) {
// If the 32 line border and the 400 pixel display do not fit, try
// cropping out the 32 line border.
if (height < 432 * scaleY) {
crop = true;
}
mCropped = crop;
mScaleFactorX = scaleX;
mScaleFactorY = scaleY;
Log.w("kegs", "using scale " + scaleX + ":" + scaleY + " " + crop + " from screen " + width + "x" + height);
Log.w("kegs", "using scale " + scaleX + ":" + scaleY + " crop=" + crop + " from screen " + width + "x" + height);
}
// call us when you update your screen size/configuration

View File

@ -9,6 +9,7 @@ import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.AsyncTask;
import android.util.Log;
@ -172,7 +173,9 @@ public class KegsMain extends Activity implements KegsKeyboard.StickyReset {
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage(getResources().getText(R.string.rom_check));
dialog.setProgressNumberFormat(null);
dialog.setProgressPercentFormat(null);
if (android.os.Build.VERSION.SDK_INT >= 11) {
dialog.setProgressPercentFormat(null);
}
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false); // lame
@ -191,9 +194,24 @@ public class KegsMain extends Activity implements KegsKeyboard.StickyReset {
}
}
private void setScreenSize() {
final int width = getResources().getDisplayMetrics().widthPixels;
final int height = getResources().getDisplayMetrics().heightPixels;
private void setScreenSize(boolean quick) {
int width;
int height;
if (quick) {
width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;
if (android.os.Build.VERSION.SDK_INT >= 11) {
// NOTE: 48 is a guess at the System Bar obstruction.
// These are 'visible insets' into the display from the window manager.
height -= 48;
}
} else {
Rect displaySize = new Rect();
mKegsView.getWindowVisibleDisplayFrame(displaySize);
width = displaySize.width();
height = displaySize.height();
}
final BitmapSize bitmapSize = new BitmapSize(width, height);
mKegsView.updateScreenSize(bitmapSize);
@ -213,10 +231,23 @@ public class KegsMain extends Activity implements KegsKeyboard.StickyReset {
mKegsView.getThread().updateScreen();
}
private void workaroundScreenSize() {
// First use displayMetrics.
setScreenSize(true);
// Then update with getWindowVisibleDisplayFrame in 250ms.
//
// We want to use getWindowVisibleDisplayFrame, but it's not
// always immediately available. Bug workaround.
mKegsView.postDelayed(new Runnable() {
public void run() { setScreenSize(false); }
}, 250);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setScreenSize();
workaroundScreenSize();
}
@Override
@ -238,7 +269,7 @@ public class KegsMain extends Activity implements KegsKeyboard.StickyReset {
mKegsView = (KegsView)findViewById(R.id.kegsview);
setScreenSize(); // TODO This causes an unnecessary requestLayout of KegsView.
workaroundScreenSize();
mKegsTouch = new KegsTouch(mKegsView.getEventQueue());
final GestureDetector inputDetect = new GestureDetector(this, mKegsTouch);