PackageReplacedReceiver.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.receivers;
  21. import android.app.NotificationChannelGroup;
  22. import android.app.NotificationManager;
  23. import android.content.BroadcastReceiver;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.pm.PackageInfo;
  27. import android.content.pm.PackageManager;
  28. import android.os.Build;
  29. import android.util.Log;
  30. import com.nextcloud.talk.application.NextcloudTalkApplication;
  31. import com.nextcloud.talk.utils.NotificationUtils;
  32. import com.nextcloud.talk.utils.database.user.UserUtils;
  33. import com.nextcloud.talk.utils.preferences.AppPreferences;
  34. import javax.inject.Inject;
  35. import autodagger.AutoInjector;
  36. @AutoInjector(NextcloudTalkApplication.class)
  37. public class PackageReplacedReceiver extends BroadcastReceiver {
  38. private static final String TAG = "PackageReplacedReceiver";
  39. @Inject
  40. UserUtils userUtils;
  41. @Inject
  42. AppPreferences appPreferences;
  43. @Override
  44. public void onReceive(Context context, Intent intent) {
  45. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  46. if (!appPreferences.getIsNotificationChannelUpgradedToV2() && intent != null && intent.getAction() != null &&
  47. intent.getAction().equals("android.intent.action.MY_PACKAGE_REPLACED")) {
  48. try {
  49. PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
  50. if (packageInfo.versionCode > 43 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  51. NotificationManager notificationManager =
  52. (NotificationManager) context.getSystemService(Context
  53. .NOTIFICATION_SERVICE);
  54. if (notificationManager != null) {
  55. for (NotificationChannelGroup notificationChannelGroup : notificationManager
  56. .getNotificationChannelGroups()) {
  57. notificationManager.deleteNotificationChannelGroup(notificationChannelGroup.getId());
  58. }
  59. notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_CALLS);
  60. notificationManager.deleteNotificationChannel(NotificationUtils.NOTIFICATION_CHANNEL_MESSAGES);
  61. appPreferences.setNotificationChannelIsUpgradedToV2(true);
  62. }
  63. }
  64. } catch (PackageManager.NameNotFoundException e) {
  65. Log.e(TAG, "Failed to fetch package info");
  66. }
  67. }
  68. }
  69. }