From 1678a2e7abeb471cf7c700fc785fe53b4dc3f633 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Wed, 20 Jun 2018 08:08:34 +0200 Subject: [PATCH] Delete notification groups on upgrade Signed-off-by: Mario Danic --- .../receivers/PackageReplacedReceiver.java | 41 +++++++++++++++++-- .../utils/preferences/AppPreferences.java | 9 ++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java b/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java index 0aae5f14a..159b5ea7c 100644 --- a/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java +++ b/app/src/main/java/com/nextcloud/talk/receivers/PackageReplacedReceiver.java @@ -29,29 +29,64 @@ import android.content.pm.PackageManager; import android.os.Build; import android.util.Log; +import com.nextcloud.talk.R; +import com.nextcloud.talk.application.NextcloudTalkApplication; +import com.nextcloud.talk.models.database.UserEntity; import com.nextcloud.talk.utils.NotificationUtils; +import com.nextcloud.talk.utils.database.user.UserUtils; +import com.nextcloud.talk.utils.preferences.AppPreferences; +import java.util.zip.CRC32; + +import javax.inject.Inject; + +import autodagger.AutoInjector; + +@AutoInjector(NextcloudTalkApplication.class) public class PackageReplacedReceiver extends BroadcastReceiver { private static final String TAG = "PackageReplacedReceiver"; + @Inject + UserUtils userUtils; + + @Inject + AppPreferences appPreferences; + @Override public void onReceive(Context context, Intent intent) { - if (intent != null && intent.getAction() != null && + NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); + + if (!appPreferences.getIsNotificationChannelUpgradedToV2() && intent != null && intent.getAction() != null && intent.getAction().equals("android.intent.action.MY_PACKAGE_REPLACED")) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); - if (packageInfo.versionCode == 99 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (packageInfo.versionCode > 42 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context .NOTIFICATION_SERVICE); + if (notificationManager != null) { + CRC32 crc32; + UserEntity userEntity; + String groupName; + for (Object userEntityObject : userUtils.getUsers()) { + crc32 = new CRC32(); + userEntity = (UserEntity) userEntityObject; + groupName = String.format(context.getResources().getString(R.string + .nc_notification_channel), userEntity.getDisplayName(), userEntity.getBaseUrl()); + crc32.update(groupName.getBytes()); + notificationManager.deleteNotificationChannelGroup(Long.toString(crc32.getValue())); + } notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_CALLS); notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES); + + appPreferences.setNotificationChannelIsUpgradedToV2(true); } + } } catch (PackageManager.NameNotFoundException e) { - Log.e(TAG, "Failed to find package info"); + Log.e(TAG, "Failed to fetch package info"); } } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java index f8987dc7f..338e435be 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java @@ -158,6 +158,15 @@ public interface AppPreferences { @RemoveMethod void removeMessageRingtoneUri(); + @KeyByString("notification_channels_upgrade_to_v2") + boolean getIsNotificationChannelUpgradedToV2(); + + @KeyByString("notification_channels_upgrade_to_v2") + void setNotificationChannelIsUpgradedToV2(boolean value); + + @KeyByString("notification_channels_upgrade_to_v2") + @RemoveMethod + void removeNotificationChannelUpgradeToV2(); @ClearMethod void clear();