Home | History | Annotate | Download | only in content
      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.example.android.apis.content;
     18 
     19 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import com.example.android.apis.R;
     22 
     23 import android.app.Activity;
     24 import android.app.AlarmManager;
     25 import android.app.PendingIntent;
     26 import android.content.Intent;
     27 import android.database.Cursor;
     28 import android.net.Uri;
     29 import android.os.SystemClock;
     30 import android.os.Bundle;
     31 import android.provider.BaseColumns;
     32 import android.provider.ContactsContract;
     33 import android.view.View;
     34 import android.view.View.OnClickListener;
     35 import android.widget.Button;
     36 import android.widget.Toast;
     37 
     38 import java.util.Calendar;
     39 
     40 /**
     41  * Demonstrates launching the contacts app to pick a contact.  Does not
     42  * require permission to read contacts, as that permission will be granted
     43  * when the selected contact is returned.
     44  */
     45 public class PickContact extends Activity {
     46     Toast mToast;
     47     ResultDisplayer mPendingResult;
     48 
     49     class ResultDisplayer implements OnClickListener {
     50         String mMsg;
     51         String mMimeType;
     52 
     53         ResultDisplayer(String msg, String mimeType) {
     54             mMsg = msg;
     55             mMimeType = mimeType;
     56         }
     57 
     58         public void onClick(View v) {
     59             Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
     60             intent.setType(mMimeType);
     61             mPendingResult = this;
     62             startActivityForResult(intent, 1);
     63         }
     64     }
     65 
     66     @Override
     67     protected void onCreate(Bundle savedInstanceState) {
     68         super.onCreate(savedInstanceState);
     69 
     70         setContentView(R.layout.pick_contact);
     71 
     72         // Watch for button clicks.
     73         ((Button)findViewById(R.id.pick_contact)).setOnClickListener(
     74                 new ResultDisplayer("Selected contact",
     75                         ContactsContract.Contacts.CONTENT_ITEM_TYPE));
     76         ((Button)findViewById(R.id.pick_person)).setOnClickListener(
     77                 new ResultDisplayer("Selected person",
     78                         "vnd.android.cursor.item/person"));
     79         ((Button)findViewById(R.id.pick_phone)).setOnClickListener(
     80                 new ResultDisplayer("Selected phone",
     81                         ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE));
     82         ((Button)findViewById(R.id.pick_address)).setOnClickListener(
     83                 new ResultDisplayer("Selected address",
     84                         ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE));
     85     }
     86 
     87     @Override
     88     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     89         if (data != null) {
     90             Uri uri = data.getData();
     91             if (uri != null) {
     92                 Cursor c = null;
     93                 try {
     94                     c = getContentResolver().query(uri, new String[] { BaseColumns._ID },
     95                             null, null, null);
     96                     if (c != null && c.moveToFirst()) {
     97                         int id = c.getInt(0);
     98                         if (mToast != null) {
     99                             mToast.cancel();
    100                         }
    101                         String txt = mPendingResult.mMsg + ":\n" + uri + "\nid: " + id;
    102                         mToast = Toast.makeText(this, txt, Toast.LENGTH_LONG);
    103                         mToast.show();
    104                     }
    105                 } finally {
    106                     if (c != null) {
    107                         c.close();
    108                     }
    109                 }
    110             }
    111         }
    112     }
    113 }
    114 
    115