forked from GitHub-Mirror/riotX-android
Move specific classes for FDroid into FDroid variant source code
This commit is contained in:
@ -68,15 +68,6 @@
|
||||
<service
|
||||
android:name=".core.services.CallService"
|
||||
android:exported="false" />
|
||||
<!--<service-->
|
||||
<!--android:name="im.vector.matrix.android.internal.session.sync.job.SyncService"-->
|
||||
<!--android:exported="false" />-->
|
||||
|
||||
<service
|
||||
android:name=".core.services.VectorSyncService"
|
||||
android:exported="false">
|
||||
|
||||
</service>
|
||||
|
||||
<!-- Receivers -->
|
||||
|
||||
|
@ -36,7 +36,6 @@ import com.github.piasy.biv.loader.glide.GlideImageLoader
|
||||
import com.jakewharton.threetenabp.AndroidThreeTen
|
||||
import im.vector.matrix.android.api.Matrix
|
||||
import im.vector.riotredesign.core.di.AppModule
|
||||
import im.vector.riotredesign.core.services.AlarmSyncBroadcastReceiver
|
||||
import im.vector.riotredesign.features.configuration.VectorConfiguration
|
||||
import im.vector.riotredesign.features.crypto.keysbackup.KeysBackupModule
|
||||
import im.vector.riotredesign.features.home.HomeModule
|
||||
@ -47,7 +46,6 @@ import im.vector.riotredesign.features.notifications.PushRuleTriggerListener
|
||||
import im.vector.riotredesign.features.rageshake.VectorFileLogger
|
||||
import im.vector.riotredesign.features.rageshake.VectorUncaughtExceptionHandler
|
||||
import im.vector.riotredesign.features.roomdirectory.RoomDirectoryModule
|
||||
import im.vector.riotredesign.features.settings.PreferencesManager
|
||||
import im.vector.riotredesign.features.version.getVersion
|
||||
import im.vector.riotredesign.push.fcm.FcmHelper
|
||||
import org.koin.android.ext.android.get
|
||||
@ -117,7 +115,7 @@ class VectorApplication : Application() {
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
|
||||
fun entersForeground() {
|
||||
AlarmSyncBroadcastReceiver.cancelAlarm(appContext)
|
||||
FcmHelper.onEnterForeground(appContext)
|
||||
Matrix.getInstance().currentSession?.also {
|
||||
it.stopAnyBackgroundSync()
|
||||
}
|
||||
@ -129,20 +127,8 @@ class VectorApplication : Application() {
|
||||
|
||||
notificationDrawerManager.persistInfo()
|
||||
|
||||
if (FcmHelper.isPushSupported()) {
|
||||
//TODO FCM fallback
|
||||
} else {
|
||||
//TODO check if notifications are enabled for this device
|
||||
//We need to use alarm in this mode
|
||||
if (PreferencesManager.areNotificationEnabledForDevice(applicationContext)) {
|
||||
if (Matrix.getInstance().currentSession != null) {
|
||||
AlarmSyncBroadcastReceiver.scheduleAlarm(applicationContext, 4_000L)
|
||||
Timber.i("Alarm scheduled to restart service")
|
||||
}
|
||||
}
|
||||
}
|
||||
FcmHelper.onEnterBackground(appContext, Matrix.getInstance().currentSession != null)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
@ -1,73 +0,0 @@
|
||||
package im.vector.riotredesign.core.services
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.PowerManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import timber.log.Timber
|
||||
|
||||
class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
|
||||
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
|
||||
//Aquire a lock to give enough time for the sync :/
|
||||
(context.getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
||||
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "riotx:fdroidSynclock").apply {
|
||||
acquire((10_000).toLong())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// This method is called when the BroadcastReceiver is receiving an Intent broadcast.
|
||||
Timber.d("RestartBroadcastReceiver received intent")
|
||||
Intent(context, VectorSyncService::class.java).also {
|
||||
it.action = "SLOW"
|
||||
context.startService(it)
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
ContextCompat.startForegroundService(context, intent)
|
||||
} else {
|
||||
context.startService(intent)
|
||||
}
|
||||
} catch (ex: Throwable) {
|
||||
//TODO
|
||||
Timber.e(ex)
|
||||
}
|
||||
}
|
||||
|
||||
scheduleAlarm(context, 30_000L)
|
||||
|
||||
Timber.i("Alarm scheduled to restart service")
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val REQUEST_CODE = 0
|
||||
|
||||
fun scheduleAlarm(context: Context, delay: Long) {
|
||||
//Reschedule
|
||||
val intent = Intent(context, AlarmSyncBroadcastReceiver::class.java)
|
||||
val pIntent = PendingIntent.getBroadcast(context, AlarmSyncBroadcastReceiver.REQUEST_CODE,
|
||||
intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val firstMillis = System.currentTimeMillis() + delay
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pIntent)
|
||||
} else {
|
||||
alarmMgr.set(AlarmManager.RTC_WAKEUP, firstMillis, pIntent)
|
||||
}
|
||||
}
|
||||
|
||||
fun cancelAlarm(context: Context) {
|
||||
val intent = Intent(context, AlarmSyncBroadcastReceiver::class.java)
|
||||
val pIntent = PendingIntent.getBroadcast(context, AlarmSyncBroadcastReceiver.REQUEST_CODE,
|
||||
intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmMgr.cancel(pIntent)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package im.vector.riotredesign.core.services
|
||||
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import im.vector.matrix.android.internal.session.sync.job.SyncService
|
||||
import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.features.notifications.NotificationUtils
|
||||
import timber.log.Timber
|
||||
|
||||
class VectorSyncService : SyncService() {
|
||||
|
||||
override fun onCreate() {
|
||||
Timber.v("VectorSyncService - onCreate ")
|
||||
super.onCreate()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
Timber.v("VectorSyncService - onDestroy ")
|
||||
removeForegroundNotif()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun removeForegroundNotif() {
|
||||
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.cancel(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE)
|
||||
}
|
||||
|
||||
/**
|
||||
* Service is started only in fdroid mode when no FCM is available
|
||||
* Otherwise it is bounded
|
||||
*/
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
Timber.v("VectorSyncService - onStartCommand ")
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val notification = NotificationUtils.buildForegroundServiceNotification(applicationContext, R.string.notification_listening_for_events, false)
|
||||
startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification)
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId)
|
||||
}
|
||||
|
||||
/**
|
||||
* If the service is bounded and the service was previously started we can remove foreground notif
|
||||
*/
|
||||
override fun onBind(intent: Intent?): IBinder {
|
||||
Timber.v("VectorSyncService - onBind ")
|
||||
stopForeground(true)
|
||||
return super.onBind(intent)
|
||||
}
|
||||
|
||||
override fun onUnbind(intent: Intent?): Boolean {
|
||||
Timber.v("VectorSyncService - onUnbind ")
|
||||
return super.onUnbind(intent)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user