Bug 701374 - Show go or search icon in awesomescreen field, as appropriate [r=lucasr a=android-only]
authorMark Finkle <mfinkle@mozilla.com>
Tue, 13 Dec 2011 11:31:35 -0500
changeset 84141 aa0bb5bb8931209b2ec25463364e9e5df8a4eccf
parent 84140 355b40db0f9ac00b226b14a09706a13aa74f28a5
child 84142 15e314ab8dd5c0f5c879564af8def55bb448ebe6
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr, android-only
bugs701374
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 701374 - Show go or search icon in awesomescreen field, as appropriate [r=lucasr a=android-only]
mobile/android/base/AwesomeBar.java
mobile/android/base/GeckoAppShell.java
mobile/android/chrome/content/browser.js
other-licenses/android/APKOpen.cpp
widget/src/android/AndroidBridge.cpp
widget/src/android/AndroidBridge.h
widget/src/android/AndroidJNI.cpp
--- a/mobile/android/base/AwesomeBar.java
+++ b/mobile/android/base/AwesomeBar.java
@@ -79,28 +79,28 @@ public class AwesomeBar extends Activity
 
         Log.d(LOGTAG, "creating awesomebar");
 
         setContentView(R.layout.awesomebar_search);
 
         mAwesomeTabs = (AwesomeBarTabs) findViewById(R.id.awesomebar_tabs);
         mAwesomeTabs.setOnUrlOpenListener(new AwesomeBarTabs.OnUrlOpenListener() {
             public void onUrlOpen(String url) {
-                openUrlAndFinish(url);
+                submitAndFinish(url);
             }
 
             public void onSearch(String engine) {
                 openSearchAndFinish(mText.getText().toString(), engine);
             }
         });
 
         mGoButton = (ImageButton) findViewById(R.id.awesomebar_button);
         mGoButton.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
-                openUrlAndFinish(mText.getText().toString());
+                submitAndFinish(mText.getText().toString());
             }
         });
 
         mText = (AwesomeBarEditText) findViewById(R.id.awesomebar_text);
 
         Resources resources = getResources();
         
         int padding[] = { mText.getPaddingLeft(),
@@ -161,17 +161,17 @@ public class AwesomeBar extends Activity
         });
 
         mText.setOnKeyListener(new View.OnKeyListener() {
             public boolean onKey(View v, int keyCode, KeyEvent event) {
                 if (keyCode == KeyEvent.KEYCODE_ENTER) {
                     if (event.getAction() != KeyEvent.ACTION_DOWN)
                         return true;
 
-                    openUrlAndFinish(mText.getText().toString());
+                    submitAndFinish(mText.getText().toString());
                     return true;
                 } else {
                     return false;
                 }
             }
         });
 
         mText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@@ -204,32 +204,66 @@ public class AwesomeBar extends Activity
     }
 
     @Override
     public boolean onSearchRequested() {
         cancelAndFinish();
         return true;
     }
 
+    /*
+     * This method tries to guess if the given string could be a search query or URL
+     * Search examples:
+     *  foo
+     *  foo bar.com
+     *  foo http://bar.com
+     *
+     * URL examples
+     *  foo.com
+     *  foo.c
+     *  :foo
+     *  http://foo.com bar
+    */
+    private boolean isSearchUrl(String text) {
+        text = text.trim();
+        if (text.length() == 0)
+            return false;
+
+        int colon = text.indexOf(':');
+        int dot = text.indexOf('.');
+        int space = text.indexOf(' ');
+
+        // If a space is found before any dot or colon, we assume this is a search query
+        boolean spacedOut = space > -1 && (space < colon || space < dot);
+
+        return spacedOut || (dot == -1);
+    }
+
     private void updateGoButton(String text) {
         if (text.length() == 0) {
             mGoButton.setVisibility(View.GONE);
             return;
         }
 
         mGoButton.setVisibility(View.VISIBLE);
 
         int imageResource = R.drawable.ic_awesomebar_go;
-        if (!GeckoAppShell.canCreateFixupURI(text)) {
+        if (isSearchUrl(text))
             imageResource = R.drawable.ic_awesomebar_search;
-        }
 
         mGoButton.setImageResource(imageResource);
     }
 
+    private void submitAndFinish(String url) {
+        if (isSearchUrl(url))
+            openSearchAndFinish(url, "__default__");
+        else
+            openUrlAndFinish(url);
+    }
+
     private void cancelAndFinish() {
         setResult(Activity.RESULT_CANCELED);
         finish();
     }
 
     private void finishWithResult(Intent intent) {
         setResult(Activity.RESULT_OK, intent);
         finish();
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -125,17 +125,16 @@ public class GeckoAppShell
     public static native void onResume();
     public static native void onLowMemory();
     public static native void callObserver(String observerKey, String topic, String data);
     public static native void removeObserver(String observerKey);
     public static native void loadLibs(String apkName, boolean shouldExtract);
     public static native void onChangeNetworkLinkStatus(String status);
     public static native void reportJavaCrash(String stack);
     public static native void notifyUriVisited(String uri);
-    public static native boolean canCreateFixupURI(String text);
 
     public static native void processNextNativeEvent();
 
     public static native void notifyBatteryChange(double aLevel, boolean aCharging, double aRemainingTime);
 
     public static native void notifySmsReceived(String aSender, String aBody, long aTimestamp);
     public static native ByteBuffer allocateDirectBuffer(long size);
     public static native void freeDirectBuffer(ByteBuffer buf);
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -535,20 +535,27 @@ var BrowserApp = {
       }
     });
   },
 
   getSearchOrFixupURI: function(aData) {
     let args = JSON.parse(aData);
     let uri;
     if (args.engine) {
-      let engine = Services.search.getEngineByName(args.engine);
-      uri = engine.getSubmission(args.url).uri;
-    } else
+      let engine;
+      if (args.engine == "__default__")
+        engine = Services.search.currentEngine || Services.search.defaultEngine;
+      else
+        engine = Services.search.getEngineByName(args.engine);
+
+      if (engine)
+        uri = engine.getSubmission(args.url).uri;
+    } else {
       uri = URIFixup.createFixupURI(args.url, Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP);
+    }
     return uri ? uri.spec : args.url;
   },
 
   scrollToFocusedInput: function(aBrowser) {
     let doc = aBrowser.contentDocument;
     if (!doc)
       return;
     let focused = doc.activeElement;
--- a/other-licenses/android/APKOpen.cpp
+++ b/other-licenses/android/APKOpen.cpp
@@ -293,17 +293,16 @@ SHELL_WRAPPER0(onLowMemory)
 SHELL_WRAPPER3(callObserver, jstring, jstring, jstring)
 SHELL_WRAPPER1(removeObserver, jstring)
 SHELL_WRAPPER1(onChangeNetworkLinkStatus, jstring)
 SHELL_WRAPPER1(reportJavaCrash, jstring)
 SHELL_WRAPPER0(executeNextRunnable)
 SHELL_WRAPPER1(cameraCallbackBridge, jbyteArray)
 SHELL_WRAPPER1(notifyUriVisited, jstring)
 SHELL_WRAPPER3(notifyBatteryChange, jdouble, jboolean, jdouble);
-SHELL_WRAPPER1_WITH_RETURN(canCreateFixupURI, bool, jstring);
 SHELL_WRAPPER3(notifySmsReceived, jstring, jstring, jlong);
 
 static void * xul_handle = NULL;
 static time_t apk_mtime = 0;
 #ifdef DEBUG
 extern "C" int extractLibs = 1;
 #else
 extern "C" int extractLibs = 0;
@@ -699,17 +698,16 @@ loadLibs(const char *apkName)
   GETFUNC(callObserver);
   GETFUNC(removeObserver);
   GETFUNC(onChangeNetworkLinkStatus);
   GETFUNC(reportJavaCrash);
   GETFUNC(executeNextRunnable);
   GETFUNC(cameraCallbackBridge);
   GETFUNC(notifyUriVisited);
   GETFUNC(notifyBatteryChange);
-  GETFUNC(canCreateFixupURI);
   GETFUNC(notifySmsReceived);
 #undef GETFUNC
   sStartupTimeline = (uint64_t *)__wrap_dlsym(xul_handle, "_ZN7mozilla15StartupTimeline16sStartupTimelineE");
   gettimeofday(&t1, 0);
   struct rusage usage2;
   getrusage(RUSAGE_THREAD, &usage2);
   __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Loaded libs in %dms total, %dms user, %dms system, %d faults",
                       (t1.tv_sec - t0.tv_sec)*1000 + (t1.tv_usec - t0.tv_usec)/1000, 
--- a/widget/src/android/AndroidBridge.cpp
+++ b/widget/src/android/AndroidBridge.cpp
@@ -45,19 +45,16 @@
 #include "nsXPCOMStrings.h"
 
 #include "AndroidBridge.h"
 #include "nsAppShell.h"
 #include "nsOSHelperAppService.h"
 #include "nsWindow.h"
 #include "mozilla/Preferences.h"
 #include "nsThreadUtils.h"
-#include "nsIURIFixup.h"
-#include "nsCDefaultURIFixup.h"
-#include "nsComponentManagerUtils.h"
 
 #ifdef DEBUG
 #define ALOG_BRIDGE(args...) ALOG(args)
 #else
 #define ALOG_BRIDGE(args...)
 #endif
 
 #define IME_FULLSCREEN_PREF "widget.ime.android.landscape_fullscreen"
@@ -573,35 +570,16 @@ AndroidBridge::ClipboardHasText()
         static_cast<jstring>(mJNIEnv->
                              CallStaticObjectMethod(mGeckoAppShellClass,
                                                     jGetClipboardText));
     if (!jstrType)
         return false;
     return true;
 }
 
-bool
-AndroidBridge::CanCreateFixupURI(const nsACString& aURIText)
-{
-    ALOG_BRIDGE("AndroidBridge::CanCreateFixupURI");
-
-    if (!mURIFixup) {
-        mURIFixup = do_GetService(NS_URIFIXUP_CONTRACTID);
-        if (!mURIFixup) return false;
-    }
-
-    nsCOMPtr<nsIURI> targetURI;
-
-    mURIFixup->CreateFixupURI(aURIText,
-                              nsIURIFixup::FIXUP_FLAG_NONE,
-                              getter_AddRefs(targetURI));
-
-    return (targetURI != nsnull);
-}
-
 void
 AndroidBridge::EmptyClipboard()
 {
     ALOG_BRIDGE("AndroidBridge::EmptyClipboard");
     mJNIEnv->CallStaticVoidMethod(mGeckoAppShellClass, jSetClipboardText, nsnull);
 }
 
 void
--- a/widget/src/android/AndroidBridge.h
+++ b/widget/src/android/AndroidBridge.h
@@ -47,17 +47,16 @@
 #include "nsIRunnable.h"
 #include "nsIObserver.h"
 
 #include "AndroidJavaWrappers.h"
 
 #include "nsIMutableArray.h"
 #include "nsIMIMEInfo.h"
 #include "nsColor.h"
-#include "nsIURIFixup.h"
 
 #include "nsIAndroidBridge.h"
 
 // Some debug #defines
 // #define DEBUG_ANDROID_EVENTS
 // #define DEBUG_ANDROID_WIDGET
 
 class nsWindow;
@@ -179,18 +178,16 @@ public:
     bool GetClipboardText(nsAString& aText);
 
     void SetClipboardText(const nsAString& aText);
     
     void EmptyClipboard();
 
     bool ClipboardHasText();
 
-    bool CanCreateFixupURI(const nsACString& aURIText);
-
     void ShowAlertNotification(const nsAString& aImageUrl,
                                const nsAString& aAlertTitle,
                                const nsAString& aAlertText,
                                const nsAString& aAlertData,
                                nsIObserver *aAlertListener,
                                const nsAString& aAlertName);
 
     void AlertsProgressListener_OnProgress(const nsAString& aAlertName,
@@ -429,19 +426,16 @@ protected:
     // stuff we need for CallEglCreateWindowSurface
     jclass jEGLSurfaceImplClass;
     jclass jEGLContextImplClass;
     jclass jEGLConfigImplClass;
     jclass jEGLDisplayImplClass;
     jclass jEGLContextClass;
     jclass jEGL10Class;
 
-    // Needed for canCreateFixupURI()
-    nsCOMPtr<nsIURIFixup> mURIFixup;
-
     // calls we've dlopened from libjnigraphics.so
     int (* AndroidBitmap_getInfo)(JNIEnv *env, jobject bitmap, void *info);
     int (* AndroidBitmap_lockPixels)(JNIEnv *env, jobject bitmap, void **buffer);
     int (* AndroidBitmap_unlockPixels)(JNIEnv *env, jobject bitmap);
 
     void* (*ANativeWindow_fromSurface)(JNIEnv *env, jobject surface);
     void (*ANativeWindow_release)(void *window);
     int (*ANativeWindow_setBuffersGeometry)(void *window, int width, int height, int format);
--- a/widget/src/android/AndroidJNI.cpp
+++ b/widget/src/android/AndroidJNI.cpp
@@ -82,17 +82,16 @@ extern "C" {
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_onLowMemory(JNIEnv *, jclass);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_callObserver(JNIEnv *, jclass, jstring observerKey, jstring topic, jstring data);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_removeObserver(JNIEnv *jenv, jclass, jstring jObserverKey);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_onChangeNetworkLinkStatus(JNIEnv *, jclass, jstring status);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash(JNIEnv *, jclass, jstring stack);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_executeNextRunnable(JNIEnv *, jclass);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifyUriVisited(JNIEnv *, jclass, jstring uri);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifyBatteryChange(JNIEnv* jenv, jclass, jdouble, jboolean, jdouble);
-    NS_EXPORT bool JNICALL Java_org_mozilla_gecko_GeckoAppShell_canCreateFixupURI(JNIEnv* jenv, jclass, jstring text);
     NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_notifySmsReceived(JNIEnv* jenv, jclass, jstring, jstring, jlong);
 }
 
 
 /*
  * Incoming JNI methods
  */
 
@@ -241,28 +240,16 @@ Java_org_mozilla_gecko_GeckoAppShell_not
       bool   mCharging;
       double mRemainingTime;
     };
 
     nsCOMPtr<nsIRunnable> runnable = new NotifyBatteryChangeRunnable(aLevel, aCharging, aRemainingTime);
     NS_DispatchToMainThread(runnable);
 }
 
-NS_EXPORT bool JNICALL
-Java_org_mozilla_gecko_GeckoAppShell_canCreateFixupURI(JNIEnv* jenv, jclass, jstring text)
-{
-    __android_log_print(ANDROID_LOG_INFO, "GeckoJNI", "%s", __PRETTY_FUNCTION__);
-
-    const jchar *textChars = jenv->GetStringChars(text, NULL);
-    NS_ConvertUTF16toUTF8 uriString(textChars);
-    jenv->ReleaseStringChars(text, textChars);
-
-    return AndroidBridge::Bridge()->CanCreateFixupURI(uriString);
-}
-
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_notifySmsReceived(JNIEnv* jenv, jclass,
                                                        jstring aSender,
                                                        jstring aBody,
                                                        jlong aTimestamp)
 {
     class NotifySmsReceivedRunnable : public nsRunnable {
     public: