Skip to content

Commit

Permalink
AppNotifier 1.2.1 (release 1)
Browse files Browse the repository at this point in the history
* Fix issues with notifications being generated for apps that weren't actually updated
  • Loading branch information
farmerbb committed Feb 12, 2020
1 parent 59a1c42 commit 99c8db2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
7 changes: 3 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'kotlin-kapt'
ext {
coroutines_version = "1.3.3"
room_version = "2.2.3"
dagger_version = "2.25.4"
dagger_version = "2.26"
}

repositories {
Expand All @@ -22,8 +22,8 @@ android {

minSdkVersion 21
targetSdkVersion 29
versionCode 6
versionName "1.2"
versionCode 7
versionName "1.2.1"

resConfigs "en", "fr", "de", "sv"
vectorDrawables.generatedDensities = []
Expand Down Expand Up @@ -80,7 +80,6 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation "androidx.core:core-ktx:1.1.0"
implementation 'androidx.preference:preference-ktx:1.1.0'
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
Expand Down
24 changes: 20 additions & 4 deletions app/src/main/java/com/farmerbb/appnotifier/AppNotifierService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,37 @@ package com.farmerbb.appnotifier

import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.*
import android.os.Build
import android.provider.Settings
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.core.content.pm.PackageInfoCompat
import com.farmerbb.appnotifier.room.AppUpdateDAO
import javax.inject.Inject

class AppNotifierService: Service() {

@Inject lateinit var controller: NotificationController
@Inject lateinit var dao: AppUpdateDAO
@Inject lateinit var pref: SharedPreferences

init {
AppNotifierApplication.component.inject(this)
}

private val packages = mutableMapOf<String, Long>()

private val packageAddedReceiver = object: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val packageName = intent.dataString?.removePrefix("package:").orEmpty()

getPackageInfoSafely(packageName, dao)?.let {
val versionCode = PackageInfoCompat.getLongVersionCode(it)
if(versionCode <= packages[packageName] ?: 0) return@let

packages[packageName] = versionCode

if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
controller.handleAppUpdateNotification(it)
else
Expand All @@ -55,11 +61,21 @@ class AppNotifierService: Service() {
if(intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) return

val packageName = intent.dataString?.removePrefix("package:").orEmpty()
packages.remove(packageName)
controller.cancelAppInstallNotification(packageName)
}
}

override fun onCreate() {
val packageInfo = packageManager.getInstalledPackages(0)
for(info in packageInfo) {
val versionCode = PackageInfoCompat.getLongVersionCode(info)
packages[info.packageName] = versionCode

if(info.packageName == BuildConfig.APPLICATION_ID)
pref.edit().putLong("version_code", versionCode).apply()
}

registerReceiver(packageAddedReceiver, IntentFilter(Intent.ACTION_PACKAGE_ADDED).apply {
addDataScheme("package")
})
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/farmerbb/appnotifier/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.preference.PreferenceManager
import com.farmerbb.appnotifier.room.AppUpdateDAO
Expand Down Expand Up @@ -64,7 +64,7 @@ fun Context.isPlayStoreInstalled() = try {
}

fun getPlayStoreLaunchIntent(packageName: String) = Intent(Intent.ACTION_VIEW).apply {
data = "https://play.google.com/store/apps/details?id=$packageName".toUri()
data = Uri.parse("https://play.google.com/store/apps/details?id=$packageName")
}

fun Context.startActivitySafely(intent: Intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package com.farmerbb.appnotifier.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.core.content.pm.PackageInfoCompat
import com.farmerbb.appnotifier.*
import com.farmerbb.appnotifier.room.AppUpdateDAO
import javax.inject.Inject
Expand All @@ -26,6 +28,7 @@ class PackageUpgradeReceiver: BroadcastReceiver() {

@Inject lateinit var controller: NotificationController
@Inject lateinit var dao: AppUpdateDAO
@Inject lateinit var pref: SharedPreferences

init {
AppNotifierApplication.component.inject(this)
Expand All @@ -35,14 +38,22 @@ class PackageUpgradeReceiver: BroadcastReceiver() {
if(intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return

context.apply {
initAppNotifierService()
var handled = false

getPackageInfoSafely(BuildConfig.APPLICATION_ID, dao)?.let {
controller.apply {
handleAppUpdateNotification(it)
replayAppInstalls()
val versionCode = PackageInfoCompat.getLongVersionCode(it)
if(versionCode > pref.getLong("version_code", 0)) {
controller.handleAppUpdateNotification(it)
handled = true
}
}

initAppNotifierService()

controller.apply {
if(!handled) replayAppUpdates()
replayAppInstalls()
}
}
}
}

0 comments on commit 99c8db2

Please sign in to comment.