X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/ff5202a5f6483e6082ec081ae0ec87f9eba4d967..a99a28cc61d48eca63803c3d661ea1be14f99aec:/src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java diff --git a/src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java b/src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java index f8254ff0..dc32ecc0 100644 --- a/src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java +++ b/src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java @@ -29,15 +29,16 @@ import com.owncloud.android.files.services.FileUploader; 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; @@ -59,7 +60,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { @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); @@ -104,7 +105,6 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { 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 @@ -112,7 +112,10 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { 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; } @@ -125,6 +128,12 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { 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"); @@ -142,7 +151,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { Log_OC.d(TAG, "upload file and delete file in original place"); } - context.startService(i); + return i; } private void handleNewVideoAction(Context context, Intent intent) { @@ -175,8 +184,16 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { 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; } @@ -189,40 +206,47 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { 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); @@ -248,22 +272,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { 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); @@ -275,7 +284,6 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { c.close(); db.close(); } - } public static boolean isOnline(Context context) { @@ -289,6 +297,18 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { && 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); @@ -305,4 +325,10 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { 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); + } }