1 /*
2  * Copyright (C) 2009 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.example.android.businesscard;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.AsyncTask;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.widget.Button;
27 import android.widget.TextView;
28 
29 /**
30  * A simple activity that shows a "Pick Contact" button and two fields: contact's name
31  * and phone number.  The user taps on the Pick Contact button to bring up
32  * the contact chooser.  Once this activity receives the result from contact picker,
33  * it launches an asynchronous query (queries should always be asynchronous) to load
34  * contact's name and phone number. When the query completes, the activity displays
35  * the loaded data.
36  */
37 public class BusinessCardActivity extends Activity  {
38 
39     // Request code for the contact picker activity
40     private static final int PICK_CONTACT_REQUEST = 1;
41 
42     /**
43      * An SDK-specific instance of {@link ContactAccessor}.  The activity does not need
44      * to know what SDK it is running in: all idiosyncrasies of different SDKs are
45      * encapsulated in the implementations of the ContactAccessor class.
46      */
47     private final ContactAccessor mContactAccessor = ContactAccessor.getInstance();
48 
49     /**
50      * Called with the activity is first created.
51      */
52     @Override
onCreate(Bundle savedInstanceState)53     public void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55 
56         setContentView(R.layout.business_card);
57 
58         // Install a click handler on the Pick Contact button
59         Button pickContact = (Button)findViewById(R.id.pick_contact_button);
60         pickContact.setOnClickListener(new OnClickListener() {
61 
62             public void onClick(View v) {
63                 pickContact();
64             }
65         });
66     }
67 
68     /**
69      * Click handler for the Pick Contact button.  Invokes a contact picker activity.
70      * The specific intent used to bring up that activity differs between versions
71      * of the SDK, which is why we delegate the creation of the intent to ContactAccessor.
72      */
pickContact()73     protected void pickContact() {
74         startActivityForResult(mContactAccessor.getPickContactIntent(), PICK_CONTACT_REQUEST);
75     }
76 
77     /**
78      * Invoked when the contact picker activity is finished. The {@code contactUri} parameter
79      * will contain a reference to the contact selected by the user. We will treat it as
80      * an opaque URI and allow the SDK-specific ContactAccessor to handle the URI accordingly.
81      */
82     @Override
onActivityResult(int requestCode, int resultCode, Intent data)83     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
84         if (requestCode == PICK_CONTACT_REQUEST && resultCode == RESULT_OK) {
85             loadContactInfo(data.getData());
86         }
87     }
88 
89     /**
90      * Load contact information on a background thread.
91      */
loadContactInfo(Uri contactUri)92     private void loadContactInfo(Uri contactUri) {
93 
94         /*
95          * We should always run database queries on a background thread. The database may be
96          * locked by some process for a long time.  If we locked up the UI thread while waiting
97          * for the query to come back, we might get an "Application Not Responding" dialog.
98          */
99         AsyncTask<Uri, Void, ContactInfo> task = new AsyncTask<Uri, Void, ContactInfo>() {
100 
101             @Override
102             protected ContactInfo doInBackground(Uri... uris) {
103                 return mContactAccessor.loadContact(getContentResolver(), uris[0]);
104             }
105 
106             @Override
107             protected void onPostExecute(ContactInfo result) {
108                 bindView(result);
109             }
110         };
111 
112         task.execute(contactUri);
113     }
114 
115     /**
116      * Displays contact information: name and phone number.
117      */
bindView(ContactInfo contactInfo)118     protected void bindView(ContactInfo contactInfo) {
119         TextView displayNameView = (TextView) findViewById(R.id.display_name_text_view);
120         displayNameView.setText(contactInfo.getDisplayName());
121 
122         TextView phoneNumberView = (TextView) findViewById(R.id.phone_number_text_view);
123         phoneNumberView.setText(contactInfo.getPhoneNumber());
124     }
125 }
126