Skip to content

Commit 5bad185

Browse files
committed
Use context from mUsbReceiver instead of storing it in SdlReceiver
1 parent 6b65585 commit 5bad185

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class SdlReceiver extends SdlBroadcastReceiver {
2222

2323
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
2424
private PendingIntent pendingIntent;
25-
private WeakReference<Context> cachedContext;
2625
private Intent cachedIntent;
2726

2827
@Override
@@ -40,7 +39,6 @@ public void onSdlEnabled(Context context, Intent intent) {
4039
if (!AndroidTools.hasForegroundServiceTypePermission(context)) {
4140
requestUsbAccessory(context);
4241
cachedIntent = intent;
43-
cachedContext = new WeakReference<>(context);
4442
this.pendingIntent = pendingIntent;
4543
DebugTool.logInfo(TAG, "Permission missing for ForegroundServiceType connected device." + context);
4644
return;
@@ -83,10 +81,10 @@ public String getSdlServiceName() {
8381

8482
public void onReceive(Context context, Intent intent) {
8583
String action = intent.getAction();
86-
if (ACTION_USB_PERMISSION.equals(action) && cachedContext != null && cachedContext.get() != null && cachedIntent != null && pendingIntent != null) {
87-
if (AndroidTools.hasForegroundServiceTypePermission(cachedContext.get())) {
84+
if (ACTION_USB_PERMISSION.equals(action) && context != null && cachedIntent != null && pendingIntent != null) {
85+
if (AndroidTools.hasForegroundServiceTypePermission(context)) {
8886
try {
89-
pendingIntent.send(cachedContext.get(), 0, cachedIntent);
87+
pendingIntent.send(context, 0, cachedIntent);
9088
} catch (PendingIntent.CanceledException e) {
9189
e.printStackTrace();
9290
}

0 commit comments

Comments
 (0)