From: David A. Velasco Date: Wed, 22 Aug 2012 08:48:16 +0000 (+0200) Subject: More robust instant photo uploads X-Git-Tag: oc-android-1.4.3~200 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/ceb3dfdd059dd4f2aca8a784476fb60e610b78f6?ds=sidebyside More robust instant photo uploads --- diff --git a/src/com/owncloud/android/files/PhotoTakenBroadcastReceiver.java b/src/com/owncloud/android/files/PhotoTakenBroadcastReceiver.java index ec313b6a..8372e779 100644 --- a/src/com/owncloud/android/files/PhotoTakenBroadcastReceiver.java +++ b/src/com/owncloud/android/files/PhotoTakenBroadcastReceiver.java @@ -23,16 +23,14 @@ import java.io.File; import com.owncloud.android.AccountUtils; import com.owncloud.android.authenticator.AccountAuthenticator; import com.owncloud.android.db.DbHandler; -import com.owncloud.android.files.services.InstantUploadService; +import com.owncloud.android.files.services.FileUploader; -import com.owncloud.android.R; import android.accounts.Account; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.ConnectivityManager; -import android.preference.Preference; import android.preference.PreferenceManager; import android.provider.MediaStore.Images.Media; import android.util.Log; @@ -40,9 +38,9 @@ import android.webkit.MimeTypeMap; public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { + public static String INSTANT_UPLOAD_DIR = "/InstantUpload/"; private static String TAG = "PhotoTakenBroadcastReceiver"; private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE }; - private static String NEW_PHOTO_ACTION = "com.android.camera.NEW_PICTURE"; @Override @@ -54,16 +52,16 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) { handleConnectivityAction(context, intent); } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) { - handleNewPhontoAction(context, intent); + handleNewPhotoAction(context, intent); } else { Log.e(TAG, "Incorrect intent sent: " + intent.getAction()); } } - private void handleNewPhontoAction(Context context, Intent intent) { + private void handleNewPhotoAction(Context context, Intent intent) { Account account = AccountUtils.getCurrentOwnCloudAccount(context); if (account == null) { - Log.w(TAG, "No owncloud account found for instant upload, abording"); + Log.w(TAG, "No owncloud account found for instant upload, aborting"); return; } @@ -77,7 +75,7 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { String file_path = c.getString(c.getColumnIndex(Media.DATA)); String file_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME)); String mime_type = c.getString(c.getColumnIndex(Media.MIME_TYPE)); - long file_size = c.getLong(c.getColumnIndex(Media.SIZE)); + //long file_size = c.getLong(c.getColumnIndex(Media.SIZE)); c.close(); @@ -87,7 +85,8 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { db.close(); return; } - + + /* Intent upload_intent = new Intent(context, InstantUploadService.class); upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account); upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path); @@ -96,6 +95,17 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mime_type); context.startService(upload_intent); + */ + + Intent i = new Intent(context, FileUploader.class); + i.putExtra(FileUploader.KEY_ACCOUNT, account); + i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path); + i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + "/" + file_name); + i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE); + i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type); + i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true); + context.startService(i); + } private void handleConnectivityAction(Context context, Intent intent) { @@ -109,7 +119,7 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { String file_path = c.getString(c.getColumnIndex("path")); File f = new File(file_path); if (f.exists()) { - Intent upload_intent = new Intent(context, InstantUploadService.class); + //Intent upload_intent = new Intent(context, InstantUploadService.class); Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE); String mimeType = null; @@ -123,7 +133,8 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { } if (mimeType == null) mimeType = "application/octet-stream"; - + + /* upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account); upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path); upload_intent.putExtra(InstantUploadService.KEY_DISPLAY_NAME, f.getName()); @@ -131,6 +142,16 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mimeType); context.startService(upload_intent); + */ + + Intent i = new Intent(context, FileUploader.class); + i.putExtra(FileUploader.KEY_ACCOUNT, account); + i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path); + i.putExtra(FileUploader.KEY_REMOTE_FILE, INSTANT_UPLOAD_DIR + f.getName()); + i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE); + i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true); + context.startService(i); + } else { Log.w(TAG, "Instant upload file " + f.getName() + " dont exist anymore"); } diff --git a/src/com/owncloud/android/files/services/FileUploader.java b/src/com/owncloud/android/files/services/FileUploader.java index 488447a8..298c93b2 100644 --- a/src/com/owncloud/android/files/services/FileUploader.java +++ b/src/com/owncloud/android/files/services/FileUploader.java @@ -7,6 +7,8 @@ import java.util.Map; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.PhotoTakenBroadcastReceiver; + import eu.alefzero.webdav.OnDatatransferProgressListener; import com.owncloud.android.utils.OwnCloudClientUtils; @@ -41,6 +43,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe public static final String KEY_ACCOUNT = "ACCOUNT"; public static final String KEY_UPLOAD_TYPE = "UPLOAD_TYPE"; public static final String ACCOUNT_NAME = "ACCOUNT_NAME"; + public static final String KEY_MIME_TYPE = "MIME_TYPE"; + public static final String KEY_INSTANT_UPLOAD = "INSTANT_UPLOAD"; public static final int UPLOAD_SINGLE_FILE = 0; public static final int UPLOAD_MULTIPLE_FILES = 1; @@ -51,12 +55,13 @@ public class FileUploader extends Service implements OnDatatransferProgressListe private Looper mServiceLooper; private ServiceHandler mServiceHandler; private Account mAccount; - private String[] mLocalPaths, mRemotePaths; + private String[] mLocalPaths, mRemotePaths, mMimeTypes; private int mUploadType; private Notification mNotification; private long mTotalDataToSend, mSendData; private int mCurrentIndexUpload, mPreviousPercent; private int mSuccessCounter; + private boolean mIsInstant; /** * Static map with the files being download and the path to the temporal file were are download @@ -124,16 +129,21 @@ public class FileUploader extends Service implements OnDatatransferProgressListe mLocalPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) }; mRemotePaths = new String[] { intent .getStringExtra(KEY_REMOTE_FILE) }; + mMimeTypes = new String[] { intent.getStringExtra(KEY_MIME_TYPE) }; + } else { // mUploadType == UPLOAD_MULTIPLE_FILES mLocalPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE); mRemotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE); + mMimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE); } - + if (mLocalPaths.length != mRemotePaths.length) { Log.e(TAG, "Different number of remote paths and local paths!"); return Service.START_NOT_STICKY; } + mIsInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false); + Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg); @@ -166,6 +176,17 @@ public class FileUploader extends Service implements OnDatatransferProgressListe mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification); + /// create remote folder for instant uploads if necessary + if (mIsInstant) { + OCFile instantUploadDir = storageManager.getFileByPath(PhotoTakenBroadcastReceiver.INSTANT_UPLOAD_DIR); + if (instantUploadDir == null) { + wc.createDirectory(PhotoTakenBroadcastReceiver.INSTANT_UPLOAD_DIR); // fail could just mean that it already exists, but local database is not synchronized; the upload will be started anyway + OCFile newDir = new OCFile(PhotoTakenBroadcastReceiver.INSTANT_UPLOAD_DIR); + newDir.setMimetype("DIR"); + newDir.setParentId(storageManager.getFileByPath(OCFile.PATH_SEPARATOR).getFileId()); + storageManager.saveFile(newDir); + } + } /// perform the upload File [] localFiles = new File[mLocalPaths.length]; @@ -176,14 +197,16 @@ public class FileUploader extends Service implements OnDatatransferProgressListe Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mLocalPaths.length + " files"); mSuccessCounter = 0; for (int i = 0; i < mLocalPaths.length; ++i) { - String mimeType = null; - try { - mimeType = MimeTypeMap.getSingleton() + String mimeType = (mMimeTypes != null) ? mMimeTypes[i] : null; + if (mimeType == null) { + try { + mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension( mLocalPaths[i].substring(mLocalPaths[i] .lastIndexOf('.') + 1)); - } catch (IndexOutOfBoundsException e) { - Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]); + } } if (mimeType == null) mimeType = "application/octet-stream"; diff --git a/src/com/owncloud/android/files/services/InstantUploadService.java b/src/com/owncloud/android/files/services/InstantUploadService.java index a0b5b779..87430697 100644 --- a/src/com/owncloud/android/files/services/InstantUploadService.java +++ b/src/com/owncloud/android/files/services/InstantUploadService.java @@ -18,26 +18,17 @@ package com.owncloud.android.files.services; -import java.io.IOException; import java.util.HashMap; import java.util.LinkedList; import java.util.List; -import org.apache.commons.httpclient.HttpException; -import org.apache.jackrabbit.webdav.client.methods.MkColMethod; - -import com.owncloud.android.AccountUtils; -import com.owncloud.android.authenticator.AccountAuthenticator; import com.owncloud.android.utils.OwnCloudClientUtils; -import com.owncloud.android.utils.OwnCloudVersion; import eu.alefzero.webdav.WebdavClient; import android.accounts.Account; -import android.accounts.AccountManager; import android.app.Service; import android.content.Intent; -import android.net.Uri; import android.os.IBinder; import android.util.Log; @@ -136,9 +127,7 @@ public class InstantUploadService extends Service { WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(account, getApplicationContext()); - int status = 0; - wdc.createDirectory(INSTANT_UPLOAD_DIR); - Log.e(TAG, "mkcol returned " + status); + wdc.createDirectory(INSTANT_UPLOAD_DIR); // fail could just mean that it already exists; put will be tried anyway wdc.putFile(filepath, INSTANT_UPLOAD_DIR + "/" + filename, mimetype); } }