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
17package com.android.contacts;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.os.Bundle;
22import android.provider.ContactsContract;
23import android.provider.ContactsContract.Intents.UI;
24
25import com.android.contacts.list.ContactsRequest;
26
27/**
28 * A convenience class that helps launch contact search from within the app.
29 */
30public class ContactsSearchManager {
31
32    /**
33     * An extra that provides context for search UI and defines the scope for
34     * the search queries.
35     */
36    public static final String ORIGINAL_REQUEST_KEY = "originalRequest";
37
38    /**
39     * Starts the contact list activity in the search mode.
40     */
41    public static void startSearch(Activity context, String initialQuery) {
42        context.startActivity(buildIntent(context, initialQuery, null));
43    }
44
45    public static void startSearchForResult(Activity context, String initialQuery,
46            int requestCode, ContactsRequest originalRequest) {
47        context.startActivityForResult(
48                buildIntent(context, initialQuery, originalRequest), requestCode);
49    }
50
51    public static void startSearch(Activity context, String initialQuery,
52            ContactsRequest originalRequest) {
53        context.startActivity(buildIntent(context, initialQuery, originalRequest));
54    }
55
56    private static Intent buildIntent(
57            Activity context, String initialQuery, ContactsRequest originalRequest) {
58        Intent intent = new Intent();
59        intent.setData(ContactsContract.Contacts.CONTENT_URI);
60        intent.setAction(UI.FILTER_CONTACTS_ACTION);
61
62        Intent originalIntent = context.getIntent();
63        Bundle originalExtras = originalIntent.getExtras();
64        if (originalExtras != null) {
65            intent.putExtras(originalExtras);
66        }
67        intent.putExtra(UI.FILTER_TEXT_EXTRA_KEY, initialQuery);
68        if (originalRequest != null) {
69            intent.putExtra(ORIGINAL_REQUEST_KEY, originalRequest);
70        }
71        return intent;
72    }
73}
74