forked from x0b/rcx
-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
478 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
app/src/main/java/de/felixnuesse/extract/notifications/AppUpdateNotification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package de.felixnuesse.extract.notifications | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.util.Log | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import ca.pkay.rcloneexplorer.R | ||
import ca.pkay.rcloneexplorer.util.PermissionManager | ||
import de.felixnuesse.extract.extensions.tag | ||
import de.felixnuesse.extract.updates.UpdateUserchoiceReceiver | ||
import de.felixnuesse.extract.updates.UpdateUserchoiceReceiver.Companion.ACTION_DOWNLOAD | ||
import de.felixnuesse.extract.updates.UpdateUserchoiceReceiver.Companion.ACTION_IGNORE | ||
import de.felixnuesse.extract.updates.UpdateUserchoiceReceiver.Companion.IGNORE_VERSION_EXTRA | ||
|
||
|
||
class AppUpdateNotification(private var mContext: Context) { | ||
|
||
companion object { | ||
var NOTIFICATION_CHANNEL_ID = "NOTIFICATION_CHANNEL_ID" | ||
var NOTIFICATION_ID = 63598 | ||
} | ||
|
||
@SuppressLint("MissingPermission") // Handled by PermissionManager | ||
fun showNotification(version: String) { | ||
createNotificationChannel() | ||
val builder = NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID) | ||
.setSmallIcon(R.drawable.appicon) | ||
.setContentTitle(mContext.getString(R.string.app_update_notification_title)) | ||
.setContentText(mContext.getString(R.string.app_update_notification_description, version)) | ||
.setPriority(NotificationCompat.PRIORITY_LOW) | ||
.addAction(R.drawable.ic_twotone_cancel_24, mContext.getString(R.string.app_updates_notification_action_ignore), getIgnoreVersionIntent(version)) | ||
.addAction(R.drawable.ic_check, mContext.getString(R.string.app_updates_notification_action_apply), getApplyIntent()) | ||
.setAutoCancel(true) | ||
|
||
val notificationManager = NotificationManagerCompat.from(mContext) | ||
|
||
if(PermissionManager(mContext).grantedNotifications()) { | ||
notificationManager.notify(NOTIFICATION_ID, builder.build()) | ||
} else { | ||
Log.e(tag(), "We don't have notification permission!") | ||
} | ||
} | ||
|
||
private fun getIgnoreVersionIntent(version: String): PendingIntent { | ||
val ignoreIntent = Intent(mContext, UpdateUserchoiceReceiver::class.java) | ||
ignoreIntent.setAction(ACTION_IGNORE) | ||
ignoreIntent.putExtra(IGNORE_VERSION_EXTRA, version) | ||
return PendingIntent.getBroadcast(mContext, 0, ignoreIntent, PendingIntent.FLAG_IMMUTABLE) | ||
} | ||
|
||
private fun getApplyIntent(): PendingIntent { | ||
val downloadIntent = Intent(mContext, UpdateUserchoiceReceiver::class.java) | ||
downloadIntent.setAction(ACTION_DOWNLOAD) | ||
return PendingIntent.getBroadcast(mContext, 0, downloadIntent, PendingIntent.FLAG_IMMUTABLE) | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val name = mContext.getString(R.string.app_update_notification_channel_name) | ||
val descriptionText = mContext.getString(R.string.app_update_notification_channel_description) | ||
val importance = NotificationManager.IMPORTANCE_LOW | ||
val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance).apply { | ||
description = descriptionText | ||
} | ||
|
||
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
} | ||
|
||
fun cancelNotification(){ | ||
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.cancel(NOTIFICATION_ID) | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/de/felixnuesse/extract/updates/UpdateChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package de.felixnuesse.extract.updates | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import androidx.preference.PreferenceManager | ||
import ca.pkay.rcloneexplorer.R | ||
import de.felixnuesse.extract.updates.workmanager.UpdateManager | ||
|
||
|
||
class UpdateChecker(private var mContext: Context) { | ||
|
||
|
||
private val preferenceManager = PreferenceManager.getDefaultSharedPreferences(mContext) | ||
|
||
private var checkForUpdates = preferenceManager.getBoolean(mContext.getString(R.string.pref_key_app_updates), true) | ||
|
||
private var updateManager = UpdateManager(mContext) | ||
|
||
|
||
init { | ||
if(checkForUpdates) { | ||
checkForUpdates = isUpdateableInstall() | ||
} | ||
} | ||
|
||
fun schedule() { | ||
if(!checkForUpdates) { | ||
updateManager.cancelAll() | ||
return | ||
} | ||
updateManager.queueRepeating() | ||
} | ||
|
||
private fun isUpdateableInstall(): Boolean { | ||
val storeApps = mutableListOf( | ||
"com.android.vending", | ||
"com.google.android.feedback", | ||
"org.fdroid.basic", | ||
"org.fdroid.fdroid", | ||
"com.aurora.store" | ||
) | ||
|
||
val sourcePackage = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
mContext.packageManager.getInstallSourceInfo(mContext.packageName).installingPackageName | ||
} else { | ||
mContext.packageManager.getInstallerPackageName(mContext.packageName) | ||
} | ||
|
||
return !storeApps.contains(sourcePackage) | ||
} | ||
|
||
} |
Oops, something went wrong.