Compare commits
No commits in common. 'main' and 'jiaoqinyu' have entirely different histories.
@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* 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.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.content.DialogInterface.OnClickListener;
|
||||||
|
import android.content.DialogInterface.OnDismissListener;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.media.AudioManager;
|
||||||
|
import android.media.MediaPlayer;
|
||||||
|
import android.media.RingtoneManager;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.view.Window;
|
||||||
|
import android.view.WindowManager;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.tool.DataUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
|
||||||
|
private long mNoteId; // 存储笔记的 ID
|
||||||
|
private String mSnippet; // 存储笔记的简短内容(摘要)
|
||||||
|
private static final int SNIPPET_PREW_MAX_LEN = 60; // 摘要最大长度
|
||||||
|
MediaPlayer mPlayer; // 用于播放闹铃音的媒体播放器
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
requestWindowFeature(Window.FEATURE_NO_TITLE); // 请求无标题窗口
|
||||||
|
|
||||||
|
final Window win = getWindow();
|
||||||
|
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); // 当屏幕锁定时仍然显示窗口
|
||||||
|
|
||||||
|
// 如果屏幕关闭,保持屏幕亮起
|
||||||
|
if (!isScreenOn()) {
|
||||||
|
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|
||||||
|
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
|
||||||
|
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
|
||||||
|
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent intent = getIntent(); // 获取传递进来的 Intent
|
||||||
|
|
||||||
|
try {
|
||||||
|
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); // 获取笔记的 ID
|
||||||
|
// 获取笔记的简短摘要,如果摘要太长,截取并附加说明
|
||||||
|
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
|
||||||
|
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
|
||||||
|
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
|
||||||
|
: mSnippet;
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return; // 如果获取笔记信息失败,则返回
|
||||||
|
}
|
||||||
|
|
||||||
|
mPlayer = new MediaPlayer(); // 初始化媒体播放器
|
||||||
|
// 如果笔记在数据库中是可见的,显示对话框并播放闹铃音
|
||||||
|
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
|
||||||
|
showActionDialog();
|
||||||
|
playAlarmSound();
|
||||||
|
} else {
|
||||||
|
finish(); // 如果笔记不可见,则结束此活动
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查屏幕是否开启
|
||||||
|
private boolean isScreenOn() {
|
||||||
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||||
|
return pm.isScreenOn();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 播放闹铃音
|
||||||
|
private void playAlarmSound() {
|
||||||
|
// 获取系统的默认闹铃音 URI
|
||||||
|
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
|
||||||
|
|
||||||
|
// 获取当前铃声模式,检查闹铃是否会被静音
|
||||||
|
int silentModeStreams = Settings.System.getInt(getContentResolver(),
|
||||||
|
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
|
||||||
|
|
||||||
|
// 根据静音模式设置音频流类型
|
||||||
|
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
|
||||||
|
mPlayer.setAudioStreamType(silentModeStreams);
|
||||||
|
} else {
|
||||||
|
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); // 设置音频流类型为闹铃
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 设置闹铃音频数据源,并准备播放
|
||||||
|
mPlayer.setDataSource(this, url);
|
||||||
|
mPlayer.prepare();
|
||||||
|
mPlayer.setLooping(true); // 设置闹铃音循环播放
|
||||||
|
mPlayer.start(); // 开始播放
|
||||||
|
} catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
|
||||||
|
// 捕获并打印异常
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示提示操作对话框
|
||||||
|
private void showActionDialog() {
|
||||||
|
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
|
||||||
|
dialog.setTitle(R.string.app_name); // 设置对话框标题为应用名称
|
||||||
|
dialog.setMessage(mSnippet); // 显示笔记的简短摘要
|
||||||
|
dialog.setPositiveButton(R.string.notealert_ok, this); // 设置“确定”按钮
|
||||||
|
// 如果屏幕开启,设置“进入”按钮
|
||||||
|
if (isScreenOn()) {
|
||||||
|
dialog.setNegativeButton(R.string.notealert_enter, this);
|
||||||
|
}
|
||||||
|
dialog.show().setOnDismissListener(this); // 显示对话框并设置监听器
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理按钮点击事件
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
switch (which) {
|
||||||
|
case DialogInterface.BUTTON_NEGATIVE:
|
||||||
|
// 如果点击了“进入”按钮,启动编辑笔记活动
|
||||||
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
||||||
|
intent.setAction(Intent.ACTION_VIEW); // 设置意图为查看笔记
|
||||||
|
intent.putExtra(Intent.EXTRA_UID, mNoteId); // 传递笔记 ID
|
||||||
|
startActivity(intent); // 启动活动
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对话框关闭时停止播放闹铃音
|
||||||
|
public void onDismiss(DialogInterface dialog) {
|
||||||
|
stopAlarmSound();
|
||||||
|
finish(); // 结束当前活动
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止播放闹铃音
|
||||||
|
private void stopAlarmSound() {
|
||||||
|
if (mPlayer != null) {
|
||||||
|
mPlayer.stop(); // 停止播放
|
||||||
|
mPlayer.release(); // 释放播放器资源
|
||||||
|
mPlayer = null; // 清空播放器对象
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public class GTaskStringUtils {
|
||||||
|
|
||||||
|
// GTASK JSON 数据字段常量
|
||||||
|
public final static String GTASK_JSON_ACTION_ID = "action_id"; // 操作ID
|
||||||
|
public final static String GTASK_JSON_ACTION_LIST = "action_list"; // 操作列表
|
||||||
|
public final static String GTASK_JSON_ACTION_TYPE = "action_type"; // 操作类型
|
||||||
|
public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; // 创建操作
|
||||||
|
public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; // 获取所有操作
|
||||||
|
public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; // 移动操作
|
||||||
|
public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; // 更新操作
|
||||||
|
public final static String GTASK_JSON_CREATOR_ID = "creator_id"; // 创建者ID
|
||||||
|
public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; // 子实体
|
||||||
|
public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; // 客户端版本
|
||||||
|
public final static String GTASK_JSON_COMPLETED = "completed"; // 是否完成
|
||||||
|
public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; // 当前列表ID
|
||||||
|
public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; // 默认列表ID
|
||||||
|
public final static String GTASK_JSON_DELETED = "deleted"; // 是否删除
|
||||||
|
public final static String GTASK_JSON_DEST_LIST = "dest_list"; // 目标列表
|
||||||
|
public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; // 目标父级
|
||||||
|
public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; // 目标父级类型
|
||||||
|
public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; // 实体差异
|
||||||
|
public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; // 实体类型
|
||||||
|
public final static String GTASK_JSON_GET_DELETED = "get_deleted"; // 获取已删除的
|
||||||
|
public final static String GTASK_JSON_ID = "id"; // ID
|
||||||
|
public final static String GTASK_JSON_INDEX = "index"; // 索引
|
||||||
|
public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; // 最后修改时间
|
||||||
|
public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; // 最新同步点
|
||||||
|
public final static String GTASK_JSON_LIST_ID = "list_id"; // 列表ID
|
||||||
|
public final static String GTASK_JSON_LISTS = "lists"; // 列表
|
||||||
|
public final static String GTASK_JSON_NAME = "name"; // 名称
|
||||||
|
public final static String GTASK_JSON_NEW_ID = "new_id"; // 新ID
|
||||||
|
public final static String GTASK_JSON_NOTES = "notes"; // 笔记
|
||||||
|
public final static String GTASK_JSON_PARENT_ID = "parent_id"; // 父级ID
|
||||||
|
public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; // 上一个兄弟ID
|
||||||
|
public final static String GTASK_JSON_RESULTS = "results"; // 结果
|
||||||
|
public final static String GTASK_JSON_SOURCE_LIST = "source_list"; // 源列表
|
||||||
|
public final static String GTASK_JSON_TASKS = "tasks"; // 任务
|
||||||
|
public final static String GTASK_JSON_TYPE = "type"; // 类型
|
||||||
|
public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; // 任务组类型
|
||||||
|
public final static String GTASK_JSON_TYPE_TASK = "TASK"; // 任务类型
|
||||||
|
public final static String GTASK_JSON_USER = "user"; // 用户
|
||||||
|
|
||||||
|
// MIUI 笔记相关文件夹前缀
|
||||||
|
public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; // MIUI笔记文件夹前缀
|
||||||
|
|
||||||
|
// 默认文件夹名称
|
||||||
|
public final static String FOLDER_DEFAULT = "Default"; // 默认文件夹
|
||||||
|
public final static String FOLDER_CALL_NOTE = "Call_Note"; // 通话笔记文件夹
|
||||||
|
public final static String FOLDER_META = "METADATA"; // 元数据文件夹
|
||||||
|
|
||||||
|
// 元数据头部字段
|
||||||
|
public final static String META_HEAD_GTASK_ID = "meta_gid"; // GTASK ID
|
||||||
|
public final static String META_HEAD_NOTE = "meta_note"; // 笔记元数据
|
||||||
|
public final static String META_HEAD_DATA = "meta_data"; // 数据元数据
|
||||||
|
|
||||||
|
// 元数据笔记名称,表示不允许更新和删除
|
||||||
|
public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; // 元数据笔记名称
|
||||||
|
}
|
@ -0,0 +1,505 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import android.content.ContentResolver;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.data.Notes.DataColumns;
|
||||||
|
import net.micode.notes.data.Notes.NoteColumns;
|
||||||
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
||||||
|
import net.micode.notes.tool.GTaskStringUtils;
|
||||||
|
import net.micode.notes.tool.ResourceParser;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
|
public class SqlNote {
|
||||||
|
private static final String TAG = SqlNote.class.getSimpleName();
|
||||||
|
|
||||||
|
private static final int INVALID_ID = -99999;
|
||||||
|
|
||||||
|
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,
|
||||||
|
NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID, NoteColumns.SNIPPET, NoteColumns.TYPE,
|
||||||
|
NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID,
|
||||||
|
NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID,
|
||||||
|
NoteColumns.VERSION
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final int ID_COLUMN = 0;
|
||||||
|
|
||||||
|
public static final int ALERTED_DATE_COLUMN = 1;
|
||||||
|
|
||||||
|
public static final int BG_COLOR_ID_COLUMN = 2;
|
||||||
|
|
||||||
|
public static final int CREATED_DATE_COLUMN = 3;
|
||||||
|
|
||||||
|
public static final int HAS_ATTACHMENT_COLUMN = 4;
|
||||||
|
|
||||||
|
public static final int MODIFIED_DATE_COLUMN = 5;
|
||||||
|
|
||||||
|
public static final int NOTES_COUNT_COLUMN = 6;
|
||||||
|
|
||||||
|
public static final int PARENT_ID_COLUMN = 7;
|
||||||
|
|
||||||
|
public static final int SNIPPET_COLUMN = 8;
|
||||||
|
|
||||||
|
public static final int TYPE_COLUMN = 9;
|
||||||
|
|
||||||
|
public static final int WIDGET_ID_COLUMN = 10;
|
||||||
|
|
||||||
|
public static final int WIDGET_TYPE_COLUMN = 11;
|
||||||
|
|
||||||
|
public static final int SYNC_ID_COLUMN = 12;
|
||||||
|
|
||||||
|
public static final int LOCAL_MODIFIED_COLUMN = 13;
|
||||||
|
|
||||||
|
public static final int ORIGIN_PARENT_ID_COLUMN = 14;
|
||||||
|
|
||||||
|
public static final int GTASK_ID_COLUMN = 15;
|
||||||
|
|
||||||
|
public static final int VERSION_COLUMN = 16;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
private ContentResolver mContentResolver;
|
||||||
|
|
||||||
|
private boolean mIsCreate;
|
||||||
|
|
||||||
|
private long mId;
|
||||||
|
|
||||||
|
private long mAlertDate;
|
||||||
|
|
||||||
|
private int mBgColorId;
|
||||||
|
|
||||||
|
private long mCreatedDate;
|
||||||
|
|
||||||
|
private int mHasAttachment;
|
||||||
|
|
||||||
|
private long mModifiedDate;
|
||||||
|
|
||||||
|
private long mParentId;
|
||||||
|
|
||||||
|
private String mSnippet;
|
||||||
|
|
||||||
|
private int mType;
|
||||||
|
|
||||||
|
private int mWidgetId;
|
||||||
|
|
||||||
|
private int mWidgetType;
|
||||||
|
|
||||||
|
private long mOriginParent;
|
||||||
|
|
||||||
|
private long mVersion;
|
||||||
|
|
||||||
|
private ContentValues mDiffNoteValues;
|
||||||
|
|
||||||
|
private ArrayList<SqlData> mDataList;
|
||||||
|
|
||||||
|
public SqlNote(Context context) {
|
||||||
|
mContext = context;
|
||||||
|
mContentResolver = context.getContentResolver();
|
||||||
|
mIsCreate = true;
|
||||||
|
mId = INVALID_ID;
|
||||||
|
mAlertDate = 0;
|
||||||
|
mBgColorId = ResourceParser.getDefaultBgId(context);
|
||||||
|
mCreatedDate = System.currentTimeMillis();
|
||||||
|
mHasAttachment = 0;
|
||||||
|
mModifiedDate = System.currentTimeMillis();
|
||||||
|
mParentId = 0;
|
||||||
|
mSnippet = "";
|
||||||
|
mType = Notes.TYPE_NOTE;
|
||||||
|
mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
|
||||||
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
|
||||||
|
mOriginParent = 0;
|
||||||
|
mVersion = 0;
|
||||||
|
mDiffNoteValues = new ContentValues();
|
||||||
|
mDataList = new ArrayList<SqlData>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SqlNote(Context context, Cursor c) {
|
||||||
|
mContext = context;
|
||||||
|
mContentResolver = context.getContentResolver();
|
||||||
|
mIsCreate = false;
|
||||||
|
loadFromCursor(c);
|
||||||
|
mDataList = new ArrayList<SqlData>();
|
||||||
|
if (mType == Notes.TYPE_NOTE)
|
||||||
|
loadDataContent();
|
||||||
|
mDiffNoteValues = new ContentValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SqlNote(Context context, long id) {
|
||||||
|
mContext = context;
|
||||||
|
mContentResolver = context.getContentResolver();
|
||||||
|
mIsCreate = false;
|
||||||
|
loadFromCursor(id);
|
||||||
|
mDataList = new ArrayList<SqlData>();
|
||||||
|
if (mType == Notes.TYPE_NOTE)
|
||||||
|
loadDataContent();
|
||||||
|
mDiffNoteValues = new ContentValues();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadFromCursor(long id) {
|
||||||
|
Cursor c = null;
|
||||||
|
try {
|
||||||
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
|
||||||
|
new String[] {
|
||||||
|
String.valueOf(id)
|
||||||
|
}, null);
|
||||||
|
if (c != null) {
|
||||||
|
c.moveToNext();
|
||||||
|
loadFromCursor(c);
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "loadFromCursor: cursor = null");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (c != null)
|
||||||
|
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);
|
||||||
|
mCreatedDate = c.getLong(CREATED_DATE_COLUMN);
|
||||||
|
mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN);
|
||||||
|
mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN);
|
||||||
|
mParentId = c.getLong(PARENT_ID_COLUMN);
|
||||||
|
mSnippet = c.getString(SNIPPET_COLUMN);
|
||||||
|
mType = c.getInt(TYPE_COLUMN);
|
||||||
|
mWidgetId = c.getInt(WIDGET_ID_COLUMN);
|
||||||
|
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
|
||||||
|
mVersion = c.getLong(VERSION_COLUMN);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}, null);
|
||||||
|
if (c != null) {
|
||||||
|
if (c.getCount() == 0) {
|
||||||
|
Log.w(TAG, "it seems that the note has not data");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (c.moveToNext()) {
|
||||||
|
SqlData data = new SqlData(mContext, c);
|
||||||
|
mDataList.add(data);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "loadDataContent: cursor = null");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (c != null)
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setContent(JSONObject js) {
|
||||||
|
try {
|
||||||
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
||||||
|
if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
|
||||||
|
Log.w(TAG, "cannot set system folder");
|
||||||
|
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
|
||||||
|
// for folder we can only update the snnipet and type
|
||||||
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
||||||
|
.getString(NoteColumns.SNIPPET) : "";
|
||||||
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
||||||
|
}
|
||||||
|
mSnippet = snippet;
|
||||||
|
|
||||||
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
||||||
|
: Notes.TYPE_NOTE;
|
||||||
|
if (mIsCreate || mType != type) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
||||||
|
}
|
||||||
|
mType = type;
|
||||||
|
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
|
||||||
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
||||||
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
||||||
|
if (mIsCreate || mId != id) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.ID, id);
|
||||||
|
}
|
||||||
|
mId = id;
|
||||||
|
|
||||||
|
long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note
|
||||||
|
.getLong(NoteColumns.ALERTED_DATE) : 0;
|
||||||
|
if (mIsCreate || mAlertDate != alertDate) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate);
|
||||||
|
}
|
||||||
|
mAlertDate = alertDate;
|
||||||
|
|
||||||
|
int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note
|
||||||
|
.getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext);
|
||||||
|
if (mIsCreate || mBgColorId != bgColorId) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId);
|
||||||
|
}
|
||||||
|
mBgColorId = bgColorId;
|
||||||
|
|
||||||
|
long createDate = note.has(NoteColumns.CREATED_DATE) ? note
|
||||||
|
.getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();
|
||||||
|
if (mIsCreate || mCreatedDate != createDate) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate);
|
||||||
|
}
|
||||||
|
mCreatedDate = createDate;
|
||||||
|
|
||||||
|
int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note
|
||||||
|
.getInt(NoteColumns.HAS_ATTACHMENT) : 0;
|
||||||
|
if (mIsCreate || mHasAttachment != hasAttachment) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment);
|
||||||
|
}
|
||||||
|
mHasAttachment = hasAttachment;
|
||||||
|
|
||||||
|
long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note
|
||||||
|
.getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis();
|
||||||
|
if (mIsCreate || mModifiedDate != modifiedDate) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate);
|
||||||
|
}
|
||||||
|
mModifiedDate = modifiedDate;
|
||||||
|
|
||||||
|
long parentId = note.has(NoteColumns.PARENT_ID) ? note
|
||||||
|
.getLong(NoteColumns.PARENT_ID) : 0;
|
||||||
|
if (mIsCreate || mParentId != parentId) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
|
||||||
|
}
|
||||||
|
mParentId = parentId;
|
||||||
|
|
||||||
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
||||||
|
.getString(NoteColumns.SNIPPET) : "";
|
||||||
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
||||||
|
}
|
||||||
|
mSnippet = snippet;
|
||||||
|
|
||||||
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
||||||
|
: Notes.TYPE_NOTE;
|
||||||
|
if (mIsCreate || mType != type) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
||||||
|
}
|
||||||
|
mType = type;
|
||||||
|
|
||||||
|
int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID)
|
||||||
|
: AppWidgetManager.INVALID_APPWIDGET_ID;
|
||||||
|
if (mIsCreate || mWidgetId != widgetId) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId);
|
||||||
|
}
|
||||||
|
mWidgetId = widgetId;
|
||||||
|
|
||||||
|
int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note
|
||||||
|
.getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE;
|
||||||
|
if (mIsCreate || mWidgetType != widgetType) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType);
|
||||||
|
}
|
||||||
|
mWidgetType = widgetType;
|
||||||
|
|
||||||
|
long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note
|
||||||
|
.getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
|
||||||
|
if (mIsCreate || mOriginParent != originParent) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent);
|
||||||
|
}
|
||||||
|
mOriginParent = originParent;
|
||||||
|
|
||||||
|
for (int i = 0; i < dataArray.length(); i++) {
|
||||||
|
JSONObject data = dataArray.getJSONObject(i);
|
||||||
|
SqlData sqlData = null;
|
||||||
|
if (data.has(DataColumns.ID)) {
|
||||||
|
long dataId = data.getLong(DataColumns.ID);
|
||||||
|
for (SqlData temp : mDataList) {
|
||||||
|
if (dataId == temp.getId()) {
|
||||||
|
sqlData = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sqlData == null) {
|
||||||
|
sqlData = new SqlData(mContext);
|
||||||
|
mDataList.add(sqlData);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlData.setContent(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e(TAG, e.toString());
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject getContent() {
|
||||||
|
try {
|
||||||
|
JSONObject js = new JSONObject();
|
||||||
|
|
||||||
|
if (mIsCreate) {
|
||||||
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject note = new JSONObject();
|
||||||
|
if (mType == Notes.TYPE_NOTE) {
|
||||||
|
note.put(NoteColumns.ID, mId);
|
||||||
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
||||||
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
||||||
|
note.put(NoteColumns.CREATED_DATE, mCreatedDate);
|
||||||
|
note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment);
|
||||||
|
note.put(NoteColumns.MODIFIED_DATE, mModifiedDate);
|
||||||
|
note.put(NoteColumns.PARENT_ID, mParentId);
|
||||||
|
note.put(NoteColumns.SNIPPET, mSnippet);
|
||||||
|
note.put(NoteColumns.TYPE, mType);
|
||||||
|
note.put(NoteColumns.WIDGET_ID, mWidgetId);
|
||||||
|
note.put(NoteColumns.WIDGET_TYPE, mWidgetType);
|
||||||
|
note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent);
|
||||||
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note);
|
||||||
|
|
||||||
|
JSONArray dataArray = new JSONArray();
|
||||||
|
for (SqlData sqlData : mDataList) {
|
||||||
|
JSONObject data = sqlData.getContent();
|
||||||
|
if (data != null) {
|
||||||
|
dataArray.put(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
|
||||||
|
} else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {
|
||||||
|
note.put(NoteColumns.ID, mId);
|
||||||
|
note.put(NoteColumns.TYPE, mType);
|
||||||
|
note.put(NoteColumns.SNIPPET, mSnippet);
|
||||||
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note);
|
||||||
|
}
|
||||||
|
|
||||||
|
return js;
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e(TAG, e.toString());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParentId(long id) {
|
||||||
|
mParentId = id;
|
||||||
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGtaskId(String gid) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSyncId(long syncId) {
|
||||||
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetLocalModified() {
|
||||||
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return mId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getParentId() {
|
||||||
|
return mParentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSnippet() {
|
||||||
|
return mSnippet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNoteType() {
|
||||||
|
return mType == Notes.TYPE_NOTE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void commit(boolean validateVersion) {
|
||||||
|
if (mIsCreate) {
|
||||||
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
|
||||||
|
mDiffNoteValues.remove(NoteColumns.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
|
||||||
|
try {
|
||||||
|
mId = Long.valueOf(uri.getPathSegments().get(1));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Log.e(TAG, "Get note id error :" + e.toString());
|
||||||
|
throw new ActionFailureException("create note failed");
|
||||||
|
}
|
||||||
|
if (mId == 0) {
|
||||||
|
throw new IllegalStateException("Create thread id failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mType == Notes.TYPE_NOTE) {
|
||||||
|
for (SqlData sqlData : mDataList) {
|
||||||
|
sqlData.commit(mId, false, -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) {
|
||||||
|
Log.e(TAG, "No such note");
|
||||||
|
throw new IllegalStateException("Try to update note with invalid id");
|
||||||
|
}
|
||||||
|
if (mDiffNoteValues.size() > 0) {
|
||||||
|
mVersion ++;
|
||||||
|
int result = 0;
|
||||||
|
if (!validateVersion) {
|
||||||
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
||||||
|
+ NoteColumns.ID + "=?)", new String[] {
|
||||||
|
String.valueOf(mId)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
||||||
|
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
|
||||||
|
new String[] {
|
||||||
|
String.valueOf(mId), String.valueOf(mVersion)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (result == 0) {
|
||||||
|
Log.w(TAG, "there is no update. maybe user updates note when syncing");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mType == Notes.TYPE_NOTE) {
|
||||||
|
for (SqlData sqlData : mDataList) {
|
||||||
|
sqlData.commit(mId, validateVersion, mVersion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// refresh local info
|
||||||
|
loadFromCursor(mId);
|
||||||
|
if (mType == Notes.TYPE_NOTE)
|
||||||
|
loadDataContent();
|
||||||
|
|
||||||
|
mDiffNoteValues.clear();
|
||||||
|
mIsCreate = false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue