Android Open Source - android-textlater Contacts Activity






From Project

Back to project page android-textlater.

License

The source code is released under:

Apache License

If you think the Android project android-textlater listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 The Android Open Source Project
 *// w  w  w  .jav a 2 s  .co  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.michael.feng.textlater;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListActivity;

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

public class ContactsActivity extends SherlockListActivity {

  private ListView contactListView;
  private ArrayList<HashMap<String, String>> contactList;
  private ImageView backButton;
  private ListViewAdapter listViewAdapter;
  private int resultCode = 0;  
  private String contactNames = "";
  private String contactNumbers = "";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
    getSupportActionBar().setDisplayUseLogoEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setCustomView(R.layout.abs_layout_contacts);

    // Init Back Button
    backButton = (ImageView) findViewById(R.id.back);
    backButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        finish();
      }
    });
    backButton.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View arg0, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          // touch down code
          backButton.setBackgroundColor(Color.rgb(104, 156, 210));
          break;

        case MotionEvent.ACTION_MOVE:
          // touch move code
          break;

        case MotionEvent.ACTION_UP:
          // touch up code
          backButton.setBackgroundColor(Color.TRANSPARENT);
          break;
        }
        return false;
      }
    });

    // Init Contacts ListView
    contactListView = (ListView) findViewById(android.R.id.list);
    
    Intent in = getIntent();
    if(in.hasExtra("contactNames")) {
      contactNames = in.getStringExtra("contactNames");
    }
    if(in.hasExtra("contactNumbers")) {
      contactNumbers = in.getStringExtra("contactNumbers");
    }

    listContacts();
  }

  // Get all Contacts from Device
  public void listContacts() {
    Cursor phones = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    contactList = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = null;
    while (phones.moveToNext()) {
      String name = phones.getString(phones
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      String number = phones.getString(phones
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      map = new HashMap<String, String>();
      map.put("contactName", name);
      map.put("contactNumber", number);
      contactList.add(map);
    }

    listViewAdapter = new ListViewAdapter(this);
    contactListView.setAdapter(listViewAdapter);

  }
  
  public class ListViewAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private int listLayout = R.layout.listitem_contacts;

    public ListViewAdapter(Context con) {
      mInflater = LayoutInflater.from(con);
    }

    public void setListLayout(int layoutId) {
      listLayout = layoutId;
    }

    public int getCount() {
      return contactList.size();
    }

    public Object getItem(int position) {
      return position;
    }

    public long getItemId(int position) {
      return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
      final ListContent holder;
      View view = convertView;
      if (view == null) {
        view = mInflater.inflate(listLayout, null);
        holder = new ListContent();
        holder.contactName = (TextView) view.findViewById(R.id.contactName);
        holder.contactNumber = (TextView) view.findViewById(R.id.contactNumber);
        view.setTag(holder);
      } else {
        holder = (ListContent) view.getTag();
      }
      holder.contactName.setText(contactList.get(position).get("contactName"));
      holder.contactNumber.setText(contactList.get(position).get("contactNumber"));
      holder.contactName.setOnClickListener(contactListener);
      return view;
    }
  }

  static class ListContent {
    TextView contactName;
    TextView contactNumber;
  }
  
  public OnClickListener contactListener = new OnClickListener() {
    public void onClick(View view) {
      int position = contactListView.getPositionForView((View) view.getParent());
      String contactName   = contactList.get(position).get("contactName");
      String contactNumber = contactList.get(position).get("contactNumber");
      
      // Set selected contact result back to NewActivity 
      setReturnContact(contactName, contactNumber);
          
      Log.d("contactName  =>", contactName);
      Log.d("contactNumber=>", contactNumber);
      finish();
    }
  };
  
  public void setReturnContact(String contactName, String contactNumber) {
    Intent intent = new Intent();  
    
    if(null != contactNames && !"".equals(contactNames)) {
      contactNames   += contactName   + "; ";
      contactNumbers += contactNumber + "; ";
    } else {
      contactNames   = contactName   + "; ";
      contactNumbers = contactNumber + "; "; 
    }
    intent.putExtra("contactNames",    contactNames);
    intent.putExtra("contactNumbers",  contactNumbers);
    
        this.setResult(resultCode, intent);
  }

}




Java Source Code List

com.michael.feng.textlater.AlarmReceiver.java
com.michael.feng.textlater.ContactsActivity.java
com.michael.feng.textlater.DetailActivity.java
com.michael.feng.textlater.MainActivity.java
com.michael.feng.textlater.MessageDAO.java
com.michael.feng.textlater.Message.java
com.michael.feng.textlater.NewActivity.java
com.michael.feng.textlater.SQLiteHelper.java
com.michael.feng.textlater.SendActivity.java