Home | History | Annotate | Download | only in contacts
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.provider.ContactsContract;
     23 import android.provider.ContactsContract.Intents.UI;
     24 
     25 /**
     26  * A convenience class that helps launch contact search from within the app.
     27  */
     28 public class ContactsSearchManager {
     29 
     30     /**
     31      * An extra that provides context for search UI and defines the scope for
     32      * the search queries.
     33      */
     34     public static final String ORIGINAL_ACTION_EXTRA_KEY = "originalAction";
     35 
     36     /**
     37      * An extra that provides context for search UI and defines the scope for
     38      * the search queries.
     39      */
     40     public static final String ORIGINAL_COMPONENT_EXTRA_KEY = "originalComponent";
     41 
     42     /**
     43      * Starts the contact list activity in the search mode.
     44      */
     45     public static void startSearch(Activity context, String initialQuery) {
     46         context.startActivity(buildIntent(context, initialQuery));
     47     }
     48 
     49     public static void startSearchForResult(Activity context, String initialQuery,
     50             int requestCode) {
     51         context.startActivityForResult(buildIntent(context, initialQuery), requestCode);
     52     }
     53 
     54     private static Intent buildIntent(Activity context, String initialQuery) {
     55         Intent intent = new Intent();
     56         intent.setData(ContactsContract.Contacts.CONTENT_URI);
     57         intent.setAction(UI.FILTER_CONTACTS_ACTION);
     58 
     59         Intent originalIntent = context.getIntent();
     60         Bundle originalExtras = originalIntent.getExtras();
     61         if (originalExtras != null) {
     62             intent.putExtras(originalExtras);
     63         }
     64         intent.putExtra(UI.FILTER_TEXT_EXTRA_KEY, initialQuery);
     65         intent.putExtra(ORIGINAL_ACTION_EXTRA_KEY, originalIntent.getAction());
     66         intent.putExtra(ORIGINAL_COMPONENT_EXTRA_KEY, originalIntent.getComponent().getClassName());
     67         return intent;
     68     }
     69 }
     70