diff --git a/src/model/Note.java b/src/model/Note.java new file mode 100644 index 0000000..a0e070d --- /dev/null +++ b/src/model/Note.java @@ -0,0 +1,268 @@ +/* + * 单个便签项 +* 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.model;//包名 +import android.content.ContentProviderOperation;//批量的更新、插入、删除数据 +import android.content.ContentProviderResult;//操作结果 +import android.content.ContentUris;//添加或者修改uri后面的ID +import android.content.ContentValues;//存储基本数据类型的数据 +import android.content.Context;// 获取调用内容 +import android.content.OperationApplicationException;//操作数据容错 +import android.net.Uri;//Uri相关操作,标识唯一的资源 +import android.os.RemoteException;//异常处理 +import android.util.Log;//输出日志,比如说出错、警告等 + +import net.micode.notes.data.Notes;//小米便签一些属性的操作 +import net.micode.notes.data.Notes.CallNote;//调用操作 +import net.micode.notes.data.Notes.DataColumns;//数据栏 +import net.micode.notes.data.Notes.NoteColumns;//笔记栏 +import net.micode.notes.data.Notes.TextNote;//操作区 + +import java.util.ArrayList;//导入Java,util,ArrayList + + +public class Note {//定义note类,处理单个便签 + private ContentValues mNoteDiffValues;//声明一个ContentValues私有变量ContentValues,用来存储note与上次修改后的改动 + private NoteData mNoteData;//声明一个私有变量NoteData,用来记录单个便签的基本信息 + private static final String TAG = "Note";//设置软件标签 + /** + * Create a new note id for adding a new note to databases + */ + public static synchronized long getNewNoteId(Context context, long folderId) { + //Create a new note in the database + /* + * 作用:为新便签在数据库里边创建一个新的ID + * 实现:创建一个内容集合,存入创建便签的时间,最后修改的时间,再根据当前便签所处的上下文和文件夹的ID,从内容解析器中得到的路径uri里面 + * 获取新的便签的ID。最后判断ID是否合法,然后返回 + * 参数:@context:便签所处的上下文 + * @folderId:所处的便签文件夹ID + * @return:新创建便签的ID + */ + ContentValues values = new ContentValues(); + long createdTime = System.currentTimeMillis(); + values.put(NoteColumns.CREATED_DATE, createdTime); + values.put(NoteColumns.MODIFIED_DATE, createdTime); + values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); + values.put(NoteColumns.LOCAL_MODIFIED, 1); + values.put(NoteColumns.PARENT_ID, folderId); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); + + long noteId = 0; + try { + noteId = Long.valueOf(uri.getPathSegments().get(1)); + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e.toString()); + noteId = 0; + } + if (noteId == -1) { + throw new IllegalStateException("Wrong note id:" + noteId); + } + return noteId; + } + + public Note() {//定义两个变量用来存储便签的数据,一个是存储便签属性、一个是存储便签内容 + mNoteDiffValues = new ContentValues(); + mNoteData = new NoteData(); + } + + public void setNoteValue(String key, String value) { + /* + * 作用:设置便签的属性 + * 实现:通过传入key,指定修改便签的属性;传入value,指定修改的值,通过属性管理器mNoteDiffValues写入。最后,改变最后修改时间 + * 参数:@key:要修改的属性 + * @value:要修改的值 + */ + mNoteDiffValues.put(key, value); + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + + public void setTextData(String key, String value) {//设置数据库表格的标签文本内容的数据 + mNoteData.setTextData(key, value); + } + + public void setTextDataId(long id) {//设置文本数据的ID + mNoteData.setTextDataId(id); + } + + public long getTextDataId() {//获取文本数据的ID + return mNoteData.mTextDataId; + } + + public void setCallDataId(long id) {//设置电话号码数据的ID + mNoteData.setCallDataId(id); + } + + public void setCallData(String key, String value) {//设置电话号码的数据 + mNoteData.setCallData(key, value); + } + + public boolean isLocalModified() {//判断便签是否进行了本地修改 + return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); + } + + public boolean syncNote(Context context, long noteId) {//判断对修改过的便签是否进行同步 + if (noteId <= 0) { + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + if (!isLocalModified()) { + return true; + } + + /** + * In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and + * {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the + * note data info + */ + if (context.getContentResolver().update( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, + null) == 0) { + Log.e(TAG, "Update note error, should not happen"); + // Do not return, fall through + } + mNoteDiffValues.clear(); + + if (mNoteData.isLocalModified() + && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { + return false; + } + + return true; + } + + private class NoteData {//定义一个基本的便签内容的数据类,主要包含文本数据和电话号码数据 + private long mTextDataId;//文本数据id + + private ContentValues mTextDataValues;//文本数据内容 + + private long mCallDataId;//电话号码数据ID + + private ContentValues mCallDataValues;//电话号码数据内容 + + private static final String TAG = "NoteData";//默认构造函数 + + public NoteData() {//NoteData的构造函数,初始化四个变量 + mTextDataValues = new ContentValues(); + mCallDataValues = new ContentValues(); + mTextDataId = 0; + mCallDataId = 0; + } + + boolean isLocalModified() {//判断是否本地修改 + return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; + } + + void setTextDataId(long id) {//设置文本数据的ID + if(id <= 0) { + throw new IllegalArgumentException("Text data id should larger than 0"); + } + mTextDataId = id; + } + + void setCallDataId(long id) {//设置电话号码对应的id + if (id <= 0) { + throw new IllegalArgumentException("Call data id should larger than 0"); + } + mCallDataId = id; + } + + void setCallData(String key, String value) {//设置电话号码数据内容,并且保存修改时间 + mCallDataValues.put(key, value); + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + + void setTextData(String key, String value) {//设置文本数据 + mTextDataValues.put(key, value); + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + + Uri pushIntoContentResolver(Context context, long noteId) {//使用uri将数据添加到数据库 + /** + * Check for safety + */ + if (noteId <= 0) {//判断数据是否合法 + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + ArrayList operationList = new ArrayList(); + ContentProviderOperation.Builder builder = null; + + if(mTextDataValues.size() > 0) {//把文本数据存入DataColumns + mTextDataValues.put(DataColumns.NOTE_ID, noteId); + if (mTextDataId == 0) {//文本数据ID为零,意味着这个id是新建默认的id + mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mTextDataValues); + try { + setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); + } catch (NumberFormatException e) { + Log.e(TAG, "Insert new text data fail with noteId" + noteId); + mTextDataValues.clear(); + return null; + } + } else { + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mTextDataId)); + builder.withValues(mTextDataValues); + operationList.add(builder.build()); + } + mTextDataValues.clear(); + } + + if(mCallDataValues.size() > 0) {//电话号码同步 + mCallDataValues.put(DataColumns.NOTE_ID, noteId); + if (mCallDataId == 0) {//将电话号码的id设定为uri提供的id + mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mCallDataValues); + try { + setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); + } catch (NumberFormatException e) { + Log.e(TAG, "Insert new call data fail with noteId" + noteId); + mCallDataValues.clear(); + return null; + } + } else {//当电话号码不为新建时,更新电话号码ID + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mCallDataId)); + builder.withValues(mCallDataValues); + operationList.add(builder.build()); + } + mCallDataValues.clear(); + } + + if (operationList.size() > 0) {//存储过程中如果遇到异常,通过如下进行处理 + try {//抛出远程异常,并写回日志 + ContentProviderResult[] results = context.getContentResolver().applyBatch( + Notes.AUTHORITY, operationList); + return (results == null || results.length == 0 || results[0] == null) ? null + : ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); + } catch (RemoteException e) {//捕捉操作异常并写回日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + return null; + } catch (OperationApplicationException e) { + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));//异常日志 + return null; + } + } + return null; + } + } +} diff --git a/src/model/WorkingNote.java b/src/model/WorkingNote.java new file mode 100644 index 0000000..f78e8b0 --- /dev/null +++ b/src/model/WorkingNote.java @@ -0,0 +1,370 @@ +/*当前活动便签项 + * 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.model;//在model包中 + +import android.appwidget.AppWidgetManager;//需要调用的其他类 +import android.content.ContentUris; +import android.content.Context; +import android.database.Cursor; +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.CallNote; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.data.Notes.TextNote; +import net.micode.notes.tool.ResourceParser.NoteBgResources; + + +public class WorkingNote {/*声明WorkingNote类,创建小米便签的主要类, + 包括创建空便签,保存便签,加载小米便签内容,和设置小米便签的一些小部件之类的操作*/ + // Note for the working note + private Note mNote; + // Note Id + private long mNoteId; + // Note content + private String mContent; + // Note mode + private int mMode; + + private long mAlertDate; + + private long mModifiedDate; + + private int mBgColorId; + + private int mWidgetId; + + private int mWidgetType; + + private long mFolderId; + + private Context mContext; + + private static final String TAG = "WorkingNote"; + + private boolean mIsDeleted; + + private NoteSettingChangedListener mNoteSettingStatusListener; + + public static final String[] DATA_PROJECTION = new String[] {/*声明NOTE_PROJECTION字符串数组, + 用于存储数据信息。*/ + DataColumns.ID, + DataColumns.CONTENT, + DataColumns.MIME_TYPE, + DataColumns.DATA1, + DataColumns.DATA2, + DataColumns.DATA3, + DataColumns.DATA4, + }; + + public static final String[] NOTE_PROJECTION = new String[] {//保存便签属性信息的string数组 + NoteColumns.PARENT_ID, + NoteColumns.ALERTED_DATE, + NoteColumns.BG_COLOR_ID, + NoteColumns.WIDGET_ID, + NoteColumns.WIDGET_TYPE, + NoteColumns.MODIFIED_DATE + }; + + private static final int DATA_ID_COLUMN = 0;//以下定义的是上述两个列表中各元素的索引 + + private static final int DATA_CONTENT_COLUMN = 1; + + private static final int DATA_MIME_TYPE_COLUMN = 2; + + private static final int DATA_MODE_COLUMN = 3; + + private static final int NOTE_PARENT_ID_COLUMN = 0; + + private static final int NOTE_ALERTED_DATE_COLUMN = 1; + + private static final int NOTE_BG_COLOR_ID_COLUMN = 2; + + private static final int NOTE_WIDGET_ID_COLUMN = 3; + + private static final int NOTE_WIDGET_TYPE_COLUMN = 4; + + private static final int NOTE_MODIFIED_DATE_COLUMN = 5; + + // New note construct + private WorkingNote(Context context, long folderId) {//初始化WorkingNote类的内部数据变量 + mContext = context; + mAlertDate = 0; + mModifiedDate = System.currentTimeMillis();//获取系统当前时间的方法,返回当前时间 + mFolderId = folderId; + mNote = new Note();//加载一个已存在的便签 + mNoteId = 0; + mIsDeleted = false; + mMode = 0; + mWidgetType = Notes.TYPE_WIDGET_INVALIDE;//默认是不可见 + } + + // Existing note construct + private WorkingNote(Context context, long noteId, long folderId) {//载入便签 + mContext = context; + mNoteId = noteId; + mFolderId = folderId; + mIsDeleted = false; + mNote = new Note(); + loadNote(); + } + + private void loadNote() {//加载已有的便签 + Cursor cursor = mContext.getContentResolver().query( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, + null, null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); + mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); + mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); + mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); + mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); + mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); + } + cursor.close(); + } else { + Log.e(TAG, "No note with id:" + mNoteId); + throw new IllegalArgumentException("Unable to find note with id " + mNoteId); + } + loadNoteData(); + } + + private void loadNoteData() {//加载便签数据 + Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, + DataColumns.NOTE_ID + "=?", new String[] { + String.valueOf(mNoteId) + }, null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + String type = cursor.getString(DATA_MIME_TYPE_COLUMN); + if (DataConstants.NOTE.equals(type)) { + mContent = cursor.getString(DATA_CONTENT_COLUMN); + mMode = cursor.getInt(DATA_MODE_COLUMN); + mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); + } else if (DataConstants.CALL_NOTE.equals(type)) { + mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); + } else { + Log.d(TAG, "Wrong note type with type:" + type); + } + } while (cursor.moveToNext()); + } + cursor.close(); + } else { + Log.e(TAG, "No data with id:" + mNoteId); + throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); + } + } + + public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, + int widgetType, int defaultBgColorId) {//创建一个空便签 + WorkingNote note = new WorkingNote(context, folderId); + note.setBgColorId(defaultBgColorId); + note.setWidgetId(widgetId); + note.setWidgetType(widgetType); + return note; + } + + public static WorkingNote load(Context context, long id) {//加载已经创建的便签 + return new WorkingNote(context, id, 0); + } + + public synchronized boolean saveNote() {//保存便签,成功保存返回true,否则返回false + if (isWorthSaving()) { + if (!existInDatabase()) { + if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { + Log.e(TAG, "Create new note fail with id:" + mNoteId); + return false; + } + } + + mNote.syncNote(mContext, mNoteId); + + /** + * Update widget content if there exist any widget of this note + */ + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE + && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); + } + return true; + } else { + return false; + } + } + + public boolean existInDatabase() {//查看该note是否已经存放到数据库中 + return mNoteId > 0; + } + + private boolean isWorthSaving() {//判断是否需要保存 + if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) + || (existInDatabase() && !mNote.isLocalModified())) { + return false; + } else { + return true; + } + } + + public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {//设置状态改变的监听器 + mNoteSettingStatusListener = l; + } + + public void setAlertDate(long date, boolean set) {//设置提醒日期 + if (date != mAlertDate) { + mAlertDate = date; + mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); + } + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onClockAlertChanged(date, set); + } + } + + public void markDeleted(boolean mark) {//设置删除标志 + mIsDeleted = mark; + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); + } + } + + public void setBgColorId(int id) {//设定背景颜色 + if (id != mBgColorId) { + mBgColorId = id; + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onBackgroundColorChanged(); + } + mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); + } + } + + public void setCheckListMode(int mode) {//设置检查列表模式 + if (mMode != mode) { + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); + } + mMode = mode; + mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); + } + } + + public void setWidgetType(int type) {//设置窗口类型 + if (type != mWidgetType) { + mWidgetType = type; + mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); + } + } + + public void setWidgetId(int id) {//设置窗口编号 + if (id != mWidgetId) { + mWidgetId = id; + mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); + } + } + + public void setWorkingText(String text) {//写入文本内容 + if (!TextUtils.equals(mContent, text)) { + mContent = text; + mNote.setTextData(DataColumns.CONTENT, mContent); + } + } + + public void convertToCallNote(String phoneNumber, long callDate) {//转换成电话提醒的便签 + mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); + mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); + mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); + } + + public boolean hasClockAlert() {//检测是否有时钟提醒,mAlertDate > 0返回真,否则返回假 + return (mAlertDate > 0 ? true : false); + } + + public String getContent() {//获取便签内容 + return mContent; + } + + public long getAlertDate() {//获取提醒时间 + return mAlertDate; + } + + public long getModifiedDate() {//获取修改时间 + return mModifiedDate; + } + + public int getBgColorResId() {//获取背景颜色来源id + return NoteBgResources.getNoteBgResource(mBgColorId); + } + + public int getBgColorId() {//获取背景颜色id + return mBgColorId; + } + + public int getTitleBgResId() {//获取标题背景颜色资源的id + return NoteBgResources.getNoteTitleBgResource(mBgColorId); + } + + public int getCheckListMode() {//获取清单模式 + return mMode; + } + + public long getNoteId() {//获取便签id + return mNoteId; + } + + public long getFolderId() {//获取文件夹id + return mFolderId; + } + + public int getWidgetId() {//获取窗口id + return mWidgetId; + } + + public int getWidgetType() {//监听检测便签设置变化的接口 + return mWidgetType; + } + + public interface NoteSettingChangedListener {//监听便签设置是否改变 + /** + * Called when the background color of current note has just changed + */ + void onBackgroundColorChanged();//背景颜色改变 + + /** + * Called when user set clock + */ + void onClockAlertChanged(long date, boolean set);//提醒时间按钮,可进行时间的更改和提醒的开关 + + /** + * Call when user create note from widget + */ + void onWidgetChanged();//窗口改变 + + /** + * Call when switch between check list mode and normal mode + * @param oldMode is previous mode before change + * @param newMode is new mode + */ + void onCheckListModeChanged(int oldMode, int newMode);//便签检查列表模式改变 + } +} diff --git a/src/ui/NotesListAdapter.java b/src/ui/NotesListAdapter.java new file mode 100644 index 0000000..fea3146 --- /dev/null +++ b/src/ui/NotesListAdapter.java @@ -0,0 +1,184 @@ +/* + * 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.ui;//引入tools包 + +import android.content.Context;//导入各种类 +import android.database.Cursor; +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; + +import net.micode.notes.data.Notes; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; + + +public class NotesListAdapter extends CursorAdapter {//继承自CursorAdapter,它为cursor和ListView提供了连接的桥梁。因此,该类为cursor和便签编辑提供了沟通渠道 + private static final String TAG = "NotesListAdapter"; + private Context mContext; + private HashMap mSelectedIndex; + private int mNotesCount; + private boolean mChoiceMode; + + public static class AppWidgetAttribute {//表示桌面widget的属性,包括编号和类型 + public int widgetId; + public int widgetType; + }; + + public NotesListAdapter(Context context) {//初始化便签链接 + super(context, null); + mSelectedIndex = new HashMap(); + mContext = context; + mNotesCount = 0; + } + + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) {//利用NotesListLtem类创建新布局 + return new NotesListItem(context); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) {//将光标指向的数据与创建的视图捆绑起来 + if (view instanceof NotesListItem) { + NoteItemData itemData = new NoteItemData(context, cursor); + ((NotesListItem) view).bind(context, itemData, mChoiceMode, + isSelectedItem(cursor.getPosition())); + } + } + + public void setCheckedItem(final int position, final boolean checked) {//设置勾选框 + mSelectedIndex.put(position, checked); + notifyDataSetChanged(); + } + + public boolean isInChoiceMode() {//判断是否被勾选 + return mChoiceMode; + } + + public void setChoiceMode(boolean mode) {//重置勾选框 + mSelectedIndex.clear(); + mChoiceMode = mode; + } + + public void selectAll(boolean checked) {//设置全部勾选 + Cursor cursor = getCursor(); + for (int i = 0; i < getCount(); i++) { + if (cursor.moveToPosition(i)) { + if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { + setCheckedItem(i, checked); + } + } + } + } + + public HashSet getSelectedItemIds() {// 获取已经被勾选的项目的id号加入到散列表itemSet中 + HashSet itemSet = new HashSet(); + for (Integer position : mSelectedIndex.keySet()) { + if (mSelectedIndex.get(position) == true) { + Long id = getItemId(position); + if (id == Notes.ID_ROOT_FOLDER) { + Log.d(TAG, "Wrong item id, should not happen"); + } else { + itemSet.add(id); + } + } + } + + return itemSet; + } + + public HashSet getSelectedWidget() {//获取桌面组件选项表 + HashSet itemSet = new HashSet(); + for (Integer position : mSelectedIndex.keySet()) {//遍历被选中的列表 + if (mSelectedIndex.get(position) == true) { + Cursor c = (Cursor) getItem(position); + if (c != null) { + AppWidgetAttribute widget = new AppWidgetAttribute(); + NoteItemData item = new NoteItemData(mContext, c); + widget.widgetId = item.getWidgetId(); + widget.widgetType = item.getWidgetType(); + itemSet.add(widget); + /** + * Don't close cursor here, only the adapter could close it + */ + } else { + Log.e(TAG, "Invalid cursor");//设置标签,非法的cursor + return null; + } + } + } + return itemSet; + } + + public int getSelectedCount() {//获取选项个数 + Collection values = mSelectedIndex.values();//获取选项下标的值 + if (null == values) { + return 0; + } + Iterator iter = values.iterator();//初始化叠加器 + int count = 0; + while (iter.hasNext()) { + if (true == iter.next()) { + count++; + } + } + return count; + } + + public boolean isAllSelected() {//判断是否全选 + int checkedCount = getSelectedCount(); + return (checkedCount != 0 && checkedCount == mNotesCount); + } + + public boolean isSelectedItem(final int position) {//判断是否为选项表 + if (null == mSelectedIndex.get(position)) { + return false; + } + return mSelectedIndex.get(position); + } + + @Override + protected void onContentChanged() {//在activity发生变化时,重新计算便签数量 + super.onContentChanged(); + calcNotesCount(); + } + + @Override + public void changeCursor(Cursor cursor) {//光标变动时重新计算便签数量 + super.changeCursor(cursor); + calcNotesCount(); + } + + private void calcNotesCount() {//计算当前界面便签的数量 + mNotesCount = 0; + for (int i = 0; i < getCount(); i++) {//遍历所有数据 + Cursor c = (Cursor) getItem(i); + if (c != null) {//判断语句,如果光标不是null,那么便得到信息,便签数目加1 + if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {//若选项的数据类型为便签类型,那么计数+1 + mNotesCount++; + } + } else {//设置为无效的光标 + Log.e(TAG, "Invalid cursor"); + return; + } + } + } +}