|
| 1 | +package com.catherine.materialdesignapp.content_providers; |
| 2 | + |
| 3 | +import android.content.ContentResolver; |
| 4 | +import android.content.ContentValues; |
| 5 | +import android.database.ContentObserver; |
| 6 | +import android.database.Cursor; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Handler; |
| 9 | +import android.provider.UserDictionary; |
| 10 | +import android.util.Log; |
| 11 | + |
| 12 | +import com.catherine.materialdesignapp.MyApplication; |
| 13 | +import com.catherine.materialdesignapp.models.Word; |
| 14 | + |
| 15 | +import java.util.LinkedList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class UserDictionaryDao { |
| 19 | + public final static String TAG = UserDictionaryDao.class.getSimpleName(); |
| 20 | + private final Uri database = Uri.parse("content://user_dictionary/words"); |
| 21 | + private ContentResolver contentResolver; |
| 22 | + |
| 23 | + public UserDictionaryDao() { |
| 24 | + contentResolver = MyApplication.INSTANCE.getApplicationContext().getContentResolver(); |
| 25 | +// contentResolver.registerContentObserver(database, true, new UserDictionaryObserver(new Handler())); |
| 26 | + } |
| 27 | + |
| 28 | + public List<Word> query() { |
| 29 | + String[] projection = {UserDictionary.Words.WORD, UserDictionary.Words.FREQUENCY, UserDictionary.Words.SHORTCUT, UserDictionary.Words.LOCALE}; |
| 30 | + Cursor cursor = contentResolver.query(database, projection, null, null, null); |
| 31 | + List<Word> words = new LinkedList<>(); |
| 32 | + if (cursor != null) { |
| 33 | + Log.d(TAG, "cursor counts: " + cursor.getCount()); |
| 34 | + while (cursor.moveToNext()) { |
| 35 | + Word word = new Word(); |
| 36 | + word.word = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD)); |
| 37 | + word.frequency = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY)); |
| 38 | + word.shortcut = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.SHORTCUT)); |
| 39 | + word.locale = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.SHORTCUT)); |
| 40 | + words.add(word); |
| 41 | + } |
| 42 | + cursor.close(); |
| 43 | + } |
| 44 | + return words; |
| 45 | + } |
| 46 | + |
| 47 | + public void insert(Word word) { |
| 48 | + ContentValues newValues = new ContentValues(); |
| 49 | + newValues.put(UserDictionary.Words.SHORTCUT, word.locale); |
| 50 | + newValues.put(UserDictionary.Words.LOCALE, word.locale); |
| 51 | + newValues.put(UserDictionary.Words.WORD, word.word); |
| 52 | + newValues.put(UserDictionary.Words.FREQUENCY, word.frequency); |
| 53 | + contentResolver.registerContentObserver(database, true, new UserDictionaryObserver(new Handler())); |
| 54 | + contentResolver.insert(database, newValues); |
| 55 | + } |
| 56 | + |
| 57 | + private class UserDictionaryObserver extends ContentObserver { |
| 58 | + UserDictionaryObserver(Handler handler) { |
| 59 | + super(handler); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onChange(boolean selfChange, Uri uri) { |
| 64 | + super.onChange(selfChange, uri); |
| 65 | + Log.d(TAG, "onChange:" + selfChange + "/uri:" + uri); |
| 66 | + contentResolver.unregisterContentObserver(this); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean deliverSelfNotifications() { |
| 71 | + return super.deliverSelfNotifications(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onChange(boolean selfChange) { |
| 76 | + super.onChange(selfChange); |
| 77 | + Log.d(TAG, "onChange:" + selfChange); |
| 78 | + contentResolver.unregisterContentObserver(this); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments