Skip to content

Commit

Permalink
Improved pending intent handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lubenard committed Apr 6, 2022
1 parent 53b7ccc commit 48c9e7b
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.lubenard.oring_reminder;

import static org.junit.Assert.assertEquals;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lubenard.oring_reminder;

import static android.os.Build.VERSION.SDK_INT;
import static com.lubenard.oring_reminder.utils.Utils.getIntentMutableFlag;

import android.app.Activity;
import android.app.AlarmManager;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
// Set the 'Add break' button to visible
remoteViews.setViewVisibility(R.id.widget_button_start_stop_break_session, View.VISIBLE);

PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, intent3, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, intent3, getIntentMutableFlag());
remoteViews.setOnClickPendingIntent(R.id.widget_button_start_stop_break_session, pendingIntent3);

int totalTimePause = AfterBootBroadcastReceiver.computeTotalTimePause(dbManager, lastEntry.getId());
Expand Down Expand Up @@ -134,7 +135,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
Intent intent2 = new Intent(context, getClass());
intent2.setAction(WIDGET_BUTTON_STOP);

PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, getIntentMutableFlag());
remoteViews.setOnClickPendingIntent(R.id.widget_button_new_stop_session, pendingIntent2);

intent.putExtra("switchToEntry", lastEntry.getId());
Expand All @@ -151,11 +152,11 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
// Action if user click on the button
Intent intent2 = new Intent(context, getClass());
intent2.setAction(WIDGET_BUTTON_START);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, getIntentMutableFlag());
remoteViews.setOnClickPendingIntent(R.id.widget_button_new_stop_session, pendingIntent2 );
}

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, getIntentMutableFlag());
remoteViews.setOnClickPendingIntent(R.id.widget_root_view, pendingIntent);

// Update the widget view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
Expand All @@ -12,10 +11,8 @@
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.util.Log;
import android.view.MenuItem;

Expand All @@ -30,7 +27,6 @@
import com.lubenard.oring_reminder.ui.MainFragment;
import com.lubenard.oring_reminder.utils.Utils;

import java.util.Arrays;
import java.util.concurrent.Callable;

public class MainActivity extends AppCompatActivity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class ShortcutManager extends BroadcastReceiver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Toast;

import androidx.preference.EditTextPreferenceDialogFragmentCompat;

import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.ui.EditEntryFragment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lubenard.oring_reminder.ui;

import static android.os.Build.VERSION.SDK_INT;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
Expand Down Expand Up @@ -35,8 +37,8 @@
import com.lubenard.oring_reminder.CurrentSessionWidgetProvider;
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.broadcast_receivers.NotificationSenderBroadcastReceiver;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.broadcast_receivers.NotificationSenderBroadcastReceiver;
import com.lubenard.oring_reminder.custom_components.RingSession;
import com.lubenard.oring_reminder.utils.Utils;

Expand All @@ -46,8 +48,6 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static android.os.Build.VERSION.SDK_INT;

public class EditEntryFragment extends Fragment {

public static final String TAG = "EditEntryFragment";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lubenard.oring_reminder.ui;

import static android.os.Build.VERSION.SDK_INT;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
Expand All @@ -19,7 +21,6 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -33,9 +34,9 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.broadcast_receivers.AfterBootBroadcastReceiver;
import com.lubenard.oring_reminder.broadcast_receivers.NotificationSenderBreaksBroadcastReceiver;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.custom_components.RingSession;
import com.lubenard.oring_reminder.utils.Utils;

Expand All @@ -44,8 +45,6 @@
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static android.os.Build.VERSION.SDK_INT;

public class EntryDetailsFragment extends Fragment {

public static final String TAG = "EntryDetailsFragment";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@

import android.content.Context;
import android.os.Bundle;

import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.ui.adapters.HistoryListAdapter;
import com.lubenard.oring_reminder.custom_components.RingSession;
import com.lubenard.oring_reminder.utils.Utils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup;
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.custom_components.RingSession;
import com.lubenard.oring_reminder.ui.adapters.HistoryListAdapter;
import com.lubenard.oring_reminder.utils.Utils;

import java.util.ArrayList;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.ui.adapters.CustomSpermoListAdapter;
import com.lubenard.oring_reminder.custom_components.Spermograms;
import com.lubenard.oring_reminder.ui.adapters.CustomSpermoListAdapter;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.ui.adapters.CustomListSearchAdapter;
import com.lubenard.oring_reminder.custom_components.RingSession;
import com.lubenard.oring_reminder.ui.adapters.CustomListSearchAdapter;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@
import android.widget.TimePicker;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.lubenard.oring_reminder.BackupRestore;
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.broadcast_receivers.NotificationSenderBroadcastReceiver;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.broadcast_receivers.NotificationSenderBroadcastReceiver;
import com.lubenard.oring_reminder.utils.Utils;

import java.util.Calendar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.lubenard.oring_reminder.ui;

import android.Manifest;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
Expand All @@ -22,15 +19,12 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;

import com.github.barteksc.pdfviewer.PDFView;
import com.lubenard.oring_reminder.BackupRestore;
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.custom_components.Spermograms;
import com.lubenard.oring_reminder.utils.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import com.lubenard.oring_reminder.DbManager;
import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.ui.viewHolders.SpermoListViewHolder;
import com.lubenard.oring_reminder.custom_components.Spermograms;
import com.lubenard.oring_reminder.ui.viewHolders.SpermoListViewHolder;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import androidx.recyclerview.widget.RecyclerView;

import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.ui.viewHolders.HistoryViewHolder;
import com.lubenard.oring_reminder.custom_components.RingSession;
import com.lubenard.oring_reminder.ui.viewHolders.HistoryViewHolder;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import androidx.recyclerview.widget.RecyclerView;

import com.lubenard.oring_reminder.R;

import com.lubenard.oring_reminder.custom_components.Spermograms;
import com.lubenard.oring_reminder.ui.MySpermogramsFragment;
import com.lubenard.oring_reminder.ui.adapters.CustomSpermoListAdapter;
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/com/lubenard/oring_reminder/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.NotificationCompat;

import com.lubenard.oring_reminder.MainActivity;
import com.lubenard.oring_reminder.broadcast_receivers.NotificationReceiverBroadcastReceiver;
import com.lubenard.oring_reminder.R;
import com.lubenard.oring_reminder.broadcast_receivers.NotificationReceiverBroadcastReceiver;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -207,21 +207,21 @@ public static void sendNotification(Context context, String title, String conten
*/
public static void sendNotificationWithQuickAnswer(Context context, String title, String content, int drawable, long entryId) {
// First let's create the intent
PendingIntent pi = PendingIntent.getActivity(context, 1, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
PendingIntent pi = PendingIntent.getActivity(context, 1, new Intent(context, MainActivity.class), getIntentMutableFlag());

//Pending intent for a notification button when user removed protection
PendingIntent removedProtection =
PendingIntent.getBroadcast(context, 1, new Intent(context, NotificationReceiverBroadcastReceiver.class)
.putExtra("action", 1)
.putExtra("entryId", entryId),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
getIntentMutableFlag());

//Pending intent for a notification button when user dismissed notification
PendingIntent dismissedNotif =
PendingIntent.getBroadcast(context, 2, new Intent(context, NotificationReceiverBroadcastReceiver.class)
.putExtra("action", 0)
.putExtra("entryId", entryId),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
getIntentMutableFlag());

// Get the notification manager and build it
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Expand All @@ -234,4 +234,13 @@ public static void sendNotificationWithQuickAnswer(Context context, String title
.setContentIntent(pi);
mNotificationManager.notify(0, permNotifBuilder.build());
}

/**
* @return return the corresponding pending intent flag according to android version
*/
public static int getIntentMutableFlag() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
return PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE;
return PendingIntent.FLAG_UPDATE_CURRENT;
}
}
1 change: 0 additions & 1 deletion app/src/main/res/layout/debug_menu.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.lubenard.oring_reminder;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

import static org.junit.Assert.*;
import org.junit.Test;

/**
* Example local unit test, which will execute on the development machine (host).
Expand Down

0 comments on commit 48c9e7b

Please sign in to comment.