From 8852fe52a9fd6ac82fca6190863f0cdce539b88f Mon Sep 17 00:00:00 2001 From: Saanteye <1269321486@qq.com> Date: Fri, 19 Apr 2024 17:50:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/micode/notes/gtask/data/SqlNote.java | 77 ++++++++++--------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/src/Notes/src/net/micode/notes/gtask/data/SqlNote.java b/src/Notes/src/net/micode/notes/gtask/data/SqlNote.java index 79a4095..752e515 100644 --- a/src/Notes/src/net/micode/notes/gtask/data/SqlNote.java +++ b/src/Notes/src/net/micode/notes/gtask/data/SqlNote.java @@ -1,19 +1,3 @@ -/* - * 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.gtask.data; import android.appwidget.AppWidgetManager; @@ -39,10 +23,14 @@ import java.util.ArrayList; public class SqlNote { + /* + * 功能描述:得到类的简写名称存入字符串TAG中 + * 实现过程:调用getSimpleName ()函数 + */ private static final String TAG = SqlNote.class.getSimpleName(); private static final int INVALID_ID = -99999; - + // 集合了interface NoteColumns中所有SF常量(17个) public static final String[] PROJECTION_NOTE = new String[] { NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE, @@ -51,7 +39,7 @@ public class SqlNote { NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID, NoteColumns.VERSION }; - + //以下设置17个列的编号 public static final int ID_COLUMN = 0; public static final int ALERTED_DATE_COLUMN = 1; @@ -86,6 +74,8 @@ public class SqlNote { public static final int VERSION_COLUMN = 16; + + //一下定义了17个内部的变量,其中12个可以由content中获得,5个需要初始化为0或者new private Context mContext; private ContentResolver mContentResolver; @@ -122,6 +112,7 @@ public class SqlNote { private ArrayList mDataList; + //构造函数只有context,对所有的变量进行初始化 public SqlNote(Context context) { mContext = context; mContentResolver = context.getContentResolver(); @@ -129,8 +120,10 @@ public class SqlNote { mId = INVALID_ID; mAlertDate = 0; mBgColorId = ResourceParser.getDefaultBgId(context); + //调用系统函数获得创建时间 mCreatedDate = System.currentTimeMillis(); mHasAttachment = 0; + //最后一次修改时间初始化为创建时间 mModifiedDate = System.currentTimeMillis(); mParentId = 0; mSnippet = ""; @@ -143,6 +136,7 @@ public class SqlNote { mDataList = new ArrayList(); } + //构造函数有context和一个数据库的cursor,多数变量通过cursor指向的一条记录直接进行初始化 public SqlNote(Context context, Cursor c) { mContext = context; mContentResolver = context.getContentResolver(); @@ -154,6 +148,7 @@ public class SqlNote { mDiffNoteValues = new ContentValues(); } + //构造函数 public SqlNote(Context context, long id) { mContext = context; mContentResolver = context.getContentResolver(); @@ -165,16 +160,18 @@ public class SqlNote { mDiffNoteValues = new ContentValues(); } - + //通过id从光标处加载数据 private void loadFromCursor(long id) { Cursor c = null; try { + //通过id获得对应的ContentResolver中的cursor c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)", new String[] { - String.valueOf(id) + String.valueOf(id) }, null); if (c != null) { c.moveToNext(); + //然后加载数据进行初始化 loadFromCursor(c); } else { Log.w(TAG, "loadFromCursor: cursor = null"); @@ -184,8 +181,9 @@ public class SqlNote { c.close(); } } - + //通过游标从光标处加载数据 private void loadFromCursor(Cursor c) { + //直接从一条记录中的获得以下变量的初始值 mId = c.getLong(ID_COLUMN); mAlertDate = c.getLong(ALERTED_DATE_COLUMN); mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); @@ -199,14 +197,14 @@ public class SqlNote { mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); mVersion = c.getLong(VERSION_COLUMN); } - + //通过content机制获取共享数据并加载到数据库当前游标处 private void loadDataContent() { Cursor c = null; mDataList.clear(); try { c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA, "(note_id=?)", new String[] { - String.valueOf(mId) + String.valueOf(mId) }, null); if (c != null) { if (c.getCount() == 0) { @@ -225,7 +223,7 @@ public class SqlNote { c.close(); } } - + //设置通过content机制用于共享的数据信息 public boolean setContent(JSONObject js) { try { JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); @@ -358,7 +356,7 @@ public class SqlNote { } return true; } - + //获取content机制提供的数据并加载到note中 public JSONObject getContent() { try { JSONObject js = new JSONObject(); @@ -369,7 +367,8 @@ public class SqlNote { } JSONObject note = new JSONObject(); - if (mType == Notes.TYPE_NOTE) { + if (mType == Notes.TYPE_NOTE) //类型为note时 + { note.put(NoteColumns.ID, mId); note.put(NoteColumns.ALERTED_DATE, mAlertDate); note.put(NoteColumns.BG_COLOR_ID, mBgColorId); @@ -406,40 +405,40 @@ public class SqlNote { } return null; } - + //给当前id设置父id public void setParentId(long id) { mParentId = id; mDiffNoteValues.put(NoteColumns.PARENT_ID, id); } - + //给当前id设置Gtaskid public void setGtaskId(String gid) { mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); } - + //给当前id设置同步id public void setSyncId(long syncId) { mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); } - + //初始化本地修改,即撤销所有当前修改 public void resetLocalModified() { mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); } - + //获得当前id public long getId() { return mId; } - + //获得当前id的父id public long getParentId() { return mParentId; } - + //获取小片段即用于显示的部分便签内容 public String getSnippet() { return mSnippet; } - + //判断是否为便签类型 public boolean isNoteType() { return mType == Notes.TYPE_NOTE; } - + //commit函数用于把当前造作所做的修改保存到数据库 public void commit(boolean validateVersion) { if (mIsCreate) { if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { @@ -459,6 +458,7 @@ public class SqlNote { if (mType == Notes.TYPE_NOTE) { for (SqlData sqlData : mDataList) { + //直接使用sqldata中的实现 sqlData.commit(mId, false, -1); } } @@ -471,13 +471,14 @@ public class SqlNote { mVersion ++; int result = 0; if (!validateVersion) { + //构造字符串 result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + NoteColumns.ID + "=?)", new String[] { - String.valueOf(mId) + String.valueOf(mId) }); } else { result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" - + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", + + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", new String[] { String.valueOf(mId), String.valueOf(mVersion) }); @@ -502,4 +503,4 @@ public class SqlNote { mDiffNoteValues.clear(); mIsCreate = false; } -} +} \ No newline at end of file -- 2.34.1