to top
BasicContactables / src / com.example.android.basiccontactables /

MainActivity.java

1
/*
2
 * Copyright (C) 2012 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
package com.example.android.basiccontactables;
17
 
18
import android.app.Activity;
19
import android.app.SearchManager;
20
import android.content.Context;
21
import android.content.Intent;
22
import android.os.Bundle;
23
import android.view.Menu;
24
import android.widget.SearchView;
25
 
26
/**
27
 * Simple one-activity app that takes a search term via the Action Bar
28
 * and uses it as a query to search the contacts database via the Contactables
29
 * table.
30
 */
31
public class MainActivity extends Activity {
32
 
33
    public static final int CONTACT_QUERY_LOADER = 0;
34
    public static final String QUERY_KEY = "query";
35
 
36
 
37
    @Override
38
    protected void onCreate(Bundle savedInstanceState) {
39
        super.onCreate(savedInstanceState);
40
        setContentView(R.layout.sample_main);
41
 
42
        if (getIntent() != null) {
43
            handleIntent(getIntent());
44
        }
45
    }
46
 
47
    @Override
48
    protected void onNewIntent(Intent intent) {
49
        handleIntent(intent);
50
    }
51
 
52
    /**
53
     * Assuming this activity was started with a new intent, process the incoming information and
54
     * react accordingly.
55
     * @param intent
56
     */
57
    private void handleIntent(Intent intent) {
58
        // Special processing of the incoming intent only occurs if the if the action specified
59
        // by the intent is ACTION_SEARCH.
60
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
61
            // SearchManager.QUERY is the key that a SearchManager will use to send a query string
62
            // to an Activity.
63
            String query = intent.getStringExtra(SearchManager.QUERY);
64
 
65
            // We need to create a bundle containing the query string to send along to the
66
            // LoaderManager, which will be handling querying the database and returning results.
67
            Bundle bundle = new Bundle();
68
            bundle.putString(QUERY_KEY, query);
69
 
70
            ContactablesLoaderCallbacks loaderCallbacks = new ContactablesLoaderCallbacks(this);
71
 
72
            // Start the loader with the new query, and an object that will handle all callbacks.
73
            getLoaderManager().restartLoader(CONTACT_QUERY_LOADER, bundle, loaderCallbacks);
74
        }
75
    }
76
 
77
    @Override
78
    public boolean onCreateOptionsMenu(Menu menu) {
79
        // Inflate the menu; this adds items to the action bar if it is present.
80
        getMenuInflater().inflate(R.menu.main, menu);
81
 
82
        // Associate searchable configuration with the SearchView
83
        SearchManager searchManager =
84
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
85
        SearchView searchView =
86
                (SearchView) menu.findItem(R.id.search).getActionView();
87
        searchView.setSearchableInfo(
88
                searchManager.getSearchableInfo(getComponentName()));
89
 
90
        return true;
91
    }
92
}