2017-01-06 14:38:05 +01:00
|
|
|
package org.mariotaku.twidere.util
|
|
|
|
|
|
|
|
import android.content.Context
|
2017-04-17 15:10:14 +02:00
|
|
|
import android.content.SharedPreferences
|
2017-01-06 14:38:05 +01:00
|
|
|
import android.support.annotation.StringDef
|
2017-04-13 05:30:40 +02:00
|
|
|
import android.util.Log
|
2017-01-06 14:38:05 +01:00
|
|
|
import com.squareup.otto.Bus
|
|
|
|
import org.mariotaku.abstask.library.AbstractTask
|
|
|
|
import org.mariotaku.abstask.library.TaskStarter
|
2017-04-17 15:10:14 +02:00
|
|
|
import org.mariotaku.kpreferences.get
|
2017-04-14 11:34:36 +02:00
|
|
|
import org.mariotaku.ktextension.mapToArray
|
2017-02-10 16:38:59 +01:00
|
|
|
import org.mariotaku.ktextension.toNulls
|
2017-04-13 05:30:40 +02:00
|
|
|
import org.mariotaku.twidere.TwidereConstants.LOGTAG
|
2017-01-06 14:38:05 +01:00
|
|
|
import org.mariotaku.twidere.constant.IntentConstants.INTENT_PACKAGE_PREFIX
|
|
|
|
import org.mariotaku.twidere.constant.dataSyncProviderInfoKey
|
|
|
|
import org.mariotaku.twidere.constant.stopAutoRefreshWhenBatteryLowKey
|
|
|
|
import org.mariotaku.twidere.model.AccountPreferences
|
2017-04-21 18:32:03 +02:00
|
|
|
import org.mariotaku.twidere.model.RefreshTaskParam
|
2017-01-06 14:38:05 +01:00
|
|
|
import org.mariotaku.twidere.model.UserKey
|
2017-04-21 18:32:03 +02:00
|
|
|
import org.mariotaku.twidere.model.pagination.Pagination
|
|
|
|
import org.mariotaku.twidere.model.pagination.SinceMaxPagination
|
2017-02-10 16:38:59 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Activities
|
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Statuses
|
2017-03-12 14:27:55 +01:00
|
|
|
import org.mariotaku.twidere.task.filter.RefreshFiltersSubscriptionsTask
|
2017-02-16 07:50:25 +01:00
|
|
|
import org.mariotaku.twidere.task.twitter.GetActivitiesAboutMeTask
|
|
|
|
import org.mariotaku.twidere.task.twitter.GetHomeTimelineTask
|
|
|
|
import org.mariotaku.twidere.task.twitter.message.GetMessagesTask
|
2017-01-06 14:38:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 2017/1/6.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class TaskServiceRunner(
|
|
|
|
val context: Context,
|
2017-04-17 15:10:14 +02:00
|
|
|
val preferences: SharedPreferences,
|
2017-04-24 06:48:47 +02:00
|
|
|
val activityTracker: ActivityTracker,
|
2017-01-06 14:38:05 +01:00
|
|
|
val bus: Bus
|
|
|
|
) {
|
|
|
|
|
|
|
|
fun runTask(@Action action: String, callback: (Boolean) -> Unit): Boolean {
|
2017-04-13 05:30:40 +02:00
|
|
|
Log.d(LOGTAG, "TaskServiceRunner run task $action")
|
2017-01-06 14:38:05 +01:00
|
|
|
when (action) {
|
2017-01-12 17:26:44 +01:00
|
|
|
ACTION_REFRESH_HOME_TIMELINE, ACTION_REFRESH_NOTIFICATIONS,
|
|
|
|
ACTION_REFRESH_DIRECT_MESSAGES, ACTION_REFRESH_FILTERS_SUBSCRIPTIONS -> {
|
2017-01-06 14:38:05 +01:00
|
|
|
val task = createRefreshTask(action) ?: return false
|
|
|
|
task.callback = callback
|
|
|
|
TaskStarter.execute(task)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
ACTION_SYNC_DRAFTS, ACTION_SYNC_FILTERS, ACTION_SYNC_USER_NICKNAMES, ACTION_SYNC_USER_COLORS -> {
|
|
|
|
val runner = preferences[dataSyncProviderInfoKey]?.newSyncTaskRunner(context) ?: return false
|
|
|
|
return runner.runTask(action, callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fun createRefreshTask(@Action action: String): AbstractTask<*, *, (Boolean) -> Unit>? {
|
|
|
|
if (!Utils.isBatteryOkay(context) && preferences[stopAutoRefreshWhenBatteryLowKey]) {
|
|
|
|
// Low battery, don't refresh
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
when (action) {
|
|
|
|
ACTION_REFRESH_HOME_TIMELINE -> {
|
|
|
|
val task = GetHomeTimelineTask(context)
|
2017-04-24 06:48:47 +02:00
|
|
|
task.params = AutoRefreshTaskParam(context, preferences, activityTracker.isEmpty,
|
2017-04-17 15:10:14 +02:00
|
|
|
AccountPreferences::isAutoRefreshHomeTimelineEnabled) { accountKeys ->
|
2017-04-24 06:48:47 +02:00
|
|
|
DataStoreUtils.getNewestStatusIds(context, Statuses.CONTENT_URI,
|
|
|
|
accountKeys.toNulls())
|
2017-01-06 14:38:05 +01:00
|
|
|
}
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
ACTION_REFRESH_NOTIFICATIONS -> {
|
|
|
|
val task = GetActivitiesAboutMeTask(context)
|
2017-04-24 06:48:47 +02:00
|
|
|
task.params = AutoRefreshTaskParam(context, preferences, activityTracker.isEmpty,
|
2017-04-17 15:10:14 +02:00
|
|
|
AccountPreferences::isAutoRefreshMentionsEnabled) { accountKeys ->
|
2017-03-26 12:05:34 +02:00
|
|
|
DataStoreUtils.getRefreshNewestActivityMaxPositions(context,
|
|
|
|
Activities.AboutMe.CONTENT_URI, accountKeys.toNulls())
|
2017-01-06 14:38:05 +01:00
|
|
|
}
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
ACTION_REFRESH_DIRECT_MESSAGES -> {
|
2017-02-08 17:45:20 +01:00
|
|
|
val task = GetMessagesTask(context)
|
2017-02-14 17:26:48 +01:00
|
|
|
task.params = object : GetMessagesTask.RefreshNewTaskParam(context) {
|
2017-04-23 18:40:32 +02:00
|
|
|
|
2017-04-24 06:48:47 +02:00
|
|
|
override val isBackground: Boolean = activityTracker.isEmpty
|
2017-04-23 18:40:32 +02:00
|
|
|
|
2017-02-14 17:26:48 +01:00
|
|
|
override val accountKeys: Array<UserKey> by lazy {
|
2017-04-17 15:10:14 +02:00
|
|
|
AccountPreferences.getAccountPreferences(context, preferences,
|
|
|
|
DataStoreUtils.getAccountKeys(context)).filter {
|
2017-02-14 17:26:48 +01:00
|
|
|
it.isAutoRefreshEnabled && it.isAutoRefreshDirectMessagesEnabled
|
2017-04-14 11:34:36 +02:00
|
|
|
}.mapToArray(AccountPreferences::accountKey)
|
2017-02-14 17:26:48 +01:00
|
|
|
}
|
2017-01-06 14:38:05 +01:00
|
|
|
}
|
|
|
|
return task
|
|
|
|
}
|
2017-01-12 17:26:44 +01:00
|
|
|
ACTION_REFRESH_FILTERS_SUBSCRIPTIONS -> {
|
|
|
|
return RefreshFiltersSubscriptionsTask(context)
|
|
|
|
}
|
2017-01-06 14:38:05 +01:00
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:26:44 +01:00
|
|
|
@StringDef(ACTION_REFRESH_HOME_TIMELINE, ACTION_REFRESH_NOTIFICATIONS, ACTION_REFRESH_DIRECT_MESSAGES,
|
|
|
|
ACTION_REFRESH_FILTERS_SUBSCRIPTIONS, ACTION_SYNC_DRAFTS, ACTION_SYNC_FILTERS,
|
2017-01-06 14:38:05 +01:00
|
|
|
ACTION_SYNC_USER_NICKNAMES, ACTION_SYNC_USER_COLORS)
|
|
|
|
@Retention(AnnotationRetention.SOURCE)
|
|
|
|
annotation class Action
|
|
|
|
|
|
|
|
class AutoRefreshTaskParam(
|
|
|
|
val context: Context,
|
2017-04-17 15:10:14 +02:00
|
|
|
val preferences: SharedPreferences,
|
2017-04-24 06:48:47 +02:00
|
|
|
override val isBackground: Boolean,
|
2017-01-06 14:38:05 +01:00
|
|
|
val refreshable: (AccountPreferences) -> Boolean,
|
|
|
|
val getSinceIds: (Array<UserKey>) -> Array<String?>?
|
2017-04-21 18:32:03 +02:00
|
|
|
) : RefreshTaskParam {
|
|
|
|
|
2017-02-10 16:38:59 +01:00
|
|
|
override val accountKeys: Array<UserKey> by lazy {
|
2017-04-17 15:10:14 +02:00
|
|
|
return@lazy AccountPreferences.getAccountPreferences(context, preferences,
|
|
|
|
DataStoreUtils.getAccountKeys(context)).filter {
|
2017-02-10 16:38:59 +01:00
|
|
|
it.isAutoRefreshEnabled && refreshable(it)
|
2017-04-14 11:34:36 +02:00
|
|
|
}.mapToArray(AccountPreferences::accountKey)
|
2017-01-06 14:38:05 +01:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:32:03 +02:00
|
|
|
override val pagination: Array<Pagination?>?
|
|
|
|
get() = getSinceIds(accountKeys)?.mapToArray { sinceId ->
|
|
|
|
SinceMaxPagination().also { it.sinceId = sinceId }
|
|
|
|
}
|
2017-01-06 14:38:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
@Action
|
|
|
|
const val ACTION_REFRESH_HOME_TIMELINE = INTENT_PACKAGE_PREFIX + "REFRESH_HOME_TIMELINE"
|
|
|
|
@Action
|
|
|
|
const val ACTION_REFRESH_NOTIFICATIONS = INTENT_PACKAGE_PREFIX + "REFRESH_NOTIFICATIONS"
|
|
|
|
@Action
|
|
|
|
const val ACTION_REFRESH_DIRECT_MESSAGES = INTENT_PACKAGE_PREFIX + "REFRESH_DIRECT_MESSAGES"
|
|
|
|
@Action
|
2017-01-12 17:26:44 +01:00
|
|
|
const val ACTION_REFRESH_FILTERS_SUBSCRIPTIONS = INTENT_PACKAGE_PREFIX + "REFRESH_FILTERS_SUBSCRIPTIONS"
|
|
|
|
@Action
|
2017-01-06 14:38:05 +01:00
|
|
|
const val ACTION_SYNC_DRAFTS = INTENT_PACKAGE_PREFIX + "SYNC_DRAFTS"
|
|
|
|
@Action
|
|
|
|
const val ACTION_SYNC_FILTERS = INTENT_PACKAGE_PREFIX + "SYNC_FILTERS"
|
|
|
|
@Action
|
|
|
|
const val ACTION_SYNC_USER_NICKNAMES = INTENT_PACKAGE_PREFIX + "SYNC_USER_NICKNAMES"
|
|
|
|
@Action
|
|
|
|
const val ACTION_SYNC_USER_COLORS = INTENT_PACKAGE_PREFIX + "SYNC_USER_COLORS"
|
|
|
|
|
|
|
|
val ACTIONS_SYNC = arrayOf(ACTION_SYNC_DRAFTS, ACTION_SYNC_FILTERS, ACTION_SYNC_USER_COLORS,
|
|
|
|
ACTION_SYNC_USER_NICKNAMES)
|
|
|
|
}
|
|
|
|
|
|
|
|
data class SyncFinishedEvent(val syncType: String, val success: Boolean)
|
|
|
|
|
|
|
|
}
|
2017-01-12 17:26:44 +01:00
|
|
|
|