Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #957 from corona-warn-app/dev
Browse files Browse the repository at this point in the history
Dev 1.2.0 to Staging
  • Loading branch information
pwoessner authored Jul 31, 2020
2 parents 5ef86cc + 2d02523 commit 795f82e
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.Application
import android.content.Context
import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.net.wifi.WifiManager
import android.os.Bundle
import android.os.PowerManager
import androidx.lifecycle.Lifecycle
Expand All @@ -21,10 +22,12 @@ import de.rki.coronawarnapp.notification.NotificationHelper
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction
import de.rki.coronawarnapp.util.ConnectivityHelper
import de.rki.coronawarnapp.worker.BackgroundWorkHelper
import de.rki.coronawarnapp.worker.BackgroundWorkScheduler
import kotlinx.coroutines.launch
import org.conscrypt.Conscrypt
import timber.log.Timber
import java.security.Security
import java.util.UUID

class CoronaWarnApplication : Application(), LifecycleObserver,
Application.ActivityLifecycleCallbacks, Configuration.Provider {
Expand Down Expand Up @@ -73,10 +76,25 @@ class CoronaWarnApplication : Application(), LifecycleObserver,
// job execution
val wakeLock: PowerManager.WakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG).apply {
newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
TAG + "-WAKE-" + UUID.randomUUID().toString()
).apply {
acquire(TEN_MINUTE_TIMEOUT_IN_MS)
}
}

// we keep a wifi lock to wake up the wifi connection in case the device is dozing
val wifiLock: WifiManager.WifiLock =
(getSystemService(Context.WIFI_SERVICE) as WifiManager).run {
createWifiLock(
WifiManager.WIFI_MODE_FULL_HIGH_PERF,
TAG + "-WIFI-" + UUID.randomUUID().toString()
).apply {
acquire()
}
}

try {
BackgroundWorkHelper.sendDebugNotification(
"Automatic mode is on", "Check if we have downloaded keys already today"
Expand All @@ -85,12 +103,16 @@ class CoronaWarnApplication : Application(), LifecycleObserver,
} catch (e: Exception) {
BackgroundWorkHelper.sendDebugNotification(
"RetrieveDiagnosisKeysTransaction failed",
e.localizedMessage ?: "Unknown exception occurred in onCreate"
(e.localizedMessage
?: "Unknown exception occurred in onCreate") + "\n\n" + (e.cause
?: "Cause is unknown").toString()
)
wakeLock.release()
// retry the key retrieval in case of an error with a scheduled work
BackgroundWorkScheduler.scheduleDiagnosisKeyOneTimeWork()
}

wakeLock.release()
if (wifiLock.isHeld) wifiLock.release()
if (wakeLock.isHeld) wakeLock.release()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,27 @@ object NotificationHelper {
*
* @see NotificationCompat.VISIBILITY_PUBLIC
*/
private fun buildNotification(title: String, content: String, visibility: Int): Notification? {
private fun buildNotification(
title: String,
content: String,
visibility: Int,
expandableLongText: Boolean = false
): Notification? {
val builder = NotificationCompat.Builder(CoronaWarnApplication.getAppContext(), channelId)
.setSmallIcon(NotificationConstants.NOTIFICATION_SMALL_ICON)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(visibility)
.setContentIntent(createPendingIntentToMainActivity())
.setAutoCancel(true)

if (expandableLongText) {
builder
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(content)
)
}

if (title.isNotEmpty()) {
builder.setContentTitle(title)
}
Expand Down Expand Up @@ -142,8 +155,14 @@ object NotificationHelper {
* @param content: String
* @param visibility: Int
*/
fun sendNotification(title: String, content: String, visibility: Int) {
val notification = buildNotification(title, content, visibility) ?: return
fun sendNotification(
title: String,
content: String,
visibility: Int,
expandableLongText: Boolean = false
) {
val notification =
buildNotification(title, content, visibility, expandableLongText) ?: return
with(NotificationManagerCompat.from(CoronaWarnApplication.getAppContext())) {
notify(Random.nextInt(), notification)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
currentDate.withTimeAtStartOfDay() != lastFetch.withTimeAtStartOfDay()
) {
BackgroundWorkHelper.sendDebugNotification(
"Start RetrieveDiagnosisKeysTransaction", "No keys fetched today yet"
"Start RetrieveDiagnosisKeysTransaction",
"No keys fetched today yet \n${DateTime.now()}\nUTC: $currentDate"
)
start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ object BackgroundWorkHelper {
*/
fun sendDebugNotification(title: String, content: String) {
if (!LocalData.backgroundNotification()) return
NotificationHelper.sendNotification(title, content, NotificationCompat.PRIORITY_HIGH)
NotificationHelper.sendNotification(title, content, NotificationCompat.PRIORITY_HIGH, true)
}
}
Loading

0 comments on commit 795f82e

Please sign in to comment.