# HG changeset patch # User "Vincent Veronis" # Date 1305478296 -7200 # Node ID b9e58686cae36e7b7a2bb4d90c20f8cb0e18b9bc # Parent 37e794524cace733ccefd0d234c3fade84692862 add message provider diff -r 37e794524cac -r b9e58686cae3 src/com/beem/project/beem/providers/MessageProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/providers/MessageProvider.java Sun May 15 18:51:36 2011 +0200 @@ -0,0 +1,215 @@ +/* + BEEM is a videoconference application on the Android Platform. + + Copyright (C) 2009 by Frederic-Charles Barthelery, + Jean-Manuel Da Silva, + Nikita Kozlov, + Philippe Lago, + Jean Baptiste Vergely, + Vincent Veronis. + + This file is part of BEEM. + + BEEM is free software: you can redistribute it 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. + + BEEM 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 BEEM. If not, see . + + Please send bug reports with examples or suggestions to + contact@beem-project.com or http://dev.beem-project.com/ + + Epitech, hereby disclaims all copyright interest in the program "Beem" + written by Frederic-Charles Barthelery, + Jean-Manuel Da Silva, + Nikita Kozlov, + Philippe Lago, + Jean Baptiste Vergely, + Vincent Veronis. + + Nicolas Sadirac, November 26, 2009 + President of Epitech. + + Flavien Astraud, November 26, 2009 + Head of the EIP Laboratory. + + */ + +package com.beem.project.beem.providers; + +import java.util.HashMap; + +import android.content.ContentProvider; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.content.UriMatcher; +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; +import android.util.Log; + +/** + * Provider to store messages send/receive. + * @author marseille + */ +public class MessageProvider extends ContentProvider { + + private static final String TAG = "MessageProvider"; + private static final String DATABASE_NAME = "messages.db"; + private static final int DATABASE_VERSION = 1; + private static final int MESSAGES = 1; + private static final String MESSAGES_TABLE_NAME = "messages"; + private static final UriMatcher sUriMatcher; + + public static final String AUTHORITY = "com.beem.project.beem.providers.messageprovider"; + + private static HashMap messagesProjectionMap; + private DatabaseHelper dbHelper; + + /** + * Constructor. + */ + public MessageProvider() { + + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + SQLiteDatabase db = dbHelper.getWritableDatabase(); + int count; + switch (sUriMatcher.match(uri)) { + case MESSAGES: + count = db.delete(MESSAGES_TABLE_NAME, selection, selectionArgs); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + return count; + } + + @Override + public String getType(Uri uri) { + switch (sUriMatcher.match(uri)) { + case MESSAGES: + return Messages.CONTENT_TYPE; + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + } + + @Override + public Uri insert(Uri uri, ContentValues initialValues) { + if (sUriMatcher.match(uri) != MESSAGES) { + throw new IllegalArgumentException("Unknown URI " + uri); + } + + ContentValues values; + if (initialValues != null) { + values = new ContentValues(initialValues); + } else { + values = new ContentValues(); + } + + SQLiteDatabase db = dbHelper.getWritableDatabase(); + long rowId = db.insert(MESSAGES_TABLE_NAME, Messages.BODY, values); + if (rowId > 0) { + Uri messageUri = ContentUris.withAppendedId(Messages.CONTENT_URI, rowId); + getContext().getContentResolver().notifyChange(messageUri, null); + return messageUri; + } + throw new SQLException("Failed to insert row into " + uri); + } + + @Override + public boolean onCreate() { + dbHelper = new DatabaseHelper(getContext()); + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); + + switch (sUriMatcher.match(uri)) { + case MESSAGES: + qb.setTables(MESSAGES_TABLE_NAME); + qb.setProjectionMap(messagesProjectionMap); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + SQLiteDatabase db = dbHelper.getReadableDatabase(); + Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); + + c.setNotificationUri(getContext().getContentResolver(), uri); + return c; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + SQLiteDatabase db = dbHelper.getWritableDatabase(); + int count; + switch (sUriMatcher.match(uri)) { + case MESSAGES: + count = db.update(MESSAGES_TABLE_NAME, values, selection, selectionArgs); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + return count; + } + + private static class DatabaseHelper extends SQLiteOpenHelper { + + DatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("CREATE TABLE " + MESSAGES_TABLE_NAME + " (" + Messages._ID + + " INTEGER PRIMARY KEY AUTOINCREMENT," + Messages.FROM + " VARCHAR(255)," + Messages.MESSAGE_ID + + " VARCHAR(255)," + Messages.TO + " VARCHAR(255)," + Messages.BODY + " LONGTEXT" + ");"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + + ", which will destroy all old data"); + db.execSQL("DROP TABLE IF EXISTS " + MESSAGES_TABLE_NAME); + onCreate(db); + } + } + + static { + sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); + sUriMatcher.addURI(AUTHORITY, MESSAGES_TABLE_NAME, MESSAGES); + + messagesProjectionMap = new HashMap(); + messagesProjectionMap.put(Messages._ID, Messages._ID); + messagesProjectionMap.put(Messages.FROM, Messages.FROM); + messagesProjectionMap.put(Messages.MESSAGE_ID, Messages.MESSAGE_ID); + messagesProjectionMap.put(Messages.TO, Messages.TO); + messagesProjectionMap.put(Messages.BODY, Messages.BODY); + } + +} diff -r 37e794524cac -r b9e58686cae3 src/com/beem/project/beem/providers/Messages.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/providers/Messages.java Sun May 15 18:51:36 2011 +0200 @@ -0,0 +1,32 @@ +package com.beem.project.beem.providers; + +import android.net.Uri; +import android.provider.BaseColumns; + +public class Messages implements BaseColumns { + + /** + * Constructor. + */ + public Messages() { + + } + + public static final Uri CONTENT_URI = Uri.parse("content://" + MessageProvider.AUTHORITY + "/messages"); + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.com.beem.project.beem.provider.messages"; + + public static final String _ID = "_ID"; + public static final String FROM = "FROM"; + public static final String MESSAGE_ID = "MESSAGE_ID"; + public static final String TO = "TO"; + public static final String TYPE = "TYPE"; + public static final String SUBJECT = "SUBJECT"; + public static final String BODY = "BODY"; + public static final String THREAD = "THREAD"; + public static final String EXTRAS = "EXTRAS"; + public static final String IS_RECEIVE = "IS_RECEIVE"; + public static final String DATE_RECEIVE = "DATE_RECEIVE"; + public static final String DATE_READ = "DATE_READ"; + + +}