Skip to content

Commit

Permalink
service_only bootstrap now works with android (7 and 8 tested) (#1725)
Browse files Browse the repository at this point in the history
service_only bootstrap now works with android (7 and 8 tested)
  • Loading branch information
zworkb authored and AndreMiras committed Jun 6, 2019
1 parent 6d038fa commit 1d375fb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import org.renpy.android.Hardware;

//imports for channel definition
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.graphics.Color;

public class PythonService extends Service implements Runnable {

Expand Down Expand Up @@ -90,13 +94,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
protected void doStartForeground(Bundle extras) {
String serviceTitle = extras.getString("serviceTitle");
String serviceDescription = extras.getString("serviceDescription");

Notification notification;
Context context = getApplicationContext();
Intent contextIntent = new Intent(context, PythonActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
notification = new Notification(
context.getApplicationInfo().icon, serviceTitle, System.currentTimeMillis());
try {
Expand All @@ -109,7 +113,19 @@ protected void doStartForeground(Bundle extras) {
IllegalArgumentException | InvocationTargetException e) {
}
} else {
Notification.Builder builder = new Notification.Builder(context);
// for android 8+ we need to create our own channel
// https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a"; //TODO: make this configurable
String channelName = "PythonSerice"; //TODO: make this configurable
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName,
NotificationManager.IMPORTANCE_NONE);

chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(chan);

Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID);
builder.setContentTitle(serviceTitle);
builder.setContentText(serviceDescription);
builder.setContentIntent(pIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@

{% if service %}
<service android:name="org.kivy.android.PythonService"
android:process=":pythonservice" />
android:process=":pythonservice"
android:exported="true"/>
{% endif %}
{% for name in service_names %}
<service android:name="{{ args.package }}.Service{{ name|capitalize }}"
android:process=":service_{{ name }}" />
android:process=":service_{{ name }}"
android:exported="true" />
{% endfor %}

{% if args.billing_pubkey %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Service{{ name|capitalize }} extends PythonService {
* {@inheritDoc}
*/
@Override
public int getStartType() {
public int startType() {
return START_STICKY;
}
{% endif %}
Expand Down
5 changes: 3 additions & 2 deletions pythonforandroid/recipes/android/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def prebuild_arch(self, arch):
bootstrap = bootstrap_name = ctx_bootstrap

is_sdl2 = bootstrap_name in ('sdl2', 'sdl2python3', 'sdl2_gradle')
is_webview = bootstrap_name in ('webview',)
is_webview = bootstrap_name == 'webview'
is_service_only = bootstrap_name == 'service_only'

if is_sdl2 or is_webview:
if is_sdl2 or is_webview or is_service_only:
if is_sdl2:
bootstrap = 'sdl2'
java_ns = u'org.kivy.android'
Expand Down
3 changes: 2 additions & 1 deletion testapps/setup_testapp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'blacklist-requirements': 'openssl,sqlite3',
'android-api': 27,
'ndk-api': 21,
'ndk-dir': '/home/asandy/android/crystax-ndk-10.3.2',
'sdk-dir':'/opt/android/android-sdk/',
'ndk-dir':'/opt/android/android-ndk-r17c/',
'dist-name': 'testapp_service',
'ndk-version': '10.3.2',
'bootstrap': 'service_only',
Expand Down

0 comments on commit 1d375fb

Please sign in to comment.