Compare commits

...

6 Commits

@ -26,9 +26,12 @@ import android.util.Log;
import java.util.HashMap;
public class Contact {
// 定义一个HashMap类型的静态变量sContactCache
private static HashMap<String, String> sContactCache;
// 定义一个字符串类型的静态变量TAG
private static final String TAG = "Contact";
// 定义一个字符串类型的静态变量CALLER_ID_SELECTION
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
+ " AND " + Data.RAW_CONTACT_ID + " IN "
@ -36,17 +39,23 @@ public class Contact {
+ " FROM phone_lookup"
+ " WHERE min_match = '+')";
public static String getContact(Context context, String phoneNumber) {
public static String getContact(Context context, String phoneNumber) {
// 判断sContactCache是否为空
if(sContactCache == null) {
// 初始化sContactCache
sContactCache = new HashMap<String, String>();
}
// 判断sContactCache中是否包含phoneNumber
if(sContactCache.containsKey(phoneNumber)) {
// 返回sContactCache中phoneNumber对应的value
return sContactCache.get(phoneNumber);
}
// 替换+号,生成查询语句
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
// 查询数据
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
new String [] { Phone.DISPLAY_NAME },
@ -54,10 +63,14 @@ public class Contact {
new String[] { phoneNumber },
null);
// 判断cursor是否为空
if (cursor != null && cursor.moveToFirst()) {
try {
// 获取查询结果
String name = cursor.getString(0);
// 将查询结果存入sContactCache中
sContactCache.put(phoneNumber, name);
// 返回查询结果
return name;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString());
@ -70,4 +83,4 @@ public class Contact {
return null;
}
}
}
}

@ -14,12 +14,16 @@
* limitations under the License.
*/
package net.micode.notes.data;
import android.net.Uri;
public class Notes {
// 定义数据库的权限
public static final String AUTHORITY = "micode_notes";
// 定义日志标签
public static final String TAG = "Notes";
// 定义笔记类型
public static final int TYPE_NOTE = 0;
public static final int TYPE_FOLDER = 1;
public static final int TYPE_SYSTEM = 2;
@ -30,11 +34,16 @@ public class Notes {
* {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder
* {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records
*/
// 定义根文件夹的ID
public static final int ID_ROOT_FOLDER = 0;
// 定义临时文件夹的ID
public static final int ID_TEMPARAY_FOLDER = -1;
// 定义通话记录文件夹的ID
public static final int ID_CALL_RECORD_FOLDER = -2;
// 定义垃圾文件夹的ID
public static final int ID_TRASH_FOLER = -3;
// 定义日志的额外信息
public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date";
public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id";
public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id";
@ -42,18 +51,22 @@ public class Notes {
public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id";
public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date";
// 定义小部件类型
public static final int TYPE_WIDGET_INVALIDE = -1;
public static final int TYPE_WIDGET_2X = 0;
public static final int TYPE_WIDGET_4X = 1;
public static class DataConstants {
// 定义一个NOTE变量值为TextNote.CONTENT_ITEM_TYPE
public static final String NOTE = TextNote.CONTENT_ITEM_TYPE;
// 定义一个CALL_NOTE变量值为CallNote.CONTENT_ITEM_TYPE
public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE;
}
/**
* Uri to query all notes and folders
*/
// 定义一个Uri对象用于存储note的路径
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");
/**
@ -61,6 +74,7 @@ public class Notes {
*/
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");
// 定义一个接口NoteColumns
public interface NoteColumns {
/**
* The unique ID for a row
@ -116,7 +130,6 @@ public class Notes {
* <P> Type: INTEGER (long) </P>
*/
public static final String BG_COLOR_ID = "bg_color_id";
/**
* For text note, it doesn't has attachment, for multi-media
* note, it has at least one attachment
@ -167,11 +180,13 @@ public class Notes {
public static final String VERSION = "version";
}
// 定义接口DataColumns
public interface DataColumns {
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
*/
// 定义一个静态变量ID值为"_id"
public static final String ID = "_id";
/**
@ -179,31 +194,31 @@ public class Notes {
* <P> Type: Text </P>
*/
public static final String MIME_TYPE = "mime_type";
// 定义一个静态变量ID值为"mime_type"
/**
* The reference id to note that this data belongs to
* <P> Type: INTEGER (long) </P>
*/
public static final String NOTE_ID = "note_id";
// 定义一个静态变量ID值为"note_id"
/**
* Created data for note or folder
* <P> Type: INTEGER (long) </P>
*/
public static final String CREATED_DATE = "created_date";
// 定义一个静态变量ID值为"created_date"
/**
* Latest modified date
* <P> Type: INTEGER (long) </P>
*/
public static final String MODIFIED_DATE = "modified_date";
// 定义一个静态变量ID值为"modified_date"
/**
* Data's content
* <P> Type: TEXT </P>
*/
public static final String CONTENT = "content";
// 定义一个静态变量ID值为"content"
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
@ -241,19 +256,25 @@ public class Notes {
public static final String DATA5 = "data5";
}
public static final class TextNote implements DataColumns {
/**
* Mode to indicate the text in check list mode or not
* <P> Type: Integer 1:check list mode 0: normal mode </P>
*/
// 定义一个静态变量MODE值为DATA1
public static final String MODE = DATA1;
// 定义一个静态变量MODE_CHECK_LIST值为1
public static final int MODE_CHECK_LIST = 1;
// 定义一个静态变量CONTENT_TYPE值为"vnd.android.cursor.dir/text_note"
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note";
// 定义一个静态变量CONTENT_ITEM_TYPE值为"vnd.android.cursor.item/text_note"
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note";
// 定义一个静态变量CONTENT_URI值为Uri.parse("content://" + AUTHORITY + "/text_note")
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note");
}
@ -268,12 +289,16 @@ public class Notes {
* Phone number for this record
* <P> Type: TEXT </P>
*/
// 定义一个静态常量,用于存储电话号码
public static final String PHONE_NUMBER = DATA3;
// 定义一个静态常量,用于存储内容类型
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note";
// 定义一个静态常量,用于存储内容项类型
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note";
// 定义一个静态常量用于存储内容URI
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note");
}
}

@ -28,10 +28,13 @@ import net.micode.notes.data.Notes.NoteColumns;
public class NotesDatabaseHelper extends SQLiteOpenHelper {
// 数据库名称
private static final String DB_NAME = "note.db";
// 数据库版本号
private static final int DB_VERSION = 4;
// 表名
public interface TABLE {
public static final String NOTE = "note";
@ -42,6 +45,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
private static NotesDatabaseHelper mInstance;
// 创建note表的sql语句
private static final String CREATE_NOTE_TABLE_SQL =
"CREATE TABLE " + TABLE.NOTE + "(" +
NoteColumns.ID + " INTEGER PRIMARY KEY," +
@ -63,7 +67,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" +
")";
private static final String CREATE_DATA_TABLE_SQL =
private static final String CREATE_DATA_TABLE_SQL =
"CREATE TABLE " + TABLE.DATA + "(" +
DataColumns.ID + " INTEGER PRIMARY KEY," +
DataColumns.MIME_TYPE + " TEXT NOT NULL," +
@ -106,7 +110,6 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
" END";
/**
* Increase folder's note count when insert new note to the folder
*/
@ -206,6 +209,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END";
// 创建NotesDatabaseHelper类用于创建数据库并创建note表触发器系统文件夹
public NotesDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@ -216,8 +220,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
createSystemFolder(db);
Log.d(TAG, "note table has been created");
}
private void reCreateNoteTableTriggers(SQLiteDatabase db) {
private void reCreateNoteTableTriggers(SQLiteDatabase db) {
// 删除已存在的触发器,如果它们存在
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");
@ -226,6 +230,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete");
db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash");
// 创建新的触发器,使用新的触发器创建触发器函数
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER);
@ -288,6 +293,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
}
static synchronized NotesDatabaseHelper getInstance(Context context) {
// 获取NotesDatabaseHelper实例如果mInstance为空则创建一个新的实例
if (mInstance == null) {
mInstance = new NotesDatabaseHelper(context);
}
@ -296,33 +302,40 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
// 创建note表和data表
createNoteTable(db);
createDataTable(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 判断是否需要重新创建触发器
boolean reCreateTriggers = false;
// 是否跳过v2升级
boolean skipV2 = false;
if (oldVersion == 1) {
// 升级到v2
upgradeToV2(db);
skipV2 = true; // this upgrade including the upgrade from v2 to v3
oldVersion++;
}
if (oldVersion == 2 && !skipV2) {
// 升级到v3
upgradeToV3(db);
reCreateTriggers = true;
oldVersion++;
}
if (oldVersion == 3) {
// 升级到v4
upgradeToV4(db);
oldVersion++;
}
if (reCreateTriggers) {
// 重新创建触发器
reCreateNoteTableTriggers(db);
reCreateDataTableTriggers(db);
}

@ -36,12 +36,16 @@ import net.micode.notes.data.NotesDatabaseHelper.TABLE;
public class NotesProvider extends ContentProvider {
// 创建一个UriMatcher对象用于匹配URI
private static final UriMatcher mMatcher;
// 创建一个NotesDatabaseHelper对象用于操作数据库
private NotesDatabaseHelper mHelper;
// 创建一个TAG用于标记日志
private static final String TAG = "NotesProvider";
// 创建一个URI码用于表示不同的URI
private static final int URI_NOTE = 1;
private static final int URI_NOTE_ITEM = 2;
private static final int URI_DATA = 3;
@ -50,6 +54,7 @@ public class NotesProvider extends ContentProvider {
private static final int URI_SEARCH = 5;
private static final int URI_SEARCH_SUGGEST = 6;
// 静态初始化创建一个UriMatcher对象用于匹配URI
static {
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
@ -81,6 +86,7 @@ public class NotesProvider extends ContentProvider {
@Override
public boolean onCreate() {
// 获取NotesDatabaseHelper实例
mHelper = NotesDatabaseHelper.getInstance(getContext());
return true;
}
@ -88,99 +94,136 @@ public class NotesProvider extends ContentProvider {
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
// 初始化游标
Cursor c = null;
// 获取可读的数据库
SQLiteDatabase db = mHelper.getReadableDatabase();
// 初始化id
String id = null;
// 根据uri匹配模式
switch (mMatcher.match(uri)) {
// 查询note表
case URI_NOTE:
// 查询note表并设置查询条件
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
sortOrder);
break;
// 查询note表中的某一行
case URI_NOTE_ITEM:
// 获取id
id = uri.getPathSegments().get(1);
// 查询note表并设置查询条件
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
break;
// 查询data表
case URI_DATA:
// 查询data表并设置查询条件
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
sortOrder);
break;
// 查询data表中的某一行
case URI_DATA_ITEM:
// 获取id
id = uri.getPathSegments().get(1);
// 查询data表并设置查询条件
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
break;
// 查询搜索建议
case URI_SEARCH:
case URI_SEARCH_SUGGEST:
// 检查排序,查询条件,查询参数是否有效
if (sortOrder != null || projection != null) {
throw new IllegalArgumentException(
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
}
// 初始化搜索字符串
String searchString = null;
// 根据uri匹配模式获取搜索字符串
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
// 如果uri中包含路径段则获取路径段
if (uri.getPathSegments().size() > 1) {
searchString = uri.getPathSegments().get(1);
}
} else {
// 从uri中获取查询参数
searchString = uri.getQueryParameter("pattern");
}
// 如果搜索字符串为空,则返回空游标
if (TextUtils.isEmpty(searchString)) {
return null;
}
try {
// 将搜索字符串格式化
searchString = String.format("%%%s%%", searchString);
// 查询note表并设置查询条件
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
new String[] { searchString });
} catch (IllegalStateException ex) {
Log.e(TAG, "got exception: " + ex.toString());
}
break;
// 未知uri
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
// 如果游标不为空则设置通知uri
if (c != null) {
c.setNotificationUri(getContext().getContentResolver(), uri);
}
// 返回游标
return c;
}
@Override
@Override
public Uri insert(Uri uri, ContentValues values) {
// 获取可写的数据库
SQLiteDatabase db = mHelper.getWritableDatabase();
// 定义变量
long dataId = 0, noteId = 0, insertedId = 0;
// 根据uri匹配
switch (mMatcher.match(uri)) {
// 匹配到note uri
case URI_NOTE:
// 插入note
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
break;
// 匹配到data uri
case URI_DATA:
// 判断note id是否存在
if (values.containsKey(DataColumns.NOTE_ID)) {
noteId = values.getAsLong(DataColumns.NOTE_ID);
} else {
Log.d(TAG, "Wrong data format without note id:" + values.toString());
}
// 插入data
insertedId = dataId = db.insert(TABLE.DATA, null, values);
break;
// 匹配到其他uri
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
// Notify the note uri
// 通知note uri
if (noteId > 0) {
getContext().getContentResolver().notifyChange(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
}
// Notify the data uri
// 通知data uri
if (dataId > 0) {
getContext().getContentResolver().notifyChange(
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
}
// 返回插入的id
return ContentUris.withAppendedId(uri, insertedId);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
@ -228,28 +271,34 @@ public class NotesProvider extends ContentProvider {
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase();
boolean updateData = false;
switch (mMatcher.match(uri)) {
case URI_NOTE:
// 增加版本号
increaseNoteVersion(-1, selection, selectionArgs);
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
break;
case URI_NOTE_ITEM:
// 获取id
id = uri.getPathSegments().get(1);
// 增加版本号
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs);
break;
case URI_DATA:
// 更新数据
count = db.update(TABLE.DATA, values, selection, selectionArgs);
updateData = true;
break;
case URI_DATA_ITEM:
// 获取id
id = uri.getPathSegments().get(1);
// 更新数据
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs);
updateData = true;
@ -260,18 +309,22 @@ public class NotesProvider extends ContentProvider {
if (count > 0) {
if (updateData) {
// 通知更新数据
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
}
// 通知更新笔记
getContext().getContentResolver().notifyChange(uri, null);
}
return count;
}
private String parseSelection(String selection) {
private String parseSelection(String selection) {
// 解析选择语句,返回一个字符串
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
}
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
// 增加笔记的版本
StringBuilder sql = new StringBuilder(120);
sql.append("UPDATE ");
sql.append(TABLE.NOTE);
@ -299,7 +352,7 @@ public class NotesProvider extends ContentProvider {
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
// 获取URI的类型
return null;
}
}

@ -1 +0,0 @@
111
Loading…
Cancel
Save