| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.database.ContentObserver; |
| 20 |
import android.database.Cursor; |
| 21 |
import android.os.Handler; |
| 22 |
|
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Observable; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class ContentQueryMap extends Observable { |
| 36 |
private volatile Cursor mCursor; |
| 37 |
private String[] mColumnNames; |
| 38 |
private int mKeyColumn; |
| 39 |
|
| 40 |
private Handler mHandlerForUpdateNotifications = null; |
| 41 |
private boolean mKeepUpdated = false; |
| 42 |
|
| 43 |
private Map<String, ContentValues> mValues = null; |
| 44 |
|
| 45 |
private ContentObserver mContentObserver; |
| 46 |
|
| 47 |
|
| 48 |
private boolean mDirty = false; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public ContentQueryMap(Cursor cursor, String columnNameOfKey, boolean keepUpdated, |
| 63 |
Handler handlerForUpdateNotifications) { |
| 64 |
mCursor = cursor; |
| 65 |
mColumnNames = mCursor.getColumnNames(); |
| 66 |
mKeyColumn = mCursor.getColumnIndexOrThrow(columnNameOfKey); |
| 67 |
mHandlerForUpdateNotifications = handlerForUpdateNotifications; |
| 68 |
setKeepUpdated(keepUpdated); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if (!keepUpdated) { |
| 74 |
readCursorIntoCache(cursor); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public void setKeepUpdated(boolean keepUpdated) { |
| 87 |
if (keepUpdated == mKeepUpdated) return; |
| 88 |
mKeepUpdated = keepUpdated; |
| 89 |
|
| 90 |
if (!mKeepUpdated) { |
| 91 |
mCursor.unregisterContentObserver(mContentObserver); |
| 92 |
mContentObserver = null; |
| 93 |
} else { |
| 94 |
if (mHandlerForUpdateNotifications == null) { |
| 95 |
mHandlerForUpdateNotifications = new Handler(); |
| 96 |
} |
| 97 |
if (mContentObserver == null) { |
| 98 |
mContentObserver = new ContentObserver(mHandlerForUpdateNotifications) { |
| 99 |
@Override |
| 100 |
public void onChange(boolean selfChange) { |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (countObservers() != 0) { |
| 105 |
requery(); |
| 106 |
} else { |
| 107 |
mDirty = true; |
| 108 |
} |
| 109 |
} |
| 110 |
}; |
| 111 |
} |
| 112 |
mCursor.registerContentObserver(mContentObserver); |
| 113 |
|
| 114 |
|
| 115 |
mDirty = true; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public synchronized ContentValues getValues(String rowName) { |
| 125 |
if (mDirty) requery(); |
| 126 |
return mValues.get(rowName); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
public void requery() { |
| 131 |
final Cursor cursor = mCursor; |
| 132 |
if (cursor == null) { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
return; |
| 137 |
} |
| 138 |
mDirty = false; |
| 139 |
if (!cursor.requery()) { |
| 140 |
|
| 141 |
return; |
| 142 |
} |
| 143 |
readCursorIntoCache(cursor); |
| 144 |
setChanged(); |
| 145 |
notifyObservers(); |
| 146 |
} |
| 147 |
|
| 148 |
private synchronized void readCursorIntoCache(Cursor cursor) { |
| 149 |
|
| 150 |
int capacity = mValues != null ? mValues.size() : 0; |
| 151 |
mValues = new HashMap<String, ContentValues>(capacity); |
| 152 |
while (cursor.moveToNext()) { |
| 153 |
ContentValues values = new ContentValues(); |
| 154 |
for (int i = 0; i < mColumnNames.length; i++) { |
| 155 |
if (i != mKeyColumn) { |
| 156 |
values.put(mColumnNames[i], cursor.getString(i)); |
| 157 |
} |
| 158 |
} |
| 159 |
mValues.put(cursor.getString(mKeyColumn), values); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public synchronized Map<String, ContentValues> getRows() { |
| 164 |
if (mDirty) requery(); |
| 165 |
return mValues; |
| 166 |
} |
| 167 |
|
| 168 |
public synchronized void close() { |
| 169 |
if (mContentObserver != null) { |
| 170 |
mCursor.unregisterContentObserver(mContentObserver); |
| 171 |
mContentObserver = null; |
| 172 |
} |
| 173 |
mCursor.close(); |
| 174 |
mCursor = null; |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
protected void finalize() throws Throwable { |
| 179 |
if (mCursor != null) close(); |
| 180 |
super.finalize(); |
| 181 |
} |
| 182 |
} |