Android Open Source - Pairs Set Contact Activity






From Project

Back to project page Pairs.

License

The source code is released under:

MIT License

If you think the Android project Pairs 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

package com.example.pairs;
/*from   ww w  .  j  ava 2  s.c  om*/
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SetContactActivity extends Activity {
  
  String t;
  Button saveMes;
  Button viewMes;
  Button sendMes;
  EditText message;
  ListView showCall;
  
  //?????????????????????????????????
  ArrayList<String> names = new ArrayList<String>();
  ArrayList<String> sortedName = new ArrayList<String>();
  ArrayList<String> sortedNumber = new ArrayList<String>();
  ArrayList<String> sortedTime = new ArrayList<String>();
  ArrayList<String> sortedType = new ArrayList<String>();
  
  //???????????????????
  ArrayList<String> firstClassN = new ArrayList<String>();
  ArrayList<String> secondClassN = new ArrayList<String>();
  ArrayList<String> thirdClassN = new ArrayList<String>();
  ArrayList<String> fourthClassN = new ArrayList<String>();
  ArrayList<String> fifthClassN = new ArrayList<String>();
  ArrayList<String> sixthClassN = new ArrayList<String>();
  
  //?????????????
  ArrayList<String> firstClassT = new ArrayList<String>();
  ArrayList<String> secondClassT = new ArrayList<String>();
  ArrayList<String> thirdClassT = new ArrayList<String>();
  ArrayList<String> fourthClassT = new ArrayList<String>();
  ArrayList<String> fifthClassT = new ArrayList<String>();
  ArrayList<String> sixthClassT = new ArrayList<String>();
  
  //??????????????????
  ArrayList<String> sendList = new ArrayList<String>();
  
  //?????????
  SharedPreferences sharedPreferences;
  SharedPreferences.Editor editor;
  
  SharedPreferences forReadCheck;
  SharedPreferences.Editor forWriteCheck;
  
  SharedPreferences readContent;
  SharedPreferences.Editor writeContent;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.expand_list);
    
    //????
    saveMes = (Button) findViewById(R.id.saveMes);
    viewMes = (Button) findViewById(R.id.viewMes);
    sendMes = (Button) findViewById(R.id.sendMes);
    message = (EditText) findViewById(R.id.message);
    
    readContent = getSharedPreferences("pairsMesContent", Activity.MODE_PRIVATE);
    writeContent = readContent.edit();
    
    sharedPreferences = getSharedPreferences("pairsNumber", Activity.MODE_PRIVATE);
    editor = sharedPreferences.edit();
    editor.clear();
    names.clear();
    sortedName.clear();
    sortedNumber.clear();
    sortedTime.clear();
    
    Cursor contact = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    
    //?????????????????-??????????????
       while(contact.moveToNext())
       {
         String contactId = contact.getString(contact.getColumnIndex(ContactsContract.Contacts._ID));
      String contactName =  contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
      names.add(contactName);
      
      //?????????
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
      while(phones.moveToNext())
      {
        String phoneNum = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace("-", "");
        if(phoneNum.substring(0,1).equals("+"))
          phoneNum = phoneNum.substring(3,14);
        //?????????
        editor.putString(phoneNum, contactName);
      }
      phones.close();
      editor.commit();
       }
       contact.close();
       
       //??????????????????????????????????????
       long[] times = new long[names.size()];
       String[] numbers = new String[names.size()];
       int[] types = new int[names.size()];
       for(int i = 0; i < names.size(); i++)
       {
         times[i] = 0;
         numbers[i] = "";
         types[i] = 0;
       }
       
       //??????????????????????????????????????????????
       Cursor calllog = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, "type=1 or type=2", null, null);
    String calllogName,calllogNumber;
    while(calllog.moveToNext())
    {
      if(calllog.getString(calllog.getColumnIndex(CallLog.Calls.CACHED_NAME)) != null)
      {
        calllogName = calllog.getString(calllog.getColumnIndex(CallLog.Calls.CACHED_NAME));
        calllogNumber = calllog.getString(calllog.getColumnIndex(CallLog.Calls.NUMBER));
        if(calllogNumber.substring(0,1).equals("+"))
          calllogNumber = calllogNumber.substring(3,14);
        for(int i = 0; i < names.size(); i++)
        {
          if(calllogName.equals(names.get(i)))
          {
            long time = Long.parseLong(calllog.getString(calllog.getColumnIndex(CallLog.Calls.DATE))); 
            if(time > (times[i]))
            {
              times[i] = time;
              numbers[i] = calllogNumber;
              types[i] = 1;
            }
          }
        }
      }    
    }
    calllog.close();
    
    //?????????????????????????????????????????????????????????????
    //????????????????????????long???????????1970????????????
    Cursor inbox = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
    while(inbox.moveToNext())
    {
      String number = inbox.getString(inbox.getColumnIndex("address"));
      if(number.substring(0,1).equals("+"))
        number = number.substring(3,14);
      String inboxName = sharedPreferences.getString(number, "");
      for(int i = 0; i < names.size(); i++)
      {
        if(inboxName.equals(names.get(i)))
        {
          long time = Long.parseLong(inbox.getString(inbox.getColumnIndex("date"))); 
          //???????????????????????????????????????????????
          if(time > (times[i]))
          {
            times[i] = time;
            numbers[i] = number;
            types[i] = 2;
          }
        }
      }
    }
    inbox.close();
    
    
    //?????????????????????????????????????????????????????????????????
    //????????????????????????long???????????1970????????????
    Cursor sent = getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, null);
    while(sent.moveToNext())
    {
      String number = sent.getString(sent.getColumnIndex("address"));
      if(number.substring(0,1).equals("+"))
        number = number.substring(3,14);
      String inboxName = sharedPreferences.getString(number, "");
      for(int i = 0; i < names.size(); i++)
      {
        if(inboxName.equals(names.get(i)))
        {
          long time = Long.parseLong(sent.getString(sent.getColumnIndex("date"))); 
          //???????????????????????????????????????????????
          if(time > (times[i]))
          {
            times[i] = time;
            numbers[i] = number;
            types[i] = 3;
          }
        }
      }
    }
    sent.close();
    
    String[] nameSort = new String[names.size()];
    for (int i = 0; i < names.size(); i++)
    {
      nameSort[i] = names.get(i);
    }
    
    //??????????????????????????????????????????????????????????
    for (int i = 0; i < names.size() - 1; i++)
      for (int j = i + 1; j<names.size(); j++)
        if (times[i] < times[j])
        {
          long templong = times[i]; times[i] = times[j]; times[j] = templong;
          String temps=nameSort[i]; nameSort[i]=nameSort[j]; nameSort[j]=temps;
          temps=numbers[i]; numbers[i]=numbers[j]; numbers[j]=temps;
          int tempint=types[i]; types[i]=types[j]; types[j]=tempint;
        }
    
    //???????????????ArrayList??
    for (int i = 0; i <names.size(); i++)
    {
      sortedName.add(nameSort[i]);
      sortedNumber.add(numbers[i]);
      sortedTime.add(String.valueOf(times[i]));
      sortedType.add(String.valueOf(types[i]));
    }
    
///////////////////////////////
    
    //??????????
    forReadCheck = getSharedPreferences("pairsChosen", Activity.MODE_PRIVATE);
    forWriteCheck = forReadCheck.edit();
    forWriteCheck.clear();
    forWriteCheck.commit();
    
    firstClassN.clear();
    secondClassN.clear();
    thirdClassN.clear();
    fourthClassN.clear();
    fifthClassN.clear();
    sixthClassN.clear();
    
    firstClassT.clear();
    secondClassT.clear();
    thirdClassT.clear();
    fourthClassT.clear();
    fifthClassT.clear();
    sixthClassT.clear();
    
    //????????????????????????????????????????????????????
    for (int i = 0; i < names.size(); i++)
    {
      //??????
      long curDate = new Date(System.currentTimeMillis()).getTime();
      //????????????????????????????????
      long maxDate = new Date(Long.parseLong(sortedTime.get(i))).getTime();
      //????????????
      double hours = (curDate - maxDate) / 3600000.0;
      
      //?????????????????????????
      switch(Integer.parseInt(sortedType.get(i)))
      {
      case 1: t = "??ta?????"; break;
      case 2: t = "?????ta?????"; break;
      case 3: t = "??ta?????????"; break;
      default : break;
      }
      
      //????????????????????1?????1?????1?????1????????????????????
      //????????????????????0
      if (maxDate == 0)
      {
        sixthClassN.add(sortedName.get(i));
        sixthClassT.add("?????????????");
      }
      else if (hours >= 0 && hours < 24)
      {
        firstClassN.add(sortedName.get(i));
        firstClassT.add(String.valueOf((long)hours + 1)+"???????"+t);
      }
      else if(hours >= 24 && hours < 192)
      {
        secondClassN.add(sortedName.get(i));
        secondClassT.add(String.valueOf((long)(hours / 24.0))+"????????"+t);
      }
      else if(hours >= 192 && hours < 744)
      {
        thirdClassN.add(sortedName.get(i));
        thirdClassT.add(String.valueOf((long)(hours / 168.0))+"???????????"+t);
      }
      else if(hours >= 744 && hours < 8760)
      {
        fourthClassN.add(sortedName.get(i));
        fourthClassT.add(String.valueOf((long)(hours / 744.0))+"?????????"+t);
      }
      else if(hours >= 8760)
      {
        fifthClassN.add(sortedName.get(i));
        fifthClassT.add(String.valueOf((long)(hours / 8760.0))+"????????"+t);
      }
    }
    
    final String[] first = firstClassN.toArray(new String[firstClassN.size()]);
    final String[] second = secondClassN.toArray(new String[secondClassN.size()]);
    final String[] third = thirdClassN.toArray(new String[thirdClassN.size()]);
    final String[] fourth = fourthClassN.toArray(new String[fourthClassN.size()]);
    final String[] fifth = fifthClassN.toArray(new String[fifthClassN.size()]);
    final String[] sixth = sixthClassN.toArray(new String[sixthClassN.size()]);
    
////////////////////////////////////////
    
    //???????????????????????????????
    //??????????????????ExpandableList
    final ExpandableListAdapter adapter = new BaseExpandableListAdapter()
    {
      //??????????
      int[] logos = new int[] 
          {R.drawable.day, R.drawable.week, R.drawable.month, R.drawable.year, R.drawable.syear, R.drawable.longlong};
       String[] armTypes = new String[]
           { "", "", "", "", "", ""};
      String[][] arms = new String[][]
          { first, second, third, fourth, fifth, sixth};
      
      @Override
      public Object getChild(int groupPosition, int childPosition)
      {
        return arms[groupPosition][childPosition];
      }

      @Override
      public long getChildId(int groupPosition, int childPosition)
      {
        return childPosition;
      }

      @Override
      public int getChildrenCount(int groupPosition)
      {
        return arms[groupPosition].length;
      }

      //getChildView???????????????????????????
      @Override
      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
      {
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.child_list, null);
        
        //???????????????????????????????????????CheckBox?????
        CheckBox box = (CheckBox) layout.findViewById(R.id.name);
        box.setText(arms[groupPosition][childPosition].toString());
        //???????????????TextView????
        TextView time = (TextView) layout.findViewById(R.id.time);
        box.setTextSize(15);
        time.setTextSize(15);
        switch(groupPosition)
        {
        case 0: time.setText(firstClassT.get(childPosition).toString()); break;
        case 1: time.setText(secondClassT.get(childPosition).toString()); break;
        case 2: time.setText(thirdClassT.get(childPosition).toString()); break;
        case 3: time.setText(fourthClassT.get(childPosition).toString()); break;
        case 4: time.setText(fifthClassT.get(childPosition).toString()); break;
        default : time.setText(sixthClassT.get(childPosition).toString()); break;  
        }
        
        final String msg = box.getText().toString();
        box.setChecked(forReadCheck.getBoolean(msg, false));
        box.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
        {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
          {
            //??????????????????????
            if(isChecked)
            {
              forWriteCheck.putBoolean(msg,true);
              forWriteCheck.commit();
              Toast.makeText(SetContactActivity.this, "???????:"+ msg, Toast.LENGTH_SHORT).show();
            }
            //?????????????????????????
            else
            {
              forWriteCheck.putBoolean(msg,false);
              forWriteCheck.commit();
              Toast.makeText(SetContactActivity.this, "???????:"+ msg, Toast.LENGTH_SHORT).show();
            }
          }
        });
        return layout;
      }
      
      @Override
      public Object getGroup(int groupPosition)
      {
        return armTypes[groupPosition];
      }

      @Override
      public int getGroupCount()
      {
        return armTypes.length;
      }

      @Override
      public long getGroupId(int groupPosition)
      {
        return groupPosition;
      }

      //???????????????????????
      @Override
      public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
      {
        LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.group_list, null);
        //?????????logo?????
        ImageView logo = new ImageView(SetContactActivity.this);
        logo.setImageResource(logos[groupPosition]);
        layout.addView(logo);
        //????????????????????????????????????????????????????????????????????
        TextView textView = new TextView(SetContactActivity.this);
        textView.setTextSize(20);
        textView.setText(getGroup(groupPosition).toString());
        layout.addView(textView);
        return layout;
      }

      @Override
      public boolean isChildSelectable(int groupPosition,
          int childPosition)
      {
        return true;
      }

      @Override
      public boolean hasStableIds()
      {
        return false;
      }
    };
    //???
    ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list);
    expandListView.setAdapter(adapter);  
  }  
///////////////////////////////
  
  //??????????????????
  String[] itemsSave = new String[] { "???????????1", "???????????2" };
  String[] itemsView = new String[2];
  String[] itemsSend = new String[] { "??????", "??????????1", "??????????2"};
  int mark;
  
  //???????????????
  public void simpleSave(View source)
  {
    AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("???????").setSingleChoiceItems(itemsSave, 0, new OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        switch(which)
        {
        case 0: mark = 0; break;
        case 1: mark = 1; break;
        }
      }
    }).setPositiveButton("???", new OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        //??????????????
        writeContent.putString(String.valueOf(mark), message.getText().toString());
        writeContent.commit();
        message.setText("");
        Toast.makeText(SetContactActivity.this, "?????", Toast.LENGTH_SHORT).show();
      }
    });
    setNegativeButton(builder).create().show();
  }
  
  //????????????
  public void simpleView(View source)
  {
    //??????????????????????????????
    itemsView[0] = readContent.getString("0", "???????????????").toString();
    itemsView[1] = readContent.getString("1", "?????????????").toString();
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("?????????").setItems(itemsView, new OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        //????????????
        message.setText(itemsView[which]);
      }
    });
    setNegativeButton(builder).create().show();
  }
  
  //???????????
  public void simpleSend(View source)
  {
    sendList.clear();
    final SmsManager sms = SmsManager.getDefault();
    String wantName = "????????";
    //?????????????????????????????????????????????sendList????????????
    for(int i = 0; i < sortedName.size(); i++)
    {
      if(forReadCheck.getBoolean(sortedName.get(i).toString(), false) == true)
      {
        wantName = wantName + " " + sortedName.get(i).toString();
        sendList.add(sortedNumber.get(i).toString());
      }
    }
    
    //????????????
    AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("??????").setMessage(wantName + " ?????????").setPositiveButton("???", new OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        //?????????????????????????????sendList??????????????????????????
        for (String number : sendList)
        {
          PendingIntent pi = PendingIntent.getActivity(SetContactActivity.this, 0, new Intent(), 0);
          sms.sendTextMessage(number, null, message.getText().toString(), pi, null);
        }
        Toast.makeText(SetContactActivity.this, "???????", Toast.LENGTH_SHORT).show();
        message.setText("");
      }
    });
    setNegativeButton(builder).create().show();
  }
  
  //?????????????????
  private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder)
  {
    return builder.setNegativeButton("???", new OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        
      }
    });
  }
}




Java Source Code List

com.example.pairs.AlarmActivity.java
com.example.pairs.ChooseContactActivity.java
com.example.pairs.Function.java
com.example.pairs.HelpActivity.java
com.example.pairs.Help.java
com.example.pairs.MainActivity.java
com.example.pairs.OurPage.java
com.example.pairs.Ours.java
com.example.pairs.PhotoActivity.java
com.example.pairs.SMSReceiver.java
com.example.pairs.SetContactActivity.java