mirror of
https://github.com/sismics/docs.git
synced 2024-11-26 07:34:55 +01:00
Android: file upload background service
This commit is contained in:
parent
5befef2992
commit
790453047d
@ -6,6 +6,7 @@
|
|||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".MainApplication"
|
android:name=".MainApplication"
|
||||||
@ -45,6 +46,14 @@
|
|||||||
<provider android:name=".provider.RecentSuggestionsProvider"
|
<provider android:name=".provider.RecentSuggestionsProvider"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:authorities="com.sismics.docs.provider.RecentSuggestionsProvider" />
|
android:authorities="com.sismics.docs.provider.RecentSuggestionsProvider" />
|
||||||
|
<service
|
||||||
|
android:name=".service.FileUploadService"
|
||||||
|
android:enabled="true"
|
||||||
|
android:exported="false" >
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="com.sismics.docs.file.upload"/>
|
||||||
|
</intent-filter>
|
||||||
|
</service>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -33,7 +33,7 @@ import com.sismics.docs.listener.JsonHttpResponseHandler;
|
|||||||
import com.sismics.docs.model.application.ApplicationContext;
|
import com.sismics.docs.model.application.ApplicationContext;
|
||||||
import com.sismics.docs.resource.DocumentResource;
|
import com.sismics.docs.resource.DocumentResource;
|
||||||
import com.sismics.docs.resource.FileResource;
|
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.PreferenceUtil;
|
||||||
import com.sismics.docs.util.TagUtil;
|
import com.sismics.docs.util.TagUtil;
|
||||||
|
|
||||||
@ -224,7 +224,8 @@ public class DocumentViewActivity extends ActionBarActivity {
|
|||||||
case R.id.upload_file:
|
case R.id.upload_file:
|
||||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
|
||||||
.setType("*/*")
|
.setType("*/*")
|
||||||
.putExtra("android.intent.extra.ALLOW_MULTIPLE", true);
|
.putExtra("android.intent.extra.ALLOW_MULTIPLE", true)
|
||||||
|
.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), REQUEST_CODE_ADD_FILE);
|
startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), REQUEST_CODE_ADD_FILE);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -317,7 +318,7 @@ public class DocumentViewActivity extends ActionBarActivity {
|
|||||||
// Show a progress dialog while deleting
|
// Show a progress dialog while deleting
|
||||||
final ProgressDialog progressDialog = ProgressDialog.show(DocumentViewActivity.this,
|
final ProgressDialog progressDialog = ProgressDialog.show(DocumentViewActivity.this,
|
||||||
getString(R.string.document_editing_title),
|
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() {
|
new DialogInterface.OnCancelListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCancel(DialogInterface dialog) {
|
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
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
EventBus.getDefault().unregister(this);
|
EventBus.getDefault().unregister(this);
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -83,6 +83,12 @@
|
|||||||
<string name="delete_document_title">Delete document</string>
|
<string name="delete_document_title">Delete document</string>
|
||||||
<string name="delete_document_message">Really delete this document and all associated files?</string>
|
<string name="delete_document_message">Really delete this document and all associated files?</string>
|
||||||
<string name="document_delete_failure">Network error while deleting this document</string>
|
<string name="document_delete_failure">Network error while deleting this document</string>
|
||||||
|
<string name="document_deleting_message">Deleting document</string>
|
||||||
|
<string name="error_reading_file">Error while reading the file</string>
|
||||||
|
<string name="upload_notification_title">Adding a file</string>
|
||||||
|
<string name="upload_notification_message">Uploading the new file to the document</string>
|
||||||
|
<string name="upload_notification_completed">File upload completed</string>
|
||||||
|
<string name="upload_notification_error">Error uploading the new file</string>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user