Skip to content

Commit

Permalink
Merge pull request #883 from gponsinet/fix-877
Browse files Browse the repository at this point in the history
fix(android): regressions, permissions & safe react context
  • Loading branch information
Godefroy Ponsinet committed Jan 17, 2019
2 parents 10375b3 + 423156c commit c1e934d
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

<service android:name="chat.berty.core.Notification$FCM">
<service android:name="chat.berty.core.notification.NotificationHandler">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;
import chat.berty.core.notification.NotificationNative;
import core.Core;
import core.MobileNotification;
import core.NativeNotificationDriver;

import chat.berty.ble.BleManager;

Expand All @@ -22,13 +22,13 @@ public class CoreModule extends ReactContextBaseJavaModule {
public CoreModule(ReactApplicationContext reactContext) {
super(reactContext);
this.filesDir = reactContext.getFilesDir().getAbsolutePath();
this.reactContext = reactContext;

// TODO: Get rid of this and make a proper react-native module that extends ReactContextBaseJavaModule
// See https://facebook.github.io/react-native/docs/native-modules-android
Object activityAndContextGetter = actGetter(reactContext);

BleManager.setReactGetter(activityAndContextGetter, reactContext);
this.notificationDriver.setNative(new NotificationNative());
}

private Object actGetter(final ReactApplicationContext reactContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import java.util.Collections;
import java.util.List;

import chat.berty.core.notification.NotificationModule;

public class CorePackage implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new CoreModule(reactContext));
modules.add(new NotificationModule(reactContext));

modules.add(NotificationModule.getInstance(reactContext));
return modules;
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package chat.berty.core;
package chat.berty.core.notification;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -15,17 +18,12 @@
import chat.berty.main.MainActivity;
import chat.berty.main.R;

public class DisplayNotification extends AsyncTask {
public class NotificationDisplay extends AsyncTask {
private static final String TAG = "DisplayNotification";
private static final String CHANNEL_ID = "berty-chat-android-notification-id";
private static final String CHANNEL_NAME = "berty.chat.android.core.notification.name";
private static final String CHANNEL_DESCRIPTION = "berty.chat.android.core.notification.description";

private final WeakReference<Context> contextWeakReference;
private final WeakReference<ReactApplicationContext> reactContextWeakReference;

private final android.app.NotificationManager notificationManager;

private final String title;

/**
Expand All @@ -41,8 +39,6 @@ public class DisplayNotification extends AsyncTask {
*/
@Override
protected void onPostExecute(Object o) {
contextWeakReference.clear();
reactContextWeakReference.clear();
super.onPostExecute(o);
}

Expand All @@ -51,18 +47,33 @@ protected void onPostExecute(Object o) {
private final String url;
private final String sound;

DisplayNotification(Context context, ReactApplicationContext reactContext, android.app.NotificationManager notificationManager,
String title, String body, String icon, String sound, String url) {
this.contextWeakReference = new WeakReference<>(context);
this.reactContextWeakReference = new WeakReference<>(reactContext);
this.notificationManager = notificationManager;
NotificationDisplay(String title, String body, String icon, String sound, String url) {
this.createNotificationChannel();
this.title = title;
this.body = body;
this.icon = icon;
this.sound = sound;
this.url = url;
}

private void createNotificationChannel() {
if (!NotificationModule.isInstantiated()) { return; }

Context context = NotificationModule.getInstance().getReactApplicationContext().getApplicationContext();
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = CHANNEL_NAME;
String description = CHANNEL_DESCRIPTION;
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}

/**
* Override this method to perform a computation on a background thread. The
Expand All @@ -80,9 +91,9 @@ protected void onPostExecute(Object o) {
*/
@Override
protected Object doInBackground(Object[] objects) {
Context context = contextWeakReference.get();
if (context == null) return null;
if (!NotificationModule.isInstantiated()) { return null; }

Context context = NotificationModule.getInstance().getReactApplicationContext().getApplicationContext();

int m = new Random().nextInt(6) + 5;
String notificationID = Integer.toString(m);
Expand All @@ -106,7 +117,7 @@ protected Object doInBackground(Object[] objects) {
.setVibrate(new long[]{0, 1000})
.setContentIntent(pendingIntent);

notificationManager.notify(m, mBuilder.build());
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(m, mBuilder.build());
return null;
}
}

0 comments on commit c1e934d

Please sign in to comment.