Skip to content

Commit

Permalink
fix backup, notification for android 33+
Browse files Browse the repository at this point in the history
  • Loading branch information
tom5079 committed Jan 14, 2024
1 parent f36b7f1 commit 6d108dd
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 115 deletions.
16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ configurations {
}

android {
compileSdkVersion 32
compileSdkVersion 34
defaultConfig {
applicationId "xyz.quaver.pupil"
minSdkVersion 16
targetSdkVersion 32
targetSdkVersion 34
versionCode 69
versionName "5.3.8-hotfix1"
versionName "5.3.9"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down Expand Up @@ -79,7 +79,7 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2"
implementation "org.jetbrains.kotlinx:kotlinx-datetime:0.3.2"

Expand All @@ -95,15 +95,15 @@ dependencies {

implementation "com.daimajia.swipelayout:library:1.2.0@aar"

implementation "com.google.android.material:material:1.5.0"
implementation "com.google.android.material:material:1.11.0"

implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation platform('com.google.firebase:firebase-bom:32.7.0')
implementation "com.google.firebase:firebase-analytics-ktx"
implementation "com.google.firebase:firebase-crashlytics-ktx"
implementation "com.google.firebase:firebase-perf-ktx"

implementation "com.google.android.gms:play-services-oss-licenses:17.0.0"
implementation "com.google.android.gms:play-services-mlkit-face-detection:17.0.1"
implementation "com.google.android.gms:play-services-oss-licenses:17.0.1"
implementation "com.google.android.gms:play-services-mlkit-face-detection:17.1.0"

implementation "com.github.clans:fab:1.6.4"

Expand Down
2 changes: 1 addition & 1 deletion app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"filters": [],
"attributes": [],
"versionCode": 69,
"versionName": "5.3.8-hotfix1",
"versionName": "5.3.9",
"outputFile": "app-release.apk"
}
],
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
Expand Down Expand Up @@ -45,7 +47,8 @@
</provider>

<service android:name=".services.DownloadService"
android:exported="false"/>
android:exported="false"
android:foregroundServiceType="specialUse" />

<receiver
android:name=".receiver.UpdateBroadcastReceiver"
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/xyz/quaver/pupil/services/DownloadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
Expand Down Expand Up @@ -100,13 +101,15 @@ class DownloadService : Service() {
notify(galleryID)
}

@SuppressLint("RestrictedApi")
@SuppressLint("RestrictedApi", "MissingPermission")
private fun notify(galleryID: Int) {
val max = progress[galleryID]?.size ?: 0
val progress = progress[galleryID]?.count { it == Float.POSITIVE_INFINITY } ?: 0

val notification = notification[galleryID] ?: return

if (!checkNotificationEnabled(this)) return

if (isCompleted(galleryID)) {
notification
.setContentText(getString(R.string.reader_notification_complete))
Expand Down Expand Up @@ -403,7 +406,11 @@ class DownloadService : Service() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startForeground(R.id.downloader_notification_id, serviceNotification.build())
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU) {
startForeground(R.id.downloader_notification_id, serviceNotification.build())
} else {
startForeground(R.id.downloader_notification_id, serviceNotification.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
}

when (intent?.getStringExtra(KEY_COMMAND)) {
COMMAND_DOWNLOAD -> intent.getIntExtra(KEY_ID, -1).let { if (it > 0)
Expand All @@ -424,7 +431,11 @@ class DownloadService : Service() {
override fun onBind(p0: Intent?) = binder

override fun onCreate() {
startForeground(R.id.downloader_notification_id, serviceNotification.build())
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU) {
startForeground(R.id.downloader_notification_id, serviceNotification.build())
} else {
startForeground(R.id.downloader_notification_id, serviceNotification.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
}
interceptors[Tag::class] = interceptor
}

Expand Down
31 changes: 25 additions & 6 deletions app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

package xyz.quaver.pupil.ui

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand All @@ -31,7 +33,9 @@ import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.core.view.ViewCompat
Expand All @@ -58,10 +62,12 @@ import xyz.quaver.pupil.ui.view.MainView
import xyz.quaver.pupil.ui.view.ProgressCard
import xyz.quaver.pupil.util.ItemClickSupport
import xyz.quaver.pupil.util.Preferences
import xyz.quaver.pupil.util.requestNotificationPermission
import xyz.quaver.pupil.util.checkUpdate
import xyz.quaver.pupil.util.downloader.Cache
import xyz.quaver.pupil.util.downloader.DownloadManager
import xyz.quaver.pupil.util.restore
import xyz.quaver.pupil.util.showNotificationPermissionExplanationDialog
import java.util.regex.Pattern
import kotlin.math.ceil
import kotlin.math.max
Expand Down Expand Up @@ -107,6 +113,12 @@ class MainActivity :

private lateinit var binding: MainActivityBinding

private val requestNotificationPermssionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (!isGranted) {
showNotificationPermissionExplanationDialog(this)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = MainActivityBinding.inflate(layoutInflater)
Expand All @@ -124,6 +136,8 @@ class MainActivity :
}
}

requestNotificationPermission(this, requestNotificationPermssionLauncher, false) {}

if (Preferences["download_folder", ""].isEmpty())
DownloadLocationDialogFragment().show(supportFragmentManager, "Download Location Dialog")

Expand Down Expand Up @@ -392,12 +406,17 @@ class MainActivity :
onDownloadClickedHandler = { position ->
val galleryID = galleries[position]

if (DownloadManager.getInstance(context).isDownloading(galleryID)) { //download in progress
DownloadService.cancel(this@MainActivity, galleryID)
}
else {
DownloadManager.getInstance(context).addDownloadFolder(galleryID)
DownloadService.download(this@MainActivity, galleryID)
requestNotificationPermission(
this@MainActivity,
requestNotificationPermssionLauncher
) {
if (DownloadManager.getInstance(context).isDownloading(galleryID)) { //download in progress
DownloadService.cancel(this@MainActivity, galleryID)
}
else {
DownloadManager.getInstance(context).addDownloadFolder(galleryID)
DownloadService.download(this@MainActivity, galleryID)
}
}

closeAllItems()
Expand Down
32 changes: 23 additions & 9 deletions app/src/main/java/xyz/quaver/pupil/ui/ReaderActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ import xyz.quaver.pupil.favorites
import xyz.quaver.pupil.services.DownloadService
import xyz.quaver.pupil.util.Preferences
import xyz.quaver.pupil.util.camera
import xyz.quaver.pupil.util.checkNotificationEnabled
import xyz.quaver.pupil.util.closeCamera
import xyz.quaver.pupil.util.downloader.Cache
import xyz.quaver.pupil.util.downloader.DownloadManager
import xyz.quaver.pupil.util.requestNotificationPermission
import xyz.quaver.pupil.util.showNotificationPermissionExplanationDialog
import xyz.quaver.pupil.util.startCamera

class ReaderActivity : BaseActivity() {
Expand Down Expand Up @@ -117,6 +120,12 @@ class ReaderActivity : BaseActivity() {

private lateinit var binding: ReaderActivityBinding

private val requestNotificationPermssionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (!isGranted) {
showNotificationPermissionExplanationDialog(this)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ReaderActivityBinding.inflate(layoutInflater)
Expand Down Expand Up @@ -360,15 +369,20 @@ class ReaderActivity : BaseActivity() {
animateDownloadFAB(DownloadManager.getInstance(this@ReaderActivity).getDownloadFolder(galleryID) != null) //If download in progress, animate button

setOnClickListener {
val downloadManager = DownloadManager.getInstance(this@ReaderActivity)

if (downloadManager.isDownloading(galleryID)) {
downloadManager.deleteDownloadFolder(galleryID)
animateDownloadFAB(false)
} else {
downloadManager.addDownloadFolder(galleryID)
DownloadService.download(context, galleryID, true)
animateDownloadFAB(true)
requestNotificationPermission(
this@ReaderActivity,
requestNotificationPermssionLauncher
) {
val downloadManager = DownloadManager.getInstance(this@ReaderActivity)

if (downloadManager.isDownloading(galleryID)) {
downloadManager.deleteDownloadFolder(galleryID)
animateDownloadFAB(false)
} else {
downloadManager.addDownloadFolder(galleryID)
DownloadService.download(context, galleryID, true)
animateDownloadFAB(true)
}
}
}
}
Expand Down
Loading

0 comments on commit 6d108dd

Please sign in to comment.