mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Add PduPersister and all of its dependencies
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2008 Esmertec AG.
|
||||
* 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.google.android.mms.util;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class AbstractCache<K, V> {
|
||||
private static final String TAG = "AbstractCache";
|
||||
private static final boolean DEBUG = false;
|
||||
private static final boolean LOCAL_LOGV = false;
|
||||
private static final int MAX_CACHED_ITEMS = 500;
|
||||
private final HashMap<K, CacheEntry<V>> mCacheMap;
|
||||
|
||||
protected AbstractCache() {
|
||||
mCacheMap = new HashMap<K, CacheEntry<V>>();
|
||||
}
|
||||
|
||||
public boolean put(K key, V value) {
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Trying to put " + key + " into cache.");
|
||||
}
|
||||
if (mCacheMap.size() >= MAX_CACHED_ITEMS) {
|
||||
// TODO Should remove the oldest or least hit cached entry
|
||||
// and then cache the new one.
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Failed! size limitation reached.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (key != null) {
|
||||
CacheEntry<V> cacheEntry = new CacheEntry<V>();
|
||||
cacheEntry.value = value;
|
||||
mCacheMap.put(key, cacheEntry);
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, key + " cached, " + mCacheMap.size() + " items total.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Trying to get " + key + " from cache.");
|
||||
}
|
||||
if (key != null) {
|
||||
CacheEntry<V> cacheEntry = mCacheMap.get(key);
|
||||
if (cacheEntry != null) {
|
||||
cacheEntry.hit++;
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, key + " hit " + cacheEntry.hit + " times.");
|
||||
}
|
||||
return cacheEntry.value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public V purge(K key) {
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Trying to purge " + key);
|
||||
}
|
||||
CacheEntry<V> v = mCacheMap.remove(key);
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, mCacheMap.size() + " items cached.");
|
||||
}
|
||||
return v != null ? v.value : null;
|
||||
}
|
||||
|
||||
public void purgeAll() {
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Purging cache, " + mCacheMap.size()
|
||||
+ " items dropped.");
|
||||
}
|
||||
mCacheMap.clear();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return mCacheMap.size();
|
||||
}
|
||||
|
||||
private static class CacheEntry<V> {
|
||||
int hit;
|
||||
V value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2012 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.google.android.mms.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.drm.DrmManagerClient;
|
||||
import android.util.Log;
|
||||
|
||||
public class DownloadDrmHelper {
|
||||
/**
|
||||
* The MIME type of special DRM files
|
||||
*/
|
||||
public static final String MIMETYPE_DRM_MESSAGE = "application/vnd.oma.drm.message";
|
||||
/**
|
||||
* The extensions of special DRM files
|
||||
*/
|
||||
public static final String EXTENSION_DRM_MESSAGE = ".dm";
|
||||
public static final String EXTENSION_INTERNAL_FWDL = ".fl";
|
||||
private static final String TAG = "DownloadDrmHelper";
|
||||
|
||||
/**
|
||||
* Checks if the Media Type is a DRM Media Type
|
||||
*
|
||||
* @param drmManagerClient A DrmManagerClient
|
||||
* @param mimetype Media Type to check
|
||||
* @return True if the Media Type is DRM else false
|
||||
*/
|
||||
public static boolean isDrmMimeType(Context context, String mimetype) {
|
||||
boolean result = false;
|
||||
if (context != null) {
|
||||
try {
|
||||
DrmManagerClient drmClient = new DrmManagerClient(context);
|
||||
if (drmClient != null && mimetype != null && mimetype.length() > 0) {
|
||||
result = drmClient.canHandle("", mimetype);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.w(TAG,
|
||||
"DrmManagerClient instance could not be created, context is Illegal.");
|
||||
} catch (IllegalStateException e) {
|
||||
Log.w(TAG, "DrmManagerClient didn't initialize properly.");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the Media Type needs to be DRM converted
|
||||
*
|
||||
* @param mimetype Media type of the content
|
||||
* @return True if convert is needed else false
|
||||
*/
|
||||
public static boolean isDrmConvertNeeded(String mimetype) {
|
||||
return MIMETYPE_DRM_MESSAGE.equals(mimetype);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the file extension for a DRM Forward Lock file NOTE: This
|
||||
* function shouldn't be called if the file shouldn't be DRM converted
|
||||
*/
|
||||
public static String modifyDrmFwLockFileExtension(String filename) {
|
||||
if (filename != null) {
|
||||
int extensionIndex;
|
||||
extensionIndex = filename.lastIndexOf(".");
|
||||
if (extensionIndex != -1) {
|
||||
filename = filename.substring(0, extensionIndex);
|
||||
}
|
||||
filename = filename.concat(EXTENSION_INTERNAL_FWDL);
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the original mime type of DRM protected content.
|
||||
*
|
||||
* @param context The context
|
||||
* @param path Path to the file
|
||||
* @param containingMime The current mime type of of the file i.e. the
|
||||
* containing mime type
|
||||
* @return The original mime type of the file if DRM protected else the
|
||||
* currentMime
|
||||
*/
|
||||
public static String getOriginalMimeType(Context context, String path, String containingMime) {
|
||||
String result = containingMime;
|
||||
DrmManagerClient drmClient = new DrmManagerClient(context);
|
||||
try {
|
||||
if (drmClient.canHandle(path, null)) {
|
||||
result = drmClient.getOriginalMimeType(path);
|
||||
}
|
||||
} catch (IllegalArgumentException ex) {
|
||||
Log.w(TAG,
|
||||
"Can't get original mime type since path is null or empty string.");
|
||||
} catch (IllegalStateException ex) {
|
||||
Log.w(TAG, "DrmManagerClient didn't initialize properly.");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright (C) 2012 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.google.android.mms.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.drm.DrmConvertedStatus;
|
||||
import android.drm.DrmManagerClient;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
public class DrmConvertSession {
|
||||
private static final String TAG = "DrmConvertSession";
|
||||
private DrmManagerClient mDrmClient;
|
||||
private int mConvertSessionId;
|
||||
|
||||
private DrmConvertSession(DrmManagerClient drmClient, int convertSessionId) {
|
||||
mDrmClient = drmClient;
|
||||
mConvertSessionId = convertSessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start of converting a file.
|
||||
*
|
||||
* @param context The context of the application running the convert session.
|
||||
* @param mimeType Mimetype of content that shall be converted.
|
||||
* @return A convert session or null in case an error occurs.
|
||||
*/
|
||||
public static DrmConvertSession open(Context context, String mimeType) {
|
||||
DrmManagerClient drmClient = null;
|
||||
int convertSessionId = -1;
|
||||
if (context != null && mimeType != null && !mimeType.equals("")) {
|
||||
try {
|
||||
drmClient = new DrmManagerClient(context);
|
||||
try {
|
||||
convertSessionId = drmClient.openConvertSession(mimeType);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.w(TAG, "Conversion of Mimetype: " + mimeType
|
||||
+ " is not supported.", e);
|
||||
} catch (IllegalStateException e) {
|
||||
Log.w(TAG, "Could not access Open DrmFramework.", e);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.w(TAG,
|
||||
"DrmManagerClient instance could not be created, context is Illegal.");
|
||||
} catch (IllegalStateException e) {
|
||||
Log.w(TAG, "DrmManagerClient didn't initialize properly.");
|
||||
}
|
||||
}
|
||||
if (drmClient == null || convertSessionId < 0) {
|
||||
return null;
|
||||
} else {
|
||||
return new DrmConvertSession(drmClient, convertSessionId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a buffer of data to protected format.
|
||||
*
|
||||
* @param buffer Buffer filled with data to convert.
|
||||
* @param size The number of bytes that shall be converted.
|
||||
* @return A Buffer filled with converted data, if execution is ok, in all
|
||||
* other case null.
|
||||
*/
|
||||
public byte[] convert(byte[] inBuffer, int size) {
|
||||
byte[] result = null;
|
||||
if (inBuffer != null) {
|
||||
DrmConvertedStatus convertedStatus = null;
|
||||
try {
|
||||
if (size != inBuffer.length) {
|
||||
byte[] buf = new byte[size];
|
||||
System.arraycopy(inBuffer, 0, buf, 0, size);
|
||||
convertedStatus = mDrmClient.convertData(mConvertSessionId, buf);
|
||||
} else {
|
||||
convertedStatus = mDrmClient.convertData(mConvertSessionId, inBuffer);
|
||||
}
|
||||
if (convertedStatus != null &&
|
||||
convertedStatus.statusCode == DrmConvertedStatus.STATUS_OK &&
|
||||
convertedStatus.convertedData != null) {
|
||||
result = convertedStatus.convertedData;
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.w(TAG, "Buffer with data to convert is illegal. Convertsession: "
|
||||
+ mConvertSessionId, e);
|
||||
} catch (IllegalStateException e) {
|
||||
Log.w(TAG, "Could not convert data. Convertsession: " +
|
||||
mConvertSessionId, e);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Parameter inBuffer is null");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends a conversion session of a file.
|
||||
*
|
||||
* @param fileName The filename of the converted file.
|
||||
* @return Downloads.Impl.STATUS_SUCCESS if execution is ok.
|
||||
* Downloads.Impl.STATUS_FILE_ERROR in case converted file can not
|
||||
* be accessed. Downloads.Impl.STATUS_NOT_ACCEPTABLE if a problem
|
||||
* occurs when accessing drm framework.
|
||||
* Downloads.Impl.STATUS_UNKNOWN_ERROR if a general error occurred.
|
||||
*/
|
||||
public int close(String filename) {
|
||||
DrmConvertedStatus convertedStatus = null;
|
||||
int result = /*Downloads.Impl.STATUS_UNKNOWN_ERROR*/491;
|
||||
if (mDrmClient != null && mConvertSessionId >= 0) {
|
||||
try {
|
||||
convertedStatus = mDrmClient.closeConvertSession(mConvertSessionId);
|
||||
if (convertedStatus == null ||
|
||||
convertedStatus.statusCode != DrmConvertedStatus.STATUS_OK ||
|
||||
convertedStatus.convertedData == null) {
|
||||
result = /*Downloads.Impl.STATUS_NOT_ACCEPTABLE*/406;
|
||||
} else {
|
||||
RandomAccessFile rndAccessFile = null;
|
||||
try {
|
||||
rndAccessFile = new RandomAccessFile(filename, "rw");
|
||||
rndAccessFile.seek(convertedStatus.offset);
|
||||
rndAccessFile.write(convertedStatus.convertedData);
|
||||
result = /*Downloads.Impl.STATUS_SUCCESS*/200;
|
||||
} catch (FileNotFoundException e) {
|
||||
result = /*Downloads.Impl.STATUS_FILE_ERROR*/492;
|
||||
Log.w(TAG, "File: " + filename + " could not be found.", e);
|
||||
} catch (IOException e) {
|
||||
result = /*Downloads.Impl.STATUS_FILE_ERROR*/492;
|
||||
Log.w(TAG, "Could not access File: " + filename + " .", e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
result = /*Downloads.Impl.STATUS_FILE_ERROR*/492;
|
||||
Log.w(TAG, "Could not open file in mode: rw", e);
|
||||
} catch (SecurityException e) {
|
||||
Log.w(TAG, "Access to File: " + filename +
|
||||
" was denied denied by SecurityManager.", e);
|
||||
} finally {
|
||||
if (rndAccessFile != null) {
|
||||
try {
|
||||
rndAccessFile.close();
|
||||
} catch (IOException e) {
|
||||
result = /*Downloads.Impl.STATUS_FILE_ERROR*/492;
|
||||
Log.w(TAG, "Failed to close File:" + filename
|
||||
+ ".", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalStateException e) {
|
||||
Log.w(TAG, "Could not close convertsession. Convertsession: " +
|
||||
mConvertSessionId, e);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* Copyright (C) 2008 Esmertec AG.
|
||||
* 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.google.android.mms.util;
|
||||
|
||||
import android.content.ContentUris;
|
||||
import android.content.UriMatcher;
|
||||
import android.net.Uri;
|
||||
import android.provider.Telephony.Mms;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
public final class PduCache extends AbstractCache<Uri, PduCacheEntry> {
|
||||
private static final String TAG = "PduCache";
|
||||
private static final boolean DEBUG = false;
|
||||
private static final boolean LOCAL_LOGV = false;
|
||||
private static final int MMS_ALL = 0;
|
||||
private static final int MMS_ALL_ID = 1;
|
||||
private static final int MMS_INBOX = 2;
|
||||
private static final int MMS_INBOX_ID = 3;
|
||||
private static final int MMS_SENT = 4;
|
||||
private static final int MMS_SENT_ID = 5;
|
||||
private static final int MMS_DRAFTS = 6;
|
||||
private static final int MMS_DRAFTS_ID = 7;
|
||||
private static final int MMS_OUTBOX = 8;
|
||||
private static final int MMS_OUTBOX_ID = 9;
|
||||
private static final int MMS_CONVERSATION = 10;
|
||||
private static final int MMS_CONVERSATION_ID = 11;
|
||||
private static final UriMatcher URI_MATCHER;
|
||||
private static final HashMap<Integer, Integer> MATCH_TO_MSGBOX_ID_MAP;
|
||||
private static PduCache sInstance;
|
||||
|
||||
static {
|
||||
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
|
||||
URI_MATCHER.addURI("mms", null, MMS_ALL);
|
||||
URI_MATCHER.addURI("mms", "#", MMS_ALL_ID);
|
||||
URI_MATCHER.addURI("mms", "inbox", MMS_INBOX);
|
||||
URI_MATCHER.addURI("mms", "inbox/#", MMS_INBOX_ID);
|
||||
URI_MATCHER.addURI("mms", "sent", MMS_SENT);
|
||||
URI_MATCHER.addURI("mms", "sent/#", MMS_SENT_ID);
|
||||
URI_MATCHER.addURI("mms", "drafts", MMS_DRAFTS);
|
||||
URI_MATCHER.addURI("mms", "drafts/#", MMS_DRAFTS_ID);
|
||||
URI_MATCHER.addURI("mms", "outbox", MMS_OUTBOX);
|
||||
URI_MATCHER.addURI("mms", "outbox/#", MMS_OUTBOX_ID);
|
||||
URI_MATCHER.addURI("mms-sms", "conversations", MMS_CONVERSATION);
|
||||
URI_MATCHER.addURI("mms-sms", "conversations/#", MMS_CONVERSATION_ID);
|
||||
MATCH_TO_MSGBOX_ID_MAP = new HashMap<Integer, Integer>();
|
||||
MATCH_TO_MSGBOX_ID_MAP.put(MMS_INBOX, Mms.MESSAGE_BOX_INBOX);
|
||||
MATCH_TO_MSGBOX_ID_MAP.put(MMS_SENT, Mms.MESSAGE_BOX_SENT);
|
||||
MATCH_TO_MSGBOX_ID_MAP.put(MMS_DRAFTS, Mms.MESSAGE_BOX_DRAFTS);
|
||||
MATCH_TO_MSGBOX_ID_MAP.put(MMS_OUTBOX, Mms.MESSAGE_BOX_OUTBOX);
|
||||
}
|
||||
|
||||
private final HashMap<Integer, HashSet<Uri>> mMessageBoxes;
|
||||
private final HashMap<Long, HashSet<Uri>> mThreads;
|
||||
private final HashSet<Uri> mUpdating;
|
||||
|
||||
private PduCache() {
|
||||
mMessageBoxes = new HashMap<Integer, HashSet<Uri>>();
|
||||
mThreads = new HashMap<Long, HashSet<Uri>>();
|
||||
mUpdating = new HashSet<Uri>();
|
||||
}
|
||||
|
||||
synchronized public static final PduCache getInstance() {
|
||||
if (sInstance == null) {
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Constructing new PduCache instance.");
|
||||
}
|
||||
sInstance = new PduCache();
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public boolean put(Uri uri, PduCacheEntry entry) {
|
||||
int msgBoxId = entry.getMessageBox();
|
||||
HashSet<Uri> msgBox = mMessageBoxes.get(msgBoxId);
|
||||
if (msgBox == null) {
|
||||
msgBox = new HashSet<Uri>();
|
||||
mMessageBoxes.put(msgBoxId, msgBox);
|
||||
}
|
||||
long threadId = entry.getThreadId();
|
||||
HashSet<Uri> thread = mThreads.get(threadId);
|
||||
if (thread == null) {
|
||||
thread = new HashSet<Uri>();
|
||||
mThreads.put(threadId, thread);
|
||||
}
|
||||
Uri finalKey = normalizeKey(uri);
|
||||
boolean result = super.put(finalKey, entry);
|
||||
if (result) {
|
||||
msgBox.add(finalKey);
|
||||
thread.add(finalKey);
|
||||
}
|
||||
setUpdating(uri, false);
|
||||
return result;
|
||||
}
|
||||
|
||||
synchronized public void setUpdating(Uri uri, boolean updating) {
|
||||
if (updating) {
|
||||
mUpdating.add(uri);
|
||||
} else {
|
||||
mUpdating.remove(uri);
|
||||
}
|
||||
}
|
||||
|
||||
synchronized public boolean isUpdating(Uri uri) {
|
||||
return mUpdating.contains(uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public PduCacheEntry purge(Uri uri) {
|
||||
int match = URI_MATCHER.match(uri);
|
||||
switch (match) {
|
||||
case MMS_ALL_ID:
|
||||
return purgeSingleEntry(uri);
|
||||
case MMS_INBOX_ID:
|
||||
case MMS_SENT_ID:
|
||||
case MMS_DRAFTS_ID:
|
||||
case MMS_OUTBOX_ID:
|
||||
String msgId = uri.getLastPathSegment();
|
||||
return purgeSingleEntry(Uri.withAppendedPath(Mms.CONTENT_URI, msgId));
|
||||
// Implicit batch of purge, return null.
|
||||
case MMS_ALL:
|
||||
case MMS_CONVERSATION:
|
||||
purgeAll();
|
||||
return null;
|
||||
case MMS_INBOX:
|
||||
case MMS_SENT:
|
||||
case MMS_DRAFTS:
|
||||
case MMS_OUTBOX:
|
||||
purgeByMessageBox(MATCH_TO_MSGBOX_ID_MAP.get(match));
|
||||
return null;
|
||||
case MMS_CONVERSATION_ID:
|
||||
purgeByThreadId(ContentUris.parseId(uri));
|
||||
return null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private PduCacheEntry purgeSingleEntry(Uri key) {
|
||||
mUpdating.remove(key);
|
||||
PduCacheEntry entry = super.purge(key);
|
||||
if (entry != null) {
|
||||
removeFromThreads(key, entry);
|
||||
removeFromMessageBoxes(key, entry);
|
||||
return entry;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public void purgeAll() {
|
||||
super.purgeAll();
|
||||
mMessageBoxes.clear();
|
||||
mThreads.clear();
|
||||
mUpdating.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uri The Uri to be normalized.
|
||||
* @return Uri The normalized key of cached entry.
|
||||
*/
|
||||
private Uri normalizeKey(Uri uri) {
|
||||
int match = URI_MATCHER.match(uri);
|
||||
Uri normalizedKey = null;
|
||||
switch (match) {
|
||||
case MMS_ALL_ID:
|
||||
normalizedKey = uri;
|
||||
break;
|
||||
case MMS_INBOX_ID:
|
||||
case MMS_SENT_ID:
|
||||
case MMS_DRAFTS_ID:
|
||||
case MMS_OUTBOX_ID:
|
||||
String msgId = uri.getLastPathSegment();
|
||||
normalizedKey = Uri.withAppendedPath(Mms.CONTENT_URI, msgId);
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, uri + " -> " + normalizedKey);
|
||||
}
|
||||
return normalizedKey;
|
||||
}
|
||||
|
||||
private void purgeByMessageBox(Integer msgBoxId) {
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Purge cache in message box: " + msgBoxId);
|
||||
}
|
||||
if (msgBoxId != null) {
|
||||
HashSet<Uri> msgBox = mMessageBoxes.remove(msgBoxId);
|
||||
if (msgBox != null) {
|
||||
for (Uri key : msgBox) {
|
||||
mUpdating.remove(key);
|
||||
PduCacheEntry entry = super.purge(key);
|
||||
if (entry != null) {
|
||||
removeFromThreads(key, entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeFromThreads(Uri key, PduCacheEntry entry) {
|
||||
HashSet<Uri> thread = mThreads.get(entry.getThreadId());
|
||||
if (thread != null) {
|
||||
thread.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void purgeByThreadId(long threadId) {
|
||||
if (LOCAL_LOGV) {
|
||||
Log.v(TAG, "Purge cache in thread: " + threadId);
|
||||
}
|
||||
HashSet<Uri> thread = mThreads.remove(threadId);
|
||||
if (thread != null) {
|
||||
for (Uri key : thread) {
|
||||
mUpdating.remove(key);
|
||||
PduCacheEntry entry = super.purge(key);
|
||||
if (entry != null) {
|
||||
removeFromMessageBoxes(key, entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeFromMessageBoxes(Uri key, PduCacheEntry entry) {
|
||||
HashSet<Uri> msgBox = mThreads.get(Long.valueOf(entry.getMessageBox()));
|
||||
if (msgBox != null) {
|
||||
msgBox.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2008 Esmertec AG.
|
||||
* 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.google.android.mms.util;
|
||||
|
||||
import com.google.android.mms.pdu.GenericPdu;
|
||||
|
||||
public final class PduCacheEntry {
|
||||
private final GenericPdu mPdu;
|
||||
private final int mMessageBox;
|
||||
private final long mThreadId;
|
||||
|
||||
public PduCacheEntry(GenericPdu pdu, int msgBox, long threadId) {
|
||||
mPdu = pdu;
|
||||
mMessageBox = msgBox;
|
||||
mThreadId = threadId;
|
||||
}
|
||||
|
||||
public GenericPdu getPdu() {
|
||||
return mPdu;
|
||||
}
|
||||
|
||||
public int getMessageBox() {
|
||||
return mMessageBox;
|
||||
}
|
||||
|
||||
public long getThreadId() {
|
||||
return mThreadId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (C) 2008 Esmertec AG.
|
||||
* 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.google.android.mms.util;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
public final class SqliteWrapper {
|
||||
private static final String TAG = "SqliteWrapper";
|
||||
private static final String SQLITE_EXCEPTION_DETAIL_MESSAGE
|
||||
= "unable to open database file";
|
||||
|
||||
private SqliteWrapper() {
|
||||
// Forbidden being instantiated.
|
||||
}
|
||||
|
||||
// FIXME: It looks like outInfo.lowMemory does not work well as we expected.
|
||||
// after run command: adb shell fillup -p 100, outInfo.lowMemory is still false.
|
||||
private static boolean isLowMemory(Context context) {
|
||||
if (null == context) {
|
||||
return false;
|
||||
}
|
||||
ActivityManager am = (ActivityManager)
|
||||
context.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
|
||||
am.getMemoryInfo(outInfo);
|
||||
return outInfo.lowMemory;
|
||||
}
|
||||
|
||||
// FIXME: need to optimize this method.
|
||||
private static boolean isLowMemory(SQLiteException e) {
|
||||
return e.getMessage().equals(SQLITE_EXCEPTION_DETAIL_MESSAGE);
|
||||
}
|
||||
|
||||
public static void checkSQLiteException(Context context, SQLiteException e) {
|
||||
if (isLowMemory(e)) {
|
||||
Toast.makeText(context, "Low memory",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static Cursor query(Context context, ContentResolver resolver, Uri uri,
|
||||
String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
||||
try {
|
||||
return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
|
||||
} catch (SQLiteException e) {
|
||||
Log.e(TAG, "Catch a SQLiteException when query: ", e);
|
||||
checkSQLiteException(context, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean requery(Context context, Cursor cursor) {
|
||||
try {
|
||||
return cursor.requery();
|
||||
} catch (SQLiteException e) {
|
||||
Log.e(TAG, "Catch a SQLiteException when requery: ", e);
|
||||
checkSQLiteException(context, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static int update(Context context, ContentResolver resolver, Uri uri,
|
||||
ContentValues values, String where, String[] selectionArgs) {
|
||||
try {
|
||||
return resolver.update(uri, values, where, selectionArgs);
|
||||
} catch (SQLiteException e) {
|
||||
Log.e(TAG, "Catch a SQLiteException when update: ", e);
|
||||
checkSQLiteException(context, e);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static int delete(Context context, ContentResolver resolver, Uri uri,
|
||||
String where, String[] selectionArgs) {
|
||||
try {
|
||||
return resolver.delete(uri, where, selectionArgs);
|
||||
} catch (SQLiteException e) {
|
||||
Log.e(TAG, "Catch a SQLiteException when delete: ", e);
|
||||
checkSQLiteException(context, e);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static Uri insert(Context context, ContentResolver resolver,
|
||||
Uri uri, ContentValues values) {
|
||||
try {
|
||||
return resolver.insert(uri, values);
|
||||
} catch (SQLiteException e) {
|
||||
Log.e(TAG, "Catch a SQLiteException when insert: ", e);
|
||||
checkSQLiteException(context, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user