Browse Source

Upload fixes for api <21

uploads
Apostolos Fanakis 6 years ago
parent
commit
4d47cb2fe3
  1. 47
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java
  2. 57
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  3. 125
      app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java

47
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); TextView itemText = newFileRow.findViewById(R.id.upload_file_item_text);
itemText.setTypeface(Typeface.createFromAsset(this.getAssets() itemText.setTypeface(Typeface.createFromAsset(this.getAssets()
, "fonts/fontawesome-webfont.ttf")); , "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). newFileRow.findViewById(R.id.upload_file_item_remove).setOnClickListener(view -> {
setOnClickListener(view -> { int fileIndex = filesListView.indexOfChild(newFileRow);
int fileIndex = filesListView.indexOfChild(newFileRow); filesListView.removeViewAt(fileIndex);
filesListView.removeViewAt(fileIndex);
if (filesList.get(fileIndex).isCameraPhoto()) { if (filesList.get(fileIndex).isCameraPhoto()) {
storage.deleteFile(filesList.get(fileIndex).getPhotoFile().getAbsolutePath()); storage.deleteFile(filesList.get(fileIndex).getPhotoFile().getAbsolutePath());
} }
filesList.remove(fileIndex); filesList.remove(fileIndex);
if (filesList.isEmpty()) { if (filesList.isEmpty()) {
filesListView.setVisibility(View.GONE); filesListView.setVisibility(View.GONE);
} else if (filesList.size() == 1) { } else if (filesList.size() == 1) {
textWatcher.setFileExtension(FileUtils.getFileExtension(FileUtils. textWatcher.setFileExtension(FileUtils.getFileExtension(FileUtils.
filenameFromUri(this, filesList.get(0).getFileUri()))); filenameFromUri(this, filesList.get(0).getFileUri())));
} }
}); });
filesListView.addView(newFileRow); filesListView.addView(newFileRow);
filesListView.setVisibility(View.VISIBLE); filesListView.setVisibility(View.VISIBLE);
@ -677,14 +677,19 @@ public class UploadActivity extends BaseActivity {
uploadNotificationConfig.getCompleted().iconResourceID = android.R.drawable.stat_sys_upload_done; uploadNotificationConfig.getCompleted().iconResourceID = android.R.drawable.stat_sys_upload_done;
uploadNotificationConfig.getError().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; 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().iconColorResourceID = android.R.drawable.stat_sys_upload_done;
uploadNotificationConfig.getCancelled().autoClear = true;
Intent combinedActionsIntent = new Intent(UploadsReceiver.ACTION_COMBINED_UPLOAD); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
combinedActionsIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, uploadID); 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));
uploadNotificationConfig.setClickIntentForAllStatuses(PendingIntent.getBroadcast(context,
1, combinedActionsIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent retryIntent = new Intent(context, UploadsReceiver.class); Intent retryIntent = new Intent(context, UploadsReceiver.class);
retryIntent.setAction(UploadsReceiver.ACTION_RETRY_UPLOAD); retryIntent.setAction(UploadsReceiver.ACTION_RETRY_UPLOAD);

57
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -137,8 +137,13 @@ public abstract class BaseActivity extends AppCompatActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
updateDrawer(); 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 @Override
@ -146,7 +151,8 @@ public abstract class BaseActivity extends AppCompatActivity {
super.onPause(); super.onPause();
if (drawer != null) //close drawer animation after returning to activity if (drawer != null) //close drawer animation after returning to activity
drawer.closeDrawer(); drawer.closeDrawer();
if (uploadsShowDialogReceiver != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && uploadsShowDialogReceiver != null) {
this.unregisterReceiver(uploadsShowDialogReceiver); this.unregisterReceiver(uploadsShowDialogReceiver);
} }
} }
@ -762,33 +768,36 @@ public abstract class BaseActivity extends AppCompatActivity {
return; 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) { Intent retryIntent = new Intent(context, UploadsReceiver.class);
AlertDialog.Builder progressDialogBuilder = new AlertDialog.Builder(activityContext); retryIntent.setAction(UploadsReceiver.ACTION_RETRY_UPLOAD);
LayoutInflater inflater = LayoutInflater.from(activityContext); retryIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, dialogUploadID);
LinearLayout progressDialogLayout = (LinearLayout) inflater.inflate(R.layout.dialog_upload_progress, null);
MaterialProgressBar dialogProgressBar = progressDialogLayout.findViewById(R.id.dialogProgressBar); if (uploadsProgressDialog == null) {
dialogProgressBar.setMax(100); 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) -> { progressDialogBuilder.setView(progressDialogLayout);
progressDialog.dismiss();
});
progressDialogBuilder.setNegativeButton("Cancel", (progressDialog, progressWhich) -> {
UploadService.stopUpload(dialogUploadID);
progressDialog.dismiss();
});
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); UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent);
uploadsProgressDialog.show(); uploadsProgressDialog.show();
} else { } else {
UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID); UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent);
uploadsProgressDialog.show(); uploadsProgressDialog.show();
}
} }
} }
} }

125
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.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window; import android.view.Window;
import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.snatik.storage.Storage; import com.snatik.storage.Storage;
import net.gotev.uploadservice.MultipartUploadRequest;
import net.gotev.uploadservice.ServerResponse; import net.gotev.uploadservice.ServerResponse;
import net.gotev.uploadservice.UploadInfo; import net.gotev.uploadservice.UploadInfo;
import net.gotev.uploadservice.UploadService; import net.gotev.uploadservice.UploadService;
@ -23,6 +28,7 @@ import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
public class UploadsReceiver extends UploadServiceBroadcastReceiver { public class UploadsReceiver extends UploadServiceBroadcastReceiver {
public static final String UPLOAD_ID_KEY = "UPLOAD_ID_KEY"; 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_COMBINED_UPLOAD = "ACTION_COMBINED_UPLOAD";
public static final String ACTION_CANCEL_UPLOAD = "ACTION_CANCEL_UPLOAD"; public static final String ACTION_CANCEL_UPLOAD = "ACTION_CANCEL_UPLOAD";
@ -31,6 +37,7 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver {
private Storage storage; private Storage storage;
private static AlertDialog uploadProgressDialog; private static AlertDialog uploadProgressDialog;
private static String dialogUploadID; private static String dialogUploadID;
private static Intent multipartUploadRetryIntent;
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -47,7 +54,12 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver {
UploadService.stopUpload(uploadID); UploadService.stopUpload(uploadID);
break; break;
case ACTION_RETRY_UPLOAD: 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; break;
default: default:
super.onReceive(context, intent); super.onReceive(context, intent);
@ -57,23 +69,37 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver {
@Override @Override
public void onProgress(Context context, UploadInfo uploadInfo) { public void onProgress(Context context, UploadInfo uploadInfo) {
if (uploadProgressDialog != null && uploadProgressDialog.isShowing() && if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP &&
uploadInfo.getUploadId().equals(dialogUploadID)) { uploadInfo.getUploadId().equals(dialogUploadID) &&
Window progressWindow = uploadProgressDialog.getWindow(); uploadProgressDialog != null) {
if (progressWindow != null) { Button alertDialogNeutral = uploadProgressDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
MaterialProgressBar dialogProgressBar = progressWindow.findViewById(R.id.dialogProgressBar); alertDialogNeutral.setText("Resume on background");
TextView dialogProgressText = progressWindow.findViewById(R.id.dialog_upload_progress_text); alertDialogNeutral.setOnClickListener(v -> uploadProgressDialog.dismiss());
dialogProgressBar.setProgress(uploadInfo.getProgressPercent()); Button alertDialogNegative = uploadProgressDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
dialogProgressText.setText(context.getResources().getString( alertDialogNegative.setText("Cancel");
R.string.upload_progress_dialog_bytes_uploaded, alertDialogNegative.setOnClickListener(v -> {
(float) uploadInfo.getUploadRate(), UploadService.stopUpload(dialogUploadID);
(int) uploadInfo.getUploadedBytes() / 1000,
(int) uploadInfo.getTotalBytes() / 1000));
}
if (uploadInfo.getUploadedBytes() == uploadInfo.getTotalBytes()) {
uploadProgressDialog.dismiss(); 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 @Override
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse,
Exception exception) { 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(); Toast.makeText(context.getApplicationContext(), "Upload failed", Toast.LENGTH_SHORT).show();
if (storage == null) { if (storage == null) {
storage = new Storage(context.getApplicationContext()); storage = new Storage(context.getApplicationContext());
} }
UploadsHelper.deleteTempFiles(storage);
} }
@Override @Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { 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(); Toast.makeText(context.getApplicationContext(), "Upload completed successfully", Toast.LENGTH_SHORT).show();
if (storage == null) { if (storage == null) {
storage = new Storage(context.getApplicationContext()); storage = new Storage(context.getApplicationContext());
} }
UploadsHelper.deleteTempFiles(storage);
BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null); BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null);
UploadsHelper.deleteTempFiles(storage);
} }
@Override @Override
public void onCancelled(Context context, UploadInfo uploadInfo) { 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(); Toast.makeText(context.getApplicationContext(), "Upload canceled", Toast.LENGTH_SHORT).show();
if (storage == null) { if (storage == null) {
storage = new Storage(context.getApplicationContext()); storage = new Storage(context.getApplicationContext());
} }
UploadsHelper.deleteTempFiles(storage);
NotificationManager notificationManager = (NotificationManager) context.getApplicationContext(). /*NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().
getSystemService(Context.NOTIFICATION_SERVICE); getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) { if (notificationManager != null) {
notificationManager.cancel(uploadInfo.getNotificationID()); notificationManager.cancel(uploadInfo.getNotificationID());
} }*/
UploadsHelper.deleteTempFiles(storage);
} }
public static void setDialogDisplay(AlertDialog uploadProgressDialog, public static void setDialogDisplay(AlertDialog uploadProgressDialog, String dialogUploadID,
String dialogUploadID) { Intent multipartUploadRetryIntent) {
UploadsReceiver.uploadProgressDialog = uploadProgressDialog; UploadsReceiver.uploadProgressDialog = uploadProgressDialog;
UploadsReceiver.dialogUploadID = dialogUploadID; UploadsReceiver.dialogUploadID = dialogUploadID;
UploadsReceiver.multipartUploadRetryIntent = multipartUploadRetryIntent;
} }
} }
Loading…
Cancel
Save