SHARE
TWEET

alarmreciver

a guest Nov 11th, 2019 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.submission1.view.main;
  2.  
  3. import android.app.AlarmManager;
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.os.Build;
  12. import android.util.Log;
  13. import android.widget.Toast;
  14.  
  15. import androidx.core.app.NotificationCompat;
  16.  
  17. import com.example.submission1.BuildConfig;
  18. import com.example.submission1.R;
  19. import com.example.submission1.model.MovieModel;
  20. import com.example.submission1.rest.ApiUtil;
  21. import com.example.submission1.rest.response.MovieListResponse;
  22. import com.example.submission1.view.detail.DetailMovieActivity;
  23.  
  24. import java.text.DateFormat;
  25. import java.text.ParseException;
  26. import java.text.SimpleDateFormat;
  27. import java.util.ArrayList;
  28. import java.util.Calendar;
  29. import java.util.Locale;
  30.  
  31. import retrofit2.Call;
  32. import retrofit2.Callback;
  33. import retrofit2.Response;
  34.  
  35. public class AlarmReceiver extends BroadcastReceiver {
  36.  
  37.     private static final String EXTRA_TYPE = "type";
  38.     private static final String CHANNEL_ID_DAILY = "channel_1";
  39.     private static final CharSequence CHANNEL_NAME_DAILY = "Dialy channel";
  40.     private static final int NOTIFICATION_ID_DAILY = 2;
  41.     private final static String TIME_FORMAT = "HH:mm";
  42.  
  43.     private static int notification_request_code2 = 200;
  44.     private int idNotification = 0;
  45.  
  46.     @Override
  47.     public void onReceive(Context context, Intent intent) {
  48.         int type = intent.getIntExtra(EXTRA_TYPE,MainActivity.ID_REPEATING);
  49.         if (type==MainActivity.ID_REPEATING){ dailyReminder(context); }
  50.         if (type==MainActivity.ID_REPEATING2){ getMovieTodayRelease(context);}
  51.     }
  52.  
  53.     private String getTodayDate(){
  54.         Calendar calendar = Calendar.getInstance();
  55.         int thisYear = calendar.get(Calendar.YEAR);
  56.         int thisMonth = calendar.get(Calendar.MONTH) + 1;
  57.         int thisDate = calendar.get(Calendar.DATE);
  58.         return thisYear + "-" + thisMonth + "-" + thisDate;
  59.     }
  60.     private void getMovieTodayRelease(final Context context) {
  61.         String today_date = getTodayDate();
  62.         ApiUtil.getRequest().getTodayRelease(BuildConfig.Api_Key,today_date,today_date).enqueue(new Callback<MovieListResponse>() {
  63.             @Override
  64.             public void onResponse(Call<MovieListResponse> call, Response<MovieListResponse> response) {
  65.                 if (response.isSuccessful()){
  66.                     if (response.body().results!=null && response.body().results.size()!=0){
  67.                         makeNotif(context,response.body().results);
  68.                     }
  69.                 }
  70.             }
  71.  
  72.             @Override
  73.             public void onFailure(Call<MovieListResponse> call, Throwable t) {
  74.                 Log.d("released_today", t.getMessage());
  75.             }
  76.         });
  77.  
  78.     }
  79.  
  80.     private void makeNotif(Context context, ArrayList<MovieModel> results) {
  81.         for (MovieModel movie : results){
  82.  
  83.             Intent intent = new Intent(context, DetailMovieActivity.class);
  84.             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  85.             intent.putExtra("movie_id", movie.id);
  86.             intent.putExtra("type", "movie");
  87.             PendingIntent pendingIntent = PendingIntent.getActivity(context, notification_request_code2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  88.             NotificationCompat.Builder mBuilder;
  89.             NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  90.             mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID_DAILY)
  91.                     .setContentTitle(movie.original_title)
  92.                     .setContentText(movie.original_title + " " + context.getResources().getString(R.string.released_today))
  93.                     .setSmallIcon(R.drawable.ic_notification_black)
  94.                     .setContentIntent(pendingIntent)
  95.                     .setAutoCancel(true);
  96.  
  97.  
  98.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  99.  
  100.  
  101.                 NotificationChannel channel = new NotificationChannel(CHANNEL_ID_DAILY, CHANNEL_NAME_DAILY, NotificationManager.IMPORTANCE_DEFAULT);
  102.  
  103.                 mBuilder.setChannelId(CHANNEL_ID_DAILY);
  104.  
  105.                 if (mNotificationManager != null) {
  106.                     mNotificationManager.createNotificationChannel(channel);
  107.                 }
  108.             }
  109.  
  110.             Notification notification = mBuilder.build();
  111.  
  112.             if (mNotificationManager != null) {
  113.                 mNotificationManager.notify(idNotification, notification);
  114.             }
  115.         }
  116.     }
  117.  
  118.     public void dailyReminder(Context context) {
  119.         Intent intent = new Intent(context, MainActivity.class);
  120.         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  121.         String title= context.getResources().getString(R.string.app_name);
  122.         String text= context.getResources().getString(R.string.dialy_notif_subtext);
  123.         NotificationCompat.Builder mBuilder;
  124.         NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  125.         mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID_DAILY)
  126.                 .setContentTitle(title)
  127.                 .setContentText(text)
  128.                 .setSmallIcon(R.drawable.ic_notification_black)
  129.                 .setContentIntent(pendingIntent)
  130.                 .setAutoCancel(true);
  131.  
  132.  
  133.  
  134.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  135.  
  136.             /* Create or update. */
  137.             NotificationChannel channel = new NotificationChannel(CHANNEL_ID_DAILY, CHANNEL_NAME_DAILY, NotificationManager.IMPORTANCE_DEFAULT);
  138.  
  139.             mBuilder.setChannelId(CHANNEL_ID_DAILY);
  140.  
  141.             if (mNotificationManager != null) {
  142.                 mNotificationManager.createNotificationChannel(channel);
  143.             }
  144.         }
  145.  
  146.         Notification notification = mBuilder.build();
  147.  
  148.         if (mNotificationManager != null) {
  149.             mNotificationManager.notify(NOTIFICATION_ID_DAILY, notification);
  150.         }
  151.     }
  152.  
  153.     public void setRepeatingAlarm(Context context, int id_alarm,String time,String msg) {
  154.  
  155.         if (isDateInvalid(time, TIME_FORMAT)) return;
  156.  
  157.         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  158.         Intent intent = new Intent(context, AlarmReceiver.class);
  159.         intent.putExtra(EXTRA_TYPE, id_alarm);
  160.  
  161.         String[] timeArray = time.split(":");
  162.  
  163.         Calendar calendar = Calendar.getInstance();
  164.         calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeArray[0]));
  165.         calendar.set(Calendar.MINUTE, Integer.parseInt(timeArray[1]));
  166.         calendar.set(Calendar.SECOND, 0);
  167.  
  168.         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id_alarm, intent, 0);
  169.         if (alarmManager != null) {
  170.             alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
  171.         }
  172.  
  173.         if (msg!=null)
  174.             Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
  175.     }
  176.  
  177.     public void cancelAlarm(Context context, int id_alarm, String msg) {
  178.         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  179.         Intent intent = new Intent(context, AlarmReceiver.class);
  180.         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id_alarm, intent, 0);
  181.         pendingIntent.cancel();
  182.  
  183.         if (alarmManager != null)
  184.             alarmManager.cancel(pendingIntent);
  185.  
  186.         if (msg!=null)
  187.             Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
  188.     }
  189.  
  190.     private boolean isDateInvalid(String date, String format) {
  191.         try {
  192.             DateFormat df = new SimpleDateFormat(format, Locale.getDefault());
  193.             df.setLenient(false);
  194.             df.parse(date);
  195.             return false;
  196.         } catch (ParseException e) {
  197.             return true;
  198.         }
  199.     }
  200.  
  201.     public boolean isAlarmSet(Context context, int requestCode) {
  202.         Intent intent = new Intent(context, AlarmReceiver.class);
  203.         return PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_NO_CREATE) != null;
  204.     }
  205.  
  206. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top