package net.everythingandroid.smspopup;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Contacts;
import android.provider.Settings;
import android.provider.Contacts.PeopleColumns;
import android.provider.Contacts.PhotosColumns;
import android.telephony.PhoneNumberUtils;
import android.telephony.gsm.SmsMessage;
import android.text.TextUtils;
public class SMSPopupUtils {
//Content URIs for SMS app, these may change in future SDK
public static final Uri MMS_SMS_CONTENT_URI = Uri.parse("content://mms-sms/");
public static final Uri THREAD_ID_CONTENT_URI =
Uri.withAppendedPath(MMS_SMS_CONTENT_URI, "threadID");
public static final Uri CONVERSATION_CONTENT_URI =
Uri.withAppendedPath(MMS_SMS_CONTENT_URI, "conversations");
public static final Uri SMS_INBOX_CONTENT_URI = Uri.parse("content://sms/inbox");
public static final Uri MMS_CONTENT_URI = Uri.parse("content://mms");
public static final Uri MMS_INBOX_CONTENT_URI = Uri.withAppendedPath(MMS_CONTENT_URI, "inbox");
public static final String SMS_ID = "_id";
public static final String SMS_TO_URI = "smsto:/";
public static final String SMS_MIME_TYPE = "vnd.android-dir/mms-sms";
public static final String SMS_ADDRESS_EXTRA = "address";
public static final int READ_THREAD = 1;
public static final int MESSAGE_TYPE_SMS = 1;
public static final int MESSAGE_TYPE_MMS = 2;
private static final String TIME_FORMAT_12_HOUR = "h:mm a";
private static final String TIME_FORMAT_24_HOUR = "H:mm";
public static String getPersonName(Context context, String id, String address) {
if (id == null) {
if (address != null) {
Log.v("Contact not found, formatting number");
return PhoneNumberUtils.formatNumber(address);
} else {
return null;
}
}
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.People.CONTENT_URI, id),
new String[] { PeopleColumns.DISPLAY_NAME }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
String name = cursor.getString(0);
Log.v("Contact Display Name: " + name);
return name;
}
} finally {
cursor.close();
}
}
if (address != null) {
Log.v("Contact not found, formatting number");
return PhoneNumberUtils.formatNumber(address);
}
return null;
}
public static String getPersonIdFromPhoneNumber(Context context,
String address) {
if (address == null)
return null;
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address),
new String[] { Contacts.Phones.PERSON_ID }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
Long id = Long.valueOf(cursor.getLong(0));
Log.v("Found person: " + id);
return (String.valueOf(id));
}
} finally {
cursor.close();
}
}
return null;
}
public static byte[] getPersonPhoto(Context context, String id) {
if (id == null)
return null;
byte photo[] = null;
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.Photos.CONTENT_URI, id),
new String[] { PhotosColumns.DATA }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
photo = cursor.getBlob(0);
if (photo != null) {
return photo;
// Log.v("Found photo for person: " + id);
// bitmap = BitmapFactory.decodeStream(new
// ByteArrayInputStream(photo));
}
}
} finally {
cursor.close();
}
}
return photo;
}
public static long getThreadIdFromAddress(Context context, String address) {
if (address == null) return 0;
String THREAD_RECIPIENT_QUERY = "recipient";
Uri.Builder uriBuilder = THREAD_ID_CONTENT_URI.buildUpon();
uriBuilder.appendQueryParameter(THREAD_RECIPIENT_QUERY, address);
long threadId = 0;
Cursor cursor = context.getContentResolver().query(
uriBuilder.build(),
new String[] { SMS_ID },
null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
threadId = cursor.getLong(0);
}
} finally {
cursor.close();
}
}
return threadId;
}
public static void setThreadRead(Context context, long threadId) {
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean markRead = myPrefs.getBoolean(
context.getString(R.string.pref_markread_key),
Boolean.valueOf(context.getString(R.string.pref_markread_default)));
if (!markRead) return;
if (threadId > 0) {
ContentValues values = new ContentValues(1);
values.put("read", READ_THREAD);
ContentResolver cr = context.getContentResolver();
int result = 0;
try {
result = cr.update(
ContentUris.withAppendedId(CONVERSATION_CONTENT_URI, threadId),
values, null, null);
} catch (Exception e) {
Log.v("error marking thread read");
}
Log.v("thread id " + threadId + " marked as read, result = " + result);
}
}
public static Intent getSmsIntent() {
Intent conversations = new Intent(Intent.ACTION_MAIN);
//conversations.addCategory(Intent.CATEGORY_DEFAULT);
conversations.setType(SMS_MIME_TYPE);
//TODO: use FLAG_ACTIVITY_RESET_TASK_IF_NEEDED??
int flags =
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP;
conversations.setFlags(flags);
return conversations;
}
public static Intent getSmsToIntentFromThreadId(Context context, long threadId) {
Intent popup = new Intent(Intent.ACTION_VIEW);
//TODO: use FLAG_ACTIVITY_RESET_TASK_IF_NEEDED??
int flags =
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP;
popup.setFlags(flags);
if (threadId > 0) {
Log.v("^^Found threadId (" + threadId + "), sending to Sms intent");
popup.setData(Uri.withAppendedPath(THREAD_ID_CONTENT_URI, String.valueOf(threadId)));
} else {
return getSmsIntent();
}
return popup;
}
public static void launchEmailToIntent(Context context, String subject, boolean includeDebug) {
Intent msg = new Intent(Intent.ACTION_SEND);
String[] recipients={"Adam K <adam@everythingandroid.net>"};
String body = "";
if (includeDebug) {
body = "\n\n----------\nSysinfo - " + Build.FINGERPRINT + "\n"
+ "Model: " + Build.MODEL + "\n\n";
String[] pref_keys = {
context.getString(R.string.pref_enabled_key),
context.getString(R.string.pref_timeout_key),
context.getString(R.string.pref_privacy_key),
context.getString(R.string.pref_markread_key),
context.getString(R.string.pref_onlyShowOnKeyguard_key),
context.getString(R.string.pref_blur_key),
context.getString(R.string.pref_notif_enabled_key),
context.getString(R.string.pref_notif_sound_key),
context.getString(R.string.pref_vibrate_key),
context.getString(R.string.pref_vibrate_pattern_key),
context.getString(R.string.pref_vibrate_pattern_custom_key),
context.getString(R.string.pref_flashled_key),
context.getString(R.string.pref_flashled_color_key),
context.getString(R.string.pref_notif_repeat_key),
context.getString(R.string.pref_notif_repeat_times_key
- 1
- 2
- 3
- 4
前往页