Android Open Source - android_smsoverxmpp Util






From Project

Back to project page android_smsoverxmpp.

License

The source code is released under:

GNU General Public License

If you think the Android project android_smsoverxmpp 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.smorra.smsoverxmpp;
//www . j ava2 s . c  o  m
import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsManager;

public class Util
{
  public static ArrayList<Contact> getContacts(Context context)
  {
    ArrayList<Contact> ret = new ArrayList<Contact>();
    Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext())
    {
      String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
      int id = cursor.getInt(cursor.getColumnIndex(Contacts._ID));
      // System.out.println("NAME: " + name);
      // System.out.println("HASPHONENUMBER  " + hasPhoneNumber);
      // System.out.println("ID: " + id);
      // System.out.println("NAME: " + name);
      ArrayList<PhoneNumber> phoneNumbers = getPhoneNumbers(id, context);
      if (phoneNumbers.size() == 0)
        continue;
      if (phoneNumbers.size() == 1)
      {
        Contact contact = new Contact();
        contact.name = name;
        contact.number = phoneNumbers.get(0).number;
        ret.add(contact);
      }
      else
      {
        for (PhoneNumber number : phoneNumbers)
        {
          Contact contact = new Contact();
          contact.name = name;
          if (number.type == Phone.TYPE_HOME)
            contact.name += " (home)";

          else if (number.type == Phone.TYPE_MOBILE)
            contact.name += " (mobile)";
          else
            contact.name += " (" + number.type + ")";

          contact.number = number.number;
          ret.add(contact);
        }
      }

    }
    cursor.close();
    return ret;
  }

  public static String getPw(Context context)
  {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPref.getString("pw", "");
  }

  public static boolean getStarted(Context context)
  {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPref.getBoolean("started", false);
  }

  public static void setStarted(boolean b, Context context)
  {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    Editor e = sharedPref.edit();
    e.putBoolean("started", b);
    e.commit();
  }

  public static String generateHex(int numchars)
  {
    Random r = new Random();
    StringBuffer sb = new StringBuffer();
    while (sb.length() < numchars)
      sb.append(Integer.toHexString(r.nextInt()));

    return sb.toString().substring(0, numchars);
  }

  public static ArrayList<PhoneNumber> getPhoneNumbers(int id, Context context)
  {
    ArrayList<PhoneNumber> ret = new ArrayList<PhoneNumber>();
    Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + id, null, null);
    while (cursor.moveToNext())
    {
      PhoneNumber number = new PhoneNumber();
      number.number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER)).replace(" ", "");
      number.type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
      if (!number.number.equals(""))
        ret.add(number);
    }
    cursor.close();
    return ret;
  }



  public static void sendSms(String number, String body_)
  {
    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(body_);
    smsManager.sendMultipartTextMessage(number, null, parts, null, null);
  }

  public static String findNumber(String number, Context context)
  {
    String ret = null;
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] fields = new String[] { PhoneLookup.NUMBER };
    Cursor cursor = context.getContentResolver().query(uri, fields, null, null, null);
    if (cursor.moveToNext())
      ret = cursor.getString(0).replace(" ", "");
    cursor.close();
    return ret;
  }

  public static String xmlEncode(String str)
  {
    String ret = "";
    for (int i = 0; i < str.length(); i++)
    {
      char c = str.charAt(i);
      if (c >= 128 || c < 32)
      {
        ret += "&#" + (int) c + ";";
      }
      else
        ret += c;
    }
    return ret;
  }
}




Java Source Code List

com.smorra.smsoverxmpp.BootReceiver.java
com.smorra.smsoverxmpp.Client.java
com.smorra.smsoverxmpp.Contact.java
com.smorra.smsoverxmpp.IpReceiver.java
com.smorra.smsoverxmpp.MainActivity.java
com.smorra.smsoverxmpp.PhoneNumber.java
com.smorra.smsoverxmpp.Server.java
com.smorra.smsoverxmpp.Service_.java
com.smorra.smsoverxmpp.SmsReceiver.java
com.smorra.smsoverxmpp.Util.java