Eric Kok
6 years ago
45 changed files with 448 additions and 354 deletions
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
/* |
||||
* Copyright 2010-2013 Eric Kok et al. |
||||
* |
||||
* Transdroid 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. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.transdroid.core.gui; |
||||
|
||||
import android.app.Application; |
||||
import android.support.annotation.NonNull; |
||||
import android.support.annotation.Nullable; |
||||
import com.evernote.android.job.JobConfig; |
||||
import com.evernote.android.job.JobManager; |
||||
import com.evernote.android.job.util.JobLogger; |
||||
import org.androidannotations.annotations.Bean; |
||||
import org.androidannotations.annotations.EApplication; |
||||
import org.transdroid.core.gui.log.Log; |
||||
import org.transdroid.core.service.ScheduledJobCreator; |
||||
|
||||
@EApplication |
||||
public class TransdroidApp extends Application { |
||||
|
||||
@Bean |
||||
protected Log log; |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
super.onCreate(); |
||||
|
||||
// Configure Android-Job
|
||||
JobConfig.addLogger(new JobLogger() { |
||||
@Override |
||||
public void log(int priority, @NonNull String tag, @NonNull String message, @Nullable Throwable t) { |
||||
log.d(tag, message); |
||||
} |
||||
}); |
||||
JobManager.create(this).addJobCreator(new ScheduledJobCreator()); |
||||
} |
||||
|
||||
} |
@ -1,50 +0,0 @@
@@ -1,50 +0,0 @@
|
||||
/* |
||||
* Copyright 2010-2013 Eric Kok et al. |
||||
* |
||||
* Transdroid 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. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.transdroid.core.service; |
||||
|
||||
import org.androidannotations.annotations.EReceiver; |
||||
|
||||
import android.content.BroadcastReceiver; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
|
||||
/** |
||||
* Acts simply as an intermediary to start the appropriate background service when an alarm goes off. |
||||
* @author Eric Kok |
||||
*/ |
||||
@EReceiver |
||||
public class AlarmReceiver extends BroadcastReceiver { |
||||
|
||||
@Override |
||||
public void onReceive(Context context, Intent intent) { |
||||
switch (intent.getIntExtra("service", -1)) { |
||||
case BootReceiver.ALARM_SERVERCHECKER: |
||||
context.startService(new Intent(context, ServerCheckerService_.class)); |
||||
break; |
||||
case BootReceiver.ALARM_RSSCHECKER: |
||||
context.startService(new Intent(context, RssCheckerService_.class)); |
||||
break; |
||||
case BootReceiver.ALARM_APPUPDATES: |
||||
context.startService(new Intent(context, AppUpdateService_.class)); |
||||
break; |
||||
default: |
||||
// No valid service start ID
|
||||
break; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/* |
||||
* Copyright 2010-2013 Eric Kok et al. |
||||
* |
||||
* Transdroid 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. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.transdroid.core.service; |
||||
|
||||
import android.content.Context; |
||||
import android.support.annotation.NonNull; |
||||
import com.evernote.android.job.Job; |
||||
import com.evernote.android.job.JobManager; |
||||
import com.evernote.android.job.JobRequest; |
||||
import org.transdroid.core.app.settings.SystemSettings; |
||||
import org.transdroid.core.app.settings.SystemSettings_; |
||||
import org.transdroid.core.gui.log.Log_; |
||||
import org.transdroid.core.gui.navigation.NavigationHelper_; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
public class AppUpdateJob extends Job { |
||||
|
||||
static final String TAG = "app_update_checker"; |
||||
|
||||
private static Integer scheduledJobId; |
||||
|
||||
public static void schedule(Context context) { |
||||
SystemSettings systemSettings = SystemSettings_.getInstance_(context); |
||||
NavigationHelper_ navigationHelper = NavigationHelper_.getInstance_(context); |
||||
if (systemSettings.checkForUpdates() && navigationHelper.enableUpdateChecker()) { |
||||
Log_.getInstance_(context).d(TAG, "Schedule app update checker job"); |
||||
scheduledJobId = new JobRequest.Builder(AppUpdateJob.TAG) |
||||
.setPeriodic(TimeUnit.DAYS.toMillis(1)) |
||||
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) |
||||
.setUpdateCurrent(true) |
||||
.build() |
||||
.schedule(); |
||||
} else if (scheduledJobId != null) { |
||||
Log_.getInstance_(context).d(TAG, "Cancel rss checker job"); |
||||
JobManager.instance().cancel(scheduledJobId); |
||||
} |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
protected Result onRunJob(@NonNull Params params) { |
||||
return AppUpdateJobRunner_.getInstance_(getContext()).run(); |
||||
} |
||||
|
||||
} |
@ -1,113 +1,40 @@
@@ -1,113 +1,40 @@
|
||||
/* |
||||
/* |
||||
* Copyright 2010-2013 Eric Kok et al. |
||||
* |
||||
* |
||||
* Transdroid 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. |
||||
* |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.transdroid.core.service; |
||||
|
||||
import android.app.AlarmManager; |
||||
import android.app.PendingIntent; |
||||
import android.content.BroadcastReceiver; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.os.SystemClock; |
||||
|
||||
import org.androidannotations.annotations.Bean; |
||||
import org.androidannotations.annotations.EReceiver; |
||||
import org.transdroid.core.app.settings.*; |
||||
import org.transdroid.core.gui.log.*; |
||||
import org.transdroid.core.gui.navigation.*; |
||||
|
||||
/** |
||||
* Receives the intent that the device has been started in order to set up proper alarms for all background services. |
||||
* |
||||
* @author Eric Kok |
||||
*/ |
||||
@EReceiver |
||||
public class BootReceiver extends BroadcastReceiver { |
||||
|
||||
public static final int ALARM_SERVERCHECKER = 0; |
||||
public static final int ALARM_RSSCHECKER = 1; |
||||
public static final int ALARM_APPUPDATES = 2; |
||||
|
||||
public static PendingIntent piServerChecker = null, piRssChecker = null, piAppUpdates = null; |
||||
|
||||
@Bean |
||||
protected Log log; |
||||
|
||||
public static void startBackgroundServices(Context context, boolean forceReload) { |
||||
NotificationSettings notificationSettings = NotificationSettings_.getInstance_(context); |
||||
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
||||
// Start the alarms if one of the notifications are enabled and we do not yet have the alarms running
|
||||
// (or should reload it forcefully)
|
||||
if ((notificationSettings.isEnabledForRss() || notificationSettings.isEnabledForTorrents()) && |
||||
(forceReload || (piServerChecker == null && piRssChecker == null))) { |
||||
|
||||
Log_.getInstance_(context) |
||||
.d("BootReceiver", "Boot signal received, starting server and rss checker background services"); |
||||
// Schedule repeating alarms, with the first being (somewhat) in 1 second from now
|
||||
piServerChecker = PendingIntent.getBroadcast(context, ALARM_SERVERCHECKER, |
||||
new Intent(context, AlarmReceiver_.class).putExtra("service", ALARM_SERVERCHECKER), 0); |
||||
piRssChecker = PendingIntent.getBroadcast(context, ALARM_RSSCHECKER, |
||||
new Intent(context, AlarmReceiver_.class).putExtra("service", ALARM_RSSCHECKER), 0); |
||||
alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, |
||||
notificationSettings.getInvervalInMilliseconds(), piServerChecker); |
||||
alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, |
||||
notificationSettings.getInvervalInMilliseconds(), piRssChecker); |
||||
|
||||
} |
||||
} |
||||
|
||||
public static void startAppUpdatesService(Context context) { |
||||
SystemSettings systemSettings = SystemSettings_.getInstance_(context); |
||||
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
||||
if (NavigationHelper_.getInstance_(context).enableUpdateChecker() && systemSettings.checkForUpdates() && |
||||
piAppUpdates == null) { |
||||
|
||||
Log_.getInstance_(context).d("BootReceiver", "Boot signal received, starting app update checker service"); |
||||
// Schedule a daily, with the first being (somewhat) in 1 second from now
|
||||
piAppUpdates = PendingIntent.getBroadcast(context, ALARM_APPUPDATES, |
||||
new Intent(context, AlarmReceiver_.class).putExtra("service", ALARM_APPUPDATES), 0); |
||||
alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, |
||||
AlarmManager.INTERVAL_DAY, piAppUpdates); |
||||
|
||||
} |
||||
} |
||||
|
||||
public static void cancelBackgroundServices(Context context) { |
||||
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
||||
if (piServerChecker != null) { |
||||
alarms.cancel(piServerChecker); |
||||
piServerChecker = null; |
||||
} |
||||
if (piRssChecker != null) { |
||||
alarms.cancel(piRssChecker); |
||||
piRssChecker = null; |
||||
} |
||||
} |
||||
|
||||
public static void cancelAppUpdates(Context context) { |
||||
if (piAppUpdates != null) { |
||||
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
||||
alarms.cancel(piAppUpdates); |
||||
piAppUpdates = null; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onReceive(Context context, Intent intent) { |
||||
startBackgroundServices(context, false); |
||||
startAppUpdatesService(context); |
||||
// Ensure user-requested background jobs are scheduled
|
||||
ServerCheckerJob.schedule(context); |
||||
RssCheckerJob.schedule(context); |
||||
AppUpdateJob.schedule(context); |
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2010-2013 Eric Kok et al. |
||||
* |
||||
* Transdroid 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. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.transdroid.core.service; |
||||
|
||||
import android.content.Context; |
||||
import android.support.annotation.NonNull; |
||||
import com.evernote.android.job.Job; |
||||
import com.evernote.android.job.JobManager; |
||||
import com.evernote.android.job.JobRequest; |
||||
import org.transdroid.core.app.settings.NotificationSettings; |
||||
import org.transdroid.core.app.settings.NotificationSettings_; |
||||
import org.transdroid.core.app.settings.SystemSettings; |
||||
import org.transdroid.core.app.settings.SystemSettings_; |
||||
import org.transdroid.core.gui.log.Log_; |
||||
import org.transdroid.core.gui.navigation.NavigationHelper_; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
public class RssCheckerJob extends Job { |
||||
|
||||
static final String TAG = "rss_checker"; |
||||
|
||||
private static Integer scheduledJobId; |
||||
|
||||
public static void schedule(Context context) { |
||||
NotificationSettings notificationSettings = NotificationSettings_.getInstance_(context); |
||||
if (notificationSettings.isEnabledForRss()) { |
||||
Log_.getInstance_(context).d(TAG, "Schedule rss checker job"); |
||||
scheduledJobId = new JobRequest.Builder(RssCheckerJob.TAG) |
||||
.setPeriodic(notificationSettings.getInvervalInMilliseconds()) |
||||
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) |
||||
.setUpdateCurrent(true) |
||||
.build() |
||||
.schedule(); |
||||
} else if (scheduledJobId != null) { |
||||
Log_.getInstance_(context).d(TAG, "Cancel rss checker job"); |
||||
JobManager.instance().cancel(scheduledJobId); |
||||
} |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
protected Result onRunJob(@NonNull Params params) { |
||||
return RssCheckerJobRunner_.getInstance_(getContext()).run(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2010-2013 Eric Kok et al. |
||||
* |
||||
* Transdroid 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. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.transdroid.core.service; |
||||
|
||||
import android.support.annotation.NonNull; |
||||
import android.support.annotation.Nullable; |
||||
import com.evernote.android.job.Job; |
||||
import com.evernote.android.job.JobCreator; |
||||
|
||||
public class ScheduledJobCreator implements JobCreator { |
||||
|
||||
@Nullable |
||||
@Override |
||||
public Job create(@NonNull String tag) { |
||||
switch (tag) { |
||||
case AppUpdateJob.TAG: |
||||
return new AppUpdateJob(); |
||||
case RssCheckerJob.TAG: |
||||
return new RssCheckerJob(); |
||||
case ServerCheckerJob.TAG: |
||||
return new ServerCheckerJob(); |
||||
default: |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
/* |
||||
* Copyright 2010-2013 Eric Kok et al. |
||||
* |
||||
* Transdroid 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. |
||||
* |
||||
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.transdroid.core.service; |
||||
|
||||
import android.content.Context; |
||||
import android.support.annotation.NonNull; |
||||
import com.evernote.android.job.Job; |
||||
import com.evernote.android.job.JobManager; |
||||
import com.evernote.android.job.JobRequest; |
||||
import org.transdroid.core.app.settings.NotificationSettings; |
||||
import org.transdroid.core.app.settings.NotificationSettings_; |
||||
import org.transdroid.core.gui.log.Log_; |
||||
|
||||
public class ServerCheckerJob extends Job { |
||||
|
||||
static final String TAG = "server_checker"; |
||||
|
||||
private static Integer scheduledJobId; |
||||
|
||||
public static void schedule(Context context) { |
||||
NotificationSettings notificationSettings = NotificationSettings_.getInstance_(context); |
||||
if (notificationSettings.isEnabledForTorrents()) { |
||||
Log_.getInstance_(context).d(TAG, "Schedule server checker job"); |
||||
scheduledJobId = new JobRequest.Builder(ServerCheckerJob.TAG) |
||||
.setPeriodic(notificationSettings.getInvervalInMilliseconds()) |
||||
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) |
||||
.setUpdateCurrent(true) |
||||
.build() |
||||
.schedule(); |
||||
} else if (scheduledJobId != null) { |
||||
Log_.getInstance_(context).d(TAG, "Cancel server checker job"); |
||||
JobManager.instance().cancel(scheduledJobId); |
||||
} |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
protected Result onRunJob(@NonNull Params params) { |
||||
return ServerCheckerJobRunner_.getInstance_(getContext()).run(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue