UploadUtils.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package com.owncloud.android.utils;
  2. import com.owncloud.android.db.UploadDbObject;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.IntentFilter;
  6. import android.net.ConnectivityManager;
  7. import android.net.NetworkInfo.State;
  8. import android.os.BatteryManager;
  9. public class UploadUtils {
  10. public static boolean isCharging(Context context) {
  11. IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  12. Intent batteryStatus = context.registerReceiver(null, ifilter);
  13. int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  14. return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
  15. }
  16. public static boolean isOnline(Context context) {
  17. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  18. return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
  19. }
  20. public static boolean isConnectedViaWiFi(Context context) {
  21. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  22. return cm != null && cm.getActiveNetworkInfo() != null
  23. && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
  24. && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
  25. }
  26. /**
  27. * Returns true when user is able to cancel this upload. That is, when
  28. * upload is currently in progress or scheduled for upload.
  29. */
  30. static public boolean userCanCancelUpload(UploadDbObject uploadFile) {
  31. switch (uploadFile.getUploadStatus()) {
  32. case UPLOAD_IN_PROGRESS:
  33. case UPLOAD_LATER:
  34. case UPLOAD_FAILED_RETRY:
  35. return true;
  36. default:
  37. return false;
  38. }
  39. }
  40. /**
  41. * Returns true when user can choose to retry this upload. That is, when
  42. * user cancelled upload before or when upload has failed.
  43. *
  44. * TODO Add other cases as described by
  45. * https://github.com/owncloud/android/issues/765#issuecomment-66490312
  46. * (certificate failure, wrong credentials, remote folder gone, ...) This
  47. * needs special handling though!
  48. */
  49. static public boolean userCanRetryUpload(UploadDbObject uploadFile) {
  50. switch (uploadFile.getUploadStatus()) {
  51. case UPLOAD_CANCELLED:
  52. case UPLOAD_FAILED_RETRY://automatically retried. no need for user option.
  53. case UPLOAD_FAILED_GIVE_UP:
  54. return true;
  55. default:
  56. return false;
  57. }
  58. }
  59. }