From aa665973460d8844e09b133cad80ed78b296ddce Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 20 Sep 2016 08:45:34 +0300 Subject: [PATCH 01/19] Added connectiivity type check --- .../s3/receiver/NetworkInfoReceiver.java | 126 ++++++++++++++++++ .../s3/transferutility/DownloadTask.java | 10 +- .../s3/transferutility/TransferRecord.java | 3 +- .../s3/transferutility/TransferService.java | 66 +++------ .../s3/transferutility/TransferUtility.java | 56 +++++--- .../s3/transferutility/UploadTask.java | 18 +-- 6 files changed, 188 insertions(+), 91 deletions(-) create mode 100644 aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java new file mode 100644 index 00000000000..e5c1750f7ab --- /dev/null +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java @@ -0,0 +1,126 @@ +package com.amazonaws.mobileconnectors.s3.receiver; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Handler; +import android.util.Log; + +/** + * A Broadcast receiver to receive network connection change events. + */ +public class NetworkInfoReceiver extends BroadcastReceiver { + + private static final String TAG = "NetworkInfoReceiver"; + + private final Handler handler; + + private final ConnectivityManager connectivityManager; + + private int checkFlag; + + private int disconnectFlag; + + /** + * Constructs a NetworkInfoReceiver. + * @param context Service context + * @param handler a handle to send message to + */ + public NetworkInfoReceiver(final Context context, + final Handler handler, + final int checkFlag, final int disconnectFlag) { + this.connectivityManager = (ConnectivityManager) context + .getSystemService(Context.CONNECTIVITY_SERVICE); + this.handler = handler; + this.checkFlag = checkFlag; + this.disconnectFlag = disconnectFlag; + } + + @Override + public void onReceive(final Context context, final Intent intent) { + if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { + Type checkType = connectionCheckType; + if (connectionCheckType == null) { + checkType = DEFAULT_CONNECTION_CHECK_TYPE; + Log.w(TAG, "Using default type: " + checkType.name()); + } + boolean networkConnected = checkType.isConnected(connectivityManager); + Log.d(TAG, "Network connected: " + networkConnected); + handler.sendEmptyMessage(networkConnected ? + checkFlag : disconnectFlag); + } + } + + public static final Type DEFAULT_CONNECTION_CHECK_TYPE = Type.WIFI_ONLY; + + Type connectionCheckType = DEFAULT_CONNECTION_CHECK_TYPE; + + public void setConnectionCheckType(Type connectionCheckType) { + this.connectionCheckType = connectionCheckType; + } + + public boolean isNetworkConnected() { + return connectionCheckType.isConnected(connectivityManager); + } + + public enum Type { + ANY("any") { + @Override + public boolean verify(final NetworkInfo info) { + return info != null && info.isConnected(); + } + }, + MOBILE_ONLY("mobile_only") { + @Override + public boolean verify(final NetworkInfo info) { + return info != null && info.isConnected() + && info.getType() == ConnectivityManager.TYPE_MOBILE; + } + }, + WIFI_ONLY("wifi_only") { + @Override + public boolean verify(final NetworkInfo info) { + return (info != null && info.isConnected() + && info.getType() == ConnectivityManager.TYPE_WIFI); + } + }; + + private String intentKey; + + Type(String intentKey) { + this.intentKey = intentKey; + } + + /** + * Gets the status of network connectivity. + * + * @return true if network is connected, false otherwise. + */ + boolean isConnected(final ConnectivityManager connectivityManager) { + return verify(connectivityManager.getActiveNetworkInfo()); + } + + protected abstract boolean verify(final NetworkInfo networkInfo); + + public static Type from(final String name) { + if (name != null) { + for (final Type type : values()) { + if (type.intentKey.toLowerCase().equals(name.toLowerCase())) { + return type; + } + } + } + return null; + } + + public static Type from(final String name, final Type defaultType) { + Type type = from(name); + if (type == null) { + return defaultType; + } + return type; + } + } +} diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java index 663699e7b9f..3b4c3f9c8a8 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java @@ -16,20 +16,14 @@ package com.amazonaws.mobileconnectors.s3.transferutility; import android.util.Log; - import com.amazonaws.AmazonClientException; -import com.amazonaws.mobileconnectors.s3.transferutility.TransferService.NetworkInfoReceiver; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.retry.RetryUtils; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.S3Object; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.util.concurrent.Callable; /** diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java index 0093a520588..641f18fca2c 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java @@ -17,9 +17,8 @@ import android.database.Cursor; import android.util.Log; - import com.amazonaws.AmazonClientException; -import com.amazonaws.mobileconnectors.s3.transferutility.TransferService.NetworkInfoReceiver; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.AbortMultipartUploadRequest; import com.amazonaws.util.json.JsonUtils; diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java index 575faf5543d..23251e4fc6f 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java @@ -16,21 +16,14 @@ package com.amazonaws.mobileconnectors.s3.transferutility; import android.app.Service; -import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.database.Cursor; import android.net.ConnectivityManager; -import android.net.NetworkInfo; -import android.os.Handler; -import android.os.HandlerThread; -import android.os.IBinder; -import android.os.Looper; -import android.os.Message; +import android.os.*; import android.util.Log; - +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.services.s3.AmazonS3; import java.io.FileDescriptor; @@ -66,6 +59,7 @@ public class TransferService extends Service { static final String INTENT_ACTION_TRANSFER_CANCEL = "cancel_transfer"; static final String INTENT_BUNDLE_TRANSFER_ID = "id"; static final String INTENT_BUNDLE_S3_REFERENCE_KEY = "s3_reference_key"; + static final String INTENT_BUNDLE_CONNECTION_CHECK_TYPE = "connection_check_type"; private AmazonS3 s3; @@ -127,44 +121,6 @@ public void onCreate() { setHandlerLooper(handlerThread.getLooper()); } - /** - * A Broadcast receiver to receive network connection change events. - */ - static class NetworkInfoReceiver extends BroadcastReceiver { - private final Handler handler; - private final ConnectivityManager connManager; - - /** - * Constructs a NetworkInfoReceiver. - * - * @param handler a handle to send message to - */ - public NetworkInfoReceiver(Context context, Handler handler) { - this.handler = handler; - connManager = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - } - - @Override - public void onReceive(Context context, Intent intent) { - if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { - boolean networkConnected = isNetworkConnected(); - Log.d(TAG, "Network connected: " + networkConnected); - handler.sendEmptyMessage(networkConnected ? MSG_CHECK : MSG_DISCONNECT); - } - } - - /** - * Gets the status of network connectivity. - * - * @return true if network is connected, false otherwise. - */ - boolean isNetworkConnected() { - NetworkInfo info = connManager.getActiveNetworkInfo(); - return info != null && info.isConnected(); - } - } - @Override public int onStartCommand(Intent intent, int flags, int startId) { this.startId = startId; @@ -176,7 +132,12 @@ public int onStartCommand(Intent intent, int flags, int startId) { stopSelf(startId); return START_NOT_STICKY; } - + String networkCheckType = intent.getStringExtra(INTENT_BUNDLE_CONNECTION_CHECK_TYPE); + if (networkCheckType != null) { + networkInfoReceiver.setConnectionCheckType( + NetworkInfoReceiver.Type.from(networkCheckType, + NetworkInfoReceiver.DEFAULT_CONNECTION_CHECK_TYPE)); + } updateHandler.sendMessage(updateHandler.obtainMessage(MSG_EXEC, intent)); if (isFirst) { registerReceiver(networkInfoReceiver, new IntentFilter( @@ -303,7 +264,9 @@ void execCommand(Intent intent) { Log.e(TAG, "Can't find transfer: " + id); } } - transfer.start(s3, dbUtil, updater, networkInfoReceiver); + if (transfer != null) { + transfer.start(s3, dbUtil, updater, networkInfoReceiver); + } } else if (INTENT_ACTION_TRANSFER_CANCEL.equals(action)) { TransferRecord transfer = updater.getTransfer(id); if (transfer == null) { @@ -415,9 +378,10 @@ void pauseAllForNetwork() { * * @param looper new looper */ - void setHandlerLooper(Looper looper) { + void setHandlerLooper(final Looper looper) { updateHandler = new UpdateHandler(looper); - networkInfoReceiver = new NetworkInfoReceiver(getApplicationContext(), updateHandler); + networkInfoReceiver = new NetworkInfoReceiver(getApplicationContext(), updateHandler, + MSG_CHECK, MSG_DISCONNECT); } @Override diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 1a88536b95a..d2159753b01 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -15,17 +15,14 @@ package com.amazonaws.mobileconnectors.s3.transferutility; -import static com.amazonaws.services.s3.internal.Constants.MAXIMUM_UPLOAD_PARTS; -import static com.amazonaws.services.s3.internal.Constants.MB; - import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.util.Log; - import com.amazonaws.AmazonWebServiceRequest; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.ObjectMetadata; @@ -36,6 +33,9 @@ import java.util.List; import java.util.UUID; +import static com.amazonaws.services.s3.internal.Constants.MAXIMUM_UPLOAD_PARTS; +import static com.amazonaws.services.s3.internal.Constants.MB; + /** * The transfer utility is a high-level class for applications to upload and * download files. It inserts upload and download records into the database and @@ -105,7 +105,6 @@ public class TransferUtility { * * @param s3 The client to use when making requests to Amazon S3 * @param context The current context - * @param configuration Configuration parameters for this TransferUtility */ public TransferUtility(AmazonS3 s3, Context context) { this.s3 = s3; @@ -124,6 +123,23 @@ public TransferUtility(AmazonS3 s3, Context context) { * @return A TransferObserver used to track download progress and state */ public TransferObserver download(String bucket, String key, File file) { + return download(bucket, key, file, null); + } + + + /** + * Starts downloading the S3 object specified by the bucket and the key to + * the given file. The file must be a valid file. Directory isn't supported. + * Note that if the given file exists, it'll be overwritten. + * + * @param bucket The name of the bucket containing the object to download. + * @param key The key under which the object to download is stored. + * @param file The file to download the object's data to. + * @param connectionCheckType Type of connection check. Default is {@link NetworkInfoReceiver.Type#WIFI_ONLY} + * @return A TransferObserver used to track download progress and state + */ + public TransferObserver download(String bucket, String key, File file, + NetworkInfoReceiver.Type connectionCheckType) { if (file == null || file.isDirectory()) { throw new IllegalArgumentException("Invalid file: " + file); } @@ -135,7 +151,7 @@ public TransferObserver download(String bucket, String key, File file) { file.delete(); } - sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId); + sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, connectionCheckType); return new TransferObserver(recordId, dbUtil, bucket, key, file); } @@ -147,11 +163,13 @@ public TransferObserver download(String bucket, String key, File file) { * @param key The key in the specified bucket by which to store the new * object. * @param file The file to upload. + * @param connectionCheckType Type of connection check * @return A TransferObserver used to track upload progress and state */ - public TransferObserver upload(String bucket, String key, File file) { + public TransferObserver upload(String bucket, String key, File file, + NetworkInfoReceiver.Type connectionCheckType) { - return upload(bucket, key, file, new ObjectMetadata()); + return upload(bucket, key, file, new ObjectMetadata(), connectionCheckType); } /** @@ -163,12 +181,13 @@ public TransferObserver upload(String bucket, String key, File file) { * object. * @param file The file to upload. * @param cannedAcl The canned ACL to associate with this object + * @param connectionCheckType Type of connection check * @return A TransferObserver used to track upload progress and state */ public TransferObserver upload(String bucket, String key, File file, - CannedAccessControlList cannedAcl) { + CannedAccessControlList cannedAcl, NetworkInfoReceiver.Type connectionCheckType) { - return upload(bucket, key, file, new ObjectMetadata(), cannedAcl); + return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, connectionCheckType); } /** @@ -180,10 +199,12 @@ public TransferObserver upload(String bucket, String key, File file, * object. * @param file The file to upload. * @param metadata The S3 metadata to associate with this object + * @param connectionCheckType Type of connection check * @return A TransferObserver used to track upload progress and state */ - public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata) { - return upload(bucket, key, file, metadata, null); + public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata, + NetworkInfoReceiver.Type connectionCheckType) { + return upload(bucket, key, file, metadata, null, connectionCheckType); } /** @@ -196,10 +217,11 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad * @param file The file to upload. * @param metadata The S3 metadata to associate with this object * @param cannedAcl The canned ACL to associate with this object + * @param connectionCheckType Type of connection check. Default is {@link NetworkInfoReceiver.Type#WIFI_ONLY} * @return A TransferObserver used to track upload progress and state */ public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata, - CannedAccessControlList cannedAcl) { + CannedAccessControlList cannedAcl, NetworkInfoReceiver.Type connectionCheckType) { if (file == null || file.isDirectory()) { throw new IllegalArgumentException("Invalid file: " + file); } @@ -213,7 +235,7 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad recordId = Integer.parseInt(uri.getLastPathSegment()); } - sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId); + sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, connectionCheckType); return new TransferObserver(recordId, dbUtil, bucket, key, file); } @@ -293,7 +315,6 @@ public List getTransfersWithTypeAndState(TransferType type, * @param key The key in the specified bucket by which to store the new * object. * @param file The file to upload. - * @param isUsingEncryption Whether the upload is encrypted. * @return Number of records created in database */ private int createMultipartUploadRecords(String bucket, String key, File file, @@ -423,12 +444,17 @@ public boolean deleteTransferRecord(int id) { * @param id id of the transfer */ private void sendIntent(String action, int id) { + sendIntent(action, id, null); + } + + private void sendIntent(String action, int id, NetworkInfoReceiver.Type networkCheckType) { String s3Key = UUID.randomUUID().toString(); S3ClientReference.put(s3Key, s3); Intent intent = new Intent(appContext, TransferService.class); intent.setAction(action); intent.putExtra(TransferService.INTENT_BUNDLE_TRANSFER_ID, id); intent.putExtra(TransferService.INTENT_BUNDLE_S3_REFERENCE_KEY, s3Key); + intent.putExtra(TransferService.INTENT_BUNDLE_CONNECTION_CHECK_TYPE, networkCheckType); appContext.startService(intent); } diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java index 37e8a81bc74..fd1b6d37978 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java @@ -16,27 +16,16 @@ package com.amazonaws.mobileconnectors.s3.transferutility; import android.util.Log; - import com.amazonaws.AmazonClientException; -import com.amazonaws.mobileconnectors.s3.transferutility.TransferService.NetworkInfoReceiver; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.retry.RetryUtils; import com.amazonaws.services.s3.AmazonS3; -import com.amazonaws.services.s3.model.CannedAccessControlList; -import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest; -import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest; -import com.amazonaws.services.s3.model.ObjectMetadata; -import com.amazonaws.services.s3.model.PartETag; -import com.amazonaws.services.s3.model.PutObjectRequest; -import com.amazonaws.services.s3.model.UploadPartRequest; +import com.amazonaws.services.s3.model.*; import com.amazonaws.services.s3.util.Mimetypes; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -256,7 +245,6 @@ private String initiateMultipartUpload(PutObjectRequest putObjectRequest) /** * Creates a PutObjectRequest from the data in the TransferRecord * - * @param por The request to fill * @param upload The data for the Object Metadata * @return Returns a PutObjectRequest with filled in metadata and parameters */ From 6a8860cb6c630bcf5e8b28be1f934df56c16d63c Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 20 Sep 2016 09:10:17 +0300 Subject: [PATCH 02/19] Added default upload methods --- .../s3/transferutility/TransferUtility.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index d2159753b01..343d5f6da46 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -172,6 +172,21 @@ public TransferObserver upload(String bucket, String key, File file, return upload(bucket, key, file, new ObjectMetadata(), connectionCheckType); } + /** + * Starts uploading the file to the given bucket, using the given key. The + * file must be a valid file. Directory isn't supported. + * + * @param bucket The name of the bucket to upload the new object to. + * @param key The key in the specified bucket by which to store the new + * object. + * @param file The file to upload. + * @return A TransferObserver used to track upload progress and state + */ + public TransferObserver upload(String bucket, String key, File file) { + + return upload(bucket, key, file, new ObjectMetadata(), null); + } + /** * Starts uploading the file to the given bucket, using the given key. The * file must be a valid file. Directory isn't supported. @@ -190,6 +205,23 @@ public TransferObserver upload(String bucket, String key, File file, return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, connectionCheckType); } + /** + * Starts uploading the file to the given bucket, using the given key. The + * file must be a valid file. Directory isn't supported. + * + * @param bucket The name of the bucket to upload the new object to. + * @param key The key in the specified bucket by which to store the new + * object. + * @param file The file to upload. + * @param cannedAcl The canned ACL to associate with this object + * @return A TransferObserver used to track upload progress and state + */ + public TransferObserver upload(String bucket, String key, File file, + CannedAccessControlList cannedAcl) { + + return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, null); + } + /** * Starts uploading the file to the given bucket, using the given key. The * file must be a valid file. Directory isn't supported. @@ -207,6 +239,21 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad return upload(bucket, key, file, metadata, null, connectionCheckType); } + /** + * Starts uploading the file to the given bucket, using the given key. The + * file must be a valid file. Directory isn't supported. + * + * @param bucket The name of the bucket to upload the new object to. + * @param key The key in the specified bucket by which to store the new + * object. + * @param file The file to upload. + * @param metadata The S3 metadata to associate with this object + * @return A TransferObserver used to track upload progress and state + */ + public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata) { + return upload(bucket, key, file, metadata, null, null); + } + /** * Starts uploading the file to the given bucket, using the given key. The * file must be a valid file. Directory isn't supported. From 85d8f711777960c5112bc41f29d5a5d753c21d67 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 26 Sep 2016 16:56:56 +0200 Subject: [PATCH 03/19] Fixed wrong parameter sending via intent --- .../mobileconnectors/s3/transferutility/TransferUtility.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 343d5f6da46..22ee31e3034 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -501,7 +501,8 @@ private void sendIntent(String action, int id, NetworkInfoReceiver.Type networkC intent.setAction(action); intent.putExtra(TransferService.INTENT_BUNDLE_TRANSFER_ID, id); intent.putExtra(TransferService.INTENT_BUNDLE_S3_REFERENCE_KEY, s3Key); - intent.putExtra(TransferService.INTENT_BUNDLE_CONNECTION_CHECK_TYPE, networkCheckType); + intent.putExtra(TransferService.INTENT_BUNDLE_CONNECTION_CHECK_TYPE, + networkCheckType.name()); appContext.startService(intent); } From 996061493dda3bb27bff668e26398c57779cc150 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 5 Oct 2016 16:03:11 +0300 Subject: [PATCH 04/19] Fixing passing default connection type --- .../mobileconnectors/s3/transferutility/TransferUtility.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 22ee31e3034..9e07e156a38 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -491,7 +491,7 @@ public boolean deleteTransferRecord(int id) { * @param id id of the transfer */ private void sendIntent(String action, int id) { - sendIntent(action, id, null); + sendIntent(action, id, NetworkInfoReceiver.DEFAULT_CONNECTION_CHECK_TYPE); } private void sendIntent(String action, int id, NetworkInfoReceiver.Type networkCheckType) { From c29b21efa7cfb9b05229b1815022ee154ed7e604 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 19 Oct 2016 11:39:39 +0300 Subject: [PATCH 05/19] Added local pom.xml --- aws-android-sdk-s3/pom.xml | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index 531af44514e..ed6790bc414 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -2,19 +2,21 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - com.amazonaws + io.ebuilder.aws aws-android-sdk-s3 jar AWS SDK for Android - Amazon S3 The AWS Android SDK for Amazon S3 module holds the client classes that are used for communicating with Amazon Simple Storage Service http://aws.amazon.com/sdkforandroid - + com.amazonaws aws-android-sdk-pom 2.3.1 + 2.3.3 + com.amazonaws @@ -65,5 +67,30 @@ maven-javadoc-plugin + + + org.springframework.build + aws-maven + 5.0.0.RELEASE + + + + + + snapshot.repo.ebuilder.io + ebuilder-snapshot + s3://repo.ebuilder.io/snapshot + true + default + + + repo.ebuilder.io + ebuilder-release + s3://repo.ebuilder.io/release + false + default + + + From c885f1f989cf39a4e28369229288abd9031ae044 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 11 Nov 2016 08:34:36 +0300 Subject: [PATCH 06/19] Added sources publishing --- aws-android-sdk-s3/pom.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index ed6790bc414..89ce6ab7718 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -66,6 +66,19 @@ org.apache.maven.plugins maven-javadoc-plugin + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + package + + + From 51d92f3cbcb57456f15d6592179977d38456d704 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 11 Nov 2016 21:59:05 +0300 Subject: [PATCH 07/19] Added service status broadcasting --- aws-android-sdk-s3/pom.xml | 2 +- .../s3/transferutility/ServiceStatus.java | 20 +++++++++++++++++++ .../s3/transferutility/TransferService.java | 14 +++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index 89ce6ab7718..89b5f4e8c53 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -15,7 +15,7 @@ 2.3.1 - 2.3.3 + 2.3.4 diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java new file mode 100644 index 00000000000..9a90b8f262d --- /dev/null +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java @@ -0,0 +1,20 @@ +package com.amazonaws.mobileconnectors.s3.transferutility; + +/** + * Created by Dmitry Gorohov + */ +public enum ServiceStatus { + DESTROYED("destroyed"), + PAUSED("paused"), + RESUMED("resumed"); + + private String name; + + ServiceStatus(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java index 23251e4fc6f..c782cf9d0fd 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java @@ -196,6 +196,7 @@ void checkTransfers() { if (shouldScan && networkInfoReceiver.isNetworkConnected() && s3 != null) { loadTransfersFromDB(); shouldScan = false; + broadcastServiceStatus(ServiceStatus.RESUMED); } removeCompletedTransfers(); @@ -209,6 +210,7 @@ void checkTransfers() { * Stop the service when it's been idled for more than a minute. */ Log.d(TAG, "Stop self"); + broadcastServiceStatus(ServiceStatus.DESTROYED); stopSelf(startId); } } @@ -371,6 +373,7 @@ void pauseAllForNetwork() { } } shouldScan = true; + broadcastServiceStatus(ServiceStatus.PAUSED); } /** @@ -403,4 +406,15 @@ protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { } writer.flush(); } + + public static final String TRANSFER_SERVICE_STATUS_ACTION = "transfer-service-action"; + + public static final String BUNDLE_TRANSFER_SERVICE_STATUS = "status"; + + private void broadcastServiceStatus(final ServiceStatus status) { + final Intent intent = new Intent(); + intent.setAction(TRANSFER_SERVICE_STATUS_ACTION); + intent.putExtra(BUNDLE_TRANSFER_SERVICE_STATUS, status.getName()); + sendBroadcast(intent); + } } From c871da0acdbf6926dc8a251fee4021807fa1c5ac Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 11 Nov 2016 22:09:40 +0300 Subject: [PATCH 08/19] enum fix --- .../s3/transferutility/ServiceStatus.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java index 9a90b8f262d..7c2e8e6243d 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/ServiceStatus.java @@ -17,4 +17,15 @@ public enum ServiceStatus { public String getName() { return name; } + + public static ServiceStatus from(final String value) { + if (value != null) { + for (final ServiceStatus serviceStatus : values()) { + if (serviceStatus.getName().equalsIgnoreCase(value)) { + return serviceStatus; + } + } + } + return null; + } } From 278adcc71a2e5f4e8e90e3e0a674f744bd722c71 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 14 Nov 2016 10:23:58 +0300 Subject: [PATCH 09/19] Main thread removeal for events --- .../s3/transferutility/TransferService.java | 5 +---- .../TransferStatusUpdater.java | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java index c782cf9d0fd..dc58a226e16 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java @@ -110,15 +110,12 @@ public IBinder onBind(Intent intent) { @Override public void onCreate() { super.onCreate(); - Log.d(TAG, "Starting Transfer Service"); - dbUtil = new TransferDBUtil(getApplicationContext()); - updater = new TransferStatusUpdater(dbUtil); - handlerThread = new HandlerThread(TAG + "-AWSTransferUpdateHandlerThread"); handlerThread.start(); setHandlerLooper(handlerThread.getLooper()); + updater = new TransferStatusUpdater(dbUtil, updateHandler); } @Override diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferStatusUpdater.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferStatusUpdater.java index 2da71f3dcbc..750d9389ba6 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferStatusUpdater.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferStatusUpdater.java @@ -70,14 +70,24 @@ class TransferStatusUpdater { /** * The handler of main thread that runs callbacks. */ - private final Handler mainHandler; + private final Handler eventHandler; /** * This class is instantiated by TransferService. */ TransferStatusUpdater(TransferDBUtil dbUtil) { this.dbUtil = dbUtil; - mainHandler = new Handler(Looper.getMainLooper()); + eventHandler = new Handler(Looper.getMainLooper()); + transfers = new HashMap(); + lastUpdateTime = new HashMap(); + } + + /** + * This class is instantiated by TransferService. + */ + TransferStatusUpdater(TransferDBUtil dbUtil, Handler eventHandler) { + this.dbUtil = dbUtil; + this.eventHandler = eventHandler; transfers = new HashMap(); lastUpdateTime = new HashMap(); } @@ -165,7 +175,7 @@ void updateState(final int id, final TransferState newState) { } // invoke on main thread - mainHandler.post(new Runnable() { + eventHandler.post(new Runnable() { @Override public void run() { for (TransferListener l : list) { @@ -213,7 +223,7 @@ void updateProgress(final int id, final long bytesCurrent, final long bytesTotal lastUpdateTime.put(id, timeInMillis); // invoke on main thread - mainHandler.post(new Runnable() { + eventHandler.post(new Runnable() { @Override public void run() { for (TransferListener l : list) { @@ -238,7 +248,7 @@ void throwError(final int id, final Exception e) { return; } // invoke on main thread - mainHandler.post(new Runnable() { + eventHandler.post(new Runnable() { @Override public void run() { for (TransferListener l : list) { From 0e9bf3deefaad95a917f86c7a358b21a4f26a4ee Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 24 Nov 2016 14:29:12 +0300 Subject: [PATCH 10/19] Load s3 reference via TransferUtility --- aws-android-sdk-s3/pom.xml | 2 +- .../s3/transferutility/S3ClientReference.java | 40 +++++++++++++------ .../s3/transferutility/TransferService.java | 5 +-- .../s3/transferutility/TransferUtility.java | 14 +++---- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index 89b5f4e8c53..e0b05e062f5 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -15,7 +15,7 @@ 2.3.1 - 2.3.4 + 2.3.5 diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java index 254e24fc580..62068e30cf7 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java @@ -15,10 +15,10 @@ package com.amazonaws.mobileconnectors.s3.transferutility; +import android.util.Log; import com.amazonaws.services.s3.AmazonS3; -import java.util.HashMap; -import java.util.Map; +import java.util.concurrent.Callable; /** * A holder of S3 clients for {@link TransferUtility} to pass a reference of @@ -28,26 +28,42 @@ */ class S3ClientReference { - private static Map map = new HashMap(); + private static final String TAG = S3ClientReference.class.getSimpleName(); - public static void put(String key, AmazonS3 s3) { - map.put(key, s3); - } + private static Callable retrieveAmazonS3Client = null; + + private static AmazonS3 amazonS3 = null; + + private static final Object lock = new Object(); /** - * Retrieves the AmazonS3 client on the given key. + * Retrieves the AmazonS3 client * - * @param key key of the client - * @return an AmazonS3 instance, or null if the key doesn't exist + * @return an AmazonS3 instance, or null */ - public static AmazonS3 get(String key) { - return map.remove(key); + public static AmazonS3 get() { + if (amazonS3 == null && retrieveAmazonS3Client != null) { + synchronized (lock) { + try { + Log.w(TAG, "S3 client's retrieval attempt"); + amazonS3 = retrieveAmazonS3Client.call(); + } catch (final Exception ex) { + Log.e(TAG, "Failed to retrieve s3 client", ex); + } + } + } + return amazonS3; + } + + public static void retrieveClientOn(final Callable callable) { + retrieveAmazonS3Client = callable; } /** * Clears all references. */ public static void clear() { - map.clear(); + retrieveAmazonS3Client = null; + amazonS3 = null; } } diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java index dc58a226e16..f2c7dd2ea07 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java @@ -58,7 +58,6 @@ public class TransferService extends Service { static final String INTENT_ACTION_TRANSFER_RESUME = "resume_transfer"; static final String INTENT_ACTION_TRANSFER_CANCEL = "cancel_transfer"; static final String INTENT_BUNDLE_TRANSFER_ID = "id"; - static final String INTENT_BUNDLE_S3_REFERENCE_KEY = "s3_reference_key"; static final String INTENT_BUNDLE_CONNECTION_CHECK_TYPE = "connection_check_type"; private AmazonS3 s3; @@ -121,9 +120,7 @@ public void onCreate() { @Override public int onStartCommand(Intent intent, int flags, int startId) { this.startId = startId; - - String keyForS3Client = intent.getStringExtra(INTENT_BUNDLE_S3_REFERENCE_KEY); - s3 = S3ClientReference.get(keyForS3Client); + s3 = S3ClientReference.get(); if (s3 == null) { Log.w(TAG, "TransferService can't get s3 client, and it will stop."); stopSelf(startId); diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 9e07e156a38..eb135ace942 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -31,7 +31,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.UUID; +import java.util.concurrent.Callable; import static com.amazonaws.services.s3.internal.Constants.MAXIMUM_UPLOAD_PARTS; import static com.amazonaws.services.s3.internal.Constants.MB; @@ -94,7 +94,6 @@ public class TransferUtility { */ static final int MINIMUM_UPLOAD_PART_SIZE = 5 * MB; - private final AmazonS3 s3; private final Context appContext; private final TransferDBUtil dbUtil; @@ -103,13 +102,13 @@ public class TransferUtility { * initializes configuration of TransferUtility and a key for S3 client weak * reference. * - * @param s3 The client to use when making requests to Amazon S3 + * @param clientRetrieve The client callback to retrieve instance * @param context The current context */ - public TransferUtility(AmazonS3 s3, Context context) { - this.s3 = s3; + public TransferUtility(Callable clientRetrieve, Context context) { this.appContext = context.getApplicationContext(); this.dbUtil = new TransferDBUtil(appContext); + S3ClientReference.retrieveClientOn(clientRetrieve); } /** @@ -495,12 +494,9 @@ private void sendIntent(String action, int id) { } private void sendIntent(String action, int id, NetworkInfoReceiver.Type networkCheckType) { - String s3Key = UUID.randomUUID().toString(); - S3ClientReference.put(s3Key, s3); - Intent intent = new Intent(appContext, TransferService.class); + final Intent intent = new Intent(appContext, TransferService.class); intent.setAction(action); intent.putExtra(TransferService.INTENT_BUNDLE_TRANSFER_ID, id); - intent.putExtra(TransferService.INTENT_BUNDLE_S3_REFERENCE_KEY, s3Key); intent.putExtra(TransferService.INTENT_BUNDLE_CONNECTION_CHECK_TYPE, networkCheckType.name()); appContext.startService(intent); From 20628188380efe3bea1b54e9dc250f6f9dfd0789 Mon Sep 17 00:00:00 2001 From: Aliaksei Zhynhiarouski Date: Sun, 27 Nov 2016 10:37:45 +0300 Subject: [PATCH 11/19] Add hard reference to S3 holder. Bump 2.3.7 version --- aws-android-sdk-s3/pom.xml | 2 +- .../s3/transferutility/Function.java | 5 ++++ .../s3/transferutility/S3ClientReference.java | 27 ++++++++----------- .../s3/transferutility/TransferService.java | 2 +- .../s3/transferutility/TransferUtility.java | 6 ++--- 5 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/Function.java diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index e0b05e062f5..a307dfb226c 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -15,7 +15,7 @@ 2.3.1 - 2.3.5 + 2.3.7 diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/Function.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/Function.java new file mode 100644 index 00000000000..cd841c2e943 --- /dev/null +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/Function.java @@ -0,0 +1,5 @@ +package com.amazonaws.mobileconnectors.s3.transferutility; + +public interface Function { + R call(P p); +} diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java index 62068e30cf7..8c8a5e77447 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/S3ClientReference.java @@ -1,12 +1,12 @@ /** * Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * + *

+ * http://aws.amazon.com/apache2.0 + *

* or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing @@ -15,11 +15,10 @@ package com.amazonaws.mobileconnectors.s3.transferutility; +import android.content.Context; import android.util.Log; import com.amazonaws.services.s3.AmazonS3; -import java.util.concurrent.Callable; - /** * A holder of S3 clients for {@link TransferUtility} to pass a reference of * AmazonS3 to {@link TransferService}. Usually objects are passed to a service @@ -30,8 +29,6 @@ class S3ClientReference { private static final String TAG = S3ClientReference.class.getSimpleName(); - private static Callable retrieveAmazonS3Client = null; - private static AmazonS3 amazonS3 = null; private static final Object lock = new Object(); @@ -41,29 +38,27 @@ class S3ClientReference { * * @return an AmazonS3 instance, or null */ - public static AmazonS3 get() { - if (amazonS3 == null && retrieveAmazonS3Client != null) { + public static AmazonS3 get(Context ctx) { + if (amazonS3 == null && TransferUtility.clientRetrieve != null) { synchronized (lock) { try { Log.w(TAG, "S3 client's retrieval attempt"); - amazonS3 = retrieveAmazonS3Client.call(); + amazonS3 = TransferUtility.clientRetrieve.call(ctx); } catch (final Exception ex) { Log.e(TAG, "Failed to retrieve s3 client", ex); } } } + if (TransferUtility.clientRetrieve == null) { + Log.e(TAG," TransferUtility.clientRetrieve is NULL"); + } return amazonS3; } - public static void retrieveClientOn(final Callable callable) { - retrieveAmazonS3Client = callable; - } - /** * Clears all references. */ public static void clear() { - retrieveAmazonS3Client = null; amazonS3 = null; } } diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java index f2c7dd2ea07..6e4e4b45384 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java @@ -120,7 +120,7 @@ public void onCreate() { @Override public int onStartCommand(Intent intent, int flags, int startId) { this.startId = startId; - s3 = S3ClientReference.get(); + s3 = S3ClientReference.get(getApplicationContext()); if (s3 == null) { Log.w(TAG, "TransferService can't get s3 client, and it will stop."); stopSelf(startId); diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index eb135ace942..1920c605510 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -31,7 +31,6 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Callable; import static com.amazonaws.services.s3.internal.Constants.MAXIMUM_UPLOAD_PARTS; import static com.amazonaws.services.s3.internal.Constants.MB; @@ -96,6 +95,7 @@ public class TransferUtility { private final Context appContext; private final TransferDBUtil dbUtil; + public static Function clientRetrieve; /** * Constructs a new TransferUtility specifying the client to use and @@ -105,10 +105,10 @@ public class TransferUtility { * @param clientRetrieve The client callback to retrieve instance * @param context The current context */ - public TransferUtility(Callable clientRetrieve, Context context) { + public TransferUtility(Function clientRetrieve, Context context) { this.appContext = context.getApplicationContext(); this.dbUtil = new TransferDBUtil(appContext); - S3ClientReference.retrieveClientOn(clientRetrieve); + TransferUtility.clientRetrieve = clientRetrieve; } /** From d53517147084cca2f23ccccf89fd9e131c0bf565 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 20 Sep 2016 08:45:34 +0300 Subject: [PATCH 12/19] Added connectiivity type check --- .../s3/receiver/NetworkInfoReceiver.java | 126 ++++++++++++++++++ .../s3/transferutility/DownloadTask.java | 10 +- .../s3/transferutility/TransferRecord.java | 3 +- .../s3/transferutility/TransferService.java | 66 +++------ .../s3/transferutility/TransferUtility.java | 56 +++++--- .../s3/transferutility/UploadTask.java | 18 +-- 6 files changed, 188 insertions(+), 91 deletions(-) create mode 100644 aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java new file mode 100644 index 00000000000..e5c1750f7ab --- /dev/null +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java @@ -0,0 +1,126 @@ +package com.amazonaws.mobileconnectors.s3.receiver; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Handler; +import android.util.Log; + +/** + * A Broadcast receiver to receive network connection change events. + */ +public class NetworkInfoReceiver extends BroadcastReceiver { + + private static final String TAG = "NetworkInfoReceiver"; + + private final Handler handler; + + private final ConnectivityManager connectivityManager; + + private int checkFlag; + + private int disconnectFlag; + + /** + * Constructs a NetworkInfoReceiver. + * @param context Service context + * @param handler a handle to send message to + */ + public NetworkInfoReceiver(final Context context, + final Handler handler, + final int checkFlag, final int disconnectFlag) { + this.connectivityManager = (ConnectivityManager) context + .getSystemService(Context.CONNECTIVITY_SERVICE); + this.handler = handler; + this.checkFlag = checkFlag; + this.disconnectFlag = disconnectFlag; + } + + @Override + public void onReceive(final Context context, final Intent intent) { + if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { + Type checkType = connectionCheckType; + if (connectionCheckType == null) { + checkType = DEFAULT_CONNECTION_CHECK_TYPE; + Log.w(TAG, "Using default type: " + checkType.name()); + } + boolean networkConnected = checkType.isConnected(connectivityManager); + Log.d(TAG, "Network connected: " + networkConnected); + handler.sendEmptyMessage(networkConnected ? + checkFlag : disconnectFlag); + } + } + + public static final Type DEFAULT_CONNECTION_CHECK_TYPE = Type.WIFI_ONLY; + + Type connectionCheckType = DEFAULT_CONNECTION_CHECK_TYPE; + + public void setConnectionCheckType(Type connectionCheckType) { + this.connectionCheckType = connectionCheckType; + } + + public boolean isNetworkConnected() { + return connectionCheckType.isConnected(connectivityManager); + } + + public enum Type { + ANY("any") { + @Override + public boolean verify(final NetworkInfo info) { + return info != null && info.isConnected(); + } + }, + MOBILE_ONLY("mobile_only") { + @Override + public boolean verify(final NetworkInfo info) { + return info != null && info.isConnected() + && info.getType() == ConnectivityManager.TYPE_MOBILE; + } + }, + WIFI_ONLY("wifi_only") { + @Override + public boolean verify(final NetworkInfo info) { + return (info != null && info.isConnected() + && info.getType() == ConnectivityManager.TYPE_WIFI); + } + }; + + private String intentKey; + + Type(String intentKey) { + this.intentKey = intentKey; + } + + /** + * Gets the status of network connectivity. + * + * @return true if network is connected, false otherwise. + */ + boolean isConnected(final ConnectivityManager connectivityManager) { + return verify(connectivityManager.getActiveNetworkInfo()); + } + + protected abstract boolean verify(final NetworkInfo networkInfo); + + public static Type from(final String name) { + if (name != null) { + for (final Type type : values()) { + if (type.intentKey.toLowerCase().equals(name.toLowerCase())) { + return type; + } + } + } + return null; + } + + public static Type from(final String name, final Type defaultType) { + Type type = from(name); + if (type == null) { + return defaultType; + } + return type; + } + } +} diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java index 663699e7b9f..3b4c3f9c8a8 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java @@ -16,20 +16,14 @@ package com.amazonaws.mobileconnectors.s3.transferutility; import android.util.Log; - import com.amazonaws.AmazonClientException; -import com.amazonaws.mobileconnectors.s3.transferutility.TransferService.NetworkInfoReceiver; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.retry.RetryUtils; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.S3Object; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.util.concurrent.Callable; /** diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java index 0093a520588..641f18fca2c 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java @@ -17,9 +17,8 @@ import android.database.Cursor; import android.util.Log; - import com.amazonaws.AmazonClientException; -import com.amazonaws.mobileconnectors.s3.transferutility.TransferService.NetworkInfoReceiver; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.AbortMultipartUploadRequest; import com.amazonaws.util.json.JsonUtils; diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java index 1fa566358ba..3a4f38067c1 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java @@ -16,21 +16,14 @@ package com.amazonaws.mobileconnectors.s3.transferutility; import android.app.Service; -import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.database.Cursor; import android.net.ConnectivityManager; -import android.net.NetworkInfo; -import android.os.Handler; -import android.os.HandlerThread; -import android.os.IBinder; -import android.os.Looper; -import android.os.Message; +import android.os.*; import android.util.Log; - +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.services.s3.AmazonS3; import java.io.FileDescriptor; @@ -66,6 +59,7 @@ public class TransferService extends Service { static final String INTENT_ACTION_TRANSFER_CANCEL = "cancel_transfer"; static final String INTENT_BUNDLE_TRANSFER_ID = "id"; static final String INTENT_BUNDLE_S3_REFERENCE_KEY = "s3_reference_key"; + static final String INTENT_BUNDLE_CONNECTION_CHECK_TYPE = "connection_check_type"; private AmazonS3 s3; @@ -127,44 +121,6 @@ public void onCreate() { setHandlerLooper(handlerThread.getLooper()); } - /** - * A Broadcast receiver to receive network connection change events. - */ - static class NetworkInfoReceiver extends BroadcastReceiver { - private final Handler handler; - private final ConnectivityManager connManager; - - /** - * Constructs a NetworkInfoReceiver. - * - * @param handler a handle to send message to - */ - public NetworkInfoReceiver(Context context, Handler handler) { - this.handler = handler; - connManager = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - } - - @Override - public void onReceive(Context context, Intent intent) { - if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { - final boolean networkConnected = isNetworkConnected(); - Log.d(TAG, "Network connected: " + networkConnected); - handler.sendEmptyMessage(networkConnected ? MSG_CHECK : MSG_DISCONNECT); - } - } - - /** - * Gets the status of network connectivity. - * - * @return true if network is connected, false otherwise. - */ - boolean isNetworkConnected() { - final NetworkInfo info = connManager.getActiveNetworkInfo(); - return info != null && info.isConnected(); - } - } - @Override public int onStartCommand(Intent intent, int flags, int startId) { this.startId = startId; @@ -180,7 +136,12 @@ public int onStartCommand(Intent intent, int flags, int startId) { stopSelf(startId); return START_NOT_STICKY; } - + String networkCheckType = intent.getStringExtra(INTENT_BUNDLE_CONNECTION_CHECK_TYPE); + if (networkCheckType != null) { + networkInfoReceiver.setConnectionCheckType( + NetworkInfoReceiver.Type.from(networkCheckType, + NetworkInfoReceiver.DEFAULT_CONNECTION_CHECK_TYPE)); + } updateHandler.sendMessage(updateHandler.obtainMessage(MSG_EXEC, intent)); if (isFirst) { registerReceiver(networkInfoReceiver, new IntentFilter( @@ -307,7 +268,9 @@ void execCommand(Intent intent) { Log.e(TAG, "Can't find transfer: " + id); } } - transfer.start(s3, dbUtil, updater, networkInfoReceiver); + if (transfer != null) { + transfer.start(s3, dbUtil, updater, networkInfoReceiver); + } } else if (INTENT_ACTION_TRANSFER_CANCEL.equals(action)) { TransferRecord transfer = updater.getTransfer(id); if (transfer == null) { @@ -419,9 +382,10 @@ void pauseAllForNetwork() { * * @param looper new looper */ - void setHandlerLooper(Looper looper) { + void setHandlerLooper(final Looper looper) { updateHandler = new UpdateHandler(looper); - networkInfoReceiver = new NetworkInfoReceiver(getApplicationContext(), updateHandler); + networkInfoReceiver = new NetworkInfoReceiver(getApplicationContext(), updateHandler, + MSG_CHECK, MSG_DISCONNECT); } @Override diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 1a88536b95a..d2159753b01 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -15,17 +15,14 @@ package com.amazonaws.mobileconnectors.s3.transferutility; -import static com.amazonaws.services.s3.internal.Constants.MAXIMUM_UPLOAD_PARTS; -import static com.amazonaws.services.s3.internal.Constants.MB; - import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.util.Log; - import com.amazonaws.AmazonWebServiceRequest; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.ObjectMetadata; @@ -36,6 +33,9 @@ import java.util.List; import java.util.UUID; +import static com.amazonaws.services.s3.internal.Constants.MAXIMUM_UPLOAD_PARTS; +import static com.amazonaws.services.s3.internal.Constants.MB; + /** * The transfer utility is a high-level class for applications to upload and * download files. It inserts upload and download records into the database and @@ -105,7 +105,6 @@ public class TransferUtility { * * @param s3 The client to use when making requests to Amazon S3 * @param context The current context - * @param configuration Configuration parameters for this TransferUtility */ public TransferUtility(AmazonS3 s3, Context context) { this.s3 = s3; @@ -124,6 +123,23 @@ public TransferUtility(AmazonS3 s3, Context context) { * @return A TransferObserver used to track download progress and state */ public TransferObserver download(String bucket, String key, File file) { + return download(bucket, key, file, null); + } + + + /** + * Starts downloading the S3 object specified by the bucket and the key to + * the given file. The file must be a valid file. Directory isn't supported. + * Note that if the given file exists, it'll be overwritten. + * + * @param bucket The name of the bucket containing the object to download. + * @param key The key under which the object to download is stored. + * @param file The file to download the object's data to. + * @param connectionCheckType Type of connection check. Default is {@link NetworkInfoReceiver.Type#WIFI_ONLY} + * @return A TransferObserver used to track download progress and state + */ + public TransferObserver download(String bucket, String key, File file, + NetworkInfoReceiver.Type connectionCheckType) { if (file == null || file.isDirectory()) { throw new IllegalArgumentException("Invalid file: " + file); } @@ -135,7 +151,7 @@ public TransferObserver download(String bucket, String key, File file) { file.delete(); } - sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId); + sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, connectionCheckType); return new TransferObserver(recordId, dbUtil, bucket, key, file); } @@ -147,11 +163,13 @@ public TransferObserver download(String bucket, String key, File file) { * @param key The key in the specified bucket by which to store the new * object. * @param file The file to upload. + * @param connectionCheckType Type of connection check * @return A TransferObserver used to track upload progress and state */ - public TransferObserver upload(String bucket, String key, File file) { + public TransferObserver upload(String bucket, String key, File file, + NetworkInfoReceiver.Type connectionCheckType) { - return upload(bucket, key, file, new ObjectMetadata()); + return upload(bucket, key, file, new ObjectMetadata(), connectionCheckType); } /** @@ -163,12 +181,13 @@ public TransferObserver upload(String bucket, String key, File file) { * object. * @param file The file to upload. * @param cannedAcl The canned ACL to associate with this object + * @param connectionCheckType Type of connection check * @return A TransferObserver used to track upload progress and state */ public TransferObserver upload(String bucket, String key, File file, - CannedAccessControlList cannedAcl) { + CannedAccessControlList cannedAcl, NetworkInfoReceiver.Type connectionCheckType) { - return upload(bucket, key, file, new ObjectMetadata(), cannedAcl); + return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, connectionCheckType); } /** @@ -180,10 +199,12 @@ public TransferObserver upload(String bucket, String key, File file, * object. * @param file The file to upload. * @param metadata The S3 metadata to associate with this object + * @param connectionCheckType Type of connection check * @return A TransferObserver used to track upload progress and state */ - public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata) { - return upload(bucket, key, file, metadata, null); + public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata, + NetworkInfoReceiver.Type connectionCheckType) { + return upload(bucket, key, file, metadata, null, connectionCheckType); } /** @@ -196,10 +217,11 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad * @param file The file to upload. * @param metadata The S3 metadata to associate with this object * @param cannedAcl The canned ACL to associate with this object + * @param connectionCheckType Type of connection check. Default is {@link NetworkInfoReceiver.Type#WIFI_ONLY} * @return A TransferObserver used to track upload progress and state */ public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata, - CannedAccessControlList cannedAcl) { + CannedAccessControlList cannedAcl, NetworkInfoReceiver.Type connectionCheckType) { if (file == null || file.isDirectory()) { throw new IllegalArgumentException("Invalid file: " + file); } @@ -213,7 +235,7 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad recordId = Integer.parseInt(uri.getLastPathSegment()); } - sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId); + sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, connectionCheckType); return new TransferObserver(recordId, dbUtil, bucket, key, file); } @@ -293,7 +315,6 @@ public List getTransfersWithTypeAndState(TransferType type, * @param key The key in the specified bucket by which to store the new * object. * @param file The file to upload. - * @param isUsingEncryption Whether the upload is encrypted. * @return Number of records created in database */ private int createMultipartUploadRecords(String bucket, String key, File file, @@ -423,12 +444,17 @@ public boolean deleteTransferRecord(int id) { * @param id id of the transfer */ private void sendIntent(String action, int id) { + sendIntent(action, id, null); + } + + private void sendIntent(String action, int id, NetworkInfoReceiver.Type networkCheckType) { String s3Key = UUID.randomUUID().toString(); S3ClientReference.put(s3Key, s3); Intent intent = new Intent(appContext, TransferService.class); intent.setAction(action); intent.putExtra(TransferService.INTENT_BUNDLE_TRANSFER_ID, id); intent.putExtra(TransferService.INTENT_BUNDLE_S3_REFERENCE_KEY, s3Key); + intent.putExtra(TransferService.INTENT_BUNDLE_CONNECTION_CHECK_TYPE, networkCheckType); appContext.startService(intent); } diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java index 37e8a81bc74..fd1b6d37978 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadTask.java @@ -16,27 +16,16 @@ package com.amazonaws.mobileconnectors.s3.transferutility; import android.util.Log; - import com.amazonaws.AmazonClientException; -import com.amazonaws.mobileconnectors.s3.transferutility.TransferService.NetworkInfoReceiver; +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.retry.RetryUtils; import com.amazonaws.services.s3.AmazonS3; -import com.amazonaws.services.s3.model.CannedAccessControlList; -import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest; -import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest; -import com.amazonaws.services.s3.model.ObjectMetadata; -import com.amazonaws.services.s3.model.PartETag; -import com.amazonaws.services.s3.model.PutObjectRequest; -import com.amazonaws.services.s3.model.UploadPartRequest; +import com.amazonaws.services.s3.model.*; import com.amazonaws.services.s3.util.Mimetypes; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -256,7 +245,6 @@ private String initiateMultipartUpload(PutObjectRequest putObjectRequest) /** * Creates a PutObjectRequest from the data in the TransferRecord * - * @param por The request to fill * @param upload The data for the Object Metadata * @return Returns a PutObjectRequest with filled in metadata and parameters */ From 97ffff2d560b67a432ac488fd25a5ece73473744 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 20 Sep 2016 09:10:17 +0300 Subject: [PATCH 13/19] Added default upload methods --- .../s3/transferutility/TransferUtility.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index d2159753b01..343d5f6da46 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -172,6 +172,21 @@ public TransferObserver upload(String bucket, String key, File file, return upload(bucket, key, file, new ObjectMetadata(), connectionCheckType); } + /** + * Starts uploading the file to the given bucket, using the given key. The + * file must be a valid file. Directory isn't supported. + * + * @param bucket The name of the bucket to upload the new object to. + * @param key The key in the specified bucket by which to store the new + * object. + * @param file The file to upload. + * @return A TransferObserver used to track upload progress and state + */ + public TransferObserver upload(String bucket, String key, File file) { + + return upload(bucket, key, file, new ObjectMetadata(), null); + } + /** * Starts uploading the file to the given bucket, using the given key. The * file must be a valid file. Directory isn't supported. @@ -190,6 +205,23 @@ public TransferObserver upload(String bucket, String key, File file, return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, connectionCheckType); } + /** + * Starts uploading the file to the given bucket, using the given key. The + * file must be a valid file. Directory isn't supported. + * + * @param bucket The name of the bucket to upload the new object to. + * @param key The key in the specified bucket by which to store the new + * object. + * @param file The file to upload. + * @param cannedAcl The canned ACL to associate with this object + * @return A TransferObserver used to track upload progress and state + */ + public TransferObserver upload(String bucket, String key, File file, + CannedAccessControlList cannedAcl) { + + return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, null); + } + /** * Starts uploading the file to the given bucket, using the given key. The * file must be a valid file. Directory isn't supported. @@ -207,6 +239,21 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad return upload(bucket, key, file, metadata, null, connectionCheckType); } + /** + * Starts uploading the file to the given bucket, using the given key. The + * file must be a valid file. Directory isn't supported. + * + * @param bucket The name of the bucket to upload the new object to. + * @param key The key in the specified bucket by which to store the new + * object. + * @param file The file to upload. + * @param metadata The S3 metadata to associate with this object + * @return A TransferObserver used to track upload progress and state + */ + public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata) { + return upload(bucket, key, file, metadata, null, null); + } + /** * Starts uploading the file to the given bucket, using the given key. The * file must be a valid file. Directory isn't supported. From 74367addd3e7c33119fc221dd28bf0865d606f3c Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 26 Sep 2016 16:56:56 +0200 Subject: [PATCH 14/19] Fixed wrong parameter sending via intent --- .../mobileconnectors/s3/transferutility/TransferUtility.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 343d5f6da46..22ee31e3034 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -501,7 +501,8 @@ private void sendIntent(String action, int id, NetworkInfoReceiver.Type networkC intent.setAction(action); intent.putExtra(TransferService.INTENT_BUNDLE_TRANSFER_ID, id); intent.putExtra(TransferService.INTENT_BUNDLE_S3_REFERENCE_KEY, s3Key); - intent.putExtra(TransferService.INTENT_BUNDLE_CONNECTION_CHECK_TYPE, networkCheckType); + intent.putExtra(TransferService.INTENT_BUNDLE_CONNECTION_CHECK_TYPE, + networkCheckType.name()); appContext.startService(intent); } From 71eb0c4339c42a74487a139373ff5b1c3010f540 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 5 Oct 2016 16:03:11 +0300 Subject: [PATCH 15/19] Fixing passing default connection type --- .../mobileconnectors/s3/transferutility/TransferUtility.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 22ee31e3034..9e07e156a38 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -491,7 +491,7 @@ public boolean deleteTransferRecord(int id) { * @param id id of the transfer */ private void sendIntent(String action, int id) { - sendIntent(action, id, null); + sendIntent(action, id, NetworkInfoReceiver.DEFAULT_CONNECTION_CHECK_TYPE); } private void sendIntent(String action, int id, NetworkInfoReceiver.Type networkCheckType) { From 7b2dd3472975c2ed93d96e32dbd95ec372628770 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 21 Dec 2016 09:11:26 +0300 Subject: [PATCH 16/19] PR review --- .../s3/receiver/NetworkInfoReceiver.java | 2 +- .../s3/transferutility/DownloadTask.java | 7 +- .../TransferConfiguration.java | 31 ++++++ .../s3/transferutility/TransferService.java | 6 +- .../s3/transferutility/TransferUtility.java | 98 +++++-------------- 5 files changed, 65 insertions(+), 79 deletions(-) create mode 100644 aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferConfiguration.java diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java index e5c1750f7ab..7b64a070224 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/receiver/NetworkInfoReceiver.java @@ -53,7 +53,7 @@ public void onReceive(final Context context, final Intent intent) { } } - public static final Type DEFAULT_CONNECTION_CHECK_TYPE = Type.WIFI_ONLY; + public static final Type DEFAULT_CONNECTION_CHECK_TYPE = Type.ANY; Type connectionCheckType = DEFAULT_CONNECTION_CHECK_TYPE; diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java index 3b4c3f9c8a8..65a8e19be2e 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/DownloadTask.java @@ -23,7 +23,12 @@ import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.S3Object; -import java.io.*; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.concurrent.Callable; /** diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferConfiguration.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferConfiguration.java new file mode 100644 index 00000000000..713ca029bb9 --- /dev/null +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferConfiguration.java @@ -0,0 +1,31 @@ +package com.amazonaws.mobileconnectors.s3.transferutility; + +import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; + +/** + * Created by Dmitry Gorohov + */ +public class TransferConfiguration { + + public TransferConfiguration() { + connectionCheckType = NetworkInfoReceiver.Type.ANY; + } + + /** + * Type of network connection validator. + */ + private NetworkInfoReceiver.Type connectionCheckType; + + public NetworkInfoReceiver.Type getConnectionCheckType() { + return connectionCheckType; + } + + public void setConnectionCheckType(final NetworkInfoReceiver.Type connectionCheckType) { + this.connectionCheckType = connectionCheckType; + } + + public TransferConfiguration withConnectionCheckType(final NetworkInfoReceiver.Type connectionCheckType) { + this.connectionCheckType = connectionCheckType; + return this; + } +} diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java index 3a4f38067c1..cdf7f6fc101 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java @@ -21,7 +21,11 @@ import android.content.pm.ApplicationInfo; import android.database.Cursor; import android.net.ConnectivityManager; -import android.os.*; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.os.Looper; +import android.os.Message; import android.util.Log; import com.amazonaws.mobileconnectors.s3.receiver.NetworkInfoReceiver; import com.amazonaws.services.s3.AmazonS3; diff --git a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java index 9e07e156a38..27320afac0f 100644 --- a/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java +++ b/aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtility.java @@ -97,6 +97,7 @@ public class TransferUtility { private final AmazonS3 s3; private final Context appContext; private final TransferDBUtil dbUtil; + private final TransferConfiguration transferConfiguration; /** * Constructs a new TransferUtility specifying the client to use and @@ -106,27 +107,26 @@ public class TransferUtility { * @param s3 The client to use when making requests to Amazon S3 * @param context The current context */ - public TransferUtility(AmazonS3 s3, Context context) { - this.s3 = s3; - this.appContext = context.getApplicationContext(); - this.dbUtil = new TransferDBUtil(appContext); + public TransferUtility(final AmazonS3 s3, final Context context) { + this(s3, context, new TransferConfiguration()); } /** - * Starts downloading the S3 object specified by the bucket and the key to - * the given file. The file must be a valid file. Directory isn't supported. - * Note that if the given file exists, it'll be overwritten. + * Constructs a new TransferUtility specifying the client to use and + * initializes configuration of TransferUtility and a key for S3 client weak + * reference. * - * @param bucket The name of the bucket containing the object to download. - * @param key The key under which the object to download is stored. - * @param file The file to download the object's data to. - * @return A TransferObserver used to track download progress and state + * @param s3 The client to use when making requests to Amazon S3 + * @param context The current context + * @param transferConfiguration Transfer configuration */ - public TransferObserver download(String bucket, String key, File file) { - return download(bucket, key, file, null); + public TransferUtility(final AmazonS3 s3, final Context context, final TransferConfiguration transferConfiguration) { + this.s3 = s3; + this.appContext = context.getApplicationContext(); + this.dbUtil = new TransferDBUtil(appContext); + this.transferConfiguration = transferConfiguration; } - /** * Starts downloading the S3 object specified by the bucket and the key to * the given file. The file must be a valid file. Directory isn't supported. @@ -135,11 +135,9 @@ public TransferObserver download(String bucket, String key, File file) { * @param bucket The name of the bucket containing the object to download. * @param key The key under which the object to download is stored. * @param file The file to download the object's data to. - * @param connectionCheckType Type of connection check. Default is {@link NetworkInfoReceiver.Type#WIFI_ONLY} * @return A TransferObserver used to track download progress and state */ - public TransferObserver download(String bucket, String key, File file, - NetworkInfoReceiver.Type connectionCheckType) { + public TransferObserver download(String bucket, String key, File file) { if (file == null || file.isDirectory()) { throw new IllegalArgumentException("Invalid file: " + file); } @@ -151,27 +149,11 @@ public TransferObserver download(String bucket, String key, File file, file.delete(); } - sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, connectionCheckType); + sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, + transferConfiguration.getConnectionCheckType()); return new TransferObserver(recordId, dbUtil, bucket, key, file); } - /** - * Starts uploading the file to the given bucket, using the given key. The - * file must be a valid file. Directory isn't supported. - * - * @param bucket The name of the bucket to upload the new object to. - * @param key The key in the specified bucket by which to store the new - * object. - * @param file The file to upload. - * @param connectionCheckType Type of connection check - * @return A TransferObserver used to track upload progress and state - */ - public TransferObserver upload(String bucket, String key, File file, - NetworkInfoReceiver.Type connectionCheckType) { - - return upload(bucket, key, file, new ObjectMetadata(), connectionCheckType); - } - /** * Starts uploading the file to the given bucket, using the given key. The * file must be a valid file. Directory isn't supported. @@ -184,25 +166,7 @@ public TransferObserver upload(String bucket, String key, File file, */ public TransferObserver upload(String bucket, String key, File file) { - return upload(bucket, key, file, new ObjectMetadata(), null); - } - - /** - * Starts uploading the file to the given bucket, using the given key. The - * file must be a valid file. Directory isn't supported. - * - * @param bucket The name of the bucket to upload the new object to. - * @param key The key in the specified bucket by which to store the new - * object. - * @param file The file to upload. - * @param cannedAcl The canned ACL to associate with this object - * @param connectionCheckType Type of connection check - * @return A TransferObserver used to track upload progress and state - */ - public TransferObserver upload(String bucket, String key, File file, - CannedAccessControlList cannedAcl, NetworkInfoReceiver.Type connectionCheckType) { - - return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, connectionCheckType); + return upload(bucket, key, file, new ObjectMetadata()); } /** @@ -219,24 +183,7 @@ public TransferObserver upload(String bucket, String key, File file, public TransferObserver upload(String bucket, String key, File file, CannedAccessControlList cannedAcl) { - return upload(bucket, key, file, new ObjectMetadata(), cannedAcl, null); - } - - /** - * Starts uploading the file to the given bucket, using the given key. The - * file must be a valid file. Directory isn't supported. - * - * @param bucket The name of the bucket to upload the new object to. - * @param key The key in the specified bucket by which to store the new - * object. - * @param file The file to upload. - * @param metadata The S3 metadata to associate with this object - * @param connectionCheckType Type of connection check - * @return A TransferObserver used to track upload progress and state - */ - public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata, - NetworkInfoReceiver.Type connectionCheckType) { - return upload(bucket, key, file, metadata, null, connectionCheckType); + return upload(bucket, key, file, new ObjectMetadata(), cannedAcl); } /** @@ -251,7 +198,7 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad * @return A TransferObserver used to track upload progress and state */ public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata) { - return upload(bucket, key, file, metadata, null, null); + return upload(bucket, key, file, metadata, null); } /** @@ -264,11 +211,10 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad * @param file The file to upload. * @param metadata The S3 metadata to associate with this object * @param cannedAcl The canned ACL to associate with this object - * @param connectionCheckType Type of connection check. Default is {@link NetworkInfoReceiver.Type#WIFI_ONLY} * @return A TransferObserver used to track upload progress and state */ public TransferObserver upload(String bucket, String key, File file, ObjectMetadata metadata, - CannedAccessControlList cannedAcl, NetworkInfoReceiver.Type connectionCheckType) { + CannedAccessControlList cannedAcl) { if (file == null || file.isDirectory()) { throw new IllegalArgumentException("Invalid file: " + file); } @@ -282,7 +228,7 @@ public TransferObserver upload(String bucket, String key, File file, ObjectMetad recordId = Integer.parseInt(uri.getLastPathSegment()); } - sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, connectionCheckType); + sendIntent(TransferService.INTENT_ACTION_TRANSFER_ADD, recordId, transferConfiguration.getConnectionCheckType()); return new TransferObserver(recordId, dbUtil, bucket, key, file); } From 717df033edf9f137637dae7b10bd7787fc23a9f6 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 6 Apr 2017 13:36:34 +0300 Subject: [PATCH 17/19] 2.4.0 --- aws-android-sdk-s3/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index fa2f9c07f90..d2b7e3210de 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -15,7 +15,7 @@ 2.4.0 - 2.3.7 + 2.4.0 From 8dc89cedad6d8a2a356646c8eb0074438ea20cf0 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 6 Apr 2017 17:40:38 +0300 Subject: [PATCH 18/19] commons-io updated to 2.5 --- aws-android-sdk-s3/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index d2b7e3210de..277ac1f1bc3 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -62,7 +62,7 @@ commons-io commons-io - 2.4 + 2.5 org.bouncycastle From d4509beb0afe73cba5da8bb7d87b79463f8aa709 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 29 Sep 2017 07:52:21 +0300 Subject: [PATCH 19/19] Nexus repository support --- aws-android-sdk-s3/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws-android-sdk-s3/pom.xml b/aws-android-sdk-s3/pom.xml index 277ac1f1bc3..8a1fd700507 100644 --- a/aws-android-sdk-s3/pom.xml +++ b/aws-android-sdk-s3/pom.xml @@ -108,14 +108,14 @@ snapshot.repo.ebuilder.io ebuilder-snapshot - s3://repo.ebuilder.io/snapshot + https://artifacts.ebuilder.io/repository/eBuilder-maven-snapshot/ true default repo.ebuilder.io ebuilder-release - s3://repo.ebuilder.io/release + https://artifacts.ebuilder.io/repository/eBuilder-maven-release/ false default