com.adampash.contactSearch.ContactsSearchModule.java Source code

Java tutorial

Introduction

Here is the source code for com.adampash.contactSearch.ContactsSearchModule.java

Source

/**
 * This file was auto-generated by the Titanium Module SDK helper for Android
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 *
 */
package com.adampash.contactSearch;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;

import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
/* import android.os.Bundle; */
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.content.ContentUris;
import android.content.ContentResolver;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;

@Kroll.module(name = "ContactsSearch", id = "com.adampash.contactSearch")
public class ContactsSearchModule extends KrollModule {

    // Standard Debugging variables
    private static final String LCAT = "ContactsSearchModule";
    private static final boolean DBG = TiConfig.LOGD;

    // You can define constants with @Kroll.constant, for example:
    // @Kroll.constant public static final String EXTERNAL_NAME = value;

    public ContactsSearchModule() {
        super();
    }

    @Kroll.onAppCreate
    public static void onAppCreate(TiApplication app) {
        Log.d(LCAT, "inside onAppCreate");
    }

    public TiApplication appContext = TiApplication.getInstance();
    public Activity activity = appContext.getCurrentActivity();

    @Kroll.method
    private HashMap[] getPeopleWithName(String query, @Kroll.argument(optional = true) boolean includPhoto) {

        List<HashMap<Object, Object>> contacts = new ArrayList<HashMap<Object, Object>>();

        if (query == "") {
            HashMap[] contactsArray = contacts.toArray(new HashMap[contacts.size()]);
            return contactsArray;
        }

        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.Contacts.PHOTO_ID };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + "1" + "'" + " AND "
                + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? OR " + ContactsContract.Contacts.DISPLAY_NAME
                + " LIKE ?";

        String[] selectionArgs = new String[2];
        selectionArgs[0] = query + "%";
        selectionArgs[1] = "% " + query + "%";
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        Cursor cursor = activity.managedQuery(uri, projection, selection, selectionArgs, sortOrder);

        try {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    //GRAB CURRENT values;
                    String lk = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    Integer id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    Integer photo_id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    HashMap<Object, Object> contact = new HashMap<Object, Object>();
                    contact.put("fullName", name);
                    contact.put("id", id);
                    contact.put("photo_id", photo_id);
                    if (photo_id != null && includPhoto) {
                        TiBlob image = fetchThumbnail(photo_id);
                        contact.put("image", image);
                    }
                    contacts.add(contact);
                }
            }

            HashMap[] contactsArray = contacts.toArray(new HashMap[contacts.size()]);
            return contactsArray;
        } finally {
            cursor.close();
        }
    }

    // bastardized from http://stackoverflow.com/questions/2383580/
    @Kroll.method
    private TiBlob fetchThumbnail(int thumbnailId) {
        ContentResolver contentResolver = appContext.getContentResolver();

        String[] PHOTO_BITMAP_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO };
        Uri uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, thumbnailId);
        Cursor cursor = contentResolver.query(uri, PHOTO_BITMAP_PROJECTION, null, null, null);

        try {
            Bitmap thumbnail = null;
            if (cursor.moveToFirst()) {
                byte[] thumbnailBytes = cursor.getBlob(0);
                if (thumbnailBytes != null) {
                    thumbnail = BitmapFactory.decodeByteArray(thumbnailBytes, 0, thumbnailBytes.length);
                }
            }
            /* return thumbnail; */
            return TiBlob.blobFromImage(thumbnail);
        } finally {
            cursor.close();
        }

    }
}