Bug 742267 - Restore AwesomeBar's cursor position when gaining input focus. r=blassey a=android-only
authorChris Peterson <cpeterson@mozilla.com>
Tue, 01 May 2012 14:56:08 -0700
changeset 95004 d3a3b430e9515a560c68243d9a1d0727ef2ccec0
parent 95003 28157498541077b2cf7ccff15a8135e3d750f282
child 95005 76299fb5d4994e17d532b41ac18c715fd254f14f
push idunknown
push userunknown
push dateunknown
reviewersblassey, android-only
bugs742267
milestone15.0a1
Bug 742267 - Restore AwesomeBar's cursor position when gaining input focus. r=blassey a=android-only
mobile/android/base/AwesomeBar.java
--- a/mobile/android/base/AwesomeBar.java
+++ b/mobile/android/base/AwesomeBar.java
@@ -35,28 +35,26 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko;
 
 import android.app.Activity;
-import android.app.ActionBar;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.res.Resources;
 import android.content.res.Configuration;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
-import android.os.Build;
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.Spanned;
 import android.text.TextUtils;
 import android.text.TextWatcher;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.ContextMenu;
@@ -68,17 +66,16 @@ import android.view.MenuItem;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 import android.view.inputmethod.EditorInfo;
 import android.widget.AdapterView;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ExpandableListView;
 import android.widget.ImageButton;
-import android.widget.RelativeLayout;
 import android.widget.ListView;
 import android.widget.TabWidget;
 import android.widget.Toast;
 
 import java.net.URLEncoder;
 import java.util.Map;
 
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
@@ -393,25 +390,31 @@ public class AwesomeBar extends GeckoAct
         } else {
             int selStart = -1;
             int selEnd = -1;
             if (mText.hasSelection()) {
                 selStart = mText.getSelectionStart();
                 selEnd = mText.getSelectionEnd();
             }
 
-            // Return focus to the edit box, and dispatch the event to it
-            mText.requestFocusFromTouch();
-
             if (selStart >= 0) {
                 // Restore the selection, which gets lost due to the focus switch
                 mText.setSelection(selStart, selEnd);
             }
 
+            // Manually dispatch the key event to the AwesomeBar before restoring (default) input
+            // focus. dispatchKeyEvent() will update AwesomeBar's cursor position.
             mText.dispatchKeyEvent(event);
+            int newCursorPos = mText.getSelectionEnd();
+
+            // requestFocusFromTouch() will select all AwesomeBar text, so we must restore cursor
+            // position so subsequent typing does not overwrite all text.
+            mText.requestFocusFromTouch();
+            mText.setSelection(newCursorPos);
+
             return true;
         }
     }
 
     @Override
     public void onResume() {
         super.onResume();
         if (mText != null && mText.getText() != null)