From 4d47cb2fe38c05a2f058d6802f64fcebeb7b6011 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Wed, 12 Sep 2018 14:49:02 +0300 Subject: [PATCH] Upload fixes for api <21 --- .../activities/upload/UploadActivity.java | 47 ++++--- .../gr/thmmy/mthmmy/base/BaseActivity.java | 57 ++++---- .../mthmmy/services/UploadsReceiver.java | 125 ++++++++++++++---- 3 files changed, 160 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java index 6477ea1f..914a50fa 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java @@ -645,24 +645,24 @@ public class UploadActivity extends BaseActivity { TextView itemText = newFileRow.findViewById(R.id.upload_file_item_text); itemText.setTypeface(Typeface.createFromAsset(this.getAssets() , "fonts/fontawesome-webfont.ttf")); - itemText.setText(faIconFromFilename(this, filename) + " " + filename); + String filenameWithIcon = faIconFromFilename(this, filename) + " " + filename; + itemText.setText(filenameWithIcon); - newFileRow.findViewById(R.id.upload_file_item_remove). - setOnClickListener(view -> { - int fileIndex = filesListView.indexOfChild(newFileRow); - filesListView.removeViewAt(fileIndex); + newFileRow.findViewById(R.id.upload_file_item_remove).setOnClickListener(view -> { + int fileIndex = filesListView.indexOfChild(newFileRow); + filesListView.removeViewAt(fileIndex); - if (filesList.get(fileIndex).isCameraPhoto()) { - storage.deleteFile(filesList.get(fileIndex).getPhotoFile().getAbsolutePath()); - } - filesList.remove(fileIndex); - if (filesList.isEmpty()) { - filesListView.setVisibility(View.GONE); - } else if (filesList.size() == 1) { - textWatcher.setFileExtension(FileUtils.getFileExtension(FileUtils. - filenameFromUri(this, filesList.get(0).getFileUri()))); - } - }); + if (filesList.get(fileIndex).isCameraPhoto()) { + storage.deleteFile(filesList.get(fileIndex).getPhotoFile().getAbsolutePath()); + } + filesList.remove(fileIndex); + if (filesList.isEmpty()) { + filesListView.setVisibility(View.GONE); + } else if (filesList.size() == 1) { + textWatcher.setFileExtension(FileUtils.getFileExtension(FileUtils. + filenameFromUri(this, filesList.get(0).getFileUri()))); + } + }); filesListView.addView(newFileRow); filesListView.setVisibility(View.VISIBLE); @@ -677,14 +677,19 @@ public class UploadActivity extends BaseActivity { uploadNotificationConfig.getCompleted().iconResourceID = android.R.drawable.stat_sys_upload_done; uploadNotificationConfig.getError().iconResourceID = android.R.drawable.stat_sys_upload_done; uploadNotificationConfig.getError().iconColorResourceID = R.color.error_red; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + uploadNotificationConfig.getError().message = "Error during upload. Click for options"; + } uploadNotificationConfig.getCancelled().iconColorResourceID = android.R.drawable.stat_sys_upload_done; + uploadNotificationConfig.getCancelled().autoClear = true; - Intent combinedActionsIntent = new Intent(UploadsReceiver.ACTION_COMBINED_UPLOAD); - combinedActionsIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, uploadID); - - uploadNotificationConfig.setClickIntentForAllStatuses(PendingIntent.getBroadcast(context, - 1, combinedActionsIntent, PendingIntent.FLAG_UPDATE_CURRENT)); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + Intent combinedActionsIntent = new Intent(UploadsReceiver.ACTION_COMBINED_UPLOAD); + combinedActionsIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, uploadID); + uploadNotificationConfig.setClickIntentForAllStatuses(PendingIntent.getBroadcast(context, + 1, combinedActionsIntent, PendingIntent.FLAG_UPDATE_CURRENT)); + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Intent retryIntent = new Intent(context, UploadsReceiver.class); retryIntent.setAction(UploadsReceiver.ACTION_RETRY_UPLOAD); diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 2b7c6c1e..74e762e9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -137,8 +137,13 @@ public abstract class BaseActivity extends AppCompatActivity { protected void onResume() { super.onResume(); updateDrawer(); - uploadsShowDialogReceiver = new UploadsShowDialogReceiver(this); - this.registerReceiver(uploadsShowDialogReceiver, new IntentFilter(UploadsReceiver.ACTION_COMBINED_UPLOAD)); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + if (uploadsShowDialogReceiver == null) { + uploadsShowDialogReceiver = new UploadsShowDialogReceiver(this); + } + this.registerReceiver(uploadsShowDialogReceiver, new IntentFilter(UploadsReceiver.ACTION_COMBINED_UPLOAD)); + } } @Override @@ -146,7 +151,8 @@ public abstract class BaseActivity extends AppCompatActivity { super.onPause(); if (drawer != null) //close drawer animation after returning to activity drawer.closeDrawer(); - if (uploadsShowDialogReceiver != null) { + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && uploadsShowDialogReceiver != null) { this.unregisterReceiver(uploadsShowDialogReceiver); } } @@ -762,33 +768,36 @@ public abstract class BaseActivity extends AppCompatActivity { return; } - String dialogUploadID = intentBundle.getString(UPLOAD_ID_KEY); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + String dialogUploadID = intentBundle.getString(UPLOAD_ID_KEY); - if (uploadsProgressDialog == null) { - AlertDialog.Builder progressDialogBuilder = new AlertDialog.Builder(activityContext); - LayoutInflater inflater = LayoutInflater.from(activityContext); - LinearLayout progressDialogLayout = (LinearLayout) inflater.inflate(R.layout.dialog_upload_progress, null); + Intent retryIntent = new Intent(context, UploadsReceiver.class); + retryIntent.setAction(UploadsReceiver.ACTION_RETRY_UPLOAD); + retryIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, dialogUploadID); - MaterialProgressBar dialogProgressBar = progressDialogLayout.findViewById(R.id.dialogProgressBar); - dialogProgressBar.setMax(100); + if (uploadsProgressDialog == null) { + AlertDialog.Builder progressDialogBuilder = new AlertDialog.Builder(activityContext); + LayoutInflater inflater = LayoutInflater.from(activityContext); + LinearLayout progressDialogLayout = (LinearLayout) inflater.inflate(R.layout.dialog_upload_progress, null); - progressDialogBuilder.setView(progressDialogLayout); + MaterialProgressBar dialogProgressBar = progressDialogLayout.findViewById(R.id.dialogProgressBar); + dialogProgressBar.setMax(100); - progressDialogBuilder.setNeutralButton("Resume on background", (progressDialog, progressWhich) -> { - progressDialog.dismiss(); - }); - progressDialogBuilder.setNegativeButton("Cancel", (progressDialog, progressWhich) -> { - UploadService.stopUpload(dialogUploadID); - progressDialog.dismiss(); - }); + progressDialogBuilder.setView(progressDialogLayout); - uploadsProgressDialog = progressDialogBuilder.create(); + uploadsProgressDialog = progressDialogBuilder.create(); + //Empty buttons are needed, they are updated with correct values in the receiver + uploadsProgressDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "placeholder", (progressDialog, progressWhich) -> { + }); + uploadsProgressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "placeholder", (progressDialog, progressWhich) -> { + }); - UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID); - uploadsProgressDialog.show(); - } else { - UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID); - uploadsProgressDialog.show(); + UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent); + uploadsProgressDialog.show(); + } else { + UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent); + uploadsProgressDialog.show(); + } } } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java b/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java index 2eaa3856..c30ed990 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java @@ -3,14 +3,19 @@ package gr.thmmy.mthmmy.services; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; +import android.os.Build; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AlertDialog; +import android.view.View; import android.view.Window; +import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.snatik.storage.Storage; +import net.gotev.uploadservice.MultipartUploadRequest; import net.gotev.uploadservice.ServerResponse; import net.gotev.uploadservice.UploadInfo; import net.gotev.uploadservice.UploadService; @@ -23,6 +28,7 @@ import me.zhanghai.android.materialprogressbar.MaterialProgressBar; public class UploadsReceiver extends UploadServiceBroadcastReceiver { public static final String UPLOAD_ID_KEY = "UPLOAD_ID_KEY"; + public static final String UPLOAD_REQUEST_KEY = "UPLOAD_REQUEST_KEY"; public static final String ACTION_COMBINED_UPLOAD = "ACTION_COMBINED_UPLOAD"; public static final String ACTION_CANCEL_UPLOAD = "ACTION_CANCEL_UPLOAD"; @@ -31,6 +37,7 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver { private Storage storage; private static AlertDialog uploadProgressDialog; private static String dialogUploadID; + private static Intent multipartUploadRetryIntent; @Override public void onReceive(Context context, Intent intent) { @@ -47,7 +54,12 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver { UploadService.stopUpload(uploadID); break; case ACTION_RETRY_UPLOAD: - //TODO + MultipartUploadRequest multipartUploadRequest = (MultipartUploadRequest) intentBundle.get(UPLOAD_REQUEST_KEY); + if (multipartUploadRequest != null) { + multipartUploadRequest.startUpload(); + } else { + Toast.makeText(context.getApplicationContext(), "Couldn't retry upload.", Toast.LENGTH_SHORT).show(); + } break; default: super.onReceive(context, intent); @@ -57,23 +69,37 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver { @Override public void onProgress(Context context, UploadInfo uploadInfo) { - if (uploadProgressDialog != null && uploadProgressDialog.isShowing() && - uploadInfo.getUploadId().equals(dialogUploadID)) { - Window progressWindow = uploadProgressDialog.getWindow(); - if (progressWindow != null) { - MaterialProgressBar dialogProgressBar = progressWindow.findViewById(R.id.dialogProgressBar); - TextView dialogProgressText = progressWindow.findViewById(R.id.dialog_upload_progress_text); - - dialogProgressBar.setProgress(uploadInfo.getProgressPercent()); - dialogProgressText.setText(context.getResources().getString( - R.string.upload_progress_dialog_bytes_uploaded, - (float) uploadInfo.getUploadRate(), - (int) uploadInfo.getUploadedBytes() / 1000, - (int) uploadInfo.getTotalBytes() / 1000)); - } - - if (uploadInfo.getUploadedBytes() == uploadInfo.getTotalBytes()) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && + uploadInfo.getUploadId().equals(dialogUploadID) && + uploadProgressDialog != null) { + Button alertDialogNeutral = uploadProgressDialog.getButton(AlertDialog.BUTTON_NEUTRAL); + alertDialogNeutral.setText("Resume on background"); + alertDialogNeutral.setOnClickListener(v -> uploadProgressDialog.dismiss()); + + Button alertDialogNegative = uploadProgressDialog.getButton(AlertDialog.BUTTON_NEGATIVE); + alertDialogNegative.setText("Cancel"); + alertDialogNegative.setOnClickListener(v -> { + UploadService.stopUpload(dialogUploadID); uploadProgressDialog.dismiss(); + }); + + if (uploadProgressDialog.isShowing()) { + Window progressWindow = uploadProgressDialog.getWindow(); + if (progressWindow != null) { + MaterialProgressBar dialogProgressBar = progressWindow.findViewById(R.id.dialogProgressBar); + TextView dialogProgressText = progressWindow.findViewById(R.id.dialog_upload_progress_text); + + dialogProgressBar.setProgress(uploadInfo.getProgressPercent()); + dialogProgressText.setText(context.getResources().getString( + R.string.upload_progress_dialog_bytes_uploaded, + (float) uploadInfo.getUploadRate(), + (int) uploadInfo.getUploadedBytes() / 1000, + (int) uploadInfo.getTotalBytes() / 1000)); + } + + if (uploadInfo.getUploadedBytes() == uploadInfo.getTotalBytes()) { + uploadProgressDialog.dismiss(); + } } } } @@ -81,42 +107,93 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver { @Override public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && + uploadInfo.getUploadId().equals(dialogUploadID) && + uploadProgressDialog != null) { + Button alertDialogNeutral = uploadProgressDialog.getButton(AlertDialog.BUTTON_NEUTRAL); + alertDialogNeutral.setText("Retry"); + alertDialogNeutral.setOnClickListener(v -> { + if (multipartUploadRetryIntent != null) { + LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context.getApplicationContext()); + localBroadcastManager.sendBroadcast(multipartUploadRetryIntent); + } + uploadProgressDialog.dismiss(); + }); + + Button alertDialogNegative = uploadProgressDialog.getButton(AlertDialog.BUTTON_NEGATIVE); + alertDialogNegative.setText("Cancel"); + alertDialogNegative.setOnClickListener(v -> { + NotificationManager notificationManager = (NotificationManager) context.getApplicationContext(). + getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager != null) { + notificationManager.cancel(uploadInfo.getNotificationID()); + } + UploadsHelper.deleteTempFiles(storage); + uploadProgressDialog.dismiss(); + }); + + if (uploadProgressDialog.isShowing()) { + Window progressWindow = uploadProgressDialog.getWindow(); + if (progressWindow != null) { + MaterialProgressBar dialogProgressBar = progressWindow.findViewById(R.id.dialogProgressBar); + TextView dialogProgressText = progressWindow.findViewById(R.id.dialog_upload_progress_text); + + dialogProgressBar.setVisibility(View.GONE); + dialogProgressText.setText("Upload failed."); + } + + if (uploadInfo.getUploadedBytes() == uploadInfo.getTotalBytes()) { + uploadProgressDialog.dismiss(); + } + } + } + Toast.makeText(context.getApplicationContext(), "Upload failed", Toast.LENGTH_SHORT).show(); if (storage == null) { storage = new Storage(context.getApplicationContext()); } - UploadsHelper.deleteTempFiles(storage); } @Override public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + uploadProgressDialog = null; + dialogUploadID = null; + } + Toast.makeText(context.getApplicationContext(), "Upload completed successfully", Toast.LENGTH_SHORT).show(); if (storage == null) { storage = new Storage(context.getApplicationContext()); } - UploadsHelper.deleteTempFiles(storage); BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null); + UploadsHelper.deleteTempFiles(storage); } @Override public void onCancelled(Context context, UploadInfo uploadInfo) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + uploadProgressDialog = null; + dialogUploadID = null; + } + Toast.makeText(context.getApplicationContext(), "Upload canceled", Toast.LENGTH_SHORT).show(); if (storage == null) { storage = new Storage(context.getApplicationContext()); } - UploadsHelper.deleteTempFiles(storage); - NotificationManager notificationManager = (NotificationManager) context.getApplicationContext(). + /*NotificationManager notificationManager = (NotificationManager) context.getApplicationContext(). getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager != null) { notificationManager.cancel(uploadInfo.getNotificationID()); - } + }*/ + UploadsHelper.deleteTempFiles(storage); } - public static void setDialogDisplay(AlertDialog uploadProgressDialog, - String dialogUploadID) { + public static void setDialogDisplay(AlertDialog uploadProgressDialog, String dialogUploadID, + Intent multipartUploadRetryIntent) { UploadsReceiver.uploadProgressDialog = uploadProgressDialog; UploadsReceiver.dialogUploadID = dialogUploadID; + UploadsReceiver.multipartUploadRetryIntent = multipartUploadRetryIntent; } } \ No newline at end of file