diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cd4582c4..18afb39f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,7 @@ + + + + + + + \ No newline at end of file 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 a4e700a4..6ace69a1 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 @@ -35,10 +35,7 @@ import android.widget.TextView; import android.widget.Toast; import net.gotev.uploadservice.MultipartUploadRequest; -import net.gotev.uploadservice.ServerResponse; -import net.gotev.uploadservice.UploadInfo; import net.gotev.uploadservice.UploadNotificationConfig; -import net.gotev.uploadservice.UploadStatusDelegate; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -53,6 +50,7 @@ import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.model.UploadCategory; import gr.thmmy.mthmmy.model.UploadFile; +import gr.thmmy.mthmmy.services.UploadsReceiver; import gr.thmmy.mthmmy.utils.AppCompatSpinnerWithoutDefault; import gr.thmmy.mthmmy.utils.FileUtils; import gr.thmmy.mthmmy.utils.TakePhoto; @@ -89,6 +87,7 @@ public class UploadActivity extends BaseActivity { private static final int MAX_FILE_SIZE_SUPPORTED = 45000000; + private UploadsReceiver uploadsReceiver = new UploadsReceiver(); private ArrayList uploadRootCategories = new ArrayList<>(); private ParseUploadPageTask parseUploadPageTask; private ArrayList bundleCategory; @@ -111,6 +110,58 @@ public class UploadActivity extends BaseActivity { private EditText uploadDescription; private AppCompatButton titleDescriptionBuilderButton; private LinearLayout filesListView; + private AlertDialog uploadProgressDialog; + private MaterialProgressBar dialogProgressBar; + private TextView dialogProgressText; + + private UploadsReceiver.Delegate uploadDelegate = new UploadsReceiver.Delegate() { + @Override + public void onProgress(long uploadedBytes, long totalBytes, int progress, double uploadRate) { + if (uploadProgressDialog != null) { + dialogProgressBar.setProgress(progress); + dialogProgressText.setText(getResources().getString( + R.string.upload_progress_dialog_bytes_uploaded, (float) uploadRate, + (int) uploadedBytes / 1000, (int) totalBytes / 1000)); + + if (uploadedBytes == totalBytes) { + uploadProgressDialog.dismiss(); + } + } + } + + @Override + public void onError(Exception exception) { + Toast.makeText(getApplicationContext(), "Upload failed", Toast.LENGTH_SHORT).show(); + progressBar.setVisibility(View.GONE); + + } + + @Override + public void onCompleted(int serverResponseCode, byte[] serverResponseBody) { + Toast.makeText(getApplicationContext(), "Upload completed successfully", Toast.LENGTH_SHORT).show(); + + uploadTitle.setText(null); + textWatcher.setFileExtension(""); + uploadFilename.setText(null); + hasModifiedFilename = false; + uploadDescription.setText(null); + filesList.clear(); + filesListView.removeAllViews(); + photoFileCreated = null; + progressBar.setVisibility(View.GONE); + + if (uploadProgressDialog != null) { + uploadProgressDialog.dismiss(); + } + + } + + @Override + public void onCancelled() { + Toast.makeText(getApplicationContext(), "Upload canceled", Toast.LENGTH_SHORT).show(); + progressBar.setVisibility(View.GONE); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { @@ -148,6 +199,9 @@ public class UploadActivity extends BaseActivity { progressBar = findViewById(R.id.progressBar); + uploadsReceiver.setDelegate(uploadDelegate); + uploadsReceiver.provideStorage(storage); + findViewById(R.id.upload_outer_scrollview).setVerticalScrollBarEnabled(false); categoriesSpinners = findViewById(R.id.upload_spinners); rootCategorySpinner = findViewById(R.id.upload_spinner_category_root); @@ -325,6 +379,26 @@ public class UploadActivity extends BaseActivity { builder.setTitle("Upload to thmmy"); builder.setMessage("Are you sure?"); builder.setPositiveButton("YES, FIRE AWAY", (dialog, which) -> { + AlertDialog.Builder progressDialogBuilder = new AlertDialog.Builder(this); + LayoutInflater inflater = this.getLayoutInflater(); + LinearLayout progressDialogLayout = (LinearLayout) inflater.inflate(R.layout.dialog_upload_progress, null); + + dialogProgressBar = progressDialogLayout.findViewById(R.id.dialogProgressBar); + dialogProgressBar.setMax(100); + dialogProgressText = progressDialogLayout.findViewById(R.id.dialog_bytes_uploaded); + + progressDialogBuilder.setView(progressDialogLayout); + progressDialogBuilder.setNeutralButton("Resume on background", (progressDialog, progressWhich) -> { + uploadProgressDialog.dismiss(); + finish(); + }); + uploadProgressDialog = progressDialogBuilder.create(); + uploadProgressDialog.setCancelable(false); + + dialog.dismiss(); + uploadProgressDialog.show(); + + //Checks settings and possibly adds "Uploaded from mTHMMY" string to description SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(view.getContext()); if (sharedPrefs.getBoolean(UPLOADING_APP_SIGNATURE_ENABLE_KEY, true)) { @@ -375,9 +449,16 @@ public class UploadActivity extends BaseActivity { tempFileUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", zipFile); + dialogProgressText.setText(R.string.upload_progress_dialog_zipping_files); UploadsHelper.zip(this, filesListArray, tempFileUri); } + for (UploadFile file : filesList) { + if (file.isCameraPhoto()) { + TakePhoto.galleryAddPic(this, file.getPhotoFile()); + } + } + try { UploadNotificationConfig uploadNotificationConfig = new UploadNotificationConfig(); uploadNotificationConfig.setIconForAllStatuses(android.R.drawable.stat_sys_upload); @@ -401,55 +482,11 @@ public class UploadActivity extends BaseActivity { .addParameter("tp-uploaduser", uploaderProfileIndex) .setNotificationConfig(uploadNotificationConfig) .setMaxRetries(2) - .setDelegate(new UploadStatusDelegate() { - @Override - public void onProgress(Context context, UploadInfo uploadInfo) { - } - - @Override - public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, - Exception exception) { - Toast.makeText(context, "Upload failed", Toast.LENGTH_SHORT).show(); - UploadsHelper.deleteTempFiles(storage); - progressBar.setVisibility(View.GONE); - } - - @Override - public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { - Toast.makeText(context, "Upload completed successfully", Toast.LENGTH_SHORT).show(); - UploadsHelper.deleteTempFiles(storage); - BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null); - - for (UploadFile file : filesList) { - if (file.isCameraPhoto()) { - TakePhoto.galleryAddPic(context, file.getPhotoFile()); - } - } - - uploadTitle.setText(null); - uploadFilename.setText(null); - hasModifiedFilename = false; - uploadDescription.setText(null); - filesList.clear(); - filesListView.removeAllViews(); - photoFileCreated = null; - progressBar.setVisibility(View.GONE); - } - - @Override - public void onCancelled(Context context, UploadInfo uploadInfo) { - Toast.makeText(context, "Upload canceled", Toast.LENGTH_SHORT).show(); - - UploadsHelper.deleteTempFiles(storage); - progressBar.setVisibility(View.GONE); - } - }) .startUpload(); } catch (Exception exception) { Timber.e(exception, "AndroidUploadService: %s", exception.getMessage()); progressBar.setVisibility(View.GONE); } - dialog.dismiss(); }); builder.setNegativeButton("NOPE", (dialog, which) -> { @@ -489,6 +526,15 @@ public class UploadActivity extends BaseActivity { protected void onResume() { drawer.setSelection(UPLOAD_ID); super.onResume(); + uploadsReceiver.setDelegate(uploadDelegate); + uploadsReceiver.provideStorage(storage); + uploadsReceiver.register(this); + } + + @Override + protected void onPause() { + super.onPause(); + uploadsReceiver.unregister(this); } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java index d7755dac..a19f926b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java @@ -25,7 +25,7 @@ import java.util.zip.ZipOutputStream; import gr.thmmy.mthmmy.utils.FileUtils; import timber.log.Timber; -class UploadsHelper { +public class UploadsHelper { private final static int BUFFER = 4096; private static final String TEMP_FILES_DIRECTORY = "~tmp_mThmmy_uploads"; @@ -131,7 +131,7 @@ class UploadsHelper { } } - static void deleteTempFiles(Storage storage) { + public static void deleteTempFiles(Storage storage) { File tempFilesDirectory = new File(Environment.getExternalStorageDirectory().getPath() + File.separatorChar + TEMP_FILES_DIRECTORY); 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 96d2008c..f6f11188 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -78,6 +78,9 @@ public abstract class BaseActivity extends AppCompatActivity { //SessionManager protected static SessionManager sessionManager; + //Storage manager + protected Storage storage; + //Bookmarks private static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; private static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; @@ -91,7 +94,6 @@ public abstract class BaseActivity extends AppCompatActivity { //Common UI elements protected Toolbar toolbar; protected Drawer drawer; - protected Storage storage; private MainActivity mainActivity; @@ -140,6 +142,10 @@ public abstract class BaseActivity extends AppCompatActivity { return sessionManager; } + public Storage getStorage() { + return storage; + } + //TODO: move stuff below (?) //------------------------------------------DRAWER STUFF---------------------------------------- protected static final int HOME_ID = 0; diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java b/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java new file mode 100644 index 00000000..ebf245cc --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java @@ -0,0 +1,75 @@ +package gr.thmmy.mthmmy.services; + +import android.content.Context; + +import com.snatik.storage.Storage; + +import net.gotev.uploadservice.ServerResponse; +import net.gotev.uploadservice.UploadInfo; +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); + + void onError(Exception exception); + + void onCompleted(int serverResponseCode, byte[] serverResponseBody); + + void onCancelled(); + } + + private Delegate delegate; + private Storage storage; + + public void setDelegate(Delegate delegate) { + this.delegate = delegate; + } + + public void provideStorage(Storage storage) { + this.storage = storage; + } + + @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); + } + } + + @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); + } + BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null); + } + + @Override + public void onCancelled(Context context, UploadInfo uploadInfo) { + if (delegate != null) { + delegate.onCancelled(); + } + if (storage != null){ + UploadsHelper.deleteTempFiles(storage); + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_upload_progress.xml b/app/src/main/res/layout/dialog_upload_progress.xml new file mode 100644 index 00000000..ac800ce9 --- /dev/null +++ b/app/src/main/res/layout/dialog_upload_progress.xml @@ -0,0 +1,38 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 17d61e6d..a3a65219 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -124,6 +124,9 @@ Please follow the filename rules as\ndescribed in this topic.\n \nThis does not rename your local files. + Uploading your files + Zipping files. + Uploading at %1$.2f Kbit/s, %2$d/%3$d KBytes uploaded. Select type of upload