Back to project page MySms.
The source code is released under:
Apache License
If you think the Android project MySms listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.henningta.mysms; //from ww w . j ava 2 s.c om import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import java.util.Calendar; import java.util.List; /** * Static class containing settings and functions for the entire application */ public class Settings { /* Keys to avoid hard-coding */ public static final String KEY_COUNT = "count"; public static final String KEY_HAS_NEW_MESSAGES = "has_new_messages"; public static final String KEY_IS_CONTACT = "is_contact"; public static final String KEY_MESSAGE = "message"; public static final String KEY_NAME = "name"; public static final String KEY_REFRESH = "refresh"; public static final String KEY_SOURCE = "source"; public static final String KEY_STATUS = "status"; public static final String KEY_TIME = "time"; /* Default values */ public static final int NOT_SET = -1; /* Intent filters for local receivers */ public static final String ACTION_CONVERSATION = "com.henningta.mysms.ACTION_CONVERSATION"; public static final String ACTION_MESSAGE = "com.henningta.mysms.ACTION_MESSAGE"; /* Unique request codes */ public static final int REQUEST_CONTACT = 1234; public static final int REQUEST_ADD_CONTACT = 9999; /* Prefs settings */ public static boolean NOTIFICATIONS_ENABLED = true; public static boolean NOTIFICATIONS_VIBRATE = true; private Settings() { // static class } public static String formatSource(String source) { StringBuilder builder = new StringBuilder(); builder.append(source); if (builder.length() == 10) { // format phone number builder.insert(0, "("); builder.insert(4, ")"); builder.insert(5, " "); builder.insert(9, "-"); } return builder.toString(); } public static String formatTime(long time) { String formattedTime; Calendar c = Calendar.getInstance(); c.setTimeInMillis(time); if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) > c.get(Calendar.DAY_OF_YEAR)) { // time is from previous day int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); String monthStr = null; switch (month) { case Calendar.JANUARY: monthStr = "Jan"; break; case Calendar.FEBRUARY: monthStr = "Feb"; break; case Calendar.MARCH: monthStr = "Mar"; break; case Calendar.APRIL: monthStr = "Apr"; break; case Calendar.MAY: monthStr = "May"; break; case Calendar.JUNE: monthStr = "Jun"; break; case Calendar.JULY: monthStr = "Jul"; break; case Calendar.AUGUST: monthStr = "Aug"; break; case Calendar.SEPTEMBER: monthStr = "Sep"; break; case Calendar.OCTOBER: monthStr = "Oct"; break; case Calendar.NOVEMBER: monthStr = "Nov"; break; case Calendar.DECEMBER: monthStr = "Dec"; break; default: break; } formattedTime = monthStr + " " + String.valueOf(day); } else { // time is from current day int hour = c.get(Calendar.HOUR); int minute = c.get(Calendar.MINUTE); if (hour == 0) hour = 12; String am_pm; if (c.get(Calendar.AM_PM) == Calendar.AM) am_pm = "am"; else am_pm = "pm"; formattedTime = String.valueOf(hour) + ":" + String.format("%02d", minute) + am_pm; } return formattedTime; } public static void setBadge(Context context, int count) { String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) return; Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); intent.putExtra("badge_count", count); intent.putExtra("badge_count_package_name", context.getPackageName()); intent.putExtra("badge_count_class_name", launcherClassName); context.sendBroadcast(intent); } private static String getLauncherClassName(Context context) { PackageManager pm = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); for (ResolveInfo resolveInfo : resolveInfos) { String pkgName = resolveInfo.activityInfo.applicationInfo.packageName; if (pkgName.equalsIgnoreCase(context.getPackageName())) return resolveInfo.activityInfo.name; } return null; } }