Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix foreground notification being mandatory and more #1886

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public class PythonService extends Service implements Runnable {
private String pythonHome;
private String pythonPath;
private String serviceEntrypoint;
private boolean serviceStartAsForeground;
// Argument to pass to Python code,
private String pythonServiceArgument;


public static PythonService mService = null;
private Intent startIntent = null;

Expand All @@ -46,10 +49,6 @@ public void setAutoRestartService(boolean restart) {
autoRestartService = restart;
}

public boolean canDisplayNotification() {
return true;
}

public int startType() {
return START_NOT_STICKY;
}
Expand Down Expand Up @@ -79,21 +78,35 @@ public int onStartCommand(Intent intent, int flags, int startId) {
pythonName = extras.getString("pythonName");
pythonHome = extras.getString("pythonHome");
pythonPath = extras.getString("pythonPath");
serviceStartAsForeground = (
extras.getString("serviceStartAsForeground") == "true"
);
pythonServiceArgument = extras.getString("pythonServiceArgument");

pythonThread = new Thread(this);
pythonThread.start();

if (canDisplayNotification()) {
if (serviceStartAsForeground) {
doStartForeground(extras);
}

return startType();
}

protected String getServiceNotificationTitleFromParams(Bundle extras) {
return extras.getString("serviceTitle");
}

protected String getServiceNotificationDescription(Bundle extras) {
return extras.getString("serviceDescription");
}

protected int getServiceId() {
return 1;
}

protected void doStartForeground(Bundle extras) {
String serviceTitle = extras.getString("serviceTitle");
String serviceDescription = extras.getString("serviceDescription");
String serviceTitle = getServiceNotificationTitleFromParams(extras);
String serviceDescription = getServiceNotificationDescription(extras);
Notification notification;
Context context = getApplicationContext();
Intent contextIntent = new Intent(context, PythonActivity.class);
Expand All @@ -116,7 +129,7 @@ protected void doStartForeground(Bundle extras) {
// 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
String channelName = "Background Service"; //TODO: make this configurable
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName,
NotificationManager.IMPORTANCE_NONE);

Expand All @@ -132,7 +145,7 @@ protected void doStartForeground(Bundle extras) {
builder.setSmallIcon(context.getApplicationInfo().icon);
notification = builder.build();
}
startForeground(1, notification);
startForeground(getServiceId(), notification);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package {{ args.package }};

import android.os.Build;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import android.content.Intent;
import android.content.Context;
import android.app.Notification;
import android.app.PendingIntent;
import android.os.Bundle;
import org.kivy.android.PythonService;
import org.kivy.android.PythonActivity;


public class Service{{ name|capitalize }} extends PythonService {
Expand All @@ -20,50 +13,25 @@ public int startType() {
}
{% endif %}

{% if not foreground %}
@Override
public boolean canDisplayNotification() {
return false;
}
{% endif %}

@Override
protected void doStartForeground(Bundle extras) {
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) {
notification = new Notification(
context.getApplicationInfo().icon, "{{ args.name }}", System.currentTimeMillis());
try {
// prevent using NotificationCompat, this saves 100kb on apk
Method func = notification.getClass().getMethod(
"setLatestEventInfo", Context.class, CharSequence.class,
CharSequence.class, PendingIntent.class);
func.invoke(notification, context, "{{ args.name }}", "{{ name| capitalize }}", pIntent);
} catch (NoSuchMethodException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) {
}
} else {
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle("{{ args.name }}");
builder.setContentText("{{ name| capitalize }}");
builder.setContentIntent(pIntent);
builder.setSmallIcon(context.getApplicationInfo().icon);
notification = builder.build();
}
startForeground({{ service_id }}, notification);
@override
protected int getServiceId() {
return {{ service_id }};
}

static public void start(Context ctx, String pythonServiceArgument) {
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
intent.putExtra("androidPrivate", ctx.getFilesDir().getAbsolutePath());
intent.putExtra("androidArgument", argument);
intent.putExtra("serviceTitle", "{{ args.name }}");
intent.putExtra("serviceDescription", "{{ name|capitalize }}");
intent.putExtra("serviceEntrypoint", "{{ entrypoint }}");
intent.putExtra("pythonName", "{{ name }}");
{% if foreground %}
intent.putExtra("serviceStartAsForeground", "true");
{% else %}
intent.putExtra("serviceStartAsForeground", "false");
{% endif %}
intent.putExtra("pythonHome", argument);
intent.putExtra("pythonPath", argument + ":" + argument + "/lib");
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,23 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
}
}

public static void start_service(String serviceTitle, String serviceDescription,
String pythonServiceArgument) {
public static void start_service(
String serviceTitle,
String serviceDescription,
String pythonServiceArgument
) {
// This is the legacy call with less parameters!
start_service(
serviceTitle, serviceDescription, pythonServiceArgument, true
);
}

public static void start_service(
String serviceTitle,
String serviceDescription,
String pythonServiceArgument,
boolean showForegroundNotification
) {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
String filesDirectory = argument;
Expand All @@ -378,6 +393,9 @@ public static void start_service(String serviceTitle, String serviceDescription,
serviceIntent.putExtra("pythonName", "python");
serviceIntent.putExtra("pythonHome", app_root_dir);
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
serviceIntent.putExtra("serviceStartAsForeground",
(showForegroundNotification ? "true" : "false")
);
serviceIntent.putExtra("serviceTitle", serviceTitle);
serviceIntent.putExtra("serviceDescription", serviceDescription);
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,23 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
}
}

public static void start_service(String serviceTitle, String serviceDescription,
String pythonServiceArgument) {
public static void start_service(
String serviceTitle,
String serviceDescription,
String pythonServiceArgument
) {
// This is the legacy call with less parameters!
start_service(
serviceTitle, serviceDescription, pythonServiceArgument, true
);
}

public static void start_service(
String serviceTitle,
String serviceDescription,
String pythonServiceArgument,
boolean showForegroundNotification
) {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
String filesDirectory = argument;
Expand All @@ -393,6 +408,9 @@ public static void start_service(String serviceTitle, String serviceDescription,
serviceIntent.putExtra("pythonName", "python");
serviceIntent.putExtra("pythonHome", app_root_dir);
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
serviceIntent.putExtra("serviceStartAsForeground",
(showForegroundNotification ? "true" : "false")
);
serviceIntent.putExtra("serviceTitle", serviceTitle);
serviceIntent.putExtra("serviceDescription", serviceDescription);
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ public int startType() {
}
{% endif %}

{% if foreground %}
/**
* {@inheritDoc}
*/
@Override
public boolean getStartForeground() {
return true;
@override
protected int getServiceId() {
return {{ service_id }};
}
{% endif %}

public static void start(Context ctx, String pythonServiceArgument) {
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
Expand All @@ -41,6 +36,11 @@ public static void start(Context ctx, String pythonServiceArgument) {
intent.putExtra("serviceTitle", "{{ name|capitalize }}");
intent.putExtra("serviceDescription", "");
intent.putExtra("pythonName", "{{ name }}");
{% if foreground %}
intent.putExtra("serviceStartAsForeground", "true");
{% else %}
intent.putExtra("serviceStartAsForeground", "false");
{% endif %}
intent.putExtra("pythonHome", argument);
intent.putExtra("androidUnpack", argument);
intent.putExtra("pythonPath", argument + ":" + argument + "/lib");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,23 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
}
}

public static void start_service(String serviceTitle, String serviceDescription,
String pythonServiceArgument) {
public static void start_service(
String serviceTitle,
String serviceDescription,
String pythonServiceArgument
) {
// This is the legacy call with less parameters!
start_service(
serviceTitle, serviceDescription, pythonServiceArgument, true
);
}

public static void start_service(
String serviceTitle,
String serviceDescription,
String pythonServiceArgument,
boolean showForegroundNotification
) {
Intent serviceIntent = new Intent(PythonActivity.mActivity, PythonService.class);
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
String filesDirectory = argument;
Expand All @@ -450,6 +465,9 @@ public static void start_service(String serviceTitle, String serviceDescription,
serviceIntent.putExtra("pythonName", "python");
serviceIntent.putExtra("pythonHome", app_root_dir);
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
serviceIntent.putExtra("serviceStartAsForeground",
(showForegroundNotification ? "true" : "false")
);
serviceIntent.putExtra("serviceTitle", serviceTitle);
serviceIntent.putExtra("serviceDescription", serviceDescription);
serviceIntent.putExtra("pythonServiceArgument", pythonServiceArgument);
Expand Down