Skip to content
Snippets Groups Projects
Commit 46bc3c47 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

boot: improve logic

Change-Id: I7bc9803010f36c18382dfa18137a0706f6c9d834
parent 3d7f75c0
No related branches found
No related tags found
No related merge requests found
......@@ -182,6 +182,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
......
......@@ -20,7 +20,6 @@ package cx.ring.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.format.DateUtils;
import android.util.Log;
......@@ -45,21 +44,20 @@ public class BootReceiver extends BroadcastReceiver {
if (intent == null || intent.getAction() == null)
return;
final String action = intent.getAction();
if (Intent.ACTION_BOOT_COMPLETED.equals(action) || Intent.ACTION_REBOOT.equals(action)) {
if (Intent.ACTION_BOOT_COMPLETED.equals(action) ||
Intent.ACTION_REBOOT.equals(action) ||
Intent.ACTION_MY_PACKAGE_REPLACED.equals(action))
{
try {
((JamiApplication) context.getApplicationContext()).getInjectionComponent().inject(this);
if (mPreferencesService.getSettings().isAllowOnStartup()) {
try {
ContextCompat.startForegroundService(context, new Intent(SyncService.ACTION_START).setClass(context, SyncService.class));
ContextCompat.startForegroundService(context, new Intent(SyncService.ACTION_START)
.setClass(context, SyncService.class)
.putExtra(SyncService.EXTRA_TIMEOUT, 5 * DateUtils.SECOND_IN_MILLIS));
} catch (IllegalStateException e) {
Log.e(TAG, "Error starting service", e);
}
new Handler().postDelayed(() -> {
try {
context.startService(new Intent(SyncService.ACTION_STOP).setClass(context, SyncService.class));
} catch (IllegalStateException ignored) {
}
}, 5 * DateUtils.SECOND_IN_MILLIS);
}
} catch (Exception e) {
Log.e(TAG, "Can't start on boot", e);
......
......@@ -49,11 +49,11 @@ public class JamiJobService extends JobService
Log.w(TAG, "onStartJob() " + params);
try {
JamiApplication.getInstance().startDaemon();
Intent serviceIntent = new Intent(SyncService.ACTION_START).setClass(this, SyncService.class);
try {
ContextCompat.startForegroundService(this, serviceIntent);
ContextCompat.startForegroundService(this, new Intent(SyncService.ACTION_START)
.setClass(this, SyncService.class));
} catch (IllegalStateException e) {
android.util.Log.e(TAG, "Error starting service", e);
Log.e(TAG, "Error starting service", e);
}
new Handler().postDelayed(() -> {
Log.w(TAG, "jobFinished() " + params);
......
......@@ -25,6 +25,7 @@ import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import androidx.annotation.Nullable;
......@@ -39,12 +40,13 @@ import cx.ring.application.JamiApplication;
import cx.ring.client.HomeActivity;
public class SyncService extends Service {
private static final String TAG = SyncService.class.getSimpleName();
public static final int NOTIF_SYNC_SERVICE_ID = 1004;
public static final String ACTION_START = "startService";
public static final String ACTION_STOP = "stopService";
public static final String EXTRA_TIMEOUT = "timeout";
private boolean isFirst = true;
private int serviceUsers = 0;
private final Random mRandom = new Random();
@Inject
......@@ -60,8 +62,7 @@ public class SyncService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (ACTION_START.equals(action)) {
if (isFirst) {
isFirst = false;
if (serviceUsers == 0) {
final Intent deleteIntent = new Intent(ACTION_STOP)
.setClass(getApplicationContext(), SyncService.class);
final Intent contentIntent = new Intent(Intent.ACTION_VIEW)
......@@ -73,7 +74,6 @@ public class SyncService extends Service {
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setAutoCancel(false)
.setOngoing(false)
.setVibrate(null)
.setSmallIcon(R.drawable.ic_ring_logo_white)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
......@@ -88,11 +88,24 @@ public class SyncService extends Service {
JamiApplication.getInstance().startDaemon();
}
serviceUsers++;
long timeout = intent.getLongExtra(EXTRA_TIMEOUT, -1);
if (timeout > 0) {
new Handler().postDelayed(() -> {
try {
startService(new Intent(SyncService.ACTION_STOP).setClass(getApplicationContext(), SyncService.class));
} catch (IllegalStateException ignored) {
}
}, timeout);
}
}
else if (ACTION_STOP.equals(action)) {
stopForeground(true);
stopSelf();
isFirst = true;
serviceUsers--;
if (serviceUsers == 0) {
stopForeground(true);
stopSelf();
}
}
return START_NOT_STICKY;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment