Skip to content

Commit 889e57a

Browse files
author
jsdario
committed
Compatible with Android 8
1 parent a9625b9 commit 889e57a

File tree

7 files changed

+49
-49
lines changed

7 files changed

+49
-49
lines changed

android/app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ def enableSeparateBuildPerCPUArchitecture = false
9494
def enableProguardInReleaseBuilds = false
9595

9696
android {
97-
compileSdkVersion 28
98-
buildToolsVersion "28.0.0"
97+
compileSdkVersion rootProject.ext.compileSdkVersion
98+
buildToolsVersion rootProject.ext.buildToolsVersion
9999

100100
defaultConfig {
101101
applicationId "com.androidwidgetpoc"
102-
minSdkVersion 16
103-
targetSdkVersion 28
102+
minSdkVersion rootProject.ext.minSdkVersion
103+
targetSdkVersion rootProject.ext.targetSdkVersion
104104
versionCode 1
105105
versionName "1.0"
106106
ndk {
@@ -138,8 +138,8 @@ android {
138138

139139
dependencies {
140140
implementation fileTree(dir: "libs", include: ["*.jar"])
141-
implementation "com.android.support:appcompat-v7:23.0.1"
142-
implementation 'com.android.support:support-v4:23.2.0' // v4
141+
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
142+
implementation "com.android.support:support-v4:${rootProject.ext.supportLibVersion}" // v4
143143
implementation "com.facebook.react:react-native:+" // From node_modules
144144
}
145145

android/app/src/main/java/com/androidwidgetpoc/BackgroundTask.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,39 @@
1818
public class BackgroundTask extends HeadlessJsTaskService {
1919

2020
private final int NOTIFICATION_ID = 12345678;
21-
private final String CHANNEL_ID = "channel_id";
21+
private final String CHANNEL_ID = "headless_task_channel";
2222

2323
@Override
2424
protected @Nullable
2525
HeadlessJsTaskConfig getTaskConfig(Intent intent) {
26+
startForeground(NOTIFICATION_ID, getNotification());
27+
28+
Bundle extras = intent.getExtras();
29+
return new HeadlessJsTaskConfig(
30+
"WidgetTask",
31+
extras != null ? Arguments.fromBundle(extras) : null,
32+
10000);
33+
}
34+
35+
private Notification getNotification() {
2636
NotificationManager notificationManager = getSystemService(NotificationManager.class);
27-
/*
28-
* This is run inside onStartCommand
29-
* */
37+
3038
// Android O requires a Notification Channel.
3139
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
3240
CharSequence name = "channel name";
3341
// Create the channel for the notification
3442
NotificationChannel mChannel =
3543
new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
3644

37-
// Set the Notification Channel for the Notification Manager.
3845
notificationManager.createNotificationChannel(mChannel);
39-
// notificationManager.startServiceInForeground(intent, NOTIFICATION_ID, getNotification());
40-
startForegroundService(NOTIFICATION_ID, getNotification());
41-
} else {
42-
startForeground(NOTIFICATION_ID, getNotification());
4346
}
4447

45-
Bundle extras = intent.getExtras();
46-
return new HeadlessJsTaskConfig(
47-
"WidgetTask",
48-
extras != null ? Arguments.fromBundle(extras) : null,
49-
10000);
50-
}
51-
52-
private Notification getNotification() {
53-
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
54-
.setContentText("Some text")
55-
.setContentTitle("Some title")
56-
.setOngoing(true)
57-
.setPriority(Notification.PRIORITY_HIGH)
48+
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
49+
.setContentText("Your JS code is going to be run in background")
50+
.setContentTitle("Launching HeadlessJsTaskService")
5851
.setSmallIcon(R.mipmap.ic_launcher)
59-
.setTicker("Some title")
6052
.setWhen(System.currentTimeMillis());
6153

62-
// Set the Channel ID for Android O.
63-
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
64-
// builder.setChannelId(CHANNEL_ID); // Channel ID
65-
// }
66-
6754
return builder.build();
6855
}
6956
}

android/app/src/main/java/com/androidwidgetpoc/BackgroundTaskBridge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private void updateView(RemoteViews widgetView, ReadableMap charm, Integer layou
9696
}
9797

9898
private void registerTask(String action, ReadableMap charm, RemoteViews widgetView, Integer button) {
99-
Intent intent = new Intent();
99+
Intent intent = new Intent(getReactApplicationContext(), WidgetProvider.class);
100100
intent.putExtra("id", charm.getString("id"));
101101
intent.setAction(action);
102102
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getReactApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

android/app/src/main/java/com/androidwidgetpoc/WidgetProvider.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.appwidget.AppWidgetProvider;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.os.Build;
67
import android.util.Log;
78

89
import com.facebook.react.HeadlessJsTaskService;
@@ -17,9 +18,8 @@ public class WidgetProvider extends AppWidgetProvider {
1718

1819
@Override
1920
public void onEnabled(Context context) {
20-
Log.d("WIDGET_PROVIDER", "En onEnabled");
21-
Intent serviceIntent = new Intent(context, BackgroundTask.class);
22-
context.startService(serviceIntent);
21+
Intent intent = new Intent(context, BackgroundTask.class);
22+
startServiceAfterSdkVersion(context, intent);
2323
HeadlessJsTaskService.acquireWakeLockNow(context);
2424
}
2525

@@ -36,6 +36,14 @@ public void onReceive(final Context context, final Intent incomingIntent) {
3636
* */
3737
Intent serviceIntent = new Intent(context, BackgroundTask.class);
3838
serviceIntent.putExtras(incomingIntent);
39-
context.startService(serviceIntent);
39+
startServiceAfterSdkVersion(context, serviceIntent);
40+
}
41+
42+
public void startServiceAfterSdkVersion (Context context, Intent intent) {
43+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
44+
context.startForegroundService(intent);
45+
} else {
46+
context.startService(intent);
47+
}
4048
}
4149
}

android/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,16 @@ allprojects {
2121
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
2222
url "$rootDir/../node_modules/react-native/android"
2323
}
24+
google()
2425
}
2526
}
27+
28+
29+
ext {
30+
compileSdkVersion = 27
31+
minSdkVersion = 16
32+
targetSdkVersion = 27
33+
buildToolsVersion = "27.0.3"
34+
supportLibVersion = "27.1.0"
35+
googlePlayServicesVersion = "11.8.0"
36+
}

jsconfig.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

widgetTask.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ type TaskInfo = {
2424
}
2525
export default function widgetTask (taskData: TaskInfo) {
2626
synchronizeWidget()
27-
triggerCharm(taskData.id)
27+
if (taskData && taskData.id) {
28+
triggerCharm(taskData.id)
29+
}
2830

2931
return new Promise(resolve => {
3032
setTimeout(() => {
3133
ToastAndroid.show(`FINISHING 🚬 ...`, ToastAndroid.SHORT);
34+
synchronizeWidget()
3235
resolve()
3336
}, 8000)
3437
})

0 commit comments

Comments
 (0)