ConnectivityActionReceiver.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.owncloud.android.files.services;
  2. import android.content.BroadcastReceiver;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.net.ConnectivityManager;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import com.owncloud.android.files.InstantUploadBroadcastReceiver;
  11. /**
  12. * Receives all connectivity action from Android OS at all times and performs required OC actions.
  13. * For now that are:
  14. * - Signal connectivity to {@link FileUploadService}.
  15. *
  16. * Later can be added:
  17. * - Signal connectivity to download service, deletion service, ...
  18. * - Handle offline mode (cf. https://github.com/owncloud/android/issues/162)
  19. *
  20. * @author LukeOwncloud
  21. *
  22. */
  23. public class ConnectivityActionReceiver extends BroadcastReceiver {
  24. private static final String TAG = "ConnectivityActionReceiver";
  25. @Override
  26. public void onReceive(final Context context, Intent intent) {
  27. // if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
  28. Log.v(TAG, "action: " + intent.getAction());
  29. Log.v(TAG, "component: " + intent.getComponent());
  30. Bundle extras = intent.getExtras();
  31. if (extras != null) {
  32. for (String key : extras.keySet()) {
  33. Log.v(TAG, "key [" + key + "]: " + extras.get(key));
  34. }
  35. } else {
  36. Log.v(TAG, "no extras");
  37. }
  38. if (InstantUploadBroadcastReceiver.isOnline(context)) {
  39. FileUploadService.retry(context);
  40. }
  41. // }
  42. }
  43. static public void enable(Context context) {
  44. PackageManager pm = context.getPackageManager();
  45. ComponentName compName =
  46. new ComponentName(context.getApplicationContext(),
  47. ConnectivityActionReceiver.class);
  48. pm.setComponentEnabledSetting(
  49. compName,
  50. PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  51. PackageManager.DONT_KILL_APP);
  52. }
  53. static public void disable(Context context) {
  54. PackageManager pm = context.getPackageManager();
  55. ComponentName compName =
  56. new ComponentName(context.getApplicationContext(),
  57. ConnectivityActionReceiver.class);
  58. pm.setComponentEnabledSetting(
  59. compName,
  60. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  61. PackageManager.DONT_KILL_APP);
  62. }
  63. }