Java tutorial
/** Copyright (c) 2013 Jordi Lpez <@wuyingren> & <@aniol> * * MIT License: * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ package cat.wuyingren.whatsannoy.utils; import android.app.AlarmManager; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.util.Log; import java.util.Calendar; import java.util.Random; import cat.wuyingren.whatsannoy.R; import cat.wuyingren.whatsannoy.activities.MainActivity; import cat.wuyingren.whatsannoy.profiles.Schedule; import cat.wuyingren.whatsannoy.sql.ScheduleDataSource; /** * @author Jordi Lpez <wuyingren> */ public class SystemUtils { /*public static int getSdkInt() { return Build.VERSION.SDK_INT; }*/ public static void createAlarm(Context context, Schedule schedule) { Log.w("UTILS", "createAlarm() / Scheduled: " + schedule.getId()); Intent alarmIntent = new Intent(context, Alarm.class); alarmIntent.putExtra(Alarm.PREF_ALARM_ID, schedule.getId()); PendingIntent pi = PendingIntent.getBroadcast(context, SystemUtils.safeLongToInt(schedule.getId()), alarmIntent, 0); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmMgr.set(AlarmManager.RTC_WAKEUP, schedule.getDate(), pi); } public static void updateAlarm(Context context, Schedule schedule) { cancelAlarm(context, schedule); createAlarm(context, schedule); } public static void cancelAlarm(Context context, Schedule schedule) { Intent intent = new Intent(context, Alarm.class); PendingIntent pi = PendingIntent.getBroadcast(context, SystemUtils.safeLongToInt(schedule.getId()), intent, 0); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmMgr.cancel(pi); } public static long createAlarm(Context context, int frequency, int alarmID) { Log.w("UTILS", "createAlarm()"); Intent alarmIntent = new Intent(context, Alarm.class); alarmIntent.putExtra(Alarm.PREF_ALARM_ID, alarmID); PendingIntent pi = PendingIntent.getBroadcast(context, -1, alarmIntent, 0); Random r = new Random(); int addMin = r.nextInt(frequency); if (addMin < 1) { addMin = 1; } Calendar c = Calendar.getInstance(); c.add(Calendar.MINUTE, addMin); c.add(Calendar.SECOND, 0); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmMgr.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); return c.getTimeInMillis(); } public static int createScheduleNotification(Context context, Schedule s) { Log.w("UTILS", "createScheduleNotification()"); ScheduleDataSource dataSource; dataSource = new ScheduleDataSource(context); dataSource.open(); // Random r = new Random(); int mId = 0;// r.nextInt(); //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String uri = s.getRingtone(); // prefs.getString("pref_general_sound_key", ""); Uri ringtone = Uri.parse(uri); if (uri == "") { // isEmpty() is not available on API 7 ringtone = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION); } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(context.getResources().getString(R.string.app_name)) .setContentText(context.getResources().getString(R.string.notification)); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(context, MainActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(MainActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mBuilder.setSound(ringtone); //mBuilder.setWhen(s.getDate()); mNotificationManager.notify(mId, mBuilder.build()); s.setIsEnabled(false); dataSource.updateSchedule(context, s); dataSource.close(); return mId; } public static int safeLongToInt(long l) { if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) { throw new IllegalArgumentException(l + " cannot be cast to int without changing its value."); } return (int) l; } // Android 1.5 has no Build.VERSION.SDK_INT public static int getSdkInt() { if (Build.VERSION.RELEASE.startsWith("1.5")) return 3; try { Log.d("TAG", String.valueOf(SDKHelper.getSdkIntInternal())); return SDKHelper.getSdkIntInternal(); } catch (VerifyError e) { return 3; } } // Needed because Java will check if the class is valid. In Android 1.5, it will not. private static class SDKHelper { private static int getSdkIntInternal() { return Build.VERSION.SDK_INT; } } }