2009-05-26 23:40:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.provider;
|
|
|
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
public abstract class EmailContent {
|
|
|
|
// Newly created objects get this id
|
2009-05-28 18:46:09 +00:00
|
|
|
private static final int NOT_SAVED = -1;
|
2009-05-26 23:40:34 +00:00
|
|
|
// The base Uri that this piece of content came from
|
2009-05-28 18:46:09 +00:00
|
|
|
public Uri mBaseUri;
|
2009-05-26 23:40:34 +00:00
|
|
|
// Lazily initialized uri for this Content
|
2009-05-28 18:46:09 +00:00
|
|
|
private Uri mUri = null;
|
2009-05-26 23:40:34 +00:00
|
|
|
// The id of the Content
|
2009-05-28 18:46:09 +00:00
|
|
|
public long mId = NOT_SAVED;
|
2009-05-26 23:40:34 +00:00
|
|
|
|
|
|
|
// Write the Content into a ContentValues container
|
|
|
|
public abstract ContentValues toContentValues();
|
|
|
|
// Read the Content from a ContentCursor
|
|
|
|
public abstract <T extends EmailContent> T restore (Cursor cursor);
|
|
|
|
|
|
|
|
// The Uri is lazily initialized
|
|
|
|
public Uri getUri() {
|
2009-05-28 18:46:09 +00:00
|
|
|
if (mUri == null)
|
|
|
|
mUri = ContentUris.withAppendedId(mBaseUri, mId);
|
|
|
|
return mUri;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSaved() {
|
2009-05-28 18:46:09 +00:00
|
|
|
return mId != NOT_SAVED;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
// The Content sub class must have a no-arg constructor
|
|
|
|
static public <T extends EmailContent> T getContent(Cursor cursor, Class<T> klass) {
|
|
|
|
try {
|
|
|
|
T content = klass.newInstance();
|
2009-05-28 18:46:09 +00:00
|
|
|
content.mId = cursor.getLong(0);
|
2009-05-26 23:40:34 +00:00
|
|
|
return (T)content.restore(cursor);
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience method that saves or updates some Content
|
|
|
|
public Uri saveOrUpdate(Context context) {
|
|
|
|
if (!isSaved())
|
2009-05-28 18:46:09 +00:00
|
|
|
return context.getContentResolver().insert(mBaseUri, toContentValues());
|
2009-05-26 23:40:34 +00:00
|
|
|
else {
|
|
|
|
if (update(context, toContentValues()) == 1)
|
|
|
|
return getUri();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Uri save(Context context) {
|
2009-05-28 18:46:09 +00:00
|
|
|
Uri res = context.getContentResolver().insert(mBaseUri, toContentValues());
|
|
|
|
mId = Long.parseLong(res.getPathSegments().get(1));
|
2009-05-26 23:40:34 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int update(Context context, ContentValues contentValues) {
|
|
|
|
return context.getContentResolver().update(getUri(), contentValues, null, null);
|
|
|
|
}
|
|
|
|
}
|