Better install/uninstall events handling

This commit is contained in:
kitsunyan
2020-06-08 12:55:44 +03:00
parent 622b8d1d1f
commit b445bc9d24
4 changed files with 57 additions and 49 deletions
+8 -8
View File
@@ -69,16 +69,16 @@ android {
ignore 'InvalidVectorPath'
}
def signingPropertiesFile = rootProject.file('keystore.properties')
if (signingPropertiesFile.exists()) {
def signingProperties = new Properties()
signingProperties.load(signingPropertiesFile.newDataInputStream())
def keystorePropertiesFile = rootProject.file('keystore.properties')
if (keystorePropertiesFile.exists()) {
def keystoreProperties = new Properties()
keystoreProperties.load(keystorePropertiesFile.newDataInputStream())
def signing = [
storeFile: signingProperties['store.file'],
storePassword: signingProperties['store.password'],
keyAlias: signingProperties['key.alias'],
keyPassword: signingProperties['key.password']
storeFile: keystoreProperties['store.file'],
storePassword: keystoreProperties['store.password'],
keyAlias: keystoreProperties['key.alias'],
keyPassword: keystoreProperties['key.password']
]
if (!signing.any { _, v -> v == null }) {
@@ -29,9 +29,9 @@ import java.net.Proxy
@Suppress("unused")
class MainApplication: Application() {
private fun PackageInfo.toInstalledItem(): InstalledItem? {
private fun PackageInfo.toInstalledItem(): InstalledItem {
val signatureString = singleSignature?.let(Utils::calculateHash).orEmpty()
return InstalledItem(packageName, versionName, versionCodeCompat, signatureString)
return InstalledItem(packageName, versionName.orEmpty(), versionCodeCompat, signatureString)
}
override fun attachBaseContext(base: Context) {
@@ -46,10 +46,52 @@ class MainApplication: Application() {
ProductPreferences.init(this)
RepositoryUpdater.init(this)
listenApplications()
listenPreferences()
Coil.setImageLoader(ImageLoader.Builder(this)
.callFactory(CoilDownloader.Factory(Cache.getImagesDir(this))).build())
if (databaseUpdated) {
forceSyncAll()
}
Cache.cleanup(this)
updateSyncJob()
}
private fun listenApplications() {
registerReceiver(object: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val packageName = intent.data?.let { if (it.scheme == "package") it.schemeSpecificPart else null }
if (packageName != null) {
when (intent.action.orEmpty()) {
Intent.ACTION_PACKAGE_ADDED,
Intent.ACTION_PACKAGE_REMOVED -> {
val packageInfo = try {
packageManager.getPackageInfo(packageName, Android.PackageManager.signaturesFlag)
} catch (e: Exception) {
null
}
if (packageInfo != null) {
Database.InstalledAdapter.put(packageInfo.toInstalledItem())
} else {
Database.InstalledAdapter.delete(packageName)
}
}
}
}
}
}, IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addDataScheme("package")
})
val installedItems = packageManager.getInstalledPackages(Android.PackageManager.signaturesFlag)
.map { it.toInstalledItem() }
Database.InstalledAdapter.putAll(installedItems)
}
private fun listenPreferences() {
updateProxy()
var lastAutoSync = Preferences[Preferences.Key.AutoSync]
var lastUpdateUnstable = Preferences[Preferences.Key.UpdateUnstable]
@@ -70,40 +112,6 @@ class MainApplication: Application() {
}
}
}
if (databaseUpdated) {
forceSyncAll()
}
Cache.cleanup(this)
updateSyncJob()
}
private fun listenApplications() {
registerReceiver(object: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val packageName = intent.data?.let { if (it.scheme == "package") it.schemeSpecificPart else null }
if (packageName != null) {
when (intent.action.orEmpty()) {
Intent.ACTION_PACKAGE_ADDED -> {
val installedItem = packageManager.getPackageInfo(packageName,
Android.PackageManager.signaturesFlag)?.toInstalledItem()
installedItem?.let(Database.InstalledAdapter::put)
}
Intent.ACTION_PACKAGE_REMOVED -> {
Database.InstalledAdapter.delete(packageName)
}
}
}
}
}, IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addDataScheme("package")
})
val installedItems = packageManager.getInstalledPackages(Android.PackageManager.signaturesFlag)
.mapNotNull { it.toInstalledItem() }
Database.InstalledAdapter.putAll(installedItems)
}
private fun updateSyncJob() {
@@ -1,6 +1,5 @@
package nya.kitsunyan.foxydroid.database
import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
@@ -386,7 +385,6 @@ object Database {
.use { it.firstOrNull()?.getInt(0) ?: 0 }
}
@SuppressLint("Recycle")
fun query(installed: Boolean, updates: Boolean, searchQuery: String,
category: String, signal: CancellationSignal?): Cursor {
val builder = QueryBuilder()
@@ -507,8 +505,10 @@ object Database {
}
fun delete(packageName: String) {
db.delete(Schema.Installed.name, "${Schema.Installed.ROW_PACKAGE_NAME} = ?", arrayOf(packageName))
notifyChanged(Subject.Products)
val count = db.delete(Schema.Installed.name, "${Schema.Installed.ROW_PACKAGE_NAME} = ?", arrayOf(packageName))
if (count > 0) {
notifyChanged(Subject.Products)
}
}
private fun transform(cursor: Cursor): InstalledItem {
@@ -261,7 +261,7 @@ class ProductFragment(): Fragment(), ProductAdapter.Callbacks {
if (recyclerView != null) {
(recyclerView.adapter as ProductAdapter).setStatus(recyclerView, status)
}
if (state is DownloadService.State.Success) {
if (state is DownloadService.State.Success && isResumed) {
state.consume()
screenActivity.startPackageInstaller(state.release.cacheFileName)
}