UploadUtils.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.owncloud.android.utils;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.net.ConnectivityManager;
  6. import android.net.NetworkInfo.State;
  7. import android.os.BatteryManager;
  8. import com.owncloud.android.db.UploadDbObject;
  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. static public boolean userCanRetryUpload(UploadDbObject uploadFile) {
  45. switch (uploadFile.getUploadStatus()) {
  46. case UPLOAD_CANCELLED:
  47. case UPLOAD_FAILED_RETRY://automatically retried. no need for user option.
  48. case UPLOAD_FAILED_GIVE_UP: //TODO this case needs to be handled as described by
  49. // https://github.com/owncloud/android/issues/765#issuecomment-66490312
  50. case UPLOAD_LATER: //upload is already schedule but allow user to increase priority
  51. case UPLOAD_SUCCEEDED: // if user wants let him to re-upload (maybe
  52. // remote file was deleted...)
  53. return true;
  54. default:
  55. return false;
  56. }
  57. }
  58. }