Merge remote-tracking branch 'remotes/upstream/avoidDuplicateFiles' into beta
authortobiasKaminsky <tobias@kaminsky.me>
Sun, 22 Nov 2015 11:19:17 +0000 (12:19 +0100)
committertobiasKaminsky <tobias@kaminsky.me>
Sun, 22 Nov 2015 11:19:17 +0000 (12:19 +0100)
1  2 
src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java

@@@ -29,16 -29,15 +29,16 @@@ import com.owncloud.android.files.servi
  import com.owncloud.android.lib.common.utils.Log_OC;
  import com.owncloud.android.utils.FileStorageUtils;
  
 -
  import android.accounts.Account;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
 +import android.content.IntentFilter;
  import android.content.SharedPreferences;
  import android.database.Cursor;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo.State;
 +import android.os.BatteryManager;
  import android.preference.PreferenceManager;
  import android.provider.MediaStore.Images;
  import android.provider.MediaStore.Video;
@@@ -60,7 -59,7 +60,7 @@@ public class InstantUploadBroadcastRece
      @Override
      public void onReceive(Context context, Intent intent) {
          Log_OC.d(TAG, "Received: " + intent.getAction());
 -        if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
 +        if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION) || intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
              handleConnectivityAction(context, intent);
          }else if (intent.getAction().equals(NEW_PHOTO_ACTION_UNOFFICIAL)) {
              handleNewPictureAction(context, intent); 
          file_name = c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
          mime_type = c.getString(c.getColumnIndex(Images.Media.MIME_TYPE));
          c.close();
 -        
          Log_OC.d(TAG, file_path + "");
  
          // save always temporally the picture to upload
          db.putFileForLater(file_path, account.name, null);
          db.close();
  
 -        if (!isOnline(context) || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
 +        if (!isOnline(context) 
 +                || (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))
 +                || (instantUploadWhenChargingOnly(context) && !isCharging(context))
 +           ) {
              return;
          }
  
          i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  
          // instant upload behaviour
+         i = addInstantUploadBehaviour(i, context);
+         context.startService(i);
+     }
+     private Intent addInstantUploadBehaviour(Intent i, Context context){
          SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
          String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
  
              Log_OC.d(TAG, "upload file and delete file in original place");
          }
  
-         context.startService(i);
+         return i;
      }
  
      private void handleNewVideoAction(Context context, Intent intent) {
          mime_type = c.getString(c.getColumnIndex(Video.Media.MIME_TYPE));
          c.close();
          Log_OC.d(TAG, file_path + "");
 +        
 +        // save always temporally the picture to upload
 +        DbHandler db = new DbHandler(context);
 +        db.putFileForLater(file_path, account.name, null);
 +        db.close();
  
 -        if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
 +        if (!isOnline(context) 
 +                || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))
 +                || (instantVideoUploadWhenChargingOnly(context) && !isCharging(context))
 +           ) {
              return;
          }
  
          i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  
          // instant upload behaviour
-         SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
-         String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
-         if (behaviour.equalsIgnoreCase("NOTHING")) {
-             Log_OC.d(TAG, "upload file and do nothing");
-             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
-         } else if (behaviour.equalsIgnoreCase("COPY")) {
-             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_COPY);
-             Log_OC.d(TAG, "upload file and copy file to oc folder");
-         } else if (behaviour.equalsIgnoreCase("MOVE")) {
-             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
-             Log_OC.d(TAG, "upload file and move file to oc folder");
-         } else if (behaviour.equalsIgnoreCase("DELETE")){
-             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_REMOVE);
-             Log_OC.d(TAG, "upload file and delete file in original place");
-         }
+         i = addInstantUploadBehaviour(i, context);
  
          context.startService(i);
  
      }
  
      private void handleConnectivityAction(Context context, Intent intent) {
 -        if (!instantPictureUploadEnabled(context)) {
 +        if (!instantPictureUploadEnabled(context) && !instantVideoUploadEnabled(context)) {
              Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
              return;
          }
  
 +        if (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context)){
 +            Account account = AccountUtils.getCurrentOwnCloudAccount(context);
 +            if (account == null) {
 +                Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
 +                return;
 +            }
 +
 +            Intent i = new Intent(context, FileUploader.class);
 +            i.putExtra(FileUploader.KEY_ACCOUNT, account);
 +            i.putExtra(FileUploader.KEY_CANCEL_ALL, true);
 +            context.startService(i);
 +        }
 +
          if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
                  && isOnline(context)
 -                && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
 +                && (!instantUploadWhenChargingOnly(context) || (instantUploadWhenChargingOnly(context) && isCharging(context)))
 +                && (!instantVideoUploadWhenChargingOnly(context) || (instantVideoUploadWhenChargingOnly(context) && isCharging(context)))
 +                && (!instantPictureUploadViaWiFiOnly(context) || (instantPictureUploadViaWiFiOnly(context) && isConnectedViaWiFi(context)))
 +                && (!instantVideoUploadViaWiFiOnly(context) || (instantVideoUploadViaWiFiOnly(context) && isConnectedViaWiFi(context)))
 +            ) {
              DbHandler db = new DbHandler(context);
              Cursor c = db.getAwaitingFiles();
              if (c.moveToFirst()) {
                  do {
 +                    if (instantPictureUploadViaWiFiOnly(context) &&
 +                            !isConnectedViaWiFi(context)){
 +                        break;
 +                    }
 +
                      String account_name = c.getString(c.getColumnIndex("account"));
                      String file_path = c.getString(c.getColumnIndex("path"));
                      File f = new File(file_path);
                          i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
  
                          // instant upload behaviour
-                         SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
-                         String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
-                         if (behaviour.equalsIgnoreCase("NOTHING")) {
-                             Log_OC.d(TAG, "upload file and do nothing");
-                             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
-                         } else if (behaviour.equalsIgnoreCase("COPY")) {
-                             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_COPY);
-                             Log_OC.d(TAG, "upload file and copy file to oc folder");
-                         } else if (behaviour.equalsIgnoreCase("MOVE")) {
-                             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
-                             Log_OC.d(TAG, "upload file and move file to oc folder");
-                         } else if (behaviour.equalsIgnoreCase("DELETE")){
-                             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_REMOVE);
-                             Log_OC.d(TAG, "upload file and delete file in original place");
-                         }
+                         i = addInstantUploadBehaviour(i, context);
  
                          context.startService(i);
  
              c.close();
              db.close();
          }
 -
      }
  
      public static boolean isOnline(Context context) {
                  && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
                  && cm.getActiveNetworkInfo().getState() == State.CONNECTED;
      }
 +    
 +    public static boolean isCharging(Context context){
 +        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
 +        Intent batteryStatus = context.registerReceiver(null, ifilter);
 +
 +        int status = 0;
 +        if (batteryStatus != null) {
 +            status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
 +        }
 +        return status == BatteryManager.BATTERY_STATUS_CHARGING ||
 +                status == BatteryManager.BATTERY_STATUS_FULL;
 +    }
  
      public static boolean instantPictureUploadEnabled(Context context) {
          return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_uploading", false);
      public static boolean instantVideoUploadViaWiFiOnly(Context context) {
          return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_wifi", false);
      }
 +    public static boolean instantUploadWhenChargingOnly(Context context) {
 +        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_upload_on_charging", false);
 +    }
 +    public static boolean instantVideoUploadWhenChargingOnly(Context context) {
 +        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("instant_video_upload_on_charging", false);
 +    }
  }