Browse Source

Create global upload receiver, Add upload progress dialog

uploads
Apostolos Fanakis 6 years ago
parent
commit
7a525fb81e
  1. 9
      app/src/main/AndroidManifest.xml
  2. 140
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java
  4. 8
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  5. 75
      app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java
  6. 38
      app/src/main/res/layout/dialog_upload_progress.xml
  7. 3
      app/src/main/res/values/strings.xml

9
app/src/main/AndroidManifest.xml

@ -20,6 +20,7 @@
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
<activity
android:name=".activities.main.MainActivity"
android:configChanges="orientation|screenSize"
@ -147,6 +148,14 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<receiver
android:name=".services.UploadsReceiver"
android:exported="false">
<intent-filter>
<action android:name="gr.thmmy.mthmmy.uploadservice.broadcast.status" />
</intent-filter>
</receiver>
</application>
</manifest>

140
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<UploadCategory> uploadRootCategories = new ArrayList<>();
private ParseUploadPageTask parseUploadPageTask;
private ArrayList<String> 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

4
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);

8
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;

75
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);
}
}
}

38
app/src/main/res/layout/dialog_upload_progress.xml

@ -0,0 +1,38 @@
<?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>

3
app/src/main/res/values/strings.xml

@ -124,6 +124,9 @@
<string name="upload_filename_info">Please follow the filename rules as\ndescribed
in&#160;<a href="https://www.thmmy.gr/smf/index.php?topic=34294.0">this topic</a>.\n
\nThis does not rename your local files.</string>
<string name="upload_progress_dialog_title">Uploading your files</string>
<string name="upload_progress_dialog_zipping_files">Zipping files.</string>
<string name="upload_progress_dialog_bytes_uploaded">Uploading at %1$.2f Kbit/s, %2$d/%3$d KBytes uploaded.</string>
<!--Upload Fields Builder Activity-->
<string name="upload_fields_builder_type_radio_buttons_title">Select type of upload</string>

Loading…
Cancel
Save