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

Commit

Permalink
Fix ISE in StartSyncReceiver (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
anselm92 authored and kordianbruck committed Nov 12, 2017
1 parent f6e4500 commit 709e835
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 62 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -619,21 +619,27 @@

<service
android:name=".services.DownloadService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<service
android:name=".services.SilenceService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<service
android:name=".services.SendMessageService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<service
android:name=".services.SendWifiMeasurementService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<service
android:name=".services.BackgroundService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<service
android:name=".services.FillCacheService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<service
android:name=".services.ScanResultsAvailableReceiver$NeverShowAgain"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/de/tum/in/tumcampusapp/auxiliary/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,11 @@ object Const {
const val ROOM_ID = "room_id"

const val NOTIFICATION_CHANNEL_DEFAULT = "default"

const val BACKGROUND_SERVICE_JOB_ID = 1000
const val SEND_MESSAGE_SERVICE_JOB_ID = 1001
const val SILENCE_SERVICE_JOB_ID = 1002
const val SEND_WIFI_SERVICE_JOB_ID = 1003
const val DOWNLOAD_SERVICE_JOB_ID = 1004
const val FILL_CACHE_SERVICE_JOB_ID = 1005
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package de.tum.in.tumcampusapp.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;

import de.tum.in.tumcampusapp.auxiliary.Const;
import de.tum.in.tumcampusapp.auxiliary.ImplicitCounter;
import de.tum.in.tumcampusapp.auxiliary.Utils;

import static de.tum.in.tumcampusapp.auxiliary.Const.BACKGROUND_SERVICE_JOB_ID;

/**
* Service used to sync data in background
*/
public class BackgroundService extends IntentService {

private static final String BACKGROUND_SERVICE = "BackgroundService";
public class BackgroundService extends JobIntentService {

public BackgroundService() {
super(BACKGROUND_SERVICE);
}

@Override
public void onCreate() {
Expand All @@ -30,19 +29,24 @@ public void onDestroy() {
Utils.log("BackgroundService has stopped");
}

static void enqueueWork(Context context, Intent work) {
enqueueWork(context, BackgroundService.class, BACKGROUND_SERVICE_JOB_ID, work);
}

/**
* Starts {@link DownloadService} with appropriate extras
*
* @param intent Intent
*/
@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(@NonNull Intent intent) {
// Download all from external
Intent service = new Intent(this, DownloadService.class);
Intent service = new Intent();
service.putExtra(Const.ACTION_EXTRA, Const.DOWNLOAD_ALL_FROM_EXTERNAL);
service.putExtra(Const.FORCE_DOWNLOAD, false);
service.putExtra(Const.APP_LAUNCHES, intent.getBooleanExtra(Const.APP_LAUNCHES, false));
startService(service);

DownloadService.enqueueWork(getBaseContext(), service);

//Upload Usage statistics
ImplicitCounter.submitCounter(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package de.tum.in.tumcampusapp.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.res.AssetManager;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.support.v4.content.LocalBroadcastManager;

import org.json.JSONException;
Expand All @@ -30,27 +31,21 @@
import de.tum.in.tumcampusapp.trace.G;
import de.tum.in.tumcampusapp.trace.Util;

import static de.tum.in.tumcampusapp.auxiliary.Const.DOWNLOAD_SERVICE_JOB_ID;

/**
* Service used to download files from external pages
*/
public class DownloadService extends IntentService {
public class DownloadService extends JobIntentService {

/**
* Download broadcast identifier
*/
public final static String BROADCAST_NAME = "de.tum.in.newtumcampus.intent.action.BROADCAST_DOWNLOAD";
private static final String DOWNLOAD_SERVICE = "DownloadService";
private static final String LAST_UPDATE = "last_update";
private static final String CSV_LOCATIONS = "locations.csv";
private LocalBroadcastManager broadcastManager;

/**
* default init (run intent in new thread)
*/
public DownloadService() {
super(DOWNLOAD_SERVICE);
}

/**
* Gets the time when BackgroundService was called last time
*
Expand Down Expand Up @@ -151,7 +146,7 @@ private static synchronized void download(Intent intent, DownloadService service

// Do all other import stuff that is not relevant for creating the viewing the start page
if (action.equals(Const.DOWNLOAD_ALL_FROM_EXTERNAL)) {
service.startService(new Intent(service, FillCacheService.class));
FillCacheService.enqueueWork(service.getBaseContext(),new Intent());
}
}

Expand All @@ -171,8 +166,12 @@ public void onDestroy() {
Utils.log("DownloadService service has stopped");
}

static void enqueueWork(Context context, Intent work) {
enqueueWork(context, DownloadService.class, DOWNLOAD_SERVICE_JOB_ID, work);
}

@Override
protected void onHandleIntent(final Intent intent) {
protected void onHandleWork(@NonNull final Intent intent) {
new Thread(() -> download(intent, DownloadService.this)).start();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package de.tum.in.tumcampusapp.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;

import de.tum.in.tumcampusapp.auxiliary.Utils;
import de.tum.in.tumcampusapp.managers.CacheManager;

import static de.tum.in.tumcampusapp.auxiliary.Const.FILL_CACHE_SERVICE_JOB_ID;

/**
* Service used to fill caches in background, for faster/offline access
**/
public class FillCacheService extends IntentService {

private static final String CACHE_SERVICE = "FillCacheService";

public FillCacheService() {
super(CACHE_SERVICE);
}
public class FillCacheService extends JobIntentService {

@Override
public void onCreate() {
Expand All @@ -29,8 +27,12 @@ public void onDestroy() {
Utils.logv("FillCacheService has stopped");
}

static void enqueueWork(Context context, Intent work) {
enqueueWork(context, FillCacheService.class, FILL_CACHE_SERVICE_JOB_ID, work);
}

@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(@NonNull Intent intent) {
new Thread(() -> {
// Fill cache service
CacheManager cache = new CacheManager(FillCacheService.this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.tum.in.tumcampusapp.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.support.v4.content.LocalBroadcastManager;

import java.io.IOException;
Expand All @@ -16,26 +18,24 @@
import de.tum.in.tumcampusapp.models.gcm.GCMChat;
import de.tum.in.tumcampusapp.models.tumcabe.ChatMessage;

import static de.tum.in.tumcampusapp.auxiliary.Const.SEND_MESSAGE_SERVICE_JOB_ID;

/**
* Service used to silence the mobile during lectures
*/
public class SendMessageService extends IntentService {
public class SendMessageService extends JobIntentService {

public static final int MAX_SEND_TRIES = 5;
/**
* Interval in milliseconds to check for current lectures
*/
private static final String SEND_MESSAGE_SERVICE = "SendMessageService";

/**
* default init (run intent in new thread)
*/
public SendMessageService() {
super(SEND_MESSAGE_SERVICE);
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, SendMessageService.class, SEND_MESSAGE_SERVICE_JOB_ID, work);
}

@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(@NonNull Intent intent) {
// Get all unsent messages from database
List<ChatMessage> unsentMsg = ChatMessageManager.getAllUnsentUpdated(this);
if (unsentMsg.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package de.tum.in.tumcampusapp.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;

import de.tum.in.tumcampusapp.managers.WifiMeasurementManager;

import static de.tum.in.tumcampusapp.auxiliary.Const.SEND_WIFI_SERVICE_JOB_ID;

/**
* This service is getting used by StartSyncReceiver to send the accumulated WifiMeasurements
* every X hours
*/
public class SendWifiMeasurementService extends IntentService {
public class SendWifiMeasurementService extends JobIntentService {

//Maximum retries for sending the wifi-measurement list to the server
public static final int MAX_SEND_TRIES = 3;
//Time between retries for trying again
public static final int TIME_BETWEEN_MILIS = 300;
private static final String SEND_WIFI_MEASUREMENT_SERVICE = "SendWifiMeasurementService";

public SendWifiMeasurementService() {
super(SEND_WIFI_MEASUREMENT_SERVICE);

static void enqueueWork(Context context, Intent work) {
enqueueWork(context, SendWifiMeasurementService.class, SEND_WIFI_SERVICE_JOB_ID, work);
}

@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(@NonNull Intent intent) {
WifiMeasurementManager wm = new WifiMeasurementManager(this);
wm.uploadMeasurementsToRemote(MAX_SEND_TRIES, TIME_BETWEEN_MILIS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tum.in.tumcampusapp.services;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand All @@ -10,6 +9,8 @@
import android.media.AudioManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -19,24 +20,19 @@
import de.tum.in.tumcampusapp.auxiliary.Utils;
import de.tum.in.tumcampusapp.managers.CalendarManager;

import static de.tum.in.tumcampusapp.auxiliary.Const.SILENCE_SERVICE_JOB_ID;

/**
* Service used to silence the mobile during lectures
*/
public class SilenceService extends IntentService {
public class SilenceService extends JobIntentService {

/**
* Interval in milliseconds to check for current lectures
*/
private static final int CHECK_INTERVAL = 60000 * 15; // 15 Minutes
private static final int CHECK_DELAY = 10000; // 10 Seconds after Calendar changed
private static final String SILENCE_SERVICE = "SilenceService";

/**
* default init (run intent in new thread)
*/
public SilenceService() {
super(SILENCE_SERVICE);
}

private static long getWaitDuration(String timeToEventString) {
long timeToEvent = Long.MAX_VALUE;
Expand All @@ -61,8 +57,12 @@ public void onDestroy() {
Utils.log("SilenceService has stopped");
}

static void enqueueWork(Context context, Intent work) {
enqueueWork(context, SilenceService.class, SILENCE_SERVICE_JOB_ID, work);
}

@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(@NonNull Intent intent) {
//Abort, if the settings changed
if (!Utils.getSettingBool(this, Const.SILENCE_SERVICE, false)) {
// Don't schedule a new run, since the service is disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.JobIntentService;

import de.tum.in.tumcampusapp.auxiliary.Const;
import de.tum.in.tumcampusapp.auxiliary.Utils;
Expand Down Expand Up @@ -51,17 +52,16 @@ public void onReceive(Context context, Intent intent) {
// Start BackgroundService
if (launch || backgroundServicePermitted) {
Utils.logv("Start background service...");
Intent i = new Intent(context, BackgroundService.class);
Intent i = new Intent();
i.putExtra(Const.APP_LAUNCHES, launch);
context.startService(i);
BackgroundService.enqueueWork(context, i);
}

context.startService(new Intent(context, SendMessageService.class));
SendMessageService.enqueueWork(context,new Intent());

// Also start the SilenceService. It checks if it is enabled, so we don't need to
context.startService(new Intent(context, SilenceService.class));
SilenceService.enqueueWork(context,new Intent());
if (intent.getAction() != "android.net.wifi.WIFI_STATE_CHANGED" && Utils.getInternalSettingBool(context, WifiMeasurementManager.WIFI_SCANS_ALLOWED, false)) {
context.startService(new Intent(context, SendWifiMeasurementService.class));
SendWifiMeasurementService.enqueueWork(context,new Intent());
}
}
}

0 comments on commit 709e835

Please sign in to comment.