Commit 71dc61ba authored by Alan McNatty's avatar Alan McNatty

Missed some files and fixed an introduced bug in the updated sync authority

parent 268396d3
......@@ -56,7 +56,7 @@ public class GlobalResources {
public static final String SYNC_CONTENT_URL = "nz.net.catalyst.MaharaDroid.Sync";
public static final String ARTEFACT_CONTENT_URL = "nz.net.catalyst.MaharaDroid.Artefact";
public static final String SYNC_AUTHORITY = "nz.net.catalyst.MaharaDroid";
public static final String SYNC_AUTHORITY = "nz.net.catalyst.MaharaDroid.Sync";
public static final String EXTRAS_SYNC_IS_PERIODIC = "nz.net.catalyst.MaharaDroid.periodic";
public static final String BROADCAST_ACTION = "nz.net.catalyst.MaharaDroid.UPLOAD_COMPLETED";
......
package nz.net.catalyst.MaharaDroid.authenticator;
import nz.net.catalyst.MaharaDroid.GlobalResources;
import nz.net.catalyst.MaharaDroid.LogConfig;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
public class AccountUtils {
static final String TAG = LogConfig.getLogTag(AccountUtils.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly
// for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
public static Account getAccount(Context context) {
AccountManager mAccountManager = AccountManager.get(context);
Account account = null;
// if ( periodic_sync != null && periodic_sync > 0 ) {
//
// TODO replicated from AuthenticatorActivity
Account[] mAccounts = mAccountManager.getAccountsByType(GlobalResources.ACCOUNT_TYPE);
if ( mAccounts.length > 0 ) {
// Just pick the first one .. support multiple accounts can come later.
account = mAccounts[0];
}
return account;
}
public static void deleteAccount(Context context) {
AccountManager mAccountManager = AccountManager.get(context);
Account[] mAccounts = mAccountManager.getAccountsByType(GlobalResources.ACCOUNT_TYPE);
for ( int i = 0; i < mAccounts.length; i++ ) {
mAccountManager.removeAccount(mAccounts[i], null, null);
}
}
}
......@@ -20,7 +20,6 @@ import nz.net.catalyst.MaharaDroid.authenticator.AuthenticatorActivity;
import nz.net.catalyst.MaharaDroid.data.SyncUtils;
import nz.net.catalyst.MaharaDroid.GlobalResources;
import nz.net.catalyst.MaharaDroid.LogConfig;
import nz.net.catalyst.MaharaDroid.Utils;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
......
/*
* MaharaDroid - Artefact uploader
*
* This file is part of MaharaDroid.
*
* Copyright [2010] [Catalyst IT Limited]
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nz.net.catalyst.MaharaDroid.data;
import nz.net.catalyst.MaharaDroid.GlobalResources;
import nz.net.catalyst.MaharaDroid.LogConfig;
import nz.net.catalyst.MaharaDroid.provider.ArtefactContentProvider;
import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.BaseColumns;
import android.util.Log;
/** Helper to the database, manages versions and creation */
public class ArtefactUtils {
static final String TAG = LogConfig.getLogTag(ArtefactUtils.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
public static final Uri URI = Uri.parse("content://" + GlobalResources.ARTEFACT_CONTENT_URL + "/" + ArtefactContentProvider.TABLE);
private static Artefact[] getArtefactsFromSelection(Context context, String selection, String[] selectionArgs) {
Artefact[] a = new Artefact[] {};
ContentProviderClient artefactContentProvider = context.getContentResolver().acquireContentProviderClient(URI);
Cursor cursor = null;
try {
cursor = artefactContentProvider.query(URI, null, selection, selectionArgs, null);
} catch (RemoteException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Failed to aquire content provider for query.");
e.printStackTrace();
}
if ( cursor == null )
return a;
try {
a = new Artefact[cursor.getCount()];
while (cursor.moveToNext()) {
a[cursor.getPosition()] = createArtefactFromCursor(cursor);
}
} finally {
cursor.close();
}
return a;
}
public static Artefact loadSavedArtefact(Context context, Long id) {
Artefact[] a = getArtefactsFromSelection(context, BaseColumns._ID + " = ?", new String[] { id.toString() });
if ( a != null && a.length > 0 )
return a[0];
return null;
}
public static int countSavedArtefacts(Context context) {
Artefact[] a = getArtefactsFromSelection(context, null, null);
if ( a != null )
return a.length;
return 0;
}
public static int uploadAllSavedArtefacts(Context context) {
Artefact[] a = getArtefactsFromSelection(context, null, null);
if ( a == null )
return 0;
for ( int i = 0 ; i < a.length; i++ ) {
a[i].upload(true, context);
}
return a.length;
}
public static Artefact[] loadSavedArtefacts(Context context) {
Artefact[] a = getArtefactsFromSelection(context, null, null);
// while (cursor.moveToNext()) {
// Artefact a = createArtefactFromCursor(cursor);
//
// // Only include artefacts with either no attached file or valid
// // files (may have been deleted in the background so we check)
// if (a.getFilename() == null
// || (a.getFilename() != null && a.getFilePath(mContext) != null)) {
// a_array[items++] = a;
// } else {
// Log.w(TAG, "File '" + a.getTitle() + "' does not exist on the device, deleting from saved artefacts");
// Toast.makeText(mContext, "File '" + a.getTitle() + "' does not exist on the device, deleting from saved artefacts", Toast.LENGTH_LONG);
// this.deleteSavedArtefact(a.getId());
// }
// }
return a;
}
private static Artefact createArtefactFromCursor(Cursor cursor) {
if ( cursor == null ) {
return null;
}
if ( VERBOSE ) Log.v(TAG, "createArtefactFromCursor draft: " + cursor.getInt(8));
if ( VERBOSE ) Log.v(TAG, "createArtefactFromCursor allow comments: " + cursor.getInt(9));
return new Artefact( cursor.getLong(0),
cursor.getLong(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getLong(6),
cursor.getString(7),
cursor.getInt(8)>0,
cursor.getInt(9)>0);
}
//---deletes a particular item---
public static int deleteSavedArtefact(Context context, Long id) {
ContentProviderClient artefactContentProvider = context.getContentResolver().acquireContentProviderClient(URI);
int deleted = 0;
try {
deleted = artefactContentProvider.delete(URI, BaseColumns._ID + " = ?", new String[] { id.toString() });
} catch (RemoteException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Failed to aquire content provider for query.");
e.printStackTrace();
}
if ( deleted > 0 )
context.getContentResolver().notifyChange(URI, null);
return deleted;
}
//---deletes all items---
public static int deleteAllSavedArtefacts(Context context) {
ContentProviderClient artefactContentProvider = context.getContentResolver().acquireContentProviderClient(URI);
int deleted = 0;
try {
deleted = artefactContentProvider.delete(URI, null, null);
} catch (RemoteException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Failed to aquire content provider for query.");
e.printStackTrace();
}
if ( deleted > 0 )
context.getContentResolver().notifyChange(URI, null);
return deleted;
}
public static Uri add(Context context, String filename, String title, String description, String tags, String journal_id, boolean is_draft, boolean allow_comments) {
ContentValues contentvalues = new ContentValues();
contentvalues.put(ArtefactContentProvider.FILENAME, filename);
contentvalues.put(ArtefactContentProvider.TITLE, title);
contentvalues.put(ArtefactContentProvider.DESCRIPTION, description);
contentvalues.put(ArtefactContentProvider.TAGS, tags);
contentvalues.put(ArtefactContentProvider.JOURNAL_ID, journal_id);
contentvalues.put(ArtefactContentProvider.IS_DRAFT, is_draft);
contentvalues.put(ArtefactContentProvider.ALLOW_COMMENTS, allow_comments);
return add(context, contentvalues);
}
public static Uri add(Context context, ContentValues cv) {
ContentProviderClient artefactContentProvider = context.getContentResolver().acquireContentProviderClient(URI);
try {
Uri u = artefactContentProvider.insert(URI, cv);
if ( u != null )
context.getContentResolver().notifyChange(u, null);
return u;
} catch (RemoteException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Failed to aquire content provider for query.");
e.printStackTrace();
}
return null;
}
public static int update(Context context, Long id, String filename, String title, String description, String tags, Long saved_id, String journal_id, boolean is_draft, boolean allow_comments) {
ContentValues contentvalues = new ContentValues();
contentvalues.put(ArtefactContentProvider.FILENAME, filename);
contentvalues.put(ArtefactContentProvider.TITLE, title);
contentvalues.put(ArtefactContentProvider.DESCRIPTION, description);
contentvalues.put(ArtefactContentProvider.TAGS, tags);
contentvalues.put(ArtefactContentProvider.SAVED_ID, saved_id);
contentvalues.put(ArtefactContentProvider.JOURNAL_ID, journal_id);
contentvalues.put(ArtefactContentProvider.IS_DRAFT, is_draft);
contentvalues.put(ArtefactContentProvider.ALLOW_COMMENTS, allow_comments);
return update(context, id, contentvalues);
}
public static int update(Context context, Long id, ContentValues cv) {
int updated = 0;
ContentProviderClient artefactContentProvider = context.getContentResolver().acquireContentProviderClient(URI);
try {
updated = artefactContentProvider.update(URI, cv, BaseColumns._ID + " = ?", new String[] { id.toString() });
} catch (RemoteException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Failed to aquire content provider for query.");
e.printStackTrace();
}
if ( updated > 0 )
context.getContentResolver().notifyChange(URI, null);
return updated;
}
}
\ No newline at end of file
This diff is collapsed.
/*
* MaharaDroid - Artefact uploader
*
* This file is part of MaharaDroid.
*
* Copyright [2010] [Catalyst IT Limited]
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nz.net.catalyst.MaharaDroid.provider;
import nz.net.catalyst.MaharaDroid.LogConfig;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;
/** Helper to the database, manages versions and creation */
public class ArtefactContentProvider extends ContentProvider {
static final String TAG = LogConfig.getLogTag(ArtefactContentProvider.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
private SQLiteDatabase sqlDB;
private DatabaseHelper dbHelper;
private static final String DATABASE_NAME = "maharadroid_artefact.db";
private static final int DATABASE_VERSION = 2;
private static Context mContext;
// Table name
public static final String TABLE = "artefacts";
// Columns
public static final String TIME = "time";
public static final String FILENAME = "filename";
public static final String URI = "uri";
public static final String TITLE = "title";
public static final String DESCRIPTION = "description";
public static final String TAGS = "tags";
public static final String SAVED_ID = "id";
public static final String JOURNAL_ID = "journal_id";
public static final String IS_DRAFT = "is_draft";
public static final String ALLOW_COMMENTS = "allow_comments";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + TABLE + "( " + BaseColumns._ID
+ " integer primary key autoincrement, " + TIME + " integer, "
+ FILENAME + " text, "
+ TITLE + " text not null, "
+ DESCRIPTION + " text, "
+ TAGS + " text, "
+ SAVED_ID + " integer, "
+ JOURNAL_ID + " text, "
+ IS_DRAFT + " boolean, "
+ ALLOW_COMMENTS + " boolean "
+ ");";
if ( DEBUG ) Log.d(TAG, "onCreate: " + sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
return;
String sql = null;
// Version 1 is the first version with SQL
if (oldVersion < 2) {
db.execSQL("DROP TABLE " + TABLE + "; ");
this.onCreate(db);
if ( DEBUG ) Log.d(TAG, "onUpgrade : " + sql);
}
}
}
public ArtefactContentProvider() {
super();
}
public ArtefactContentProvider(Context context) {
super();
this.dbHelper = new DatabaseHelper(context);
mContext = context;
}
@Override
public int delete(Uri uri, String s, String[] as) {
sqlDB = dbHelper.getWritableDatabase();
return sqlDB.delete(uri.getLastPathSegment(), s, as);
}
@Override
public String getType(Uri uri) {
return null;
}
public Uri insert(Uri uri, ContentValues contentvalues) {
if ( contentvalues == null )
return null;
// get database to insert records
sqlDB = dbHelper.getWritableDatabase();
// insert record in user table and get the row number of recently inserted record
long rowId = sqlDB.insert(uri.getLastPathSegment(), null, contentvalues);
if (rowId > 0) {
return Uri.withAppendedPath(uri, Long.toString(rowId));
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext());
return (dbHelper == null) ? false : true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = dbHelper.getReadableDatabase();
qb.setTables(uri.getLastPathSegment());
Cursor c = qb.query(db, projection, selection, selectionArgs, sortOrder, null,
sortOrder);
//c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues contentvalues, String selection,
String[] selectionArgs) {
if ( contentvalues == null )
return 0;
// get database to insert records
sqlDB = dbHelper.getWritableDatabase();
// insert record in user table and get the row number of recently inserted record
return sqlDB.update(uri.getLastPathSegment(), contentvalues, selection, selectionArgs);
}
}
\ No newline at end of file
package nz.net.catalyst.MaharaDroid.provider;
import nz.net.catalyst.MaharaDroid.GlobalResources;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
public class SyncContentProvider extends ContentProvider {
private SQLiteDatabase sqlDB;
private DatabaseHelper dbHelper;
private static final String DATABASE_NAME = "maharadroid_sync.db";
private static final int DATABASE_VERSION = 1;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create table to store user names
for ( int i = 0; i < GlobalResources.SYNC_CONTENT_TABLES.length; i++ ) {
db.execSQL("Create table "
+ GlobalResources.SYNC_CONTENT_TABLES[i]
+ "( _id INTEGER PRIMARY KEY AUTOINCREMENT, ID TEXT, VALUE TEXT);");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for ( int i = 0; i < GlobalResources.SYNC_CONTENT_TABLES.length; i++ ) {
db.execSQL("DROP TABLE IF EXISTS " + GlobalResources.SYNC_CONTENT_TABLES[i]);
}
onCreate(db);
}
}
public SyncContentProvider() {
super();
}
public SyncContentProvider(Context context) {
super();
this.dbHelper = new DatabaseHelper(context);
// SQLiteDatabase db = this.getReadableDatabase();
// try {
// db.execSQL("ALTER TABLE " + TABLE + " ADD COLUMN " + SAVED_ID + " integer;");
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
@Override
public int delete(Uri uri, String s, String[] as) {
sqlDB = dbHelper.getWritableDatabase();
return sqlDB.delete(uri.getLastPathSegment(), s, as);
}
@Override
public String getType(Uri uri) {
return null;
}
public Uri insert(Uri uri, ContentValues contentvalues) {
if ( contentvalues == null )
return null;
// get database to insert records
sqlDB = dbHelper.getWritableDatabase();
// insert record in user table and get the row number of recently inserted record
long rowId = sqlDB.insert(uri.getLastPathSegment(), "", contentvalues);
if (rowId > 0) {
return Uri.withAppendedPath(uri, Long.toString(rowId));
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext());
return (dbHelper == null) ? false : true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = dbHelper.getReadableDatabase();
qb.setTables(uri.getLastPathSegment());
Cursor c = qb.query(db, projection, selection, null, null, null,
sortOrder);
//c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues contentvalues, String s,
String[] as) {
return 0;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment