• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythonwindowsphpgamebathyscapheguic翻訳omegattwitterframeworkbtronvb.net計画中(planning stage)testdomarduinodirectxpreviewerゲームエンジン

packages/apps/AndroidTerm


Commit MetaInfo

Revisionc5b4ec402fb96f1df1e83e22cef16785035aae90 (tree)
Zeit2012-03-26 10:21:48
AutorSteven Luo <steven+android@stev...>
CommiterJack Palevich

Log Message

Revise screen resize check handling

Move the screen resize check code out of EmulatorView and into
TermViewFlipper, where it logically fits now that there are multiple
EmulatorViews per Term activity. While we're at it, instead of making
the activity tell us how much of the window doesn't belong to us, use
getGlobalVisibleRect() to figure that out ourselves, eliminating the
need for a WindowSizeCallback.

Signed-off-by: Jack Palevich <jackpal@google.com>

Ändern Zusammenfassung

Diff

--- a/src/jackpal/androidterm/EmulatorView.java
+++ b/src/jackpal/androidterm/EmulatorView.java
@@ -39,7 +39,6 @@ import android.view.KeyCharacterMap;
3939 import android.view.KeyEvent;
4040 import android.view.MotionEvent;
4141 import android.view.View;
42-import android.view.ViewGroup.LayoutParams;
4342 import android.view.inputmethod.CompletionInfo;
4443 import android.view.inputmethod.CorrectionInfo;
4544 import android.view.inputmethod.EditorInfo;
@@ -75,7 +74,6 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
7574
7675 private int mVisibleWidth;
7776 private int mVisibleHeight;
78- private Rect mVisibleRect = new Rect();
7977
8078 private TermSession mTermSession;
8179
@@ -163,8 +161,6 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
163161
164162 private static final int CURSOR_BLINK_PERIOD = 1000;
165163
166- private static final int SCREEN_CHECK_PERIOD = 1000;
167-
168164 private boolean mCursorVisible = true;
169165
170166 private boolean mIsSelectingText = false;
@@ -186,22 +182,6 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
186182
187183 private boolean mIsActive = false;
188184
189- /**
190- * True if we must poll to discover if the view has changed size.
191- * This is the only known way to detect the view changing size due to
192- * the IME being shown or hidden in API level <= 7.
193- */
194- private boolean mbPollForWindowSizeChange = AndroidCompat.SDK <= 7;
195-
196- private Runnable mCheckSize = mbPollForWindowSizeChange
197- ? new Runnable() {
198- public void run() {
199- updateSize(false);
200- mHandler.postDelayed(this, SCREEN_CHECK_PERIOD);
201- }
202- }
203- : null;
204-
205185 private Runnable mBlinkCursor = new Runnable() {
206186 public void run() {
207187 if (mCursorBlink != 0) {
@@ -215,21 +195,13 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
215195 }
216196 };
217197
218- private boolean mRedoLayout = false;
219-
220198 private GestureDetector mGestureDetector;
221199 private GestureDetector.OnGestureListener mExtGestureListener;
222200 private float mScrollRemainder;
223201 private TermKeyListener mKeyListener;
224- private WindowSizeCallback mSizeCallback;
225202
226203 private String mImeBuffer = "";
227204
228- // Used by activity to inform us how much of the window belongs to us
229- public interface WindowSizeCallback {
230- public abstract void onGetSize(Rect rect);
231- }
232-
233205 /**
234206 * Our message handler class. Implements a periodic callback.
235207 */
@@ -266,18 +238,12 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
266238 public void onResume() {
267239 mIsActive = true;
268240 updateSize(false);
269- if (mbPollForWindowSizeChange) {
270- mHandler.postDelayed(mCheckSize, SCREEN_CHECK_PERIOD);
271- }
272241 if (mCursorBlink != 0) {
273242 mHandler.postDelayed(mBlinkCursor, CURSOR_BLINK_PERIOD);
274243 }
275244 }
276245
277246 public void onPause() {
278- if (mbPollForWindowSizeChange) {
279- mHandler.removeCallbacks(mCheckSize);
280- }
281247 if (mCursorBlink != 0) {
282248 mHandler.removeCallbacks(mBlinkCursor);
283249 }
@@ -628,10 +594,6 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
628594 mKeyListener = new TermKeyListener(session);
629595 }
630596
631- public void setWindowSizeCallback(WindowSizeCallback callback) {
632- mSizeCallback = callback;
633- }
634-
635597 public void setExtGestureListener(GestureDetector.OnGestureListener listener) {
636598 mExtGestureListener = listener;
637599 }
@@ -988,53 +950,20 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
988950
989951 public void updateSize(boolean force) {
990952 if (mKnownSize) {
991- getWindowVisibleDisplayFrame(mVisibleRect);
992- /* Work around bug in getWindowVisibleDisplayFrame, and avoid
993- distracting visual glitch otherwise */
994- if (!mSettings.showStatusBar()) {
995- mVisibleRect.top = 0;
996- }
997- if (mSizeCallback != null) {
998- // Let activity adjust our size
999- mSizeCallback.onGetSize(mVisibleRect);
1000- }
1001- int w = mVisibleRect.width();
1002- int h = mVisibleRect.height();
953+ int w = getWidth();
954+ int h = getHeight();
1003955 // Log.w("Term", "(" + w + ", " + h + ")");
1004956 if (force || w != mVisibleWidth || h != mVisibleHeight) {
1005957 mVisibleWidth = w;
1006958 mVisibleHeight = h;
1007-
1008- LayoutParams params = getLayoutParams();
1009- params.width = w;
1010- params.height = h;
1011- setLayoutParams(params);
1012- mRedoLayout = true;
1013-
1014959 updateSize(mVisibleWidth, mVisibleHeight);
1015960 }
1016961 }
1017962 }
1018963
1019- /**
1020- * Called when the view changes size.
1021- * (Note: Not always called on Android 1.5 or Android 1.6)
1022- */
1023- @Override
1024- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1025- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
1026- if (mIsActive) {
1027- updateSize(false);
1028- }
1029- }
1030-
1031964 @Override
1032965 protected void onDraw(Canvas canvas) {
1033966 updateSize(false);
1034- if (mRedoLayout) {
1035- requestLayout();
1036- mRedoLayout = false;
1037- }
1038967
1039968 if (mEmulator == null) {
1040969 // Not ready yet
--- a/src/jackpal/androidterm/Term.java
+++ b/src/jackpal/androidterm/Term.java
@@ -203,14 +203,6 @@ public class Term extends Activity implements UpdateCallback {
203203 }
204204 }
205205
206- private EmulatorView.WindowSizeCallback mSizeCallback = new EmulatorView.WindowSizeCallback() {
207- public void onGetSize(Rect rect) {
208- if (mActionBarMode == TermSettings.ACTION_BAR_MODE_ALWAYS_VISIBLE) {
209- // Fixed action bar takes space away from the EmulatorView
210- rect.top += mActionBar.getHeight();
211- }
212- }
213- };
214206 private View.OnKeyListener mBackKeyListener = new View.OnKeyListener() {
215207 public boolean onKey(View v, int keyCode, KeyEvent event) {
216208 if (keyCode == KeyEvent.KEYCODE_BACK && mActionBarMode == TermSettings.ACTION_BAR_MODE_HIDES && mActionBar.isShowing()) {
@@ -349,15 +341,7 @@ public class Term extends Activity implements UpdateCallback {
349341 getWindowManager().getDefaultDisplay().getMetrics(metrics);
350342 EmulatorView emulatorView = new EmulatorView(this, session, metrics);
351343
352- FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
353- FrameLayout.LayoutParams.WRAP_CONTENT,
354- FrameLayout.LayoutParams.WRAP_CONTENT,
355- Gravity.LEFT
356- );
357- emulatorView.setLayoutParams(params);
358-
359344 emulatorView.setExtGestureListener(new EmulatorViewGestureListener(emulatorView));
360- emulatorView.setWindowSizeCallback(mSizeCallback);
361345 emulatorView.setOnKeyListener(mBackKeyListener);
362346 registerForContextMenu(emulatorView);
363347
@@ -435,16 +419,15 @@ public class Term extends Activity implements UpdateCallback {
435419 if (onResumeSelectWindow >= 0) {
436420 mViewFlipper.setDisplayedChild(onResumeSelectWindow);
437421 onResumeSelectWindow = -1;
438- } else {
439- mViewFlipper.resumeCurrentView();
440422 }
423+ mViewFlipper.onResume();
441424 }
442425
443426 @Override
444427 public void onPause() {
445428 super.onPause();
446429
447- mViewFlipper.pauseCurrentView();
430+ mViewFlipper.onPause();
448431 if (mTermSessions != null) {
449432 mTermSessions.removeCallback(this);
450433 if (mWinListAdapter != null) {
@@ -486,7 +469,7 @@ public class Term extends Activity implements UpdateCallback {
486469
487470 EmulatorView v = (EmulatorView) mViewFlipper.getCurrentView();
488471 if (v != null) {
489- v.updateSize(true);
472+ v.updateSize(false);
490473 }
491474
492475 if (mWinListAdapter != null) {
--- a/src/jackpal/androidterm/TermViewFlipper.java
+++ b/src/jackpal/androidterm/TermViewFlipper.java
@@ -20,12 +20,16 @@ import java.util.Iterator;
2020 import java.util.LinkedList;
2121
2222 import android.content.Context;
23+import android.graphics.Canvas;
24+import android.graphics.Rect;
25+import android.os.Handler;
2326 import android.util.AttributeSet;
2427 import android.view.Gravity;
2528 import android.view.View;
2629 import android.widget.Toast;
2730 import android.widget.ViewFlipper;
2831
32+import jackpal.androidterm.compat.AndroidCompat;
2933 import jackpal.androidterm.model.UpdateCallback;
3034 import jackpal.androidterm.util.TermSettings;
3135
@@ -33,6 +37,29 @@ public class TermViewFlipper extends ViewFlipper implements Iterable<View> {
3337 private Context context;
3438 private Toast mToast;
3539 private LinkedList<UpdateCallback> callbacks;
40+ private boolean mStatusBarVisible = false;
41+
42+ private int mCurWidth;
43+ private int mCurHeight;
44+ private Rect mVisibleRect = new Rect();
45+ private Rect mWindowRect = new Rect();
46+ private LayoutParams mChildParams = null;
47+ private boolean mRedoLayout = false;
48+
49+ /**
50+ * True if we must poll to discover if the view has changed size.
51+ * This is the only known way to detect the view changing size due to
52+ * the IME being shown or hidden in API level <= 7.
53+ */
54+ private boolean mbPollForWindowSizeChange = false;
55+ private static final int SCREEN_CHECK_PERIOD = 1000;
56+ private final Handler mHandler = new Handler();
57+ private Runnable mCheckSize = new Runnable() {
58+ public void run() {
59+ adjustChildSize();
60+ mHandler.postDelayed(this, SCREEN_CHECK_PERIOD);
61+ }
62+ };
3663
3764 class ViewFlipperIterator implements Iterator<View> {
3865 int pos = 0;
@@ -52,19 +79,32 @@ public class TermViewFlipper extends ViewFlipper implements Iterable<View> {
5279
5380 public TermViewFlipper(Context context) {
5481 super(context);
55- this.context = context;
56- callbacks = new LinkedList<UpdateCallback>();
82+ commonConstructor(context);
5783 }
5884
5985 public TermViewFlipper(Context context, AttributeSet attrs) {
6086 super(context, attrs);
87+ commonConstructor(context);
88+ }
89+
90+ private void commonConstructor(Context context) {
6191 this.context = context;
6292 callbacks = new LinkedList<UpdateCallback>();
93+
94+ updateVisibleRect();
95+ Rect visible = mVisibleRect;
96+ mChildParams = new LayoutParams(visible.width(), visible.height(),
97+ Gravity.TOP|Gravity.LEFT);
6398 }
6499
65100 public void updatePrefs(TermSettings settings) {
101+ mStatusBarVisible = settings.showStatusBar();
66102 int[] colorScheme = settings.getColorScheme();
67103 setBackgroundColor(colorScheme[3]);
104+
105+ if (AndroidCompat.SDK < 8) {
106+ mbPollForWindowSizeChange = !mStatusBarVisible;
107+ }
68108 }
69109
70110 public Iterator<View> iterator() {
@@ -85,6 +125,20 @@ public class TermViewFlipper extends ViewFlipper implements Iterable<View> {
85125 }
86126 }
87127
128+ public void onPause() {
129+ if (mbPollForWindowSizeChange) {
130+ mHandler.removeCallbacks(mCheckSize);
131+ }
132+ pauseCurrentView();
133+ }
134+
135+ public void onResume() {
136+ if (mbPollForWindowSizeChange) {
137+ mCheckSize.run();
138+ }
139+ resumeCurrentView();
140+ }
141+
88142 public void pauseCurrentView() {
89143 EmulatorView view = (EmulatorView) getCurrentView();
90144 if (view == null) {
@@ -141,4 +195,95 @@ public class TermViewFlipper extends ViewFlipper implements Iterable<View> {
141195 resumeCurrentView();
142196 notifyChange();
143197 }
198+
199+ @Override
200+ public void addView(View v, int index) {
201+ super.addView(v, index, mChildParams);
202+ }
203+
204+ @Override
205+ public void addView(View v) {
206+ super.addView(v, mChildParams);
207+ }
208+
209+ private void updateVisibleRect() {
210+ Rect visible = mVisibleRect;
211+ Rect window = mWindowRect;
212+
213+ /* Get rectangle representing visible area of this view, as seen by
214+ the activity (takes other views in the layout into account, but
215+ not space used by the IME) */
216+ getGlobalVisibleRect(visible);
217+
218+ /* Get rectangle representing visible area of this window (takes
219+ IME into account, but not other views in the layout) */
220+ getWindowVisibleDisplayFrame(window);
221+ /* Work around bug in getWindowVisibleDisplayFrame on API < 10, and
222+ avoid a distracting height change as status bar hides otherwise */
223+ if (!mStatusBarVisible) {
224+ window.top = 0;
225+ }
226+
227+ // Clip visible rectangle's top to the visible portion of the window
228+ if (visible.width() == 0 && visible.height() == 0) {
229+ visible.left = window.left;
230+ visible.top = window.top;
231+ } else {
232+ if (visible.left < window.left) {
233+ visible.left = window.left;
234+ }
235+ if (visible.top < window.top) {
236+ visible.top = window.top;
237+ }
238+ }
239+ // Always set the bottom of the rectangle to the window bottom
240+ /* XXX This breaks with a split action bar, but if we don't do this,
241+ it's possible that the view won't resize correctly on IME hide */
242+ visible.right = window.right;
243+ visible.bottom = window.bottom;
244+ }
245+
246+ private void adjustChildSize() {
247+ updateVisibleRect();
248+ Rect visible = mVisibleRect;
249+ int width = visible.width();
250+ int height = visible.height();
251+
252+ if (mCurWidth != width || mCurHeight != height) {
253+ mCurWidth = width;
254+ mCurHeight = height;
255+
256+ LayoutParams params = mChildParams;
257+ params.width = width;
258+ params.height = height;
259+ for (View v : this) {
260+ updateViewLayout(v, params);
261+ }
262+ mRedoLayout = true;
263+
264+ EmulatorView currentView = (EmulatorView) getCurrentView();
265+ if (currentView != null) {
266+ currentView.updateSize(false);
267+ }
268+ }
269+ }
270+
271+ /**
272+ * Called when the view changes size.
273+ * (Note: Not always called on Android < 2.2)
274+ */
275+ @Override
276+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
277+ adjustChildSize();
278+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
279+ }
280+
281+ @Override
282+ protected void onDraw(Canvas canvas) {
283+ if (mRedoLayout) {
284+ requestLayout();
285+ mRedoLayout = false;
286+ }
287+ super.onDraw(canvas);
288+ }
144289 }