Browse Source

Upload fixes for api <21

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

13
app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java

@ -645,10 +645,10 @@ 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 -> {
newFileRow.findViewById(R.id.upload_file_item_remove).setOnClickListener(view -> {
int fileIndex = filesListView.indexOfChild(newFileRow);
filesListView.removeViewAt(fileIndex);
@ -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;
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);

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

@ -137,16 +137,22 @@ public abstract class BaseActivity extends AppCompatActivity {
protected void onResume() {
super.onResume();
updateDrawer();
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
protected void onPause() {
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,8 +768,13 @@ public abstract class BaseActivity extends AppCompatActivity {
return;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
String dialogUploadID = intentBundle.getString(UPLOAD_ID_KEY);
Intent retryIntent = new Intent(context, UploadsReceiver.class);
retryIntent.setAction(UploadsReceiver.ACTION_RETRY_UPLOAD);
retryIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, dialogUploadID);
if (uploadsProgressDialog == null) {
AlertDialog.Builder progressDialogBuilder = new AlertDialog.Builder(activityContext);
LayoutInflater inflater = LayoutInflater.from(activityContext);
@ -774,24 +785,22 @@ public abstract class BaseActivity extends AppCompatActivity {
progressDialogBuilder.setView(progressDialogLayout);
progressDialogBuilder.setNeutralButton("Resume on background", (progressDialog, progressWhich) -> {
progressDialog.dismiss();
uploadsProgressDialog = progressDialogBuilder.create();
//Empty buttons are needed, they are updated with correct values in the receiver
uploadsProgressDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "placeholder", (progressDialog, progressWhich) -> {
});
progressDialogBuilder.setNegativeButton("Cancel", (progressDialog, progressWhich) -> {
UploadService.stopUpload(dialogUploadID);
progressDialog.dismiss();
uploadsProgressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "placeholder", (progressDialog, progressWhich) -> {
});
uploadsProgressDialog = progressDialogBuilder.create();
UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID);
UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent);
uploadsProgressDialog.show();
} else {
UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID);
UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent);
uploadsProgressDialog.show();
}
}
}
}
//----------------------------------MISC----------------------
protected void setMainActivity(MainActivity mainActivity) {

97
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,8 +69,21 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver {
@Override
public void onProgress(Context context, UploadInfo uploadInfo) {
if (uploadProgressDialog != null && uploadProgressDialog.isShowing() &&
uploadInfo.getUploadId().equals(dialogUploadID)) {
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);
@ -77,46 +102,98 @@ 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;
}
}
Loading…
Cancel
Save