Merge pull request #1751 from nextcloud/bugfix/756/notificationChannelsAppUpgrade

Additional fixes related to notification channels
This commit is contained in:
Marcel Hibbe 2022-01-05 11:10:16 +01:00 committed by GitHub
commit cd08a8fa5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 78 deletions

View file

@ -181,9 +181,9 @@ public class RingtoneSelectionController extends BaseController implements Flexi
private String getRingtoneString() {
if (callNotificationSounds) {
return NotificationUtils.INSTANCE.getDEFAULT_CALL_RINGTONE_URI();
return NotificationUtils.DEFAULT_CALL_RINGTONE_URI;
} else {
return NotificationUtils.INSTANCE.getDEFAULT_MESSAGE_RINGTONE_URI();
return NotificationUtils.DEFAULT_MESSAGE_RINGTONE_URI;
}
}

View file

@ -302,7 +302,7 @@ public class SettingsController extends BaseController {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID);
intent.putExtra(Settings.EXTRA_CHANNEL_ID,
NotificationUtils.INSTANCE.getNOTIFICATION_CHANNEL_CALLS_V4());
NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V4);
startActivity(intent);
});
@ -310,7 +310,7 @@ public class SettingsController extends BaseController {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID);
intent.putExtra(Settings.EXTRA_CHANNEL_ID,
NotificationUtils.INSTANCE.getNOTIFICATION_CHANNEL_MESSAGES_V3());
NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES_V4);
startActivity(intent);
});
@ -445,8 +445,8 @@ public class SettingsController extends BaseController {
private String getRingtoneName(Context context, Uri ringtoneUri) {
if (ringtoneUri == null) {
return getResources().getString(R.string.nc_settings_no_ringtone);
} else if (ringtoneUri.toString().equals(NotificationUtils.INSTANCE.getDEFAULT_CALL_RINGTONE_URI()) ||
ringtoneUri.toString().equals(NotificationUtils.INSTANCE.getDEFAULT_MESSAGE_RINGTONE_URI())) {
} else if (NotificationUtils.DEFAULT_CALL_RINGTONE_URI.equals(ringtoneUri.toString()) ||
NotificationUtils.DEFAULT_MESSAGE_RINGTONE_URI.equals(ringtoneUri.toString())) {
return getResources().getString(R.string.nc_settings_default_ringtone);
} else {
Ringtone r = RingtoneManager.getRingtone(context, ringtoneUri);

View file

@ -332,7 +332,7 @@ public class NotificationWorker extends Worker {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (CHAT.equals(decryptedPushMessage.getType()) || ROOM.equals(decryptedPushMessage.getType())) {
notificationBuilder.setChannelId(NotificationUtils.INSTANCE.getNOTIFICATION_CHANNEL_MESSAGES_V3());
notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES_V4);
}
} else {
// red color for the lights

View file

@ -20,73 +20,19 @@
package com.nextcloud.talk.receivers
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import autodagger.AutoInjector
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.utils.NotificationUtils
import com.nextcloud.talk.utils.database.user.UserUtils
import com.nextcloud.talk.utils.preferences.AppPreferences
import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class)
class PackageReplacedReceiver : BroadcastReceiver() {
@Inject
lateinit var userUtils: UserUtils
@Inject
lateinit var appPreferences: AppPreferences
override fun onReceive(context: Context, intent: Intent?) {
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
if (intent != null && intent.action != null &&
intent.action == "android.intent.action.MY_PACKAGE_REPLACED"
) {
try {
val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
if (packageInfo.versionCode > 43 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = context.getSystemService(
Context
.NOTIFICATION_SERVICE
) as NotificationManager
if (!appPreferences.isNotificationChannelUpgradedToV2) {
for (
notificationChannelGroup in notificationManager.notificationChannelGroups
) {
notificationManager.deleteNotificationChannelGroup(notificationChannelGroup.id)
}
notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_CALLS)
notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES)
appPreferences.setNotificationChannelIsUpgradedToV2(true)
}
if (!appPreferences.isNotificationChannelUpgradedToV3 && packageInfo.versionCode > 51) {
notificationManager
.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES_V2)
notificationManager
.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V2)
appPreferences.setNotificationChannelIsUpgradedToV3(true)
}
notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V3)
}
} catch (e: PackageManager.NameNotFoundException) {
Log.e(TAG, "Failed to fetch package info")
}
NotificationUtils.removeOldNotificationChannels(context)
}
}
companion object {
private val TAG = "PackageReplacedReceiver"
}
}

View file

@ -40,17 +40,23 @@ import com.nextcloud.talk.utils.preferences.AppPreferences
import java.io.IOException
object NotificationUtils {
val NOTIFICATION_CHANNEL_MESSAGES = "NOTIFICATION_CHANNEL_MESSAGES"
val NOTIFICATION_CHANNEL_MESSAGES_V2 = "NOTIFICATION_CHANNEL_MESSAGES_V2"
val NOTIFICATION_CHANNEL_MESSAGES_V3 = "NOTIFICATION_CHANNEL_MESSAGES_V3"
val NOTIFICATION_CHANNEL_CALLS = "NOTIFICATION_CHANNEL_CALLS"
val NOTIFICATION_CHANNEL_CALLS_V2 = "NOTIFICATION_CHANNEL_CALLS_V2"
val NOTIFICATION_CHANNEL_CALLS_V3 = "NOTIFICATION_CHANNEL_CALLS_V3"
val NOTIFICATION_CHANNEL_CALLS_V4 = "NOTIFICATION_CHANNEL_CALLS_V4"
val DEFAULT_CALL_RINGTONE_URI =
// Notification channel IDs commented below are no longer used.
// All old notification channels get deleted when the app is upgraded to the current version.
//
// val NOTIFICATION_CHANNEL_MESSAGES = "NOTIFICATION_CHANNEL_MESSAGES"
// val NOTIFICATION_CHANNEL_MESSAGES_V2 = "NOTIFICATION_CHANNEL_MESSAGES_V2"
// val NOTIFICATION_CHANNEL_MESSAGES_V3 = "NOTIFICATION_CHANNEL_MESSAGES_V3"
// val NOTIFICATION_CHANNEL_CALLS = "NOTIFICATION_CHANNEL_CALLS"
// val NOTIFICATION_CHANNEL_CALLS_V2 = "NOTIFICATION_CHANNEL_CALLS_V2"
// val NOTIFICATION_CHANNEL_CALLS_V3 = "NOTIFICATION_CHANNEL_CALLS_V3"
const val NOTIFICATION_CHANNEL_MESSAGES_V4 = "NOTIFICATION_CHANNEL_MESSAGES_V4"
const val NOTIFICATION_CHANNEL_CALLS_V4 = "NOTIFICATION_CHANNEL_CALLS_V4"
const val DEFAULT_CALL_RINGTONE_URI =
"android.resource://" + BuildConfig.APPLICATION_ID + "/raw/librem_by_feandesign_call"
val DEFAULT_MESSAGE_RINGTONE_URI =
const val DEFAULT_MESSAGE_RINGTONE_URI =
"android.resource://" + BuildConfig.APPLICATION_ID + "/raw/librem_by_feandesign_message"
@TargetApi(Build.VERSION_CODES.O)
@ -59,7 +65,7 @@ object NotificationUtils {
channelId: String,
channelName: String,
channelDescription: String,
sound: Uri,
sound: Uri?,
audioAttributes: AudioAttributes
) {
@ -118,7 +124,7 @@ object NotificationUtils {
createNotificationChannel(
context,
NOTIFICATION_CHANNEL_MESSAGES_V3,
NOTIFICATION_CHANNEL_MESSAGES_V4,
context.resources.getString(R.string.nc_notification_channel_messages),
context.resources.getString(R.string.nc_notification_channel_messages_description),
soundUri,
@ -134,6 +140,28 @@ object NotificationUtils {
createMessagesNotificationChannel(context, appPreferences)
}
@TargetApi(Build.VERSION_CODES.O)
fun removeOldNotificationChannels(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Current version does not use notification channel groups - delete all groups
for (channelGroup in notificationManager.notificationChannelGroups) {
notificationManager.deleteNotificationChannelGroup(channelGroup.id)
}
// Delete all notification channels created by previous versions
for (channel in notificationManager.notificationChannels) {
if (
channel.id != NOTIFICATION_CHANNEL_CALLS_V4 &&
channel.id != NOTIFICATION_CHANNEL_MESSAGES_V4
) {
notificationManager.deleteNotificationChannel(channel.id)
}
}
}
}
@TargetApi(Build.VERSION_CODES.O)
private fun getNotificationChannel(
context: Context,
@ -253,7 +281,7 @@ object NotificationUtils {
ringtonePreferencesString: String?,
defaultRingtoneUri: String,
channelId: String
): Uri {
): Uri? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = getNotificationChannel(context, channelId)
if (channel != null) {
@ -268,7 +296,7 @@ object NotificationUtils {
try {
val ringtoneSettings =
LoganSquare.parse(ringtonePreferencesString, RingtoneSettings::class.java)
return ringtoneSettings.ringtoneUri!!
return ringtoneSettings.ringtoneUri
} catch (exception: IOException) {
return Uri.parse(defaultRingtoneUri)
}
@ -278,7 +306,7 @@ object NotificationUtils {
fun getCallRingtoneUri(
context: Context,
appPreferences: AppPreferences
): Uri {
): Uri? {
return getRingtoneUri(
context,
appPreferences.callRingtoneUri, DEFAULT_CALL_RINGTONE_URI, NOTIFICATION_CHANNEL_CALLS_V4
@ -288,10 +316,10 @@ object NotificationUtils {
fun getMessageRingtoneUri(
context: Context,
appPreferences: AppPreferences
): Uri {
): Uri? {
return getRingtoneUri(
context,
appPreferences.messageRingtoneUri, DEFAULT_MESSAGE_RINGTONE_URI, NOTIFICATION_CHANNEL_MESSAGES_V3
appPreferences.messageRingtoneUri, DEFAULT_MESSAGE_RINGTONE_URI, NOTIFICATION_CHANNEL_MESSAGES_V4
)
}
}