mirror of https://github.com/ThmmyNoLife/mTHMMY
Apostolos Fanakis
6 years ago
8 changed files with 262 additions and 204 deletions
@ -1,75 +1,82 @@ |
|||
package gr.thmmy.mthmmy.services; |
|||
|
|||
import android.app.NotificationManager; |
|||
import android.content.Context; |
|||
import android.content.Intent; |
|||
import android.os.Bundle; |
|||
import android.widget.Toast; |
|||
|
|||
import com.snatik.storage.Storage; |
|||
|
|||
import net.gotev.uploadservice.ServerResponse; |
|||
import net.gotev.uploadservice.UploadInfo; |
|||
import net.gotev.uploadservice.UploadService; |
|||
import net.gotev.uploadservice.UploadServiceBroadcastReceiver; |
|||
|
|||
import gr.thmmy.mthmmy.activities.upload.UploadsHelper; |
|||
import gr.thmmy.mthmmy.base.BaseApplication; |
|||
|
|||
public class UploadsReceiver extends UploadServiceBroadcastReceiver { |
|||
public interface Delegate { |
|||
void onProgress(long uploadedBytes, long totalBytes, int progress, double uploadRate); |
|||
public static final String UPLOAD_NOTIFICATION_ID_KEY = "UPLOAD_NOTIFICATION_ID_KEY"; |
|||
|
|||
void onError(Exception exception); |
|||
public static final String ACTION_CANCEL_UPLOAD = "ACTION_CANCEL_UPLOAD"; |
|||
public static final String ACTION_RETRY_UPLOAD = "ACTION_RETRY_UPLOAD"; |
|||
|
|||
void onCompleted(int serverResponseCode, byte[] serverResponseBody); |
|||
|
|||
void onCancelled(); |
|||
} |
|||
|
|||
private Delegate delegate; |
|||
private Storage storage; |
|||
|
|||
public void setDelegate(Delegate delegate) { |
|||
this.delegate = delegate; |
|||
} |
|||
@Override |
|||
public void onReceive(Context context, Intent intent) { |
|||
String intentAction = intent.getAction(); |
|||
Bundle intentBundle = intent.getExtras(); |
|||
if (intentAction == null || intentBundle == null) { |
|||
return; |
|||
} |
|||
|
|||
public void provideStorage(Storage storage) { |
|||
this.storage = storage; |
|||
if (intentAction.equals(ACTION_CANCEL_UPLOAD)) { |
|||
String notificationID = intentBundle.getString(UPLOAD_NOTIFICATION_ID_KEY); |
|||
UploadService.stopUpload(notificationID); |
|||
} else if (intentAction.equals(ACTION_RETRY_UPLOAD)) { |
|||
//TODO
|
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onProgress(Context context, UploadInfo uploadInfo) { |
|||
if (delegate != null) { |
|||
delegate.onProgress(uploadInfo.getUploadedBytes(), uploadInfo.getTotalBytes(), |
|||
uploadInfo.getProgressPercent(), uploadInfo.getUploadRate()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, |
|||
Exception exception) { |
|||
if (delegate != null) { |
|||
delegate.onError(exception); |
|||
} |
|||
if (storage != null){ |
|||
UploadsHelper.deleteTempFiles(storage); |
|||
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 (delegate != null) { |
|||
delegate.onCompleted(serverResponse.getHttpCode(), serverResponse.getBody()); |
|||
} |
|||
if (storage != null){ |
|||
UploadsHelper.deleteTempFiles(storage); |
|||
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); |
|||
} |
|||
|
|||
@Override |
|||
public void onCancelled(Context context, UploadInfo uploadInfo) { |
|||
if (delegate != null) { |
|||
delegate.onCancelled(); |
|||
Toast.makeText(context.getApplicationContext(), "Upload canceled", Toast.LENGTH_SHORT).show(); |
|||
if (storage == null) { |
|||
storage = new Storage(context.getApplicationContext()); |
|||
} |
|||
if (storage != null){ |
|||
UploadsHelper.deleteTempFiles(storage); |
|||
UploadsHelper.deleteTempFiles(storage); |
|||
|
|||
NotificationManager notificationManager = (NotificationManager) context.getApplicationContext(). |
|||
getSystemService(Context.NOTIFICATION_SERVICE); |
|||
if (notificationManager != null) { |
|||
notificationManager.cancel(uploadInfo.getNotificationID()); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,4 @@ |
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp"> |
|||
<path android:fillColor="#26A69A" android:pathData="M19,8l-4,4h3c0,3.31 -2.69,6 -6,6 -1.01,0 -1.97,-0.25 -2.8,-0.7l-1.46,1.46C8.97,19.54 10.43,20 12,20c4.42,0 8,-3.58 8,-8h3l-4,-4zM6,12c0,-3.31 2.69,-6 6,-6 1.01,0 1.97,0.25 2.8,0.7l1.46,-1.46C15.03,4.46 13.57,4 12,4c-4.42,0 -8,3.58 -8,8H1l4,4 4,-4H6z"/> |
|||
</vector> |
@ -0,0 +1,4 @@ |
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp"> |
|||
<path android:fillColor="#26A69A" android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM17,15.59L15.59,17 12,13.41 8.41,17 7,15.59 10.59,12 7,8.41 8.41,7 12,10.59 15.59,7 17,8.41 13.41,12 17,15.59z"/> |
|||
</vector> |
@ -1,38 +0,0 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
|||
xmlns:app="http://schemas.android.com/apk/res-auto" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:orientation="vertical" |
|||
android:paddingEnd="24dp" |
|||
android:paddingStart="24dp"> |
|||
|
|||
<TextView |
|||
android:id="@+id/dialog_title" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:layout_marginBottom="20dp" |
|||
android:layout_marginTop="24dp" |
|||
android:text="@string/upload_progress_dialog_title" |
|||
android:textColor="@color/accent" |
|||
android:textSize="20sp" /> |
|||
|
|||
<me.zhanghai.android.materialprogressbar.MaterialProgressBar |
|||
android:id="@+id/dialogProgressBar" |
|||
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="@dimen/progress_bar_height" |
|||
android:indeterminate="false" |
|||
app:mpb_progressStyle="horizontal" |
|||
app:mpb_progressTint="@color/accent" /> |
|||
|
|||
<TextView |
|||
android:id="@+id/dialog_bytes_uploaded" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:layout_marginBottom="20dp" |
|||
android:layout_marginTop="12dp" |
|||
android:minEms="64" |
|||
android:text="@string/upload_progress_dialog_bytes_uploaded" |
|||
android:textColor="@color/white" /> |
|||
</LinearLayout> |
Loading…
Reference in new issue