Home | History | Annotate | Download | only in contactmanager
      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.contactmanager;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.database.Cursor;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.provider.ContactsContract;
     25 import android.util.Log;
     26 import android.view.View;
     27 import android.widget.Button;
     28 import android.widget.CheckBox;
     29 import android.widget.CompoundButton;
     30 import android.widget.CompoundButton.OnCheckedChangeListener;
     31 import android.widget.ListView;
     32 import android.widget.SimpleCursorAdapter;
     33 
     34 public final class ContactManager extends Activity
     35 {
     36 
     37     public static final String TAG = "ContactManager";
     38 
     39     private Button mAddAccountButton;
     40     private ListView mContactList;
     41     private boolean mShowInvisible;
     42     private CheckBox mShowInvisibleControl;
     43 
     44     /**
     45      * Called when the activity is first created. Responsible for initializing the UI.
     46      */
     47     @Override
     48     public void onCreate(Bundle savedInstanceState)
     49     {
     50         Log.v(TAG, "Activity State: onCreate()");
     51         super.onCreate(savedInstanceState);
     52         setContentView(R.layout.contact_manager);
     53 
     54         // Obtain handles to UI objects
     55         mAddAccountButton = (Button) findViewById(R.id.addContactButton);
     56         mContactList = (ListView) findViewById(R.id.contactList);
     57         mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);
     58 
     59         // Initialize class properties
     60         mShowInvisible = false;
     61         mShowInvisibleControl.setChecked(mShowInvisible);
     62 
     63         // Register handler for UI elements
     64         mAddAccountButton.setOnClickListener(new View.OnClickListener() {
     65             public void onClick(View v) {
     66                 Log.d(TAG, "mAddAccountButton clicked");
     67                 launchContactAdder();
     68             }
     69         });
     70         mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     71             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     72                 Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
     73                 mShowInvisible = isChecked;
     74                 populateContactList();
     75             }
     76         });
     77 
     78         // Populate the contact list
     79         populateContactList();
     80     }
     81 
     82     /**
     83      * Populate the contact list based on account currently selected in the account spinner.
     84      */
     85     private void populateContactList() {
     86         // Build adapter with contact entries
     87         Cursor cursor = getContacts();
     88         String[] fields = new String[] {
     89                 ContactsContract.Data.DISPLAY_NAME
     90         };
     91         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
     92                 fields, new int[] {R.id.contactEntryText});
     93         mContactList.setAdapter(adapter);
     94     }
     95 
     96     /**
     97      * Obtains the contact list for the currently selected account.
     98      *
     99      * @return A cursor for for accessing the contact list.
    100      */
    101     private Cursor getContacts()
    102     {
    103         // Run query
    104         Uri uri = ContactsContract.Contacts.CONTENT_URI;
    105         String[] projection = new String[] {
    106                 ContactsContract.Contacts._ID,
    107                 ContactsContract.Contacts.DISPLAY_NAME
    108         };
    109         String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
    110                 (mShowInvisible ? "0" : "1") + "'";
    111         String[] selectionArgs = null;
    112         String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    113 
    114         return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    115     }
    116 
    117     /**
    118      * Launches the ContactAdder activity to add a new contact to the selected accont.
    119      */
    120     protected void launchContactAdder() {
    121         Intent i = new Intent(this, ContactAdder.class);
    122         startActivity(i);
    123     }
    124 }
    125