Skip to content

Commit

Permalink
fix: crash on android 6 when clickAction is used
Browse files Browse the repository at this point in the history
  • Loading branch information
sAleksovski committed May 12, 2024
1 parent 7696c9a commit 2caffa1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix crash on Android 6 when `clickAction` is present in the widget
- `clickAction` now works only from Android 7 and up

## [0.13.1] - 2024-04-27

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import android.graphics.Canvas;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RemoteViews;

import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
Expand Down Expand Up @@ -69,7 +72,9 @@ public void drawWidget(int widgetId) throws Exception {
Bitmap bitmap = drawViewToBitmap(widgetWithViews.getRootView());
remoteWidgetView.setImageViewBitmap(R.id.rn_widget_image, bitmap);

addClickableAreas(widgetId, remoteWidgetView, widgetWithViews);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
addClickableAreas(widgetId, remoteWidgetView, widgetWithViews);
}
addCollectionViews(widgetId, remoteWidgetView, widgetWithViews);

AppWidgetManager.getInstance(appContext)
Expand Down Expand Up @@ -101,6 +106,7 @@ private Bitmap drawViewToBitmap(View rootView) {
return bitmap;
}

@RequiresApi(Build.VERSION_CODES.N)
private void addClickableAreas(int widgetId, RemoteViews remoteWidgetView, WidgetWithViews widgetWithViews) {
remoteWidgetView.removeAllViews(R.id.rn_widget_clickable_container);
ViewGroup rootView = (ViewGroup) widgetWithViews.getRootView();
Expand All @@ -111,6 +117,7 @@ private void addClickableAreas(int widgetId, RemoteViews remoteWidgetView, Widge
}
}

@RequiresApi(Build.VERSION_CODES.N)
private void addClickableArea(RemoteViews widgetView, ViewGroup rootWidget, ClickableView clickableView, int widgetId) {
View clickableWidget = clickableView.getView();
Rect offsetViewBounds = new Rect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.ReactApplicationContext;
import com.reactnativeandroidwidget.builder.CollectionViewItem;

Expand Down Expand Up @@ -135,13 +138,16 @@ public RemoteViews getViewAt(int position) {

ArrayList<Bundle> clickableAreas = bundle.getParcelableArrayList("clickableAreas");

for (Bundle clickableArea : clickableAreas) {
addClickableArea(listItemView, clickableArea, bitmapAt.getWidth());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
for (Bundle clickableArea : clickableAreas) {
addClickableArea(listItemView, clickableArea, bitmapAt.getWidth());
}
}

return listItemView;
}

@RequiresApi(Build.VERSION_CODES.N)
private void addClickableArea(RemoteViews widgetView, Bundle clickableArea, int imageWidth) {
RemoteViews clickableRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.rn_widget_clickable);

Expand Down
6 changes: 6 additions & 0 deletions docs/docs/handling-clicks.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ All widget [primitives](./primitives/index) can be clicked, and execute some cod

To add a click action to `FlexWidget`, we need to pass a `clickAction` prop, and an optional `clickActionData` prop. `clickActionData` can be used to add additional data that we need.

:::warning

`clickAction` only works on Android 7 and up

:::

```tsx
import { FlexWidget } from 'react-native-android-widget';

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/primitives/overlap-widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 2

# OverlapWidget

Widget container that lays out child widgets on on top of the other.
Widget container that lays out child widgets one on top of the other.

The child widgets can be positioned using margins.

Expand Down

0 comments on commit 2caffa1

Please sign in to comment.