ReceiversHelper.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic
  6. * Copyright (C) 2017 Nextcloud
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.utils;
  22. import android.content.BroadcastReceiver;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.content.IntentFilter;
  26. import com.evernote.android.job.JobRequest;
  27. import com.evernote.android.job.util.Device;
  28. import com.owncloud.android.MainApp;
  29. /*
  30. Helper for setting up network and power receivers
  31. */
  32. public class ReceiversHelper {
  33. public static void registerNetworkChangeReceiver() {
  34. Context context = MainApp.getAppContext();
  35. IntentFilter intentFilter = new IntentFilter();
  36. intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
  37. intentFilter.addAction("android.net.wifi.STATE_CHANGE");
  38. BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  39. @Override
  40. public void onReceive(Context context, Intent intent) {
  41. if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
  42. FilesSyncHelper.restartJobsIfNeeded();
  43. }
  44. }
  45. };
  46. context.registerReceiver(broadcastReceiver, intentFilter);
  47. }
  48. public static void registerPowerChangeReceiver() {
  49. Context context = MainApp.getAppContext();
  50. IntentFilter intentFilter = new IntentFilter();
  51. intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
  52. intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
  53. BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  54. @Override
  55. public void onReceive(Context context, Intent intent) {
  56. if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
  57. FilesSyncHelper.restartJobsIfNeeded();
  58. }
  59. }
  60. };
  61. context.registerReceiver(broadcastReceiver, intentFilter);
  62. }
  63. public static void registerPowerSaveReceiver() {
  64. Context context = MainApp.getAppContext();
  65. IntentFilter intentFilter = new IntentFilter();
  66. intentFilter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
  67. BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  68. @Override
  69. public void onReceive(Context context, Intent intent) {
  70. if (!PowerUtils.isPowerSaveMode(context)) {
  71. FilesSyncHelper.restartJobsIfNeeded();
  72. }
  73. }
  74. };
  75. context.registerReceiver(broadcastReceiver, intentFilter);
  76. }
  77. }