mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 14:07:55 +01:00
Android: file upload
This commit is contained in:
parent
790453047d
commit
c9210c39c4
@ -21,7 +21,8 @@ public class MainApplication extends Application {
|
|||||||
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
|
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
|
||||||
|
|
||||||
// TODO Fullscreen preview
|
// TODO Fullscreen preview
|
||||||
// TODO Files adding/deleting
|
// TODO Event on file uploaded to refresh file list
|
||||||
|
// TODO Files deleting
|
||||||
|
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,12 @@ package com.sismics.docs.activity;
|
|||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.DownloadManager;
|
import android.app.DownloadManager;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
|
import android.content.ClipData;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
@ -42,7 +44,9 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import de.greenrobot.event.EventBus;
|
import de.greenrobot.event.EventBus;
|
||||||
|
|
||||||
@ -392,10 +396,35 @@ public class DocumentViewActivity extends ActionBarActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
|
if (document == null) return;
|
||||||
|
|
||||||
if (requestCode == REQUEST_CODE_ADD_FILE && resultCode == RESULT_OK) {
|
if (requestCode == REQUEST_CODE_ADD_FILE && resultCode == RESULT_OK) {
|
||||||
Intent intent = new Intent(this, FileUploadService.class)
|
List<Uri> uriList = new ArrayList<>();
|
||||||
.putExtra(FileUploadService.PARAM_URI, data.getData());
|
// Single file upload
|
||||||
startService(intent);
|
if (data.getData() != null) {
|
||||||
|
uriList.add(data.getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle multiple file upload
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
|
||||||
|
ClipData clipData = data.getClipData();
|
||||||
|
if (clipData != null) {
|
||||||
|
for (int i = 0; i < clipData.getItemCount(); ++i) {
|
||||||
|
Uri uri = clipData.getItemAt(i).getUri();
|
||||||
|
if (uri != null) {
|
||||||
|
uriList.add(uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload all files
|
||||||
|
for (Uri uri : uriList) {
|
||||||
|
Intent intent = new Intent(this, FileUploadService.class)
|
||||||
|
.putExtra(FileUploadService.PARAM_URI, uri)
|
||||||
|
.putExtra(FileUploadService.PARAM_DOCUMENT_ID, document.optString("id"));
|
||||||
|
startService(intent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,12 +36,12 @@ public class BaseResource {
|
|||||||
/**
|
/**
|
||||||
* User-Agent to use.
|
* User-Agent to use.
|
||||||
*/
|
*/
|
||||||
private static String USER_AGENT = null;
|
protected static String USER_AGENT = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accept-Language header.
|
* Accept-Language header.
|
||||||
*/
|
*/
|
||||||
private static String ACCEPT_LANGUAGE = null;
|
protected static String ACCEPT_LANGUAGE = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP client.
|
* HTTP client.
|
||||||
@ -69,8 +69,7 @@ public class BaseResource {
|
|||||||
* @param context Context
|
* @param context Context
|
||||||
*/
|
*/
|
||||||
protected static void init(Context context) {
|
protected static void init(Context context) {
|
||||||
PersistentCookieStore cookieStore = new PersistentCookieStore(context);
|
client.setCookieStore(new PersistentCookieStore(context));
|
||||||
client.setCookieStore(cookieStore);
|
|
||||||
|
|
||||||
if (USER_AGENT == null) {
|
if (USER_AGENT == null) {
|
||||||
USER_AGENT = "Sismics Docs Android " + ApplicationUtil.getVersionName(context) + "/Android " + Build.VERSION.RELEASE + "/" + Build.MODEL;
|
USER_AGENT = "Sismics Docs Android " + ApplicationUtil.getVersionName(context) + "/Android " + Build.VERSION.RELEASE + "/" + Build.MODEL;
|
||||||
|
@ -2,8 +2,14 @@ package com.sismics.docs.resource;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.loopj.android.http.PersistentCookieStore;
|
||||||
|
import com.loopj.android.http.RequestParams;
|
||||||
|
import com.loopj.android.http.SyncHttpClient;
|
||||||
import com.sismics.docs.listener.JsonHttpResponseHandler;
|
import com.sismics.docs.listener.JsonHttpResponseHandler;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Access to /file API.
|
* Access to /file API.
|
||||||
@ -23,4 +29,32 @@ public class FileResource extends BaseResource {
|
|||||||
|
|
||||||
client.get(getApiUrl(context) + "/file/list?id=" + documentId, responseHandler);
|
client.get(getApiUrl(context) + "/file/list?id=" + documentId, responseHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PUT /file.
|
||||||
|
*
|
||||||
|
* @param context Context
|
||||||
|
* @param documentId Document ID
|
||||||
|
* @param is Input stream
|
||||||
|
* @param responseHandler Callback
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static void addSync(Context context, String documentId, InputStream is, JsonHttpResponseHandler responseHandler) throws Exception {
|
||||||
|
init(context);
|
||||||
|
|
||||||
|
SyncHttpClient client = new SyncHttpClient();
|
||||||
|
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||||
|
trustStore.load(null, null);
|
||||||
|
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
|
||||||
|
sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
|
client.setSSLSocketFactory(sf);
|
||||||
|
client.setCookieStore(new PersistentCookieStore(context));
|
||||||
|
client.setUserAgent(USER_AGENT);
|
||||||
|
client.addHeader("Accept-Language", ACCEPT_LANGUAGE);
|
||||||
|
|
||||||
|
RequestParams params = new RequestParams();
|
||||||
|
params.put("id", documentId);
|
||||||
|
params.put("file", is, "file", "application/octet-stream", true);
|
||||||
|
client.put(getApiUrl(context) + "/file", params, responseHandler);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,11 @@ import android.support.v4.app.NotificationCompat.Builder;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.sismics.docs.R;
|
import com.sismics.docs.R;
|
||||||
|
import com.sismics.docs.listener.JsonHttpResponseHandler;
|
||||||
|
import com.sismics.docs.resource.FileResource;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -26,6 +31,7 @@ public class FileUploadService extends IntentService {
|
|||||||
private static final int UPLOAD_NOTIFICATION_ID = 1;
|
private static final int UPLOAD_NOTIFICATION_ID = 1;
|
||||||
private static final int UPLOAD_NOTIFICATION_ID_DONE = 2;
|
private static final int UPLOAD_NOTIFICATION_ID_DONE = 2;
|
||||||
public static final String PARAM_URI = "uri";
|
public static final String PARAM_URI = "uri";
|
||||||
|
public static final String PARAM_DOCUMENT_ID = "documentId";
|
||||||
|
|
||||||
private NotificationManager notificationManager;
|
private NotificationManager notificationManager;
|
||||||
private Builder notification;
|
private Builder notification;
|
||||||
@ -54,7 +60,7 @@ public class FileUploadService extends IntentService {
|
|||||||
wakeLock.acquire();
|
wakeLock.acquire();
|
||||||
try {
|
try {
|
||||||
onStart();
|
onStart();
|
||||||
handleFileUpload((Uri) intent.getParcelableExtra(PARAM_URI));
|
handleFileUpload(intent.getStringExtra(PARAM_DOCUMENT_ID), (Uri) intent.getParcelableExtra(PARAM_URI));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Error uploading the file", e);
|
Log.e(TAG, "Error uploading the file", e);
|
||||||
onError();
|
onError();
|
||||||
@ -66,24 +72,23 @@ public class FileUploadService extends IntentService {
|
|||||||
/**
|
/**
|
||||||
* Actually uploading the file.
|
* Actually uploading the file.
|
||||||
*
|
*
|
||||||
|
* @param documentId Document ID
|
||||||
* @param uri Data URI
|
* @param uri Data URI
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private void handleFileUpload(final Uri uri) throws IOException {
|
private void handleFileUpload(final String documentId, final Uri uri) throws Exception {
|
||||||
InputStream is = null;
|
final InputStream is = getContentResolver().openInputStream(uri);
|
||||||
try {
|
FileResource.addSync(this, documentId, is, new JsonHttpResponseHandler() {
|
||||||
is = getContentResolver().openInputStream(uri);
|
@Override
|
||||||
} finally {
|
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||||
if (is != null) {
|
FileUploadService.this.onComplete();
|
||||||
try {
|
|
||||||
is.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
onComplete();
|
@Override
|
||||||
|
public void onAllFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
|
||||||
|
FileUploadService.this.onError();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,28 +98,13 @@ public class FileUploadService extends IntentService {
|
|||||||
notification.setContentTitle(getString(R.string.upload_notification_title))
|
notification.setContentTitle(getString(R.string.upload_notification_title))
|
||||||
.setContentText(getString(R.string.upload_notification_message))
|
.setContentText(getString(R.string.upload_notification_message))
|
||||||
.setContentIntent(PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT))
|
.setContentIntent(PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT))
|
||||||
.setSmallIcon(R.drawable.ic_launcher)
|
.setSmallIcon(R.drawable.ic_file_upload_white_24dp)
|
||||||
.setProgress(100, 0, true)
|
.setProgress(100, 0, true)
|
||||||
.setOngoing(true);
|
.setOngoing(true);
|
||||||
|
|
||||||
startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
|
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.
|
* On upload complete.
|
||||||
*/
|
*/
|
||||||
@ -130,7 +120,7 @@ public class FileUploadService extends IntentService {
|
|||||||
|
|
||||||
notification.setContentTitle(getString(R.string.upload_notification_title))
|
notification.setContentTitle(getString(R.string.upload_notification_title))
|
||||||
.setContentText(getString(R.string.upload_notification_error))
|
.setContentText(getString(R.string.upload_notification_error))
|
||||||
.setSmallIcon(R.drawable.ic_launcher)
|
.setSmallIcon(R.drawable.ic_file_upload_white_24dp)
|
||||||
.setProgress(0, 0, false)
|
.setProgress(0, 0, false)
|
||||||
.setOngoing(false);
|
.setOngoing(false);
|
||||||
|
|
||||||
|
@ -85,9 +85,8 @@
|
|||||||
<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="document_deleting_message">Deleting document</string>
|
||||||
<string name="error_reading_file">Error while reading the file</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_title">Sismics Docs</string>
|
||||||
<string name="upload_notification_message">Uploading the new file to the document</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>
|
<string name="upload_notification_error">Error uploading the new file</string>
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user