From 2b00ff1c7eb24dffcd70ced6c33db8ecf90e3f57 Mon Sep 17 00:00:00 2001 From: Bartek Przybylski Date: Sun, 3 Jun 2012 19:59:14 +0200 Subject: [PATCH] cached instant upload --- AndroidManifest.xml | 3 + src/eu/alefzero/owncloud/db/DbHandler.java | 62 ++++++------------ .../files/PhotoTakenBroadcastReceiver.java | 65 ++++++++++++++++++- .../providers/FileContentProvider.java | 1 + .../ui/activity/FileDisplayActivity.java | 1 - .../owncloud/ui/activity/Preferences.java | 51 --------------- 6 files changed, 88 insertions(+), 95 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c6b774aa06..f591da8006 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -153,6 +153,9 @@ + + + diff --git a/src/eu/alefzero/owncloud/db/DbHandler.java b/src/eu/alefzero/owncloud/db/DbHandler.java index ab35dda239..23b8bb6eab 100644 --- a/src/eu/alefzero/owncloud/db/DbHandler.java +++ b/src/eu/alefzero/owncloud/db/DbHandler.java @@ -39,54 +39,33 @@ public class DbHandler { private final String mDatabaseName = "ownCloud"; private final String TABLE_SESSIONS = "sessions"; private final int mDatabaseVersion = 1; + + private final String TABLE_INSTANT_UPLOAD = "instant_upload"; public DbHandler(Context context) { mHelper = new OpenerHepler(context); mDB = mHelper.getWritableDatabase(); } - public Vector getSessionList() { - Cursor c = mDB - .query(TABLE_SESSIONS, null, null, null, null, null, null); - Vector v = new Vector(); - if (!c.moveToFirst()) { - return v; - } - while (!c.isAfterLast()) { - v.add(new OwnCloudSession(c.getString(c - .getColumnIndex("sessionName")), c.getString(c - .getColumnIndex("sessionUrl")), c.getInt(c - .getColumnIndex("_id")))); - c.moveToNext(); - } - c.close(); - return v; - } - - public void addSession(String sessionName, String uri) { - ContentValues cv = new ContentValues(); - cv.put("sessionName", sessionName); - cv.put("sessionUrl", uri); - mDB.insert(TABLE_SESSIONS, null, cv); - } - - public void removeSessionWithId(int sessionId) { - mDB.delete(TABLE_SESSIONS, "_id = ?", - new String[] { String.valueOf(sessionId) }); - } - - public void changeSessionFields(int id, String hostname, String uri) { - ContentValues cv = new ContentValues(); - cv.put("sessionName", hostname); - cv.put("sessionUrl", uri); - mDB.update(TABLE_SESSIONS, cv, "_id = ?", - new String[] { String.valueOf(id) }); - } - public void close() { mDB.close(); } + public boolean putFileForLater(String filepath, String account) { + ContentValues cv = new ContentValues(); + cv.put("path", filepath); + cv.put("account", account); + return mDB.insert(TABLE_INSTANT_UPLOAD, null, cv) != -1; + } + + public Cursor getAwaitingFiles() { + return mDB.query(TABLE_INSTANT_UPLOAD, null, null, null, null, null, null); + } + + public void clearFiles() { + mDB.delete(TABLE_INSTANT_UPLOAD, null, null); + } + private class OpenerHepler extends SQLiteOpenHelper { public OpenerHepler(Context context) { super(context, mDatabaseName, null, mDatabaseVersion); @@ -94,9 +73,10 @@ public class DbHandler { @Override public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE " + TABLE_SESSIONS + " (" - + " _id INTEGER PRIMARY KEY, " + " sessionName TEXT, " - + " sessionUrl TEXT);"); + db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " (" + + " _id INTEGET PRIMARY KEY, " + + " path TEXT," + + " account TEXT);"); } @Override diff --git a/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java b/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java index 758413f552..b6a0a60445 100644 --- a/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java +++ b/src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java @@ -18,18 +18,24 @@ package eu.alefzero.owncloud.files; +import java.io.File; + import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; +import eu.alefzero.owncloud.authenticator.AccountAuthenticator; +import eu.alefzero.owncloud.db.DbHandler; import eu.alefzero.owncloud.files.services.InstantUploadService; 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; +import android.webkit.MimeTypeMap; public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { @@ -44,10 +50,16 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { Log.d(TAG, "Instant upload disabled, abording uploading"); return; } - if (!intent.getAction().equals(NEW_PHOTO_ACTION)) { + if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) { + handleConnectivityAction(context, intent); + } else if (intent.getAction().equals(NEW_PHOTO_ACTION)) { + handleNewPhontoAction(context, intent); + } else { Log.e(TAG, "Incorrect intent sent: " + intent.getAction()); - return; } + } + + private void handleNewPhontoAction(Context context, Intent intent) { Account account = AccountUtils.getCurrentOwnCloudAccount(context); if (account == null) { Log.w(TAG, "No owncloud account found for instant upload, abording"); @@ -68,6 +80,13 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { c.close(); + if (!isOnline(context)) { + DbHandler db = new DbHandler(context); + db.putFileForLater(file_path, account.name); + 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); @@ -78,4 +97,46 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver { context.startService(upload_intent); } + private void handleConnectivityAction(Context context, Intent intent) { + if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) || + isOnline(context)) { + DbHandler db = new DbHandler(context); + Cursor c = db.getAwaitingFiles(); + if (c.moveToFirst()) { + do { + String account_name = c.getString(c.getColumnIndex("account")); + 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); + Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE); + + String mimeType = MimeTypeMap.getSingleton() + .getMimeTypeFromExtension( + f.getName().substring(f.getName().lastIndexOf('.') + 1)); + + 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()); + upload_intent.putExtra(InstantUploadService.KEY_FILE_SIZE, f.length()); + upload_intent.putExtra(InstantUploadService.KEY_MIME_TYPE, mimeType); + + context.startService(upload_intent); + } else { + Log.w(TAG, "Instant upload file " + f.getName() + " dont exist anymore"); + } + } while(c.moveToNext()); + c.close(); + } + db.clearFiles(); + db.close(); + } + + } + + private boolean isOnline(Context context) { + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected(); + } + } diff --git a/src/eu/alefzero/owncloud/providers/FileContentProvider.java b/src/eu/alefzero/owncloud/providers/FileContentProvider.java index 06338df7ac..b54aa21d80 100644 --- a/src/eu/alefzero/owncloud/providers/FileContentProvider.java +++ b/src/eu/alefzero/owncloud/providers/FileContentProvider.java @@ -199,6 +199,7 @@ public class FileContentProvider extends ContentProvider { @Override public void onCreate(SQLiteDatabase db) { + // files table db.execSQL("CREATE TABLE " + ProviderTableMeta.DB_NAME + "(" + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " + ProviderTableMeta.FILE_NAME + " TEXT, " diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index c0a84bbc34..5c02703583 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -191,7 +191,6 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements if (!remotepath.endsWith("/")) remotepath += "/"; remotepath += URLEncoder.encode(new File(filepath).getName()); - Log.e("ASD", remotepath + ""); i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath); i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath); diff --git a/src/eu/alefzero/owncloud/ui/activity/Preferences.java b/src/eu/alefzero/owncloud/ui/activity/Preferences.java index 01a21ec61a..7de322702d 100644 --- a/src/eu/alefzero/owncloud/ui/activity/Preferences.java +++ b/src/eu/alefzero/owncloud/ui/activity/Preferences.java @@ -17,20 +17,16 @@ */ package eu.alefzero.owncloud.ui.activity; -import java.net.URI; -import java.net.URISyntaxException; import java.util.Vector; import android.accounts.Account; import android.accounts.AccountManager; -import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; -import android.preference.PreferenceScreen; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; @@ -80,28 +76,6 @@ public class Preferences extends SherlockPreferenceActivity implements actionBar.setDisplayHomeAsUpEnabled(true); } - private void populateSessionList() { - mSessions.clear(); - mSessions = mDbHandler.getSessionList(); - PreferenceScreen ps = getPreferenceScreen(); - ps.removeAll(); - addPreferencesFromResource(R.xml.preferences); - for (int i = 0; i < mSessions.size(); i++) { - Preference preference = new Preference(getBaseContext()); - preference.setTitle(mSessions.get(i).getName()); - URI uri; - try { - uri = new URI(mSessions.get(i).getUrl()); - } catch (URISyntaxException e) { - e.printStackTrace(); // should never happen - continue; - } - preference.setSummary(uri.getScheme() + "://" + uri.getHost() - + uri.getPath()); - ps.addPreference(preference); - } - } - /** * Populates the account selector */ @@ -157,13 +131,6 @@ public class Preferences extends SherlockPreferenceActivity implements .getUrl()); startActivityForResult(intent, mEditSession); break; - case R.id.SessionContextRemove: - OwnCloudSession ocs = mSessions.get(mSelectedMenuItem); - mDbHandler.removeSessionWithId(ocs.getEntryId()); - mSessions.remove(ocs); - getPreferenceScreen().removePreference( - getPreferenceScreen().getPreference(mSelectedMenuItem + 1)); - break; case android.R.id.home: intent = new Intent(getBaseContext(), FileDisplayActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); @@ -179,24 +146,6 @@ public class Preferences extends SherlockPreferenceActivity implements @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - if (resultCode == Activity.RESULT_OK) { - switch (requestCode) { - case mNewSession: - mDbHandler.addSession(data.getStringExtra("sessionName"), - data.getStringExtra("sessionURL")); - getPreferenceScreen().removeAll(); - addPreferencesFromResource(R.xml.preferences); - populateSessionList(); - break; - case mEditSession: - mDbHandler.changeSessionFields( - data.getIntExtra("sessionId", -1), - data.getStringExtra("sessionName"), - data.getStringExtra("sessionURL")); - populateSessionList(); - break; - } - } } @Override