add version check condition to compare with API 23 before adding flag

This commit is contained in:
Rohit Verma 2024-08-23 15:14:27 +05:30
parent 9db025b5c1
commit b5b40aca42
2 changed files with 71 additions and 48 deletions

View file

@ -65,14 +65,11 @@ public class NotificationHelper {
.setPriority(PRIORITY_HIGH); .setPriority(PRIORITY_HIGH);
int flags = PendingIntent.FLAG_UPDATE_CURRENT; int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Check if the API level is 31 or higher to modify the flag flags |= PendingIntent.FLAG_IMMUTABLE; // This flag was introduced in API 23
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// For API level 31 or above, PendingIntent requires either FLAG_IMMUTABLE or FLAG_MUTABLE to be set
flags |= PendingIntent.FLAG_IMMUTABLE;
} }
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, flags); final PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, flags);
notificationBuilder.setContentIntent(pendingIntent); notificationBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notificationId, notificationBuilder.build()); notificationManager.notify(notificationId, notificationBuilder.build());
} }

View file

@ -41,17 +41,28 @@ import static android.content.Intent.ACTION_VIEW;
*/ */
public class PicOfDayAppWidget extends AppWidgetProvider { public class PicOfDayAppWidget extends AppWidgetProvider {
private CompositeDisposable compositeDisposable = new CompositeDisposable(); private final CompositeDisposable compositeDisposable = new CompositeDisposable();
@Inject @Inject
MediaClient mediaClient; MediaClient mediaClient;
void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { void updateAppWidget(
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.pic_of_day_app_widget); final Context context,
final AppWidgetManager appWidgetManager,
final int appWidgetId
) {
final RemoteViews views = new RemoteViews(
context.getPackageName(), R.layout.pic_of_day_app_widget);
// Launch App on Button Click // Launch App on Button Click
Intent viewIntent = new Intent(context, MainActivity.class); final Intent viewIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, viewIntent, PendingIntent.FLAG_IMMUTABLE); int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
flags |= PendingIntent.FLAG_IMMUTABLE;
}
final PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, viewIntent, flags);
views.setOnClickPendingIntent(R.id.camera_button, pendingIntent); views.setOnClickPendingIntent(R.id.camera_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
@ -60,61 +71,76 @@ public class PicOfDayAppWidget extends AppWidgetProvider {
/** /**
* Loads the picture of the day using media wiki API * Loads the picture of the day using media wiki API
* @param context * @param context The application context.
* @param views * @param views The RemoteViews object used to update the App Widget UI.
* @param appWidgetManager * @param appWidgetManager The AppWidgetManager instance for managing the widget.
* @param appWidgetId * @param appWidgetId he ID of the App Widget to update.
*/ */
private void loadPictureOfTheDay(Context context, private void loadPictureOfTheDay(
RemoteViews views, final Context context,
AppWidgetManager appWidgetManager, final RemoteViews views,
int appWidgetId) { final AppWidgetManager appWidgetManager,
final int appWidgetId
) {
compositeDisposable.add(mediaClient.getPictureOfTheDay() compositeDisposable.add(mediaClient.getPictureOfTheDay()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
response -> { response -> {
if (response != null) { if (response != null) {
views.setTextViewText(R.id.appwidget_title, response.getDisplayTitle()); views.setTextViewText(R.id.appwidget_title, response.getDisplayTitle());
// View in browser // View in browser
Intent viewIntent = new Intent(); final Intent viewIntent = new Intent();
viewIntent.setAction(ACTION_VIEW); viewIntent.setAction(ACTION_VIEW);
viewIntent.setData(Uri.parse(response.getPageTitle().getMobileUri())); viewIntent.setData(Uri.parse(response.getPageTitle().getMobileUri()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, viewIntent, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.appwidget_image, pendingIntent);
loadImageFromUrl(response.getThumbUrl(), context, views, appWidgetManager, appWidgetId); int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
flags |= PendingIntent.FLAG_IMMUTABLE;
} }
}, final PendingIntent pendingIntent = PendingIntent.getActivity(
t -> Timber.e(t, "Fetching picture of the day failed") context, 0, viewIntent, flags);
views.setOnClickPendingIntent(R.id.appwidget_image, pendingIntent);
loadImageFromUrl(response.getThumbUrl(),
context, views, appWidgetManager, appWidgetId);
}
},
t -> Timber.e(t, "Fetching picture of the day failed")
)); ));
} }
/** /**
* Uses Fresco to load an image from Url * Uses Fresco to load an image from Url
* @param imageUrl * @param imageUrl The URL of the image to load.
* @param context * @param context The application context.
* @param views * @param views The RemoteViews object used to update the App Widget UI.
* @param appWidgetManager * @param appWidgetManager The AppWidgetManager instance for managing the widget.
* @param appWidgetId * @param appWidgetId he ID of the App Widget to update.
*/ */
private void loadImageFromUrl(String imageUrl, private void loadImageFromUrl(
Context context, final String imageUrl,
RemoteViews views, final Context context,
AppWidgetManager appWidgetManager, final RemoteViews views,
int appWidgetId) { final AppWidgetManager appWidgetManager,
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageUrl)).build(); final int appWidgetId
ImagePipeline imagePipeline = Fresco.getImagePipeline(); ) {
DataSource<CloseableReference<CloseableImage>> dataSource final ImageRequest request = ImageRequestBuilder
= imagePipeline.fetchDecodedImage(request, context); .newBuilderWithSource(Uri.parse(imageUrl)).build();
final ImagePipeline imagePipeline = Fresco.getImagePipeline();
final DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline
.fetchDecodedImage(request, context);
dataSource.subscribe(new BaseBitmapDataSubscriber() { dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override @Override
protected void onNewResultImpl(@Nullable Bitmap tempBitmap) { protected void onNewResultImpl(@Nullable final Bitmap tempBitmap) {
Bitmap bitmap = null; Bitmap bitmap = null;
if (tempBitmap != null) { if (tempBitmap != null) {
bitmap = Bitmap.createBitmap(tempBitmap.getWidth(), tempBitmap.getHeight(), Bitmap.Config.ARGB_8888); bitmap = Bitmap.createBitmap(
Canvas canvas = new Canvas(bitmap); tempBitmap.getWidth(), tempBitmap.getHeight(), Bitmap.Config.ARGB_8888
);
final Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(tempBitmap, 0f, 0f, new Paint()); canvas.drawBitmap(tempBitmap, 0f, 0f, new Paint());
} }
views.setImageViewBitmap(R.id.appwidget_image, bitmap); views.setImageViewBitmap(R.id.appwidget_image, bitmap);