cached instant upload

This commit is contained in:
Bartek Przybylski 2012-06-03 19:59:14 +02:00
parent 5a382ca421
commit 2b00ff1c7e
6 changed files with 88 additions and 95 deletions

View file

@ -153,6 +153,9 @@
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>

View file

@ -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<OwnCloudSession> getSessionList() {
Cursor c = mDB
.query(TABLE_SESSIONS, null, null, null, null, null, null);
Vector<OwnCloudSession> v = new Vector<OwnCloudSession>();
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

View file

@ -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();
}
}

View file

@ -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, "

View file

@ -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);

View file

@ -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