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); 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);
@ -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;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Intent combinedActionsIntent = new Intent(UploadsReceiver.ACTION_COMBINED_UPLOAD); Intent combinedActionsIntent = new Intent(UploadsReceiver.ACTION_COMBINED_UPLOAD);
combinedActionsIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, uploadID); combinedActionsIntent.putExtra(UploadsReceiver.UPLOAD_ID_KEY, uploadID);
uploadNotificationConfig.setClickIntentForAllStatuses(PendingIntent.getBroadcast(context, uploadNotificationConfig.setClickIntentForAllStatuses(PendingIntent.getBroadcast(context,
1, combinedActionsIntent, PendingIntent.FLAG_UPDATE_CURRENT)); 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);

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

@ -137,16 +137,22 @@ public abstract class BaseActivity extends AppCompatActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
updateDrawer(); updateDrawer();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (uploadsShowDialogReceiver == null) {
uploadsShowDialogReceiver = new UploadsShowDialogReceiver(this); uploadsShowDialogReceiver = new UploadsShowDialogReceiver(this);
}
this.registerReceiver(uploadsShowDialogReceiver, new IntentFilter(UploadsReceiver.ACTION_COMBINED_UPLOAD)); this.registerReceiver(uploadsShowDialogReceiver, new IntentFilter(UploadsReceiver.ACTION_COMBINED_UPLOAD));
} }
}
@Override @Override
protected void onPause() { protected void onPause() {
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,8 +768,13 @@ public abstract class BaseActivity extends AppCompatActivity {
return; return;
} }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
String dialogUploadID = intentBundle.getString(UPLOAD_ID_KEY); 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) { if (uploadsProgressDialog == null) {
AlertDialog.Builder progressDialogBuilder = new AlertDialog.Builder(activityContext); AlertDialog.Builder progressDialogBuilder = new AlertDialog.Builder(activityContext);
LayoutInflater inflater = LayoutInflater.from(activityContext); LayoutInflater inflater = LayoutInflater.from(activityContext);
@ -774,24 +785,22 @@ public abstract class BaseActivity extends AppCompatActivity {
progressDialogBuilder.setView(progressDialogLayout); progressDialogBuilder.setView(progressDialogLayout);
progressDialogBuilder.setNeutralButton("Resume on background", (progressDialog, progressWhich) -> { uploadsProgressDialog = progressDialogBuilder.create();
progressDialog.dismiss(); //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) -> { uploadsProgressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "placeholder", (progressDialog, progressWhich) -> {
UploadService.stopUpload(dialogUploadID);
progressDialog.dismiss();
}); });
uploadsProgressDialog = progressDialogBuilder.create(); UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent);
UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID);
uploadsProgressDialog.show(); uploadsProgressDialog.show();
} else { } else {
UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID); UploadsReceiver.setDialogDisplay(uploadsProgressDialog, dialogUploadID, retryIntent);
uploadsProgressDialog.show(); uploadsProgressDialog.show();
} }
} }
} }
}
//----------------------------------MISC---------------------- //----------------------------------MISC----------------------
protected void setMainActivity(MainActivity mainActivity) { 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.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,8 +69,21 @@ 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) &&
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(); Window progressWindow = uploadProgressDialog.getWindow();
if (progressWindow != null) { if (progressWindow != null) {
MaterialProgressBar dialogProgressBar = progressWindow.findViewById(R.id.dialogProgressBar); MaterialProgressBar dialogProgressBar = progressWindow.findViewById(R.id.dialogProgressBar);
@ -77,46 +102,98 @@ 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