From 790453047dfa77bc6684ea494af8e1397db67f48 Mon Sep 17 00:00:00 2001 From: jendib Date: Thu, 8 Jan 2015 00:34:57 +0100 Subject: [PATCH] Android: file upload background service --- docs-android/app/src/main/AndroidManifest.xml | 9 ++ .../docs/activity/DocumentViewActivity.java | 18 ++- .../docs/service/FileUploadService.java | 139 ++++++++++++++++++ .../app/src/main/res/values/strings.xml | 6 + 4 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 docs-android/app/src/main/java/com/sismics/docs/service/FileUploadService.java diff --git a/docs-android/app/src/main/AndroidManifest.xml b/docs-android/app/src/main/AndroidManifest.xml index b26ef828..c30d847e 100644 --- a/docs-android/app/src/main/AndroidManifest.xml +++ b/docs-android/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + + + + + + diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java index c2bd584e..26575b6d 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java @@ -33,7 +33,7 @@ import com.sismics.docs.listener.JsonHttpResponseHandler; import com.sismics.docs.model.application.ApplicationContext; import com.sismics.docs.resource.DocumentResource; import com.sismics.docs.resource.FileResource; -import com.sismics.docs.util.DialogUtil; +import com.sismics.docs.service.FileUploadService; import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.TagUtil; @@ -223,8 +223,9 @@ public class DocumentViewActivity extends ActionBarActivity { case R.id.upload_file: Intent intent = new Intent(Intent.ACTION_GET_CONTENT) - .setType("*/*") - .putExtra("android.intent.extra.ALLOW_MULTIPLE", true); + .setType("*/*") + .putExtra("android.intent.extra.ALLOW_MULTIPLE", true) + .addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), REQUEST_CODE_ADD_FILE); return true; @@ -317,7 +318,7 @@ public class DocumentViewActivity extends ActionBarActivity { // Show a progress dialog while deleting final ProgressDialog progressDialog = ProgressDialog.show(DocumentViewActivity.this, getString(R.string.document_editing_title), - getString(R.string.document_editing_message), true, true, + getString(R.string.document_deleting_message), true, true, new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { @@ -389,6 +390,15 @@ public class DocumentViewActivity extends ActionBarActivity { } } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_CODE_ADD_FILE && resultCode == RESULT_OK) { + Intent intent = new Intent(this, FileUploadService.class) + .putExtra(FileUploadService.PARAM_URI, data.getData()); + startService(intent); + } + } + @Override protected void onDestroy() { EventBus.getDefault().unregister(this); diff --git a/docs-android/app/src/main/java/com/sismics/docs/service/FileUploadService.java b/docs-android/app/src/main/java/com/sismics/docs/service/FileUploadService.java new file mode 100644 index 00000000..34758d55 --- /dev/null +++ b/docs-android/app/src/main/java/com/sismics/docs/service/FileUploadService.java @@ -0,0 +1,139 @@ +package com.sismics.docs.service; + +import android.app.IntentService; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Intent; +import android.net.Uri; +import android.os.PowerManager; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationCompat.Builder; +import android.util.Log; + +import com.sismics.docs.R; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Service to upload a file to a document in the background. + * + * @author bgamard + */ +public class FileUploadService extends IntentService { + private static final String TAG = "FileUploadService"; + + private static final int UPLOAD_NOTIFICATION_ID = 1; + private static final int UPLOAD_NOTIFICATION_ID_DONE = 2; + public static final String PARAM_URI = "uri"; + + private NotificationManager notificationManager; + private Builder notification; + private PowerManager.WakeLock wakeLock; + + public FileUploadService() { + super(FileUploadService.class.getName()); + } + + @Override + public void onCreate() { + super.onCreate(); + + notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + notification = new NotificationCompat.Builder(this); + PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); + wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); + } + + @Override + protected void onHandleIntent(Intent intent) { + if (intent == null) { + return; + } + + wakeLock.acquire(); + try { + onStart(); + handleFileUpload((Uri) intent.getParcelableExtra(PARAM_URI)); + } catch (Exception e) { + Log.e(TAG, "Error uploading the file", e); + onError(); + } finally { + wakeLock.release(); + } + } + + /** + * Actually uploading the file. + * + * @param uri Data URI + * @throws IOException + */ + private void handleFileUpload(final Uri uri) throws IOException { + InputStream is = null; + try { + is = getContentResolver().openInputStream(uri); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + // Ignore + } + } + } + + onComplete(); + } + + /** + * On upload start. + */ + private void onStart() { + notification.setContentTitle(getString(R.string.upload_notification_title)) + .setContentText(getString(R.string.upload_notification_message)) + .setContentIntent(PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT)) + .setSmallIcon(R.drawable.ic_launcher) + .setProgress(100, 0, true) + .setOngoing(true); + + startForeground(UPLOAD_NOTIFICATION_ID, notification.build()); + } + + /** + * On upload progress. + * + * @param progress Progression (100 based) + */ + private void onProgress(final int progress) { + notification.setContentTitle(getString(R.string.upload_notification_title)) + .setContentText(getString(R.string.upload_notification_message)) + .setSmallIcon(R.drawable.ic_launcher) + .setProgress(100, progress, false) + .setOngoing(true); + + startForeground(UPLOAD_NOTIFICATION_ID, notification.build()); + } + + /** + * On upload complete. + */ + private void onComplete() { + stopForeground(true); + } + + /** + * On upload error. + */ + private void onError() { + stopForeground(false); + + notification.setContentTitle(getString(R.string.upload_notification_title)) + .setContentText(getString(R.string.upload_notification_error)) + .setSmallIcon(R.drawable.ic_launcher) + .setProgress(0, 0, false) + .setOngoing(false); + + notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE, notification.build()); + } +} \ No newline at end of file diff --git a/docs-android/app/src/main/res/values/strings.xml b/docs-android/app/src/main/res/values/strings.xml index 2b50bb2a..853ce09d 100644 --- a/docs-android/app/src/main/res/values/strings.xml +++ b/docs-android/app/src/main/res/values/strings.xml @@ -83,6 +83,12 @@ Delete document Really delete this document and all associated files? Network error while deleting this document + Deleting document + Error while reading the file + Adding a file + Uploading the new file to the document + File upload completed + Error uploading the new file