From 31f0524e50cf980b3ced6225a5a09c1d98ba97ac Mon Sep 17 00:00:00 2001 From: YUhaoran <2784138374@qq.com> Date: Wed, 9 Oct 2024 22:51:21 +0800 Subject: [PATCH] 123 --- scr/Notes.java | 91 +++++++++++++++++++++++++ scr/NotesDatabaseHelper.java | 116 ++++++++++++++++++++++++++++++++ scr/NotesProvider.java | 127 +++++++++++++++++++++++++++++++++++ 3 files changed, 334 insertions(+) create mode 100644 scr/Notes.java create mode 100644 scr/NotesDatabaseHelper.java create mode 100644 scr/NotesProvider.java diff --git a/scr/Notes.java b/scr/Notes.java new file mode 100644 index 0000000..a05768a --- /dev/null +++ b/scr/Notes.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * 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 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; + + // 系统文件夹的ID + public static final int ID_ROOT_FOLDER = 0; + public static final int ID_TEMPARAY_FOLDER = -1; + public static final int ID_CALL_RECORD_FOLDER = -2; + public static final int ID_TRASH_FOLER = -3; + + // Intent额外数据的键名 + 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"; + public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; + 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"; + + // Widget类型 + 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 { + public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; + public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; + } + + // Uri查询所有笔记和文件夹 + public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); + + // Uri查询数据 + public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + + // NoteColumns接口定义了笔记或文件夹的属性 + public interface NoteColumns { + public static final String ID = "_id"; + public static final String PARENT_ID = "parent_id"; + public static final String CREATED_DATE = "created_date"; + public static final String MODIFIED_DATE = "modified_date"; + public static final String ALERTED_DATE = "_date"; + public static final String SNIPPET = "snippet"; + public static final String WIDGET_ID = "widget_id"; + public static final String WIDGET_TYPE = "widget_type"; + public static final String BG_COLOR_ID = "bg_color_id"; + public static final String HAS_ATTACHMENT = "has_attachment"; + public static final String NOTES_COUNT = "notes_count"; + public static final String TYPE = "type"; + public static final String SYNC_ID = "sync_id"; + public static final String LOCAL_MODIFIED = "local_modified"; + public static final String ORIGIN_PARENT_ID = "origin_parent_id"; + public static final String GTASK_ID = "gtask_id"; + public static final String VERSION = "version"; + } + + // DataColumns接口定义了笔记的数据属性 + public interface DataColumns { + public static final String ID = "_id"; + public static final String MIME_TYPE = "mime_type"; + public static final String NOTE_ID = "note_id"; + public static final String CREATED_DATE = "created_date"; + public static final String MODIFIED_DATE = "modified_date"; + public static final String CONTENT = "content"; + public static final String DATA1 = "data1"; + public static final String DATA2 = "data2"; + } +} diff --git a/scr/NotesDatabaseHelper.java b/scr/NotesDatabaseHelper.java new file mode 100644 index 0000000..634df40 --- /dev/null +++ b/scr/NotesDatabaseHelper.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * 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 net.micode.notes.data; + +import android.content.ContentValues; +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +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 { + String NOTE = "note"; + String DATA = "data"; + } + // 日志标签 + private static final String TAG = "NotesDatabaseHelper"; + // 单例实例 + private static NotesDatabaseHelper mInstance; + + // 创建笔记表的SQL语句 + private static final String CREATE_NOTE_TABLE_SQL = + "CREATE TABLE " + TABLE.NOTE + "(" + + NoteColumns.ID + " INTEGER PRIMARY KEY," + + NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + + NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + + NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + + ")"; + + // 创建数据表的SQL语句 + private static final String CREATE_DATA_TABLE_SQL = + "CREATE TABLE " + TABLE.DATA + "(" + + DataColumns.ID + " INTEGER PRIMARY KEY," + + DataColumns.MIME_TYPE + " TEXT NOT NULL," + + DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + + DataColumns.DATA1 + " INTEGER," + + DataColumns.DATA2 + " INTEGER," + + DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + + DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + + DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + + ")"; + + // 创建索引的SQL语句,用于提高查询效率 + private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = + "CREATE INDEX IF NOT EXISTS note_id_index ON " + + TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; + + // ...省略其他触发器的定义... + + // 构造函数私有化,实现单例模式 + private NotesDatabaseHelper(Context context) { + super(context, DB_NAME, null, DB_VERSION); + } + + // 获取单例实例的方法 + public static synchronized NotesDatabaseHelper getInstance(Context context) { + if (mInstance == null) { + mInstance = new NotesDatabaseHelper(context); + } + return mInstance; + } + + @Override + public void onCreate(SQLiteDatabase db) { + Log.i(TAG, "onCreate"); + db.execSQL(CREATE_NOTE_TABLE_SQL); + db.execSQL(CREATE_DATA_TABLE_SQL); + db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL); + // ...执行其他触发器的创建... + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + Log.i(TAG, "onUpgrade from version " + oldVersion + " to " + newVersion); + // ...根据需要处理数据库升级逻辑... + } +} diff --git a/scr/NotesProvider.java b/scr/NotesProvider.java new file mode 100644 index 0000000..c8081aa --- /dev/null +++ b/scr/NotesProvider.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * 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 net.micode.notes.data; + + +import android.app.SearchManager; +import android.content.ContentProvider; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Intent; +import android.content.UriMatcher; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.R; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.NoteColumns; +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,用于匹配不同的数据表或操作 + private static final int URI_NOTE = 1; + private static final int URI_NOTE_ITEM = 2; + private static final int URI_DATA = 3; + private static final int URI_DATA_ITEM = 4; + private static final int URI_SEARCH = 5; + private static final int URI_SEARCH_SUGGEST = 6; + + // 在静态代码块中初始化mMatcher,添加匹配规则 + static { + mMatcher = new UriMatcher(UriMatcher.NO_MATCH); + mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); + mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); + mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); + mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM); + mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH); + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST); + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST); + } + + // 定义一个常量字符串,用于搜索结果的投影 + private static final String NOTES_SEARCH_PROJECTION = ... //省略具体实现 + + // 定义一个常量字符串,用于搜索查询语句 + private static final String NOTES_SNIPPET_SEARCH_QUERY = ... //省略具体实现 + + @Override + public boolean onCreate() { + // 创建NotesDatabaseHelper实例并返回true + mHelper = NotesDatabaseHelper.getInstance(getContext()); + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + // 根据传入的URI进行匹配,执行相应的数据库查询操作 + Cursor c = null; + SQLiteDatabase db = mHelper.getReadableDatabase(); + String id = null; + switch (mMatcher.match(uri)) { + // ...省略其他case的处理逻辑... + } + // 如果Cursor不为空,设置通知URI并返回Cursor + if (c != null) { + c.setNotificationUri(getContext().getContentResolver(), uri); + } + return c; + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + // 根据传入的URI进行匹配,执行相应的数据库插入操作 + SQLiteDatabase db = mHelper.getWritableDatabase(); + long dataId = 0, noteId = 0, insertedId = 0; + switch (mMatcher.match(uri)) { + // ...省略其他case的处理逻辑... + } + // 如果有对应的noteId或dataId,通知相应的URI变化 + // ...省略通知逻辑... + // 返回新插入数据的URI + return ContentUris.withAppendedId(uri, insertedId); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + // 根据传入的URI进行匹配,执行相应的数据库删除操作 + int count = 0; + String id = null; + SQLiteDatabase db = mHelper.getWritableDatabase(); + boolean deleteData = false; + switch (mMatcher.match(uri)) { + // ...省略其他case的处理逻辑... + } + // 返回删除的数据条数 + return count; + } + + // ...省略其他方法的实现... +}