ReceiversHelper.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * @author Chris Narkiewicz
  6. * Copyright (C) 2017 Mario Danic
  7. * Copyright (C) 2017 Nextcloud
  8. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package com.owncloud.android.utils;
  24. import android.content.BroadcastReceiver;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.content.IntentFilter;
  28. import com.nextcloud.client.account.UserAccountManager;
  29. import com.nextcloud.client.device.PowerManagementService;
  30. import com.nextcloud.client.network.ConnectivityService;
  31. import com.nextcloud.client.network.WalledCheckCache;
  32. import com.nextcloud.common.DNSCache;
  33. import com.owncloud.android.MainApp;
  34. import com.owncloud.android.datamodel.UploadsStorageManager;
  35. /**
  36. * Helper for setting up network and power receivers
  37. */
  38. public final class ReceiversHelper {
  39. private ReceiversHelper() {
  40. // utility class -> private constructor
  41. }
  42. public static void registerNetworkChangeReceiver(final UploadsStorageManager uploadsStorageManager,
  43. final UserAccountManager accountManager,
  44. final ConnectivityService connectivityService,
  45. final PowerManagementService powerManagementService,
  46. final WalledCheckCache walledCheckCache) {
  47. Context context = MainApp.getAppContext();
  48. IntentFilter intentFilter = new IntentFilter();
  49. intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
  50. intentFilter.addAction("android.net.wifi.STATE_CHANGE");
  51. BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  52. @Override
  53. public void onReceive(Context context, Intent intent) {
  54. DNSCache.clear();
  55. walledCheckCache.clear();
  56. if (connectivityService.getConnectivity().isConnected()) {
  57. FilesSyncHelper.restartJobsIfNeeded(uploadsStorageManager,
  58. accountManager,
  59. connectivityService,
  60. powerManagementService);
  61. }
  62. }
  63. };
  64. context.registerReceiver(broadcastReceiver, intentFilter);
  65. }
  66. public static void registerPowerChangeReceiver(
  67. final UploadsStorageManager uploadsStorageManager,
  68. final UserAccountManager accountManager,
  69. final ConnectivityService connectivityService,
  70. final PowerManagementService powerManagementService
  71. ) {
  72. Context context = MainApp.getAppContext();
  73. IntentFilter intentFilter = new IntentFilter();
  74. intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
  75. intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
  76. BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  77. @Override
  78. public void onReceive(Context context, Intent intent) {
  79. if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
  80. FilesSyncHelper.restartJobsIfNeeded(uploadsStorageManager,
  81. accountManager,
  82. connectivityService,
  83. powerManagementService);
  84. }
  85. }
  86. };
  87. context.registerReceiver(broadcastReceiver, intentFilter);
  88. }
  89. public static void registerPowerSaveReceiver(
  90. final UploadsStorageManager uploadsStorageManager,
  91. final UserAccountManager accountManager,
  92. final ConnectivityService connectivityService,
  93. final PowerManagementService powerManagementService
  94. ) {
  95. Context context = MainApp.getAppContext();
  96. IntentFilter intentFilter = new IntentFilter();
  97. intentFilter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
  98. BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  99. @Override
  100. public void onReceive(Context context, Intent intent) {
  101. if (!powerManagementService.isPowerSavingEnabled()) {
  102. FilesSyncHelper.restartJobsIfNeeded(uploadsStorageManager,
  103. accountManager,
  104. connectivityService,
  105. powerManagementService);
  106. }
  107. }
  108. };
  109. context.registerReceiver(broadcastReceiver, intentFilter);
  110. }
  111. }