ConnectivityActionReceiver.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. public class ConnectivityActionReceiver extends BroadcastReceiver {
  12. private static final String TAG = "ConnectivityActionReceiver";
  13. @Override
  14. public void onReceive(final Context context, Intent intent) {
  15. if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
  16. Log.v(TAG, "action: " + intent.getAction());
  17. Log.v(TAG, "component: " + intent.getComponent());
  18. Bundle extras = intent.getExtras();
  19. if (extras != null) {
  20. for (String key : extras.keySet()) {
  21. Log.v(TAG, "key [" + key + "]: " + extras.get(key));
  22. }
  23. } else {
  24. Log.v(TAG, "no extras");
  25. }
  26. if (InstantUploadBroadcastReceiver.isOnline(context)) {
  27. FileUploadService.retry(context);
  28. }
  29. }
  30. }
  31. static public void enable(Context context) {
  32. PackageManager pm = context.getPackageManager();
  33. ComponentName compName =
  34. new ComponentName(context.getApplicationContext(),
  35. ConnectivityActionReceiver.class);
  36. pm.setComponentEnabledSetting(
  37. compName,
  38. PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  39. PackageManager.DONT_KILL_APP);
  40. }
  41. static public void disable(Context context) {
  42. PackageManager pm = context.getPackageManager();
  43. ComponentName compName =
  44. new ComponentName(context.getApplicationContext(),
  45. ConnectivityActionReceiver.class);
  46. pm.setComponentEnabledSetting(
  47. compName,
  48. PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  49. PackageManager.DONT_KILL_APP);
  50. }
  51. }