Skip to content

Commit 3534a76

Browse files
committed
Rework notification call as setLatestEventInfo got removed in API 23, and leads to compilation error.
Because we still support older SDK and to prevent using appcompat, it uses reflection to get the method. This prevent adding compatibility layer by default for all apps (about 100kb) Fixes kivy#864 Closes kivy#873
1 parent 274affa commit 3534a76

File tree

5 files changed

+120
-16
lines changed

5 files changed

+120
-16
lines changed

pythonforandroid/bootstraps/pygame/build/src/org/renpy/android/PythonService.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.renpy.android;
22

3+
import android.os.Build;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.InvocationTargetException;
36
import android.app.Service;
47
import android.os.IBinder;
58
import android.os.Bundle;
@@ -54,14 +57,31 @@ public int onStartCommand(Intent intent, int flags, int startId) {
5457
pythonThread = new Thread(this);
5558
pythonThread.start();
5659

60+
Notification notification;
5761
Context context = getApplicationContext();
58-
Notification notification = new Notification(context.getApplicationInfo().icon,
59-
serviceTitle,
60-
System.currentTimeMillis());
6162
Intent contextIntent = new Intent(context, PythonActivity.class);
6263
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
6364
PendingIntent.FLAG_UPDATE_CURRENT);
64-
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
65+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
66+
notification = new Notification(
67+
context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis());
68+
try {
69+
// prevent using NotificationCompat, this saves 100kb on apk
70+
Method func = notification.getClass().getMethod(
71+
"setLatestEventInfo", Context.class, CharSequence.class,
72+
CharSequence.class, PendingIntent.class);
73+
func.invoke(notification, context, serviceTitle, serviceDescription, pIntent);
74+
} catch (NoSuchMethodException | IllegalAccessException |
75+
IllegalArgumentException | InvocationTargetException e) {
76+
}
77+
} else {
78+
Notification.Builder builder = new Notification.Builder(context);
79+
builder.setContentTitle(serviceTitle);
80+
builder.setContentText(serviceDescription);
81+
builder.setContentIntent(pIntent);
82+
builder.setSmallIcon(context.getApplicationInfo().icon);
83+
notification = builder.build();
84+
}
6585
startForeground(1, notification);
6686

6787
return START_NOT_STICKY;

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonService.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.kivy.android;
22

3+
import android.os.Build;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.InvocationTargetException;
36
import android.app.Service;
47
import android.os.IBinder;
58
import android.os.Bundle;
@@ -88,13 +91,31 @@ protected void doStartForeground(Bundle extras) {
8891
String serviceTitle = extras.getString("serviceTitle");
8992
String serviceDescription = extras.getString("serviceDescription");
9093

94+
Notification notification;
9195
Context context = getApplicationContext();
92-
Notification notification = new Notification(context.getApplicationInfo().icon,
93-
serviceTitle, System.currentTimeMillis());
9496
Intent contextIntent = new Intent(context, PythonActivity.class);
9597
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
9698
PendingIntent.FLAG_UPDATE_CURRENT);
97-
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
99+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
100+
notification = new Notification(
101+
context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis());
102+
try {
103+
// prevent using NotificationCompat, this saves 100kb on apk
104+
Method func = notification.getClass().getMethod(
105+
"setLatestEventInfo", Context.class, CharSequence.class,
106+
CharSequence.class, PendingIntent.class);
107+
func.invoke(notification, context, serviceTitle, serviceDescription, pIntent);
108+
} catch (NoSuchMethodException | IllegalAccessException |
109+
IllegalArgumentException | InvocationTargetException e) {
110+
}
111+
} else {
112+
Notification.Builder builder = new Notification.Builder(context);
113+
builder.setContentTitle(serviceTitle);
114+
builder.setContentText(serviceDescription);
115+
builder.setContentIntent(pIntent);
116+
builder.setSmallIcon(context.getApplicationInfo().icon);
117+
notification = builder.build();
118+
}
98119
startForeground(1, notification);
99120
}
100121

pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package {{ args.package }};
22

3+
import android.os.Build;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.InvocationTargetException;
36
import android.content.Intent;
47
import android.content.Context;
58
import android.app.Notification;
@@ -26,13 +29,31 @@ public boolean canDisplayNotification() {
2629

2730
@Override
2831
protected void doStartForeground(Bundle extras) {
32+
Notification notification;
2933
Context context = getApplicationContext();
30-
Notification notification = new Notification(context.getApplicationInfo().icon,
31-
"{{ args.name }}", System.currentTimeMillis());
3234
Intent contextIntent = new Intent(context, PythonActivity.class);
3335
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
3436
PendingIntent.FLAG_UPDATE_CURRENT);
35-
notification.setLatestEventInfo(context, "{{ args.name }}", "{{ name| capitalize }}", pIntent);
37+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
38+
notification = new Notification(
39+
context.getApplicationInfo().icon, "{{ args.name }}", System.currentTimeMillis());
40+
try {
41+
// prevent using NotificationCompat, this saves 100kb on apk
42+
Method func = notification.getClass().getMethod(
43+
"setLatestEventInfo", Context.class, CharSequence.class,
44+
CharSequence.class, PendingIntent.class);
45+
func.invoke(notification, context, "{{ args.name }}", "{{ name| capitalize }}", pIntent);
46+
} catch (NoSuchMethodException | IllegalAccessException |
47+
IllegalArgumentException | InvocationTargetException e) {
48+
}
49+
} else {
50+
Notification.Builder builder = new Notification.Builder(context);
51+
builder.setContentTitle("{{ args.name }}");
52+
builder.setContentText("{{ name| capitalize }}");
53+
builder.setContentIntent(pIntent);
54+
builder.setSmallIcon(context.getApplicationInfo().icon);
55+
notification = builder.build();
56+
}
3657
startForeground({{ service_id }}, notification);
3758
}
3859

pythonforandroid/bootstraps/webview/build/src/org/kivy/android/PythonService.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.kivy.android;
22

3+
import android.os.Build;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.InvocationTargetException;
36
import android.app.Service;
47
import android.os.IBinder;
58
import android.os.Bundle;
@@ -87,13 +90,31 @@ protected void doStartForeground(Bundle extras) {
8790
String serviceTitle = extras.getString("serviceTitle");
8891
String serviceDescription = extras.getString("serviceDescription");
8992

93+
Notification notification;
9094
Context context = getApplicationContext();
91-
Notification notification = new Notification(context.getApplicationInfo().icon,
92-
serviceTitle, System.currentTimeMillis());
9395
Intent contextIntent = new Intent(context, PythonActivity.class);
9496
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
9597
PendingIntent.FLAG_UPDATE_CURRENT);
96-
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
98+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
99+
notification = new Notification(
100+
context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis());
101+
try {
102+
// prevent using NotificationCompat, this saves 100kb on apk
103+
Method func = notification.getClass().getMethod(
104+
"setLatestEventInfo", Context.class, CharSequence.class,
105+
CharSequence.class, PendingIntent.class);
106+
func.invoke(notification, context, serviceTitle, serviceDescription, pIntent);
107+
} catch (NoSuchMethodException | IllegalAccessException |
108+
IllegalArgumentException | InvocationTargetException e) {
109+
}
110+
} else {
111+
Notification.Builder builder = new Notification.Builder(context);
112+
builder.setContentTitle(serviceTitle);
113+
builder.setContentText(serviceDescription);
114+
builder.setContentIntent(pIntent);
115+
builder.setSmallIcon(context.getApplicationInfo().icon);
116+
notification = builder.build();
117+
}
97118
startForeground(1, notification);
98119
}
99120

pythonforandroid/bootstraps/webview/build/templates/Service.tmpl.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package {{ args.package }};
22

3+
import android.os.Build;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.InvocationTargetException;
36
import android.content.Intent;
47
import android.content.Context;
58
import android.app.Notification;
@@ -26,13 +29,31 @@ public boolean canDisplayNotification() {
2629

2730
@Override
2831
protected void doStartForeground(Bundle extras) {
32+
Notification notification;
2933
Context context = getApplicationContext();
30-
Notification notification = new Notification(context.getApplicationInfo().icon,
31-
"{{ args.name }}", System.currentTimeMillis());
3234
Intent contextIntent = new Intent(context, PythonActivity.class);
3335
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
3436
PendingIntent.FLAG_UPDATE_CURRENT);
35-
notification.setLatestEventInfo(context, "{{ args.name }}", "{{ name| capitalize }}", pIntent);
37+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
38+
notification = new Notification(
39+
context.getApplicationInfo().icon, "{{ args.name }}", System.currentTimeMillis());
40+
try {
41+
// prevent using NotificationCompat, this saves 100kb on apk
42+
Method func = notification.getClass().getMethod(
43+
"setLatestEventInfo", Context.class, CharSequence.class,
44+
CharSequence.class, PendingIntent.class);
45+
func.invoke(notification, context, "{{ args.name }}", "{{ name| capitalize }}", pIntent);
46+
} catch (NoSuchMethodException | IllegalAccessException |
47+
IllegalArgumentException | InvocationTargetException e) {
48+
}
49+
} else {
50+
Notification.Builder builder = new Notification.Builder(context);
51+
builder.setContentTitle("{{ args.name }}");
52+
builder.setContentText("{{ name| capitalize }}");
53+
builder.setContentIntent(pIntent);
54+
builder.setSmallIcon(context.getApplicationInfo().icon);
55+
notification = builder.build();
56+
}
3657
startForeground({{ service_id }}, notification);
3758
}
3859

0 commit comments

Comments
 (0)