Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix(logsender): run LogSenderService in foreground to avoid being kil…
Browse files Browse the repository at this point in the history
…led by system (#1385)
  • Loading branch information
itsaky committed Dec 20, 2023
1 parent eb613d5 commit 3f15af6
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 4 deletions.
4 changes: 4 additions & 0 deletions logsender/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ android {

defaultConfig {
minSdk = 16

vectorDrawables {
useSupportLibrary = true
}
}

compileOptions {
Expand Down
1 change: 1 addition & 0 deletions logsender/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.itsaky.androidide.permission.BIND_LOG_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<application>
<provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@

package com.itsaky.androidide.logsender;

import android.app.Notification;
import android.app.Notification.BigTextStyle;
import android.app.Notification.Builder;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.IBinder;
import android.widget.Toast;
import com.itsaky.androidide.logsender.utils.Logger;
Expand All @@ -31,32 +41,75 @@
public class LogSenderService extends Service {

private final LogSender logSender = new LogSender();
private static final int NOTIFICATION_ID = 644;
private static final String NOTIFICATION_CHANNEL_NAME = "LogSender Service";
private static final String NOTIFICATION_TITLE = "LogSender Service";
private static final String NOTIFICATION_TEXT = "Connected to AndroidIDE";
private static final String NOTIFICATION_CHANNEL_ID = "ide.logsender.service";
public static final String ACTION_START_SERVICE = "ide.logsender.service.start";
public static final String ACTION_STOP_SERVICE = "ide.logsender.service.stop";

@Override
public void onCreate() {
Logger.LOG.logThis();
super.onCreate();
setupNotificationChannel();
startForeground(NOTIFICATION_ID, buildNotification());
}

@Override
public IBinder onBind(Intent intent) {
Logger.debug("Unexpected request to bind.", intent);
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Logger.debug("onStartCommand", intent, flags, startId);

switch (intent.getAction()) {
case ACTION_START_SERVICE:
actionStartService();
break;
case ACTION_STOP_SERVICE:
actionStopService();
break;
default:
Logger.error("Unknown service action:", intent.getAction());
break;
}

return START_NOT_STICKY;
}

private void actionStartService() {
Logger.info("Starting log sender service...");

boolean result = false;
try {
result = logSender.bind(getApplicationContext());
Logger.debug("Bind to AndroidIDE:", result);
} catch (Exception err) {
Logger.error(getString(R.string.msg_bind_service_failed), err);
}

if (!result) {
Toast.makeText(this, getString(R.string.msg_bind_service_failed), Toast.LENGTH_SHORT).show();
stopSelf();
actionStopService();
}
}

return START_NOT_STICKY;
private void actionStopService() {
Logger.info("Stopping log sender service...");
stopSelf();
}

@Override
public void onTaskRemoved(Intent rootIntent) {
Logger.LOG.logThis();

if (!logSender.isConnected() && !logSender.isBinding()) {
Logger.debug("Not bound to AndroidIDE. Ignored.");
return;
}

Expand All @@ -67,12 +120,71 @@ public void onTaskRemoved(Intent rootIntent) {

@Override
public void onDestroy() {
Logger.LOG.logThis();
if (!logSender.isConnected() && !logSender.isBinding()) {
Logger.debug("Not bound to AndroidIDE. Ignored.");
return;
}

Logger.warn("Service is being destroyed. Destroying log sender...");
logSender.destroy(getApplicationContext());
super.onDestroy();
}

private void setupNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}

NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
);

NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}

private Notification buildNotification() {
Resources res = getResources();

// Set notification priority
// If holding a wake or wifi lock consider the notification of high priority since it's using power,
// otherwise use a low priority
int priority = Notification.PRIORITY_LOW;

// Build the notification
final Builder builder = new Builder(this);
builder.setContentTitle(NOTIFICATION_TITLE);
builder.setContentText(NOTIFICATION_TEXT);
builder.setStyle(new BigTextStyle().bigText(NOTIFICATION_TEXT));
builder.setPriority(priority);

if (VERSION.SDK_INT >= VERSION_CODES.O) {
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}

if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
builder.setShowWhen(false);
}

builder.setSmallIcon(R.drawable.ic_androidide_log);

if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
builder.setColor(0xFF607D8B);
}

builder.setOngoing(true);

// Set Exit button action
Intent exitIntent = new Intent(this, LogSenderService.class).setAction(ACTION_STOP_SERVICE);
builder.addAction(android.R.drawable.ic_delete,
res.getString(R.string.notification_action_exit),
PendingIntent.getService(this, 0, exitIntent, PendingIntent.FLAG_IMMUTABLE));

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import com.itsaky.androidide.logsender.LogSender;
Expand Down Expand Up @@ -54,7 +55,13 @@ public boolean onCreate() {

try {
final Intent intent = new Intent(application, LogSenderService.class);
application.startService(intent);
intent.setAction(LogSenderService.ACTION_START_SERVICE);

if (VERSION.SDK_INT >= VERSION_CODES.O) {
application.startForegroundService(intent);
} else {
application.startService(intent);
}
} catch (Exception e) {

// starting a background service is not allowed on Android 12+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author Akash Yadav
*/
public class Logger {
private static final ILogger LOG = ILogger.newInstance("LogSender");
public static final ILogger LOG = ILogger.newInstance("LogSender");

public static void error(Object... messages) {
LOG.error(messages);
Expand Down
26 changes: 26 additions & 0 deletions logsender/src/main/res/drawable/ic_androidide_log.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
~ This file is part of AndroidIDE.
~
~ AndroidIDE is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ AndroidIDE is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="32"
android:viewportWidth="32"
android:width="24dp">
<path
android:fillColor="#000000"
android:pathData="M10.314,7.553C10.119,7.552 9.923,7.56 9.729,7.58C9.361,7.62 9.008,7.709 8.67,7.857C8.328,8.008 8.028,8.218 7.787,8.504C7.398,8.967 7.246,9.51 7.258,10.105C7.261,10.298 7.272,10.489 7.281,10.682C7.288,10.811 7.297,10.94 7.305,11.07C7.318,11.267 7.333,11.467 7.346,11.664C7.359,11.862 7.366,12.059 7.379,12.256C7.392,12.484 7.413,12.712 7.424,12.939C7.431,13.081 7.433,13.223 7.43,13.363C7.425,13.57 7.392,13.771 7.322,13.967C7.236,14.206 7.083,14.381 6.846,14.475C6.776,14.503 6.705,14.528 6.633,14.547C6.373,14.618 6.104,14.636 5.836,14.639C5.241,14.644 4.644,14.641 4.049,14.641C4.022,14.641 3.995,14.641 3.969,14.643L3.969,15.959L4.057,15.959C4.748,15.967 5.441,15.94 6.133,15.973C6.355,15.984 6.571,16.024 6.781,16.098C7.005,16.177 7.176,16.316 7.279,16.533C7.369,16.721 7.41,16.921 7.424,17.127C7.436,17.333 7.431,17.538 7.414,17.744C7.397,17.941 7.393,18.14 7.381,18.338C7.367,18.565 7.353,18.793 7.338,19.021C7.327,19.192 7.315,19.365 7.305,19.537C7.289,19.794 7.268,20.05 7.258,20.307C7.25,20.545 7.255,20.784 7.297,21.021C7.392,21.552 7.642,21.997 8.047,22.35C8.331,22.599 8.664,22.764 9.023,22.875C9.427,22.999 9.843,23.044 10.264,23.049C10.507,23.052 10.747,23.051 10.99,23.051C11.013,23.051 11.034,23.047 11.057,23.045L11.057,21.734C11.044,21.732 11.039,21.729 11.031,21.729C10.753,21.724 10.473,21.722 10.193,21.719C9.941,21.715 9.695,21.672 9.463,21.572C9.101,21.417 8.864,21.154 8.785,20.762C8.753,20.603 8.74,20.444 8.756,20.281C8.776,20.072 8.781,19.859 8.793,19.648C8.802,19.491 8.812,19.334 8.82,19.176C8.828,19.05 8.832,18.925 8.84,18.801C8.853,18.606 8.871,18.413 8.883,18.219C8.895,18.021 8.907,17.824 8.916,17.627C8.932,17.316 8.922,17.006 8.836,16.703C8.661,16.082 8.282,15.638 7.678,15.398C7.566,15.354 7.445,15.328 7.326,15.291C7.352,15.284 7.377,15.277 7.402,15.271C7.857,15.165 8.238,14.939 8.52,14.561C8.788,14.198 8.908,13.788 8.918,13.344C8.924,13.122 8.907,12.899 8.9,12.67C8.896,12.597 8.889,12.533 8.885,12.469C8.87,12.24 8.853,12.013 8.84,11.785C8.827,11.587 8.821,11.389 8.809,11.191C8.794,10.961 8.775,10.729 8.766,10.498C8.759,10.342 8.74,10.189 8.756,10.033C8.782,9.783 8.846,9.551 9.008,9.355C9.158,9.172 9.355,9.058 9.576,8.982C9.792,8.907 10.017,8.881 10.244,8.877C10.492,8.872 10.739,8.875 10.988,8.875C11.011,8.875 11.034,8.872 11.059,8.871L11.059,7.553L10.98,7.553L10.314,7.553zM20.662,7.553C20.606,7.554 20.558,7.554 20.512,7.555L20.512,8.873L20.588,8.873C20.884,8.876 21.181,8.879 21.479,8.885C21.744,8.89 22,8.951 22.24,9.066C22.444,9.165 22.613,9.304 22.727,9.504C22.83,9.687 22.877,9.886 22.885,10.094C22.888,10.208 22.881,10.323 22.875,10.438C22.869,10.57 22.863,10.702 22.855,10.834C22.842,11.035 22.828,11.239 22.814,11.441C22.807,11.57 22.8,11.702 22.791,11.832C22.779,12.021 22.762,12.21 22.75,12.4C22.736,12.597 22.724,12.794 22.717,12.992C22.709,13.247 22.707,13.502 22.762,13.754C22.855,14.187 23.057,14.558 23.396,14.848C23.61,15.029 23.855,15.155 24.125,15.23C24.2,15.251 24.273,15.271 24.354,15.293C24.261,15.319 24.176,15.337 24.092,15.363C23.732,15.475 23.429,15.676 23.186,15.965C22.865,16.343 22.728,16.788 22.717,17.275C22.712,17.444 22.725,17.615 22.73,17.783C22.736,17.929 22.741,18.074 22.75,18.219C22.76,18.364 22.772,18.51 22.783,18.656L24.318,18.656C24.311,18.528 24.306,18.4 24.297,18.271C24.284,18.082 24.263,17.895 24.256,17.705C24.25,17.542 24.236,17.378 24.248,17.213C24.262,17.021 24.285,16.833 24.352,16.65C24.441,16.403 24.601,16.219 24.852,16.119C25.127,16.01 25.415,15.978 25.707,15.965C25.907,15.956 26.109,15.957 26.311,15.957L27.729,15.957L27.811,15.957L27.811,14.641L27.734,14.641C27.222,14.641 26.71,14.639 26.197,14.641C25.947,14.642 25.696,14.638 25.447,14.613C25.255,14.594 25.069,14.557 24.887,14.49C24.657,14.406 24.486,14.261 24.387,14.035C24.263,13.76 24.243,13.468 24.242,13.174C24.241,13.052 24.258,12.93 24.266,12.809C24.281,12.556 24.299,12.304 24.314,12.053C24.325,11.889 24.334,11.727 24.344,11.562C24.36,11.311 24.377,11.06 24.393,10.809C24.403,10.642 24.414,10.474 24.422,10.309C24.434,10.061 24.426,9.816 24.383,9.57C24.292,9.049 24.039,8.618 23.645,8.271C23.393,8.051 23.1,7.897 22.785,7.785C22.335,7.623 21.869,7.557 21.393,7.553C21.153,7.55 20.91,7.553 20.662,7.553zM12.111,11.436C12.088,11.434 12.064,11.435 12.039,11.441C11.959,11.463 11.88,11.527 11.877,11.646C11.875,11.716 11.9,11.771 11.932,11.826C12.079,12.085 12.231,12.345 12.381,12.604C12.511,12.828 12.639,13.051 12.768,13.275C12.804,13.338 12.836,13.4 12.873,13.467C12.005,13.953 11.302,14.607 10.768,15.439C10.237,16.268 9.94,17.173 9.852,18.15C9.984,18.167 21.719,18.162 21.775,18.145C21.763,18.04 21.756,17.934 21.74,17.83C21.721,17.703 21.687,17.578 21.67,17.451C21.643,17.249 21.579,17.056 21.523,16.861C21.494,16.758 21.454,16.657 21.416,16.557C21.364,16.422 21.315,16.286 21.254,16.154C21.181,15.997 21.097,15.845 21.016,15.686C20.888,15.472 20.757,15.266 20.607,15.072C20.473,14.898 20.337,14.725 20.18,14.57C20.061,14.453 19.941,14.336 19.816,14.225C19.703,14.122 19.587,14.023 19.465,13.932C19.275,13.788 19.075,13.653 18.867,13.537C18.829,13.517 18.796,13.491 18.76,13.469C18.815,13.367 18.864,13.274 18.918,13.18C19.04,12.969 19.168,12.758 19.291,12.549C19.392,12.377 19.492,12.206 19.592,12.033C19.647,11.937 19.702,11.841 19.75,11.74C19.798,11.641 19.742,11.516 19.639,11.465C19.537,11.414 19.427,11.45 19.367,11.553C19.298,11.674 19.228,11.795 19.158,11.916C19.042,12.113 18.924,12.307 18.809,12.504C18.667,12.745 18.526,12.987 18.385,13.229C18.376,13.243 18.364,13.254 18.354,13.266C18.189,13.202 18.031,13.141 17.871,13.08C17.794,13.052 17.717,13.02 17.637,13C17.449,12.949 17.259,12.904 17.07,12.859C16.975,12.837 16.879,12.821 16.783,12.807C16.706,12.794 16.628,12.786 16.551,12.777C16.479,12.768 16.409,12.75 16.338,12.748C16.122,12.742 15.906,12.744 15.689,12.742C15.445,12.74 15.203,12.758 14.961,12.793C14.775,12.819 14.588,12.851 14.406,12.891C14.102,12.957 13.806,13.05 13.518,13.168C13.439,13.2 13.36,13.233 13.281,13.266C13.212,13.147 13.147,13.036 13.082,12.924C12.886,12.589 12.692,12.253 12.496,11.918C12.425,11.796 12.349,11.674 12.279,11.551C12.241,11.485 12.182,11.441 12.111,11.436zM18.586,15.486C18.709,15.493 18.819,15.548 18.91,15.648C18.996,15.742 19.049,15.852 19.045,15.984C19.042,16.075 19.005,16.158 18.955,16.23C18.857,16.373 18.714,16.441 18.539,16.443C18.392,16.428 18.28,16.374 18.189,16.271C18.132,16.206 18.097,16.131 18.076,16.049C18.043,15.915 18.086,15.799 18.158,15.693C18.228,15.588 18.336,15.519 18.457,15.496C18.501,15.488 18.545,15.484 18.586,15.486zM13.096,15.49C13.184,15.493 13.273,15.521 13.354,15.576C13.506,15.68 13.575,15.825 13.561,16.006C13.537,16.285 13.265,16.483 13.016,16.436C12.771,16.388 12.656,16.247 12.604,16.045C12.587,15.983 12.587,15.912 12.615,15.848C12.646,15.783 12.666,15.725 12.701,15.68C12.8,15.552 12.948,15.486 13.096,15.49zM16.715,20.205L16.715,21.619L28.031,21.619L28.031,20.205L16.715,20.205zM16.715,23.035L16.715,24.449L23.787,24.449L23.787,23.035L16.715,23.035z" />
</vector>
1 change: 1 addition & 0 deletions logsender/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

<resources>
<string name="msg_bind_service_failed">Failed to bind to AndroidIDE</string>
<string name="notification_action_exit">Exit</string>
</resources>

0 comments on commit 3f15af6

Please sign in to comment.