Compare commits
No commits in common. 'zb_branch' and 'main' have entirely different histories.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,249 @@
|
|||||||
|
/*
|
||||||
|
* 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;//表示待操作的数据
|
||||||
|
import android.os.RemoteException;//远程容错
|
||||||
|
import android.util.Log;//输出日志,比如说出错、警告等
|
||||||
|
|
||||||
|
public class Note {
|
||||||
|
// private ContentValues mNoteDiffValues;
|
||||||
|
ContentValues mNoteDiffValues;//
|
||||||
|
private NoteData mNoteData;
|
||||||
|
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
|
||||||
|
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);
|
||||||
|
//ContentResolver()主要是实现外部应用对ContentProvider中的数据
|
||||||
|
//进行添加、删除、修改和查询操作
|
||||||
|
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;
|
||||||
|
}//try-catch异常处理
|
||||||
|
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) {
|
||||||
|
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) {
|
||||||
|
mNoteData.setTextDataId(id);
|
||||||
|
}//设置文本数据的ID
|
||||||
|
|
||||||
|
public long getTextDataId() {
|
||||||
|
return mNoteData.mTextDataId;
|
||||||
|
}//得到文本数据的ID
|
||||||
|
|
||||||
|
public void setCallDataId(long id) {
|
||||||
|
mNoteData.setCallDataId(id);
|
||||||
|
}//设置电话号码数据的ID
|
||||||
|
|
||||||
|
public void setCallData(String key, String value) {
|
||||||
|
mNoteData.setCallData(key, value);
|
||||||
|
}//得到电话号码数据的ID
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
private ContentValues mTextDataValues;//文本数据
|
||||||
|
|
||||||
|
private long mCallDataId;
|
||||||
|
|
||||||
|
private ContentValues mCallDataValues;//电话号码数据
|
||||||
|
|
||||||
|
private static final String TAG = "NoteData";
|
||||||
|
|
||||||
|
public NoteData() {
|
||||||
|
mTextDataValues = new ContentValues();
|
||||||
|
mCallDataValues = new ContentValues();
|
||||||
|
mTextDataId = 0;
|
||||||
|
mCallDataId = 0;
|
||||||
|
}
|
||||||
|
//下面是上述几个函数的具体实现
|
||||||
|
boolean isLocalModified() {
|
||||||
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setTextDataId(long id) {
|
||||||
|
if(id <= 0) {
|
||||||
|
throw new IllegalArgumentException("Text data id should larger than 0");
|
||||||
|
}
|
||||||
|
mTextDataId = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setCallDataId(long 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的操作存储到数据库
|
||||||
|
Uri pushIntoContentResolver(Context context, long noteId) {
|
||||||
|
/**
|
||||||
|
* Check for safety
|
||||||
|
*/
|
||||||
|
if (noteId <= 0) {
|
||||||
|
throw new IllegalArgumentException("Wrong note id:" + noteId);
|
||||||
|
}//判断数据是否合法
|
||||||
|
|
||||||
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
||||||
|
ContentProviderOperation.Builder builder = null;//数据库的操作列表
|
||||||
|
|
||||||
|
if(mTextDataValues.size() > 0) {
|
||||||
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
|
||||||
|
if (mTextDataId == 0) {
|
||||||
|
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();
|
||||||
|
}//把文本数据存入DataColumns
|
||||||
|
|
||||||
|
if(mCallDataValues.size() > 0) {
|
||||||
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
||||||
|
if (mCallDataId == 0) {
|
||||||
|
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 {
|
||||||
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
||||||
|
Notes.CONTENT_DATA_URI, mCallDataId));
|
||||||
|
builder.withValues(mCallDataValues);
|
||||||
|
operationList.add(builder.build());
|
||||||
|
}
|
||||||
|
mCallDataValues.clear();
|
||||||
|
}//把电话号码数据存入DataColumns
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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.tool;//包内含有相关类和其他应用程序内部的类
|
||||||
|
|
||||||
|
import android.content.Context;//安卓开发的标准类库
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.os.Environment;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.text.format.DateFormat;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import net.micode.notes.R;//用于管理笔记数据和资源n
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.data.Notes.DataColumns;
|
||||||
|
import net.micode.notes.data.Notes.DataConstants;
|
||||||
|
import net.micode.notes.data.Notes.NoteColumns;
|
||||||
|
|
||||||
|
import java.io.File;//在JAVA中实现操作功能的类
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
//笔记备份工具类
|
||||||
|
public class BackupUtils {
|
||||||
|
private static final String TAG = "BackupUtils";
|
||||||
|
// Singleton stuff 单例模式
|
||||||
|
private static BackupUtils sInstance;
|
||||||
|
|
||||||
|
public static synchronized BackupUtils getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new BackupUtils(context);
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
// 备份和恢复功能
|
||||||
|
//定义备份或恢复数据时可能遇到的状态码。
|
||||||
|
|
||||||
|
public class BackupState {
|
||||||
|
|
||||||
|
//当前SD未挂载
|
||||||
|
|
||||||
|
public static final int STATE_SD_CARD_UNMOUNTED = 0;
|
||||||
|
|
||||||
|
//备份文件不存在
|
||||||
|
|
||||||
|
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
|
||||||
|
|
||||||
|
// 数据格式不正确,可能被其他程序更改
|
||||||
|
|
||||||
|
public static final int STATE_DATA_DESTROYED = 2;
|
||||||
|
|
||||||
|
//运行时异常导致恢复或备份失败
|
||||||
|
|
||||||
|
public static final int STATE_SYSTEM_ERROR = 3;
|
||||||
|
|
||||||
|
// 备份或恢复成功
|
||||||
|
|
||||||
|
public static final int STATE_SUCCESS = 4;
|
||||||
|
|
||||||
|
// 用于导出文件的目录
|
||||||
|
private TextExport mTextExport;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BackupUtils(Context context) {
|
||||||
|
mTextExport = new TextExport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean externalStorageAvailable() {
|
||||||
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int exportToText() {
|
||||||
|
return mTextExport.exportToText();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExportedTextFileName() {
|
||||||
|
return mTextExport.mFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExportedTextFileDir() {
|
||||||
|
return mTextExport.mFileDirectory;
|
||||||
|
}
|
||||||
|
// 笔记备份类
|
||||||
|
private static class TextExport {
|
||||||
|
private static final String[] NOTE_PROJECTION = {
|
||||||
|
NoteColumns.ID,
|
||||||
|
NoteColumns.MODIFIED_DATE,
|
||||||
|
NoteColumns.SNIPPET,
|
||||||
|
NoteColumns.TYPE
|
||||||
|
};
|
||||||
|
//定义Note数据表中各列对应的索引常量。
|
||||||
|
private static final int NOTE_COLUMN_ID = 0;
|
||||||
|
|
||||||
|
private static final int NOTE_COLUMN_MODIFIED_DATE = 1;
|
||||||
|
|
||||||
|
private static final int NOTE_COLUMN_SNIPPET = 2;
|
||||||
|
|
||||||
|
private static final String[] DATA_PROJECTION = {
|
||||||
|
DataColumns.CONTENT,
|
||||||
|
DataColumns.MIME_TYPE,
|
||||||
|
DataColumns.DATA1,
|
||||||
|
DataColumns.DATA2,
|
||||||
|
DataColumns.DATA3,
|
||||||
|
DataColumns.DATA4,
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final int DATA_COLUMN_CONTENT = 0;
|
||||||
|
|
||||||
|
private static final int DATA_COLUMN_MIME_TYPE = 1;
|
||||||
|
|
||||||
|
private static final int DATA_COLUMN_CALL_DATE = 2;
|
||||||
|
|
||||||
|
private static final int DATA_COLUMN_PHONE_NUMBER = 4;
|
||||||
|
|
||||||
|
private final String [] TEXT_FORMAT;
|
||||||
|
private static final int FORMAT_FOLDER_NAME = 0;
|
||||||
|
private static final int FORMAT_NOTE_DATE = 1;
|
||||||
|
private static final int FORMAT_NOTE_CONTENT = 2;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private String mFileName;
|
||||||
|
private String mFileDirectory;
|
||||||
|
// 备份和恢复功能
|
||||||
|
public TextExport(Context context) {
|
||||||
|
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
|
||||||
|
mContext = context;
|
||||||
|
mFileName = "";
|
||||||
|
mFileDirectory = "";
|
||||||
|
}
|
||||||
|
//同上
|
||||||
|
private String getFormat(int id) {
|
||||||
|
return TEXT_FORMAT[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出笔记到文本
|
||||||
|
private void exportFolderToText(String folderId, PrintStream ps) {
|
||||||
|
// Query notes belong to this folder
|
||||||
|
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
||||||
|
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
|
||||||
|
folderId
|
||||||
|
}, null);
|
||||||
|
// 查询笔记
|
||||||
|
if (notesCursor != null) {
|
||||||
|
if (notesCursor.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
// Print note's last modified date
|
||||||
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
||||||
|
mContext.getString(R.string.format_datetime_mdhm),
|
||||||
|
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
|
||||||
|
// Query data belong to this note
|
||||||
|
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
|
||||||
|
exportNoteToText(noteId, ps);
|
||||||
|
} while (notesCursor.moveToNext());
|
||||||
|
}
|
||||||
|
notesCursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 导出笔记到文本
|
||||||
|
private void exportNoteToText(String noteId, PrintStream ps) {
|
||||||
|
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
|
||||||
|
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
|
||||||
|
noteId
|
||||||
|
}, null);
|
||||||
|
// 查询笔记数据
|
||||||
|
if (dataCursor != null) {
|
||||||
|
if (dataCursor.moveToFirst()) {
|
||||||
|
do {// 开始遍历笔记数据
|
||||||
|
// 获取当前行的MIME类型
|
||||||
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
||||||
|
if (DataConstants.CALL_NOTE.equals(mimeType)) {
|
||||||
|
// 处理电话笔记:打印电话号码、通话日期和附件位置
|
||||||
|
// 获取并打印电话号码
|
||||||
|
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
|
||||||
|
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
|
||||||
|
String location = dataCursor.getString(DATA_COLUMN_CONTENT);
|
||||||
|
|
||||||
|
if (!TextUtils.isEmpty(phoneNumber)) {
|
||||||
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
||||||
|
phoneNumber));
|
||||||
|
}
|
||||||
|
// 获取并打印通话日期
|
||||||
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
|
||||||
|
.format(mContext.getString(R.string.format_datetime_mdhm),
|
||||||
|
callDate)));
|
||||||
|
// 获取并打印附件位置(如果存在)
|
||||||
|
if (!TextUtils.isEmpty(location)) {
|
||||||
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
||||||
|
location));
|
||||||
|
}
|
||||||
|
} else if (DataConstants.NOTE.equals(mimeType)) {
|
||||||
|
// 处理普通笔记:打印笔记内容(如果非空)
|
||||||
|
String content = dataCursor.getString(DATA_COLUMN_CONTENT);
|
||||||
|
if (!TextUtils.isEmpty(content)) {
|
||||||
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
||||||
|
content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (dataCursor.moveToNext());
|
||||||
|
}
|
||||||
|
// 关闭数据游标
|
||||||
|
dataCursor.close();
|
||||||
|
}
|
||||||
|
// print a line separator between note
|
||||||
|
|
||||||
|
try {
|
||||||
|
ps.write(new byte[] {
|
||||||
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
||||||
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 导出笔记到文本
|
||||||
|
public int exportToText() {
|
||||||
|
if (!externalStorageAvailable()) {
|
||||||
|
Log.d(TAG, "Media was not mounted");
|
||||||
|
return STATE_SD_CARD_UNMOUONTED;
|
||||||
|
}
|
||||||
|
// 错误输出
|
||||||
|
PrintStream ps = getExportToTextPrintStream();
|
||||||
|
if (ps == null) {
|
||||||
|
Log.e(TAG, "get print stream error");
|
||||||
|
return STATE_SYSTEM_ERROR;
|
||||||
|
}
|
||||||
|
// First export folder and its notes
|
||||||
|
|
||||||
|
// 查询文件夹
|
||||||
|
Cursor folderCursor = mContext.getContentResolver().query(
|
||||||
|
Notes.CONTENT_NOTE_URI,
|
||||||
|
NOTE_PROJECTION,
|
||||||
|
"(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND "
|
||||||
|
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
|
||||||
|
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null);
|
||||||
|
// 遍历文件夹
|
||||||
|
if (folderCursor != null) {
|
||||||
|
if (folderCursor.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
// 获取并处理当前文件夹的名称
|
||||||
|
String folderName = "";
|
||||||
|
// 判断当前文件夹是否为通话记录文件夹,据此设置不同的文件夹名称
|
||||||
|
if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) {
|
||||||
|
folderName = mContext.getString(R.string.call_record_folder_name);
|
||||||
|
} else {
|
||||||
|
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
|
||||||
|
}
|
||||||
|
// 如果文件夹名称不为空,则打印到文本
|
||||||
|
if (!TextUtils.isEmpty(folderName)) {
|
||||||
|
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
|
||||||
|
}
|
||||||
|
// 获取并导出当前文件夹的详细信息到文本
|
||||||
|
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
|
||||||
|
exportFolderToText(folderId, ps);
|
||||||
|
} while (folderCursor.moveToNext());
|
||||||
|
}
|
||||||
|
// 关闭文件夹游标
|
||||||
|
folderCursor.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export trash folder
|
||||||
|
|
||||||
|
// Export notes in root's folder
|
||||||
|
Cursor noteCursor = mContext.getContentResolver().query(
|
||||||
|
Notes.CONTENT_NOTE_URI,
|
||||||
|
NOTE_PROJECTION,
|
||||||
|
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
|
||||||
|
+ "=0", null, null);
|
||||||
|
// 遍历笔记
|
||||||
|
if (noteCursor != null) {
|
||||||
|
if (noteCursor.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
||||||
|
mContext.getString(R.string.format_datetime_mdhm),
|
||||||
|
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
|
||||||
|
// Query data belong to this note
|
||||||
|
String noteId = noteCursor.getString(NOTE_COLUMN_ID);
|
||||||
|
exportNoteToText(noteId, ps);
|
||||||
|
} while (noteCursor.moveToNext());
|
||||||
|
}
|
||||||
|
noteCursor.close();
|
||||||
|
}
|
||||||
|
ps.close();
|
||||||
|
|
||||||
|
return STATE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a print stream pointed to the file {@generateExportedTextFile}
|
||||||
|
*/
|
||||||
|
// 获取打印流
|
||||||
|
private PrintStream getExportToTextPrintStream() {
|
||||||
|
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
|
||||||
|
R.string.file_name_txt_format);
|
||||||
|
if (file == null) {
|
||||||
|
Log.e(TAG, "create file to exported failed");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
mFileName = file.getName();
|
||||||
|
mFileDirectory = mContext.getString(R.string.file_path);
|
||||||
|
|
||||||
|
PrintStream ps = null;
|
||||||
|
// 创建文件输出流
|
||||||
|
try {
|
||||||
|
FileOutputStream fos = new FileOutputStream(file);
|
||||||
|
ps = new PrintStream(fos);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the text file to store imported data
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 生成文件
|
||||||
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(Environment.getExternalStorageDirectory());
|
||||||
|
sb.append(context.getString(filePathResId));
|
||||||
|
File filedir = new File(sb.toString());
|
||||||
|
sb.append(context.getString(
|
||||||
|
fileNameFormatResId,
|
||||||
|
DateFormat.format(context.getString(R.string.format_date_ymd),
|
||||||
|
System.currentTimeMillis())));
|
||||||
|
File file = new File(sb.toString());
|
||||||
|
// 创建文件
|
||||||
|
try {
|
||||||
|
// 检查目录是否存在,不存在则创建目录
|
||||||
|
if (!filedir.exists()) {
|
||||||
|
filedir.mkdir();
|
||||||
|
}
|
||||||
|
// 检查文件是否存在,不存在则创建文件
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
// 处理安全异常
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// 处理IO异常
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遇到异常返回null
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="net.micode.notes.tool" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,174 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
// 类成员变量
|
||||||
|
private static final String TAG = "NotesListAdapter";
|
||||||
|
private Context mContext; // 上下文
|
||||||
|
private HashMap<Integer, Boolean> mSelectedIndex; // 存储每个条目是否被选中的状态
|
||||||
|
private int mNotesCount; // 笔记的数量
|
||||||
|
private boolean mChoiceMode; // 选择模式标志
|
||||||
|
|
||||||
|
// AppWidgetAttribute 内部类,用于存储应用 Widget 的属性
|
||||||
|
public static class AppWidgetAttribute {
|
||||||
|
public int widgetId; // Widget ID
|
||||||
|
public int widgetType; // Widget 类型
|
||||||
|
};
|
||||||
|
|
||||||
|
// 构造函数
|
||||||
|
public NotesListAdapter(Context context) {
|
||||||
|
super(context, null); // 调用父类的构造函数
|
||||||
|
mSelectedIndex = new HashMap<Integer, Boolean>();
|
||||||
|
mContext = context;
|
||||||
|
mNotesCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 实现 CursorAdapter 的新视图创建方法
|
||||||
|
@Override
|
||||||
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
||||||
|
return new NotesListItem(context); // 返回自定义的列表项视图
|
||||||
|
}
|
||||||
|
|
||||||
|
// 实现 CursorAdapter 的绑定视图方法
|
||||||
|
@Override
|
||||||
|
public void bindView(View view, Context context, Cursor cursor) {
|
||||||
|
if (view instanceof NotesListItem) {
|
||||||
|
NoteItemData itemData = new NoteItemData(context, cursor); // 创建 NoteItemData 实例
|
||||||
|
((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); // 设置选中状态
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取所有选中项的 ID
|
||||||
|
public HashSet<Long> getSelectedItemIds() {
|
||||||
|
HashSet<Long> itemSet = new HashSet<Long>();
|
||||||
|
// 遍历 mSelectedIndex 并添加选中项的 ID
|
||||||
|
return itemSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashSet<AppWidgetAttribute> getSelectedWidget() {
|
||||||
|
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
|
||||||
|
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");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return itemSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否所有项都已选中
|
||||||
|
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() {
|
||||||
|
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) {
|
||||||
|
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
|
||||||
|
mNotesCount++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "Invalid cursor");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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.ui;
|
||||||
|
|
||||||
|
private ImageView mAlert; // 用于显示提醒图标的 ImageView
|
||||||
|
private TextView mTitle; // 显示笔记标题的 TextView
|
||||||
|
private TextView mTime; // 显示笔记修改时间的 TextView
|
||||||
|
private TextView mCallName; // 显示通话记录名称的 TextView
|
||||||
|
private NoteItemData mItemData; // 当前条目关联的数据对象
|
||||||
|
private CheckBox mCheckBox; // 如果处于选择模式,用于选择当前条目的 CheckBox
|
||||||
|
|
||||||
|
|
||||||
|
public class NotesListItem extends LinearLayout {
|
||||||
|
private ImageView mAlert;
|
||||||
|
private TextView mTitle;
|
||||||
|
private TextView mTime;
|
||||||
|
private TextView mCallName;
|
||||||
|
private NoteItemData mItemData;
|
||||||
|
private CheckBox mCheckBox;
|
||||||
|
|
||||||
|
public NotesListItem(Context context) {
|
||||||
|
super(context);
|
||||||
|
inflate(context, R.layout.note_item, this);
|
||||||
|
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
|
||||||
|
mTitle = (TextView) findViewById(R.id.tv_title);
|
||||||
|
mTime = (TextView) findViewById(R.id.tv_time);
|
||||||
|
mCallName = (TextView) findViewById(R.id.tv_name);
|
||||||
|
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
|
||||||
|
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
|
||||||
|
mCheckBox.setVisibility(View.VISIBLE);
|
||||||
|
mCheckBox.setChecked(checked);
|
||||||
|
} else {
|
||||||
|
mCheckBox.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
mItemData = data;
|
||||||
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
|
||||||
|
mCallName.setVisibility(View.GONE);
|
||||||
|
mAlert.setVisibility(View.VISIBLE);
|
||||||
|
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
|
||||||
|
mTitle.setText(context.getString(R.string.call_record_folder_name)
|
||||||
|
+ context.getString(R.string.format_folder_files_count, data.getNotesCount()));
|
||||||
|
mAlert.setImageResource(R.drawable.call_record);
|
||||||
|
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
|
||||||
|
mCallName.setVisibility(View.VISIBLE);
|
||||||
|
mCallName.setText(data.getCallName());
|
||||||
|
mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem);
|
||||||
|
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
|
||||||
|
if (data.hasAlert()) {
|
||||||
|
mAlert.setImageResource(R.drawable.clock);
|
||||||
|
mAlert.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
mAlert.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mCallName.setVisibility(View.GONE);
|
||||||
|
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
|
||||||
|
|
||||||
|
if (data.getType() == Notes.TYPE_FOLDER) {
|
||||||
|
mTitle.setText(data.getSnippet()
|
||||||
|
+ context.getString(R.string.format_folder_files_count,
|
||||||
|
data.getNotesCount()));
|
||||||
|
mAlert.setVisibility(View.GONE);
|
||||||
|
} else {
|
||||||
|
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
|
||||||
|
if (data.hasAlert()) {
|
||||||
|
mAlert.setImageResource(R.drawable.clock);
|
||||||
|
mAlert.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
mAlert.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
|
||||||
|
|
||||||
|
setBackground(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setBackground(NoteItemData data) {
|
||||||
|
int id = data.getBgColorId();
|
||||||
|
if (data.getType() == Notes.TYPE_NOTE) {
|
||||||
|
if (data.isSingle() || data.isOneFollowingFolder()) {
|
||||||
|
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
|
||||||
|
} else if (data.isLast()) {
|
||||||
|
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
|
||||||
|
} else if (data.isFirst() || data.isMultiFollowingFolder()) {
|
||||||
|
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
|
||||||
|
} else {
|
||||||
|
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public NoteItemData getItemData() {
|
||||||
|
return mItemData;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,388 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
|
import android.accounts.AccountManager;
|
||||||
|
import android.app.ActionBar;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.preference.Preference;
|
||||||
|
import android.preference.Preference.OnPreferenceClickListener;
|
||||||
|
import android.preference.PreferenceActivity;
|
||||||
|
import android.preference.PreferenceCategory;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.text.format.DateFormat;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.data.Notes.NoteColumns;
|
||||||
|
import net.micode.notes.gtask.remote.GTaskSyncService;
|
||||||
|
|
||||||
|
|
||||||
|
public class NotesPreferenceActivity extends PreferenceActivity {
|
||||||
|
public static final String PREFERENCE_NAME = "notes_preferences";
|
||||||
|
|
||||||
|
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
|
||||||
|
|
||||||
|
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
|
||||||
|
|
||||||
|
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
|
||||||
|
|
||||||
|
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
|
||||||
|
|
||||||
|
private static final String AUTHORITIES_FILTER_KEY = "authorities";
|
||||||
|
|
||||||
|
private PreferenceCategory mAccountCategory;
|
||||||
|
|
||||||
|
private GTaskReceiver mReceiver;
|
||||||
|
|
||||||
|
private Account[] mOriAccounts;
|
||||||
|
|
||||||
|
private boolean mHasAddedAccount;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle icicle) {
|
||||||
|
super.onCreate(icicle);
|
||||||
|
|
||||||
|
/* using the app icon for navigation */
|
||||||
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
|
|
||||||
|
addPreferencesFromResource(R.xml.preferences);
|
||||||
|
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
|
||||||
|
mReceiver = new GTaskReceiver();
|
||||||
|
IntentFilter filter = new IntentFilter();
|
||||||
|
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
|
||||||
|
registerReceiver(mReceiver, filter);
|
||||||
|
|
||||||
|
mOriAccounts = null;
|
||||||
|
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
|
||||||
|
getListView().addHeaderView(header, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
|
||||||
|
// need to set sync account automatically if user has added a new
|
||||||
|
// account
|
||||||
|
if (mHasAddedAccount) {
|
||||||
|
Account[] accounts = getGoogleAccounts();
|
||||||
|
if (mOriAccounts != null && accounts.length > mOriAccounts.length) {
|
||||||
|
for (Account accountNew : accounts) {
|
||||||
|
boolean found = false;
|
||||||
|
for (Account accountOld : mOriAccounts) {
|
||||||
|
if (TextUtils.equals(accountOld.name, accountNew.name)) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
setSyncAccount(accountNew.name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
refreshUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
if (mReceiver != null) {
|
||||||
|
unregisterReceiver(mReceiver);
|
||||||
|
}
|
||||||
|
super.onDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadAccountPreference() {
|
||||||
|
mAccountCategory.removeAll();
|
||||||
|
|
||||||
|
Preference accountPref = new Preference(this);
|
||||||
|
final String defaultAccount = getSyncAccountName(this);
|
||||||
|
accountPref.setTitle(getString(R.string.preferences_account_title));
|
||||||
|
accountPref.setSummary(getString(R.string.preferences_account_summary));
|
||||||
|
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||||
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
|
if (!GTaskSyncService.isSyncing()) {
|
||||||
|
if (TextUtils.isEmpty(defaultAccount)) {
|
||||||
|
// the first time to set account
|
||||||
|
showSelectAccountAlertDialog();
|
||||||
|
} else {
|
||||||
|
// if the account has already been set, we need to promp
|
||||||
|
// user about the risk
|
||||||
|
showChangeAccountConfirmAlertDialog();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Toast.makeText(NotesPreferenceActivity.this,
|
||||||
|
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mAccountCategory.addPreference(accountPref);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadSyncButton() {
|
||||||
|
Button syncButton = (Button) findViewById(R.id.preference_sync_button);
|
||||||
|
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
||||||
|
|
||||||
|
// set button state
|
||||||
|
if (GTaskSyncService.isSyncing()) {
|
||||||
|
syncButton.setText(getString(R.string.preferences_button_sync_cancel));
|
||||||
|
syncButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
public void onClick(View v) {
|
||||||
|
GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
syncButton.setText(getString(R.string.preferences_button_sync_immediately));
|
||||||
|
syncButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
public void onClick(View v) {
|
||||||
|
GTaskSyncService.startSync(NotesPreferenceActivity.this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
|
||||||
|
|
||||||
|
// set last sync time
|
||||||
|
if (GTaskSyncService.isSyncing()) {
|
||||||
|
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
|
||||||
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
long lastSyncTime = getLastSyncTime(this);
|
||||||
|
if (lastSyncTime != 0) {
|
||||||
|
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
|
||||||
|
DateFormat.format(getString(R.string.preferences_last_sync_time_format),
|
||||||
|
lastSyncTime)));
|
||||||
|
lastSyncTimeView.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
lastSyncTimeView.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshUI() {
|
||||||
|
loadAccountPreference();
|
||||||
|
loadSyncButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showSelectAccountAlertDialog() {
|
||||||
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
||||||
|
|
||||||
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
||||||
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
||||||
|
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
|
||||||
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
||||||
|
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
|
||||||
|
|
||||||
|
dialogBuilder.setCustomTitle(titleView);
|
||||||
|
dialogBuilder.setPositiveButton(null, null);
|
||||||
|
|
||||||
|
Account[] accounts = getGoogleAccounts();
|
||||||
|
String defAccount = getSyncAccountName(this);
|
||||||
|
|
||||||
|
mOriAccounts = accounts;
|
||||||
|
mHasAddedAccount = false;
|
||||||
|
|
||||||
|
if (accounts.length > 0) {
|
||||||
|
CharSequence[] items = new CharSequence[accounts.length];
|
||||||
|
final CharSequence[] itemMapping = items;
|
||||||
|
int checkedItem = -1;
|
||||||
|
int index = 0;
|
||||||
|
for (Account account : accounts) {
|
||||||
|
if (TextUtils.equals(account.name, defAccount)) {
|
||||||
|
checkedItem = index;
|
||||||
|
}
|
||||||
|
items[index++] = account.name;
|
||||||
|
}
|
||||||
|
dialogBuilder.setSingleChoiceItems(items, checkedItem,
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
setSyncAccount(itemMapping[which].toString());
|
||||||
|
dialog.dismiss();
|
||||||
|
refreshUI();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
|
||||||
|
dialogBuilder.setView(addAccountView);
|
||||||
|
|
||||||
|
final AlertDialog dialog = dialogBuilder.show();
|
||||||
|
addAccountView.setOnClickListener(new View.OnClickListener() {
|
||||||
|
public void onClick(View v) {
|
||||||
|
mHasAddedAccount = true;
|
||||||
|
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
|
||||||
|
intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] {
|
||||||
|
"gmail-ls"
|
||||||
|
});
|
||||||
|
startActivityForResult(intent, -1);
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showChangeAccountConfirmAlertDialog() {
|
||||||
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
||||||
|
|
||||||
|
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
|
||||||
|
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
|
||||||
|
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
|
||||||
|
getSyncAccountName(this)));
|
||||||
|
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
|
||||||
|
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
|
||||||
|
dialogBuilder.setCustomTitle(titleView);
|
||||||
|
|
||||||
|
CharSequence[] menuItemArray = new CharSequence[] {
|
||||||
|
getString(R.string.preferences_menu_change_account),
|
||||||
|
getString(R.string.preferences_menu_remove_account),
|
||||||
|
getString(R.string.preferences_menu_cancel)
|
||||||
|
};
|
||||||
|
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
if (which == 0) {
|
||||||
|
showSelectAccountAlertDialog();
|
||||||
|
} else if (which == 1) {
|
||||||
|
removeSyncAccount();
|
||||||
|
refreshUI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialogBuilder.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Account[] getGoogleAccounts() {
|
||||||
|
AccountManager accountManager = AccountManager.get(this);
|
||||||
|
return accountManager.getAccountsByType("com.google");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setSyncAccount(String account) {
|
||||||
|
if (!getSyncAccountName(this).equals(account)) {
|
||||||
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
||||||
|
SharedPreferences.Editor editor = settings.edit();
|
||||||
|
if (account != null) {
|
||||||
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
|
||||||
|
} else {
|
||||||
|
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
||||||
|
}
|
||||||
|
editor.commit();
|
||||||
|
|
||||||
|
// clean up last sync time
|
||||||
|
setLastSyncTime(this, 0);
|
||||||
|
|
||||||
|
// clean up local gtask related info
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(NoteColumns.GTASK_ID, "");
|
||||||
|
values.put(NoteColumns.SYNC_ID, 0);
|
||||||
|
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
Toast.makeText(NotesPreferenceActivity.this,
|
||||||
|
getString(R.string.preferences_toast_success_set_accout, account),
|
||||||
|
Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeSyncAccount() {
|
||||||
|
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
|
||||||
|
SharedPreferences.Editor editor = settings.edit();
|
||||||
|
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
|
||||||
|
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME);
|
||||||
|
}
|
||||||
|
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
|
||||||
|
editor.remove(PREFERENCE_LAST_SYNC_TIME);
|
||||||
|
}
|
||||||
|
editor.commit();
|
||||||
|
|
||||||
|
// clean up local gtask related info
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(NoteColumns.GTASK_ID, "");
|
||||||
|
values.put(NoteColumns.SYNC_ID, 0);
|
||||||
|
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSyncAccountName(Context context) {
|
||||||
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
||||||
|
Context.MODE_PRIVATE);
|
||||||
|
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setLastSyncTime(Context context, long time) {
|
||||||
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
||||||
|
Context.MODE_PRIVATE);
|
||||||
|
SharedPreferences.Editor editor = settings.edit();
|
||||||
|
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long getLastSyncTime(Context context) {
|
||||||
|
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
|
||||||
|
Context.MODE_PRIVATE);
|
||||||
|
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class GTaskReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
refreshUI();
|
||||||
|
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
|
||||||
|
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
|
||||||
|
syncStatus.setText(intent
|
||||||
|
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case android.R.id.home:
|
||||||
|
Intent intent = new Intent(this, NotesListActivity.class);
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
startActivity(intent);
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
Loading…
Reference in new issue