Google Labs 2.0 Gesture Search logo

Gesture Search Inside Applications Gesture Search can now be easily integrated into an application by developers, for users to search application-specific data using Gesture Search. For example, a mobile ordering application for a restaurant might have a list of hundreds of items for a customer to choose from. It will be difficult for a user to locate a desired item in the list. For another example, a modern mobile application that is rich in functions (e.g., a web browser) may have tens of menu options or settings for a user select that are often organized in a hierarchical way, e.g., top-levels and sub menus. It can be difficult and time consuming for a user to discover and find an feature. With Gesture Search, a developer can organize the data (or options) in a single list so that a user can find an item by simply drawing gestures.

In this example, we will demonstrate how we can embed Gesture Search (1.4.0 or later) into an application that allows a user to find information about a specific country by drawing gestures on the touchscreen. To use Gesture Search in the application, we first need to create a content provider named CountryProvider, according to the format required by Android Search framework, which consists of 238 country names. Then in GestureSearchAPIDemo, the main activity of the application, we invoke Gesture Search when a user selects a menu item (Gesture Search can be invoked in other ways depending on specific applications.) To do so, we create an Intent with the action "com.google.android.apps.gesturesearch.SEARCH" and the Uri of the content provider. If the data is protected (see AndroidManifest.xml for instance), we also need to grant the read permission for the content Uri to Gesture Search. We then call startActivityForResult to invoke Gesture Search.
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, GESTURE_SEARCH_ID, 0, R.string.menu_gesture_search)
        .setShortcut('0', 'g').setIcon(android.R.drawable.ic_menu_search);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case GESTURE_SEARCH_ID:
        try {
          Intent intent = new Intent();
          intent.setAction("com.google.android.apps.gesturesearch.SEARCH");
          intent.setData(SuggestionProvider.CONTENT_URI);
          intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
          intent.putExtra(SHOW_MODE, SHOW_ALL);
          intent.putExtra(THEME, THEME_LIGHT);
          startActivityForResult(intent, GESTURE_SEARCH_ID);
        } catch (ActivityNotFoundException e) {
          Log.e("GestureSearchExample", "Gesture Search is not installed");
        }
        break;
    }
    return super.onOptionsItemSelected(item);
  }
In the above code snippet, we also specify that we want to show all of the country names when Gesture Search is brought up by intent.putExtra(SHOW_MODE, SHOW_ALL). The parameter name and its possible values are defined as:
  /** 
   * Optionally, specify what should be shown when launching Gesture Search.
   * If this is not specified, SHOW_HISTORY will be used as a default value.
   */
  private static String SHOW_MODE = "show";
  /** Possible values for invoking mode */
  // Show the visited items
  private static final int SHOW_HISTORY = 0;
  // Show nothing (a blank screen)
  private static final int SHOW_NONE = 1;
  // Show all of date items
  private static final int SHOW_ALL = 2;

  /**
   * The theme of Gesture Search can be light or dark. 
   * By default, Gesture Search will use a dark theme.
   */
  private static final String THEME = "theme";
  private static final int THEME_LIGHT = 0;
  private static final int THEME_DARK = 1;

  /** Keys for results returned by Gesture Search */
  private static final String SELECTED_ITEM_ID = "selected_item_id";
  private static final String SELECTED_ITEM_NAME = "selected_item_name";
As we can see, alternatively, we can show the country name that a user recently clicked on or nothing. Gesture Search is then shown up with a list of all the country names. A user can draw gestures directly on top of the list and a target item will pop up at the top of the list (see Gesture Search for more user-facing interaction details). When a user clicks on a country name, Gesture Search exits and returns the result to the calling application, and the following method will be invoked for processing the user selection result. As shown in this code snippet, we can read the Id and the name of the data item that has been selected.
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
      switch (requestCode) {
        case GESTURE_SEARCH_ID:
          long selectedItemId = data.getLongExtra(SELECTED_ITEM_ID, -1);
          String selectedItemName = data.getStringExtra(SELECTED_ITEM_NAME);
          // Print out the Id and name of the item that is selected 
          // by the user in Gesture Search
          Log.d("GestureSearchExample", selectedItemId + ": " + selectedItemName);
          break;
      }   
    }
  }
Since the API relies on Gesture Search, you need to make sure users have already installed Gesture Search on their Android devices. If Gesture Search is not installed, you should catch the ActivityNotFoundException as shown in the above code snippet, and inform the user to install Gesture Search, e.g., using a MessageBox. Download the sample code at http://code.google.com/p/gesture-search-api-demo.



©2010 Google - Home - About Google - Privacy policy - Terms of Service