Compare commits
12 Commits
Author | SHA1 | Date |
---|---|---|
|
f63c0dbd13 | 2 years ago |
|
a84d6048a6 | 2 years ago |
|
167564b111 | 2 years ago |
|
f4a8a3eb61 | 2 years ago |
|
e6b2e6614b | 2 years ago |
|
6950040c98 | 2 years ago |
|
a17031161b | 2 years ago |
|
1fc5013a68 | 2 years ago |
|
3d70e32323 | 2 years ago |
|
2b2b69b687 | 2 years ago |
|
9cb1aaa3cb | 2 years ago |
|
8b9b242ce5 | 2 years ago |
@ -0,0 +1,150 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!-- 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="net.micode.notes"
|
||||||
|
android:versionCode="1"
|
||||||
|
android:versionName="0.1" >
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||||
|
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
|
||||||
|
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
|
||||||
|
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
|
||||||
|
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
|
||||||
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||||
|
|
||||||
|
<application
|
||||||
|
android:icon="@drawable/icon_app"
|
||||||
|
android:label="@string/app_name" >
|
||||||
|
<activity
|
||||||
|
android:name=".ui.NotesListActivity"
|
||||||
|
android:configChanges="keyboardHidden|orientation|screenSize"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:launchMode="singleTop"
|
||||||
|
android:theme="@style/NoteTheme"
|
||||||
|
android:uiOptions="splitActionBarWhenNarrow"
|
||||||
|
android:windowSoftInputMode="adjustPan" >
|
||||||
|
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:name=".ui.NoteEditActivity"
|
||||||
|
android:configChanges="keyboardHidden|orientation|screenSize"
|
||||||
|
android:launchMode="singleTop"
|
||||||
|
android:theme="@style/NoteTheme" >
|
||||||
|
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.VIEW" />
|
||||||
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
<data android:mimeType="vnd.android.cursor.item/text_note" />
|
||||||
|
<data android:mimeType="vnd.android.cursor.item/call_note" />
|
||||||
|
</intent-filter>
|
||||||
|
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.INSERT_OR_EDIT" />
|
||||||
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
<data android:mimeType="vnd.android.cursor.item/text_note" />
|
||||||
|
<data android:mimeType="vnd.android.cursor.item/call_note" />
|
||||||
|
</intent-filter>
|
||||||
|
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.SEARCH" />
|
||||||
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
</intent-filter>
|
||||||
|
|
||||||
|
<meta-data
|
||||||
|
android:name="android.app.searchable"
|
||||||
|
android:resource="@xml/searchable" />
|
||||||
|
</activity>
|
||||||
|
|
||||||
|
<provider
|
||||||
|
android:name="net.micode.notes.data.NotesProvider"
|
||||||
|
android:authorities="micode_notes"
|
||||||
|
android:multiprocess="true" />
|
||||||
|
|
||||||
|
<receiver
|
||||||
|
android:name=".widget.NoteWidgetProvider_2x"
|
||||||
|
android:label="@string/app_widget2x2" >
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||||
|
<action android:name="android.appwidget.action.APPWIDGET_DELETED" />
|
||||||
|
<action android:name="android.intent.action.PRIVACY_MODE_CHANGED" />
|
||||||
|
</intent-filter>
|
||||||
|
|
||||||
|
<meta-data
|
||||||
|
android:name="android.appwidget.provider"
|
||||||
|
android:resource="@xml/widget_2x_info" />
|
||||||
|
</receiver>
|
||||||
|
<receiver
|
||||||
|
android:name=".widget.NoteWidgetProvider_4x"
|
||||||
|
android:label="@string/app_widget4x4" >
|
||||||
|
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||||
|
<action android:name="android.appwidget.action.APPWIDGET_DELETED" />
|
||||||
|
<action android:name="android.intent.action.PRIVACY_MODE_CHANGED" />
|
||||||
|
</intent-filter>
|
||||||
|
|
||||||
|
<meta-data
|
||||||
|
android:name="android.appwidget.provider"
|
||||||
|
android:resource="@xml/widget_4x_info" />
|
||||||
|
</receiver>
|
||||||
|
|
||||||
|
<receiver android:name=".ui.AlarmInitReceiver" >
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
|
<receiver
|
||||||
|
android:name="net.micode.notes.ui.AlarmReceiver"
|
||||||
|
android:process=":remote" >
|
||||||
|
</receiver>
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:name=".ui.AlarmAlertActivity"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:launchMode="singleInstance"
|
||||||
|
android:theme="@android:style/Theme.Holo.Wallpaper.NoTitleBar" >
|
||||||
|
</activity>
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:name="net.micode.notes.ui.NotesPreferenceActivity"
|
||||||
|
android:label="@string/preferences_title"
|
||||||
|
android:launchMode="singleTop"
|
||||||
|
android:theme="@android:style/Theme.Holo.Light" >
|
||||||
|
</activity>
|
||||||
|
|
||||||
|
<service
|
||||||
|
android:name="net.micode.notes.gtask.remote.GTaskSyncService"
|
||||||
|
android:exported="false" >
|
||||||
|
</service>
|
||||||
|
|
||||||
|
<meta-data
|
||||||
|
android:name="android.app.default_searchable"
|
||||||
|
android:value=".ui.NoteEditActivity" />
|
||||||
|
</application>
|
||||||
|
</manifest>
|
@ -0,0 +1,134 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
//
|
||||||
|
//package net.micode.notes.data;
|
||||||
|
//
|
||||||
|
//import android.content.Context;
|
||||||
|
//import android.database.Cursor;
|
||||||
|
//import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||||
|
//import android.provider.ContactsContract.Data;
|
||||||
|
//import android.telephony.PhoneNumberUtils;
|
||||||
|
//import android.util.Log;
|
||||||
|
//
|
||||||
|
//import java.util.HashMap;
|
||||||
|
//
|
||||||
|
//public class Contact {
|
||||||
|
// private static HashMap<String, String> sContactCache;
|
||||||
|
// private static final String TAG = "Contact";
|
||||||
|
//
|
||||||
|
// private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
|
||||||
|
// + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
|
||||||
|
// + " AND " + Data.RAW_CONTACT_ID + " IN "
|
||||||
|
// + "(SELECT raw_contact_id "
|
||||||
|
// + " FROM phone_lookup"
|
||||||
|
// + " WHERE min_match = '+')";
|
||||||
|
//
|
||||||
|
// public static String getContact(Context context, String phoneNumber) {
|
||||||
|
// if(sContactCache == null) {
|
||||||
|
// sContactCache = new HashMap<String, String>();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if(sContactCache.containsKey(phoneNumber)) {
|
||||||
|
// return sContactCache.get(phoneNumber);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// String selection = CALLER_ID_SELECTION.replace("+",
|
||||||
|
// PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
|
||||||
|
// Cursor cursor = context.getContentResolver().query(
|
||||||
|
// Data.CONTENT_URI,
|
||||||
|
// new String [] { Phone.DISPLAY_NAME },
|
||||||
|
// selection,
|
||||||
|
// new String[] { phoneNumber },
|
||||||
|
// null);
|
||||||
|
//
|
||||||
|
// if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
// try {
|
||||||
|
// String name = cursor.getString(0);
|
||||||
|
// sContactCache.put(phoneNumber, name);
|
||||||
|
// return name;
|
||||||
|
// } catch (IndexOutOfBoundsException e) {
|
||||||
|
// Log.e(TAG, " Cursor get string error " + e.toString());
|
||||||
|
// return null;
|
||||||
|
// } finally {
|
||||||
|
// cursor.close();
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// Log.d(TAG, "No contact matched with number:" + phoneNumber);
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
package net.micode.notes.data; //包名
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||||
|
import android.provider.ContactsContract.Data;
|
||||||
|
import android.telephony.PhoneNumberUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.util.HashMap; //引入HashMap集合
|
||||||
|
|
||||||
|
public class Contact { //Contact类
|
||||||
|
|
||||||
|
private static HashMap<String, String> sContactCache; //静态HashMap成员变量用于缓存联系人
|
||||||
|
private static final String TAG = "Contact"; //日志标签
|
||||||
|
|
||||||
|
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER //SQL语句定义联系人查询条件
|
||||||
|
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
|
||||||
|
+ " AND " + Data.RAW_CONTACT_ID + " IN "
|
||||||
|
+ "(SELECT raw_contact_id "
|
||||||
|
+ " FROM phone_lookup"
|
||||||
|
+ " WHERE min_match = '+')";
|
||||||
|
|
||||||
|
public static String getContact(Context context, String phoneNumber) { //获取联系人方法
|
||||||
|
|
||||||
|
if(sContactCache == null) { //如果缓存为空,初始化
|
||||||
|
sContactCache = new HashMap<String, String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(sContactCache.containsKey(phoneNumber)) { //如果缓存中包含该号码,直接返回
|
||||||
|
return sContactCache.get(phoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
String selection = CALLER_ID_SELECTION.replace("+",
|
||||||
|
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //替换SQL语句中的+为完整号码
|
||||||
|
Cursor cursor = context.getContentResolver().query(
|
||||||
|
Data.CONTENT_URI,
|
||||||
|
new String [] { Phone.DISPLAY_NAME }, //查询联系人姓名
|
||||||
|
selection,
|
||||||
|
new String[] { phoneNumber },
|
||||||
|
null);
|
||||||
|
|
||||||
|
if (cursor != null && cursor.moveToFirst()) { //如果查询结果不为空且有第一条记录
|
||||||
|
try {
|
||||||
|
String name = cursor.getString(0); //获取第一列联系人姓名
|
||||||
|
sContactCache.put(phoneNumber, name); //添加至缓存
|
||||||
|
return name;
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
Log.e(TAG, " Cursor get string error " + e.toString());
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
cursor.close(); //关闭游标
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "No contact matched with number:" + phoneNumber);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,371 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.micode.notes.data;
|
||||||
|
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import net.micode.notes.data.Notes.DataColumns;
|
||||||
|
import net.micode.notes.data.Notes.DataConstants;
|
||||||
|
import net.micode.notes.data.Notes.NoteColumns;
|
||||||
|
|
||||||
|
//NotesDatabaseHelper类继承SQLiteOpenHelper
|
||||||
|
public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
||||||
|
//数据库名
|
||||||
|
private static final String DB_NAME = "note.db";
|
||||||
|
//数据库版本
|
||||||
|
private static final int DB_VERSION = 4;
|
||||||
|
//内部接口定义表名常量
|
||||||
|
public interface TABLE {
|
||||||
|
public static final String NOTE = "note";
|
||||||
|
|
||||||
|
public static final String DATA = "data";
|
||||||
|
}
|
||||||
|
//日志标签
|
||||||
|
private static final String TAG = "NotesDatabaseHelper";
|
||||||
|
//静态实例
|
||||||
|
private static NotesDatabaseHelper mInstance;
|
||||||
|
//创建笔记表SQL语句
|
||||||
|
private static final String CREATE_NOTE_TABLE_SQL =
|
||||||
|
//ID主键
|
||||||
|
"CREATE TABLE " + TABLE.NOTE + "(" +
|
||||||
|
NoteColumns.ID + " INTEGER PRIMARY KEY," +
|
||||||
|
//笔记表结构定义
|
||||||
|
NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
|
||||||
|
NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
|
||||||
|
NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," +
|
||||||
|
NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," +
|
||||||
|
NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," +
|
||||||
|
NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" +
|
||||||
|
")";
|
||||||
|
//创建数据表SQL语句
|
||||||
|
private static final String CREATE_DATA_TABLE_SQL =
|
||||||
|
"CREATE TABLE " + TABLE.DATA + "(" +
|
||||||
|
DataColumns.ID + " INTEGER PRIMARY KEY," +
|
||||||
|
//数据表结构定义
|
||||||
|
DataColumns.MIME_TYPE + " TEXT NOT NULL," +
|
||||||
|
DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," +
|
||||||
|
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
|
||||||
|
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
|
||||||
|
DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," +
|
||||||
|
DataColumns.DATA1 + " INTEGER," +
|
||||||
|
DataColumns.DATA2 + " INTEGER," +
|
||||||
|
DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," +
|
||||||
|
DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," +
|
||||||
|
DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" +
|
||||||
|
")";
|
||||||
|
//创建数据表NOTE_ID索引SQL语句
|
||||||
|
private static final String CREATE_DATA_NOTE_ID_INDEX_SQL =
|
||||||
|
//如果不存在,则创建note_id_index索引
|
||||||
|
"CREATE INDEX IF NOT EXISTS note_id_index ON " +
|
||||||
|
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increase folder's note count when move note to the folder //注释:当移动笔记到文件夹时,增加文件夹的笔记数
|
||||||
|
*/
|
||||||
|
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
|
||||||
|
//创建触发器,在PARENT_ID列更新后,增加目标文件夹的NOTES_COUNT
|
||||||
|
"CREATE TRIGGER increase_folder_count_on_update "+
|
||||||
|
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
|
||||||
|
" BEGIN " +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
|
||||||
|
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrease folder's note count when move note from folder//注释:当从文件夹移动笔记时,减少文件夹的笔记数
|
||||||
|
*/
|
||||||
|
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
|
||||||
|
"CREATE TRIGGER decrease_folder_count_on_update " +
|
||||||
|
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
|
||||||
|
" BEGIN " +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
|
||||||
|
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
|
||||||
|
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increase folder's note count when insert new note to the folder 在文件夹中插入新笔记时增加文件夹的笔记计数
|
||||||
|
*/
|
||||||
|
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER =
|
||||||
|
"CREATE TRIGGER increase_folder_count_on_insert " +
|
||||||
|
" AFTER INSERT ON " + TABLE.NOTE +
|
||||||
|
" BEGIN " +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
|
||||||
|
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrease folder's note count when delete note from the folder 从文件夹中删除笔记时减少文件夹的笔记计数
|
||||||
|
*/
|
||||||
|
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER =
|
||||||
|
"CREATE TRIGGER decrease_folder_count_on_delete " +
|
||||||
|
" AFTER DELETE ON " + TABLE.NOTE +
|
||||||
|
" BEGIN " +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
|
||||||
|
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
|
||||||
|
" AND " + NoteColumns.NOTES_COUNT + ">0;" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update note's content when insert data with type {@link DataConstants#NOTE} 插入数据类型为 {@link 数据常量#NOTE} 的数据时更新注释的内容
|
||||||
|
*/
|
||||||
|
private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER =
|
||||||
|
"CREATE TRIGGER update_note_content_on_insert " +
|
||||||
|
" AFTER INSERT ON " + TABLE.DATA +
|
||||||
|
" WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
|
||||||
|
" BEGIN" +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
|
||||||
|
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update note's content when data with {@link DataConstants#NOTE} type has changed 当具有 {@link 数据常量#NOTE} 类型的数据发生更改时更新注释的内容
|
||||||
|
*/
|
||||||
|
private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER =
|
||||||
|
"CREATE TRIGGER update_note_content_on_update " +
|
||||||
|
" AFTER UPDATE ON " + TABLE.DATA +
|
||||||
|
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
|
||||||
|
" BEGIN" +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
|
||||||
|
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update note's content when data with {@link DataConstants#NOTE} type has deleted 删除具有 {@link 数据常量#NOTE} 类型的数据时更新注释的内容
|
||||||
|
*/
|
||||||
|
private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER =
|
||||||
|
"CREATE TRIGGER update_note_content_on_delete " +
|
||||||
|
" AFTER delete ON " + TABLE.DATA +
|
||||||
|
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
|
||||||
|
" BEGIN" +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.SNIPPET + "=''" +
|
||||||
|
" WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete datas belong to note which has been deleted 删除数据属于已删除的注释
|
||||||
|
*/
|
||||||
|
private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER =
|
||||||
|
"CREATE TRIGGER delete_data_on_delete " +
|
||||||
|
" AFTER DELETE ON " + TABLE.NOTE +
|
||||||
|
" BEGIN" +
|
||||||
|
" DELETE FROM " + TABLE.DATA +
|
||||||
|
" WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete notes belong to folder which has been deleted 删除笔记属于已删除的文件夹
|
||||||
|
*/
|
||||||
|
private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER =
|
||||||
|
"CREATE TRIGGER folder_delete_notes_on_delete " +
|
||||||
|
" AFTER DELETE ON " + TABLE.NOTE +
|
||||||
|
" BEGIN" +
|
||||||
|
" DELETE FROM " + TABLE.NOTE +
|
||||||
|
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
|
||||||
|
" END";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move notes belong to folder which has been moved to trash folder 移动笔记属于已移至垃圾箱文件夹的文件夹
|
||||||
|
*/
|
||||||
|
private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER =
|
||||||
|
"CREATE TRIGGER folder_move_notes_on_trash " +
|
||||||
|
" AFTER UPDATE ON " + TABLE.NOTE +
|
||||||
|
" WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
|
||||||
|
" BEGIN" +
|
||||||
|
" UPDATE " + TABLE.NOTE +
|
||||||
|
" SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
|
||||||
|
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
|
||||||
|
" END";
|
||||||
|
//构造方法,调用父类构造方法创建数据库
|
||||||
|
public NotesDatabaseHelper(Context context) {
|
||||||
|
super(context, DB_NAME, null, DB_VERSION);
|
||||||
|
}
|
||||||
|
//执行创建笔记表SQL语句
|
||||||
|
public void createNoteTable(SQLiteDatabase db) {
|
||||||
|
db.execSQL(CREATE_NOTE_TABLE_SQL);
|
||||||
|
//重新创建笔记表触发器
|
||||||
|
reCreateNoteTableTriggers(db);
|
||||||
|
//创建系统文件夹
|
||||||
|
createSystemFolder(db);
|
||||||
|
//打印日志,提示笔记表已创建
|
||||||
|
Log.d(TAG, "note table has been created");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reCreateNoteTableTriggers(SQLiteDatabase db) {
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash");
|
||||||
|
|
||||||
|
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
|
||||||
|
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
|
||||||
|
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER);
|
||||||
|
db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER);
|
||||||
|
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER);
|
||||||
|
db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER);
|
||||||
|
db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createSystemFolder(SQLiteDatabase db) {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* call record foler for call notes
|
||||||
|
*/
|
||||||
|
values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER);
|
||||||
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
||||||
|
db.insert(TABLE.NOTE, null, values);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* root folder which is default folder
|
||||||
|
*/
|
||||||
|
values.clear();
|
||||||
|
values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER);
|
||||||
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
||||||
|
db.insert(TABLE.NOTE, null, values);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* temporary folder which is used for moving note
|
||||||
|
*/
|
||||||
|
values.clear();
|
||||||
|
values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER);
|
||||||
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
||||||
|
db.insert(TABLE.NOTE, null, values);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create trash folder
|
||||||
|
*/
|
||||||
|
values.clear();
|
||||||
|
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
|
||||||
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
||||||
|
db.insert(TABLE.NOTE, null, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createDataTable(SQLiteDatabase db) {
|
||||||
|
db.execSQL(CREATE_DATA_TABLE_SQL);
|
||||||
|
reCreateDataTableTriggers(db);
|
||||||
|
db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL);
|
||||||
|
Log.d(TAG, "data table has been created");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reCreateDataTableTriggers(SQLiteDatabase db) {
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete");
|
||||||
|
|
||||||
|
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER);
|
||||||
|
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER);
|
||||||
|
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER);
|
||||||
|
}
|
||||||
|
|
||||||
|
static synchronized NotesDatabaseHelper getInstance(Context context) {
|
||||||
|
if (mInstance == null) {
|
||||||
|
mInstance = new NotesDatabaseHelper(context);
|
||||||
|
}
|
||||||
|
return mInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(SQLiteDatabase db) {
|
||||||
|
createNoteTable(db);
|
||||||
|
createDataTable(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
boolean reCreateTriggers = false;
|
||||||
|
boolean skipV2 = false;
|
||||||
|
|
||||||
|
if (oldVersion == 1) {
|
||||||
|
upgradeToV2(db);
|
||||||
|
skipV2 = true; // this upgrade including the upgrade from v2 to v3
|
||||||
|
oldVersion++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldVersion == 2 && !skipV2) {
|
||||||
|
upgradeToV3(db);
|
||||||
|
reCreateTriggers = true;
|
||||||
|
oldVersion++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldVersion == 3) {
|
||||||
|
upgradeToV4(db);
|
||||||
|
oldVersion++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reCreateTriggers) {
|
||||||
|
reCreateNoteTableTriggers(db);
|
||||||
|
reCreateDataTableTriggers(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldVersion != newVersion) {
|
||||||
|
throw new IllegalStateException("Upgrade notes database to version " + newVersion
|
||||||
|
+ "fails");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void upgradeToV2(SQLiteDatabase db) {
|
||||||
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE);
|
||||||
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA);
|
||||||
|
createNoteTable(db);
|
||||||
|
createDataTable(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void upgradeToV3(SQLiteDatabase db) {
|
||||||
|
// drop unused triggers
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete");
|
||||||
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update");
|
||||||
|
// add a column for gtask id
|
||||||
|
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID
|
||||||
|
+ " TEXT NOT NULL DEFAULT ''");
|
||||||
|
// add a trash system folder
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
|
||||||
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
||||||
|
db.insert(TABLE.NOTE, null, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void upgradeToV4(SQLiteDatabase db) {
|
||||||
|
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
|
||||||
|
+ " INTEGER NOT NULL DEFAULT 0");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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.database.Cursor;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import net.micode.notes.tool.GTaskStringUtils;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
|
||||||
|
public class MetaData extends Task { //继承Task类
|
||||||
|
private final static String TAG = MetaData.class.getSimpleName();
|
||||||
|
|
||||||
|
private String mRelatedGid = null; //相关任务ID
|
||||||
|
|
||||||
|
public void setMeta(String gid, JSONObject metaInfo) { //设置元数据
|
||||||
|
try {
|
||||||
|
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); //保存任务ID
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e(TAG, "failed to put related gid"); //输出错误日志
|
||||||
|
}
|
||||||
|
setNotes(metaInfo.toString());//设置注释
|
||||||
|
setName(GTaskStringUtils.META_NOTE_NAME);//设置名称
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRelatedGid() { //获取相关任务ID
|
||||||
|
return mRelatedGid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isWorthSaving() { //重写isWorthSaving()方法
|
||||||
|
return getNotes() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setContentByRemoteJSON(JSONObject js) {//重写setContentByRemoteJSON()方法
|
||||||
|
super.setContentByRemoteJSON(js);
|
||||||
|
if (getNotes() != null) {
|
||||||
|
try {
|
||||||
|
JSONObject metaInfo = new JSONObject(getNotes().trim());
|
||||||
|
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); //获取相关任务ID
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.w(TAG, "failed to get related gid");
|
||||||
|
mRelatedGid = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setContentByLocalJSON(JSONObject js) {
|
||||||
|
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject getLocalJSONFromContent() {
|
||||||
|
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSyncAction(Cursor c) {
|
||||||
|
throw new IllegalAccessError("MetaData:getSyncAction should not be called");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,189 @@
|
|||||||
|
/*
|
||||||
|
* 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.content.ContentResolver;
|
||||||
|
import android.content.ContentUris;
|
||||||
|
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.DataConstants;
|
||||||
|
import net.micode.notes.data.Notes.NoteColumns;
|
||||||
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
||||||
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
|
||||||
|
public class SqlData {
|
||||||
|
private static final String TAG = SqlData.class.getSimpleName();
|
||||||
|
|
||||||
|
private static final int INVALID_ID = -99999;
|
||||||
|
|
||||||
|
public static final String[] PROJECTION_DATA = new String[] {
|
||||||
|
DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
|
||||||
|
DataColumns.DATA3
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final int DATA_ID_COLUMN = 0;
|
||||||
|
|
||||||
|
public static final int DATA_MIME_TYPE_COLUMN = 1;
|
||||||
|
|
||||||
|
public static final int DATA_CONTENT_COLUMN = 2;
|
||||||
|
|
||||||
|
public static final int DATA_CONTENT_DATA_1_COLUMN = 3;
|
||||||
|
|
||||||
|
public static final int DATA_CONTENT_DATA_3_COLUMN = 4;
|
||||||
|
|
||||||
|
private ContentResolver mContentResolver;
|
||||||
|
|
||||||
|
private boolean mIsCreate;
|
||||||
|
|
||||||
|
private long mDataId;
|
||||||
|
|
||||||
|
private String mDataMimeType;
|
||||||
|
|
||||||
|
private String mDataContent;
|
||||||
|
|
||||||
|
private long mDataContentData1;
|
||||||
|
|
||||||
|
private String mDataContentData3;
|
||||||
|
|
||||||
|
private ContentValues mDiffDataValues;
|
||||||
|
|
||||||
|
public SqlData(Context context) {
|
||||||
|
mContentResolver = context.getContentResolver();
|
||||||
|
mIsCreate = true;
|
||||||
|
mDataId = INVALID_ID;
|
||||||
|
mDataMimeType = DataConstants.NOTE;
|
||||||
|
mDataContent = "";
|
||||||
|
mDataContentData1 = 0;
|
||||||
|
mDataContentData3 = "";
|
||||||
|
mDiffDataValues = new ContentValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SqlData(Context context, Cursor c) {
|
||||||
|
mContentResolver = context.getContentResolver();
|
||||||
|
mIsCreate = false;
|
||||||
|
loadFromCursor(c);
|
||||||
|
mDiffDataValues = new ContentValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadFromCursor(Cursor c) {
|
||||||
|
mDataId = c.getLong(DATA_ID_COLUMN);
|
||||||
|
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
|
||||||
|
mDataContent = c.getString(DATA_CONTENT_COLUMN);
|
||||||
|
mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);
|
||||||
|
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(JSONObject js) throws JSONException {
|
||||||
|
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
|
||||||
|
if (mIsCreate || mDataId != dataId) {
|
||||||
|
mDiffDataValues.put(DataColumns.ID, dataId);
|
||||||
|
}
|
||||||
|
mDataId = dataId;
|
||||||
|
|
||||||
|
String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE)
|
||||||
|
: DataConstants.NOTE;
|
||||||
|
if (mIsCreate || !mDataMimeType.equals(dataMimeType)) {
|
||||||
|
mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType);
|
||||||
|
}
|
||||||
|
mDataMimeType = dataMimeType;
|
||||||
|
|
||||||
|
String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : "";
|
||||||
|
if (mIsCreate || !mDataContent.equals(dataContent)) {
|
||||||
|
mDiffDataValues.put(DataColumns.CONTENT, dataContent);
|
||||||
|
}
|
||||||
|
mDataContent = dataContent;
|
||||||
|
|
||||||
|
long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0;
|
||||||
|
if (mIsCreate || mDataContentData1 != dataContentData1) {
|
||||||
|
mDiffDataValues.put(DataColumns.DATA1, dataContentData1);
|
||||||
|
}
|
||||||
|
mDataContentData1 = dataContentData1;
|
||||||
|
|
||||||
|
String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : "";
|
||||||
|
if (mIsCreate || !mDataContentData3.equals(dataContentData3)) {
|
||||||
|
mDiffDataValues.put(DataColumns.DATA3, dataContentData3);
|
||||||
|
}
|
||||||
|
mDataContentData3 = dataContentData3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject getContent() throws JSONException {
|
||||||
|
if (mIsCreate) {
|
||||||
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
JSONObject js = new JSONObject();
|
||||||
|
js.put(DataColumns.ID, mDataId);
|
||||||
|
js.put(DataColumns.MIME_TYPE, mDataMimeType);
|
||||||
|
js.put(DataColumns.CONTENT, mDataContent);
|
||||||
|
js.put(DataColumns.DATA1, mDataContentData1);
|
||||||
|
js.put(DataColumns.DATA3, mDataContentData3);
|
||||||
|
return js;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void commit(long noteId, boolean validateVersion, long version) {
|
||||||
|
|
||||||
|
if (mIsCreate) {
|
||||||
|
if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) {
|
||||||
|
mDiffDataValues.remove(DataColumns.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
mDiffDataValues.put(DataColumns.NOTE_ID, noteId);
|
||||||
|
Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues);
|
||||||
|
try {
|
||||||
|
mDataId = Long.valueOf(uri.getPathSegments().get(1));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Log.e(TAG, "Get note id error :" + e.toString());
|
||||||
|
throw new ActionFailureException("create note failed");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (mDiffDataValues.size() > 0) {
|
||||||
|
int result = 0;
|
||||||
|
if (!validateVersion) {
|
||||||
|
result = mContentResolver.update(ContentUris.withAppendedId(
|
||||||
|
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null);
|
||||||
|
} else {
|
||||||
|
result = mContentResolver.update(ContentUris.withAppendedId(
|
||||||
|
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
|
||||||
|
" ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE
|
||||||
|
+ " WHERE " + NoteColumns.VERSION + "=?)", new String[] {
|
||||||
|
String.valueOf(noteId), String.valueOf(version)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (result == 0) {
|
||||||
|
Log.w(TAG, "there is no update. maybe user updates note when syncing");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mDiffDataValues.clear();
|
||||||
|
mIsCreate = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return mDataId;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,266 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.OperationApplicationException;
|
||||||
|
import android.net.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;
|
||||||
|
|
||||||
|
|
||||||
|
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,49 @@
|
|||||||
|
package net.micode.notes.ui;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.View.OnClickListener;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.PopupMenu;
|
||||||
|
import android.widget.PopupMenu.OnMenuItemClickListener;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
|
||||||
|
public class DropdownMenu {
|
||||||
|
private Button mButton;
|
||||||
|
private PopupMenu mPopupMenu;
|
||||||
|
//声明一个下拉菜单
|
||||||
|
private Menu mMenu;
|
||||||
|
|
||||||
|
public DropdownMenu(Context context, Button button, int menuId) {
|
||||||
|
mButton = button;
|
||||||
|
mButton.setBackgroundResource(R.drawable.dropdown_icon);
|
||||||
|
//设置这个view的背景
|
||||||
|
mPopupMenu = new PopupMenu(context, mButton);
|
||||||
|
mMenu = mPopupMenu.getMenu();
|
||||||
|
mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
|
||||||
|
//MenuInflater是用来实例化Menu目录下的Menu布局文件
|
||||||
|
//根据ID来确认menu的内容选项
|
||||||
|
mButton.setOnClickListener(new OnClickListener() {
|
||||||
|
public void onClick(View v) {
|
||||||
|
mPopupMenu.show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
|
||||||
|
if (mPopupMenu != null) {
|
||||||
|
mPopupMenu.setOnMenuItemClickListener(listener);
|
||||||
|
}//设置菜单的监听
|
||||||
|
}
|
||||||
|
|
||||||
|
public MenuItem findItem(int id) {
|
||||||
|
return mMenu.findItem(id);
|
||||||
|
}//对于菜单选项的初始化,根据索引搜索菜单需要的选项
|
||||||
|
|
||||||
|
public void setTitle(CharSequence title) {
|
||||||
|
mButton.setText(title);
|
||||||
|
}//布局文件,设置标题
|
||||||
|
}
|
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* 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.widget;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.appwidget.AppWidgetProvider;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.data.Notes.NoteColumns;
|
||||||
|
import net.micode.notes.tool.ResourceParser;
|
||||||
|
import net.micode.notes.ui.NoteEditActivity;
|
||||||
|
import net.micode.notes.ui.NotesListActivity;
|
||||||
|
|
||||||
|
public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
||||||
|
public static final String [] PROJECTION = new String [] {
|
||||||
|
NoteColumns.ID,
|
||||||
|
NoteColumns.BG_COLOR_ID,
|
||||||
|
NoteColumns.SNIPPET
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final int COLUMN_ID = 0;
|
||||||
|
public static final int COLUMN_BG_COLOR_ID = 1;
|
||||||
|
public static final int COLUMN_SNIPPET = 2;
|
||||||
|
|
||||||
|
private static final String TAG = "NoteWidgetProvider";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||||
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
||||||
|
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
|
||||||
|
values,
|
||||||
|
NoteColumns.WIDGET_ID + "=?",
|
||||||
|
new String[] { String.valueOf(appWidgetIds[i])});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
||||||
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
||||||
|
PROJECTION,
|
||||||
|
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
|
||||||
|
new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) },
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
|
update(context, appWidgetManager, appWidgetIds, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
||||||
|
boolean privacyMode) {
|
||||||
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
||||||
|
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
|
||||||
|
int bgId = ResourceParser.getDefaultBgId(context);
|
||||||
|
String snippet = "";
|
||||||
|
Intent intent = new Intent(context, NoteEditActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||||
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
|
||||||
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
|
||||||
|
|
||||||
|
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
|
||||||
|
if (c != null && c.moveToFirst()) {
|
||||||
|
if (c.getCount() > 1) {
|
||||||
|
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
|
||||||
|
c.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
snippet = c.getString(COLUMN_SNIPPET);
|
||||||
|
bgId = c.getInt(COLUMN_BG_COLOR_ID);
|
||||||
|
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
|
||||||
|
intent.setAction(Intent.ACTION_VIEW);
|
||||||
|
} else {
|
||||||
|
snippet = context.getResources().getString(R.string.widget_havenot_content);
|
||||||
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c != null) {
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
||||||
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
||||||
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
|
||||||
|
/**
|
||||||
|
* Generate the pending intent to start host for the widget
|
||||||
|
*/
|
||||||
|
PendingIntent pendingIntent = null;
|
||||||
|
if (privacyMode) {
|
||||||
|
rv.setTextViewText(R.id.widget_text,
|
||||||
|
context.getString(R.string.widget_under_visit_mode));
|
||||||
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
|
||||||
|
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
} else {
|
||||||
|
rv.setTextViewText(R.id.widget_text, snippet);
|
||||||
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
||||||
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract int getBgResourceId(int bgId);
|
||||||
|
|
||||||
|
protected abstract int getLayoutId();
|
||||||
|
|
||||||
|
protected abstract int getWidgetType();
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* 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.widget;
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.tool.ResourceParser;
|
||||||
|
|
||||||
|
|
||||||
|
public class NoteWidgetProvider_2x extends NoteWidgetProvider {
|
||||||
|
@Override
|
||||||
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
|
super.update(context, appWidgetManager, appWidgetIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getLayoutId() {
|
||||||
|
return R.layout.widget_2x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getBgResourceId(int bgId) {
|
||||||
|
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getWidgetType() {
|
||||||
|
return Notes.TYPE_WIDGET_2X;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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.widget;
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.tool.ResourceParser;
|
||||||
|
|
||||||
|
|
||||||
|
public class NoteWidgetProvider_4x extends NoteWidgetProvider {
|
||||||
|
@Override
|
||||||
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
|
super.update(context, appWidgetManager, appWidgetIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int getLayoutId() {
|
||||||
|
return R.layout.widget_4x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getBgResourceId(int bgId) {
|
||||||
|
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getWidgetType() {
|
||||||
|
return Notes.TYPE_WIDGET_4X;
|
||||||
|
}
|
||||||
|
}
|
After Width: | Height: | Size: 3.5 KiB |
After Width: | Height: | Size: 245 B |
After Width: | Height: | Size: 3.5 KiB |
After Width: | Height: | Size: 3.9 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 443 B |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 3.5 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 5.0 KiB |
After Width: | Height: | Size: 5.5 KiB |
After Width: | Height: | Size: 4.9 KiB |
After Width: | Height: | Size: 3.8 KiB |
After Width: | Height: | Size: 5.9 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 3.5 KiB |
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 4.0 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 6.7 KiB |
After Width: | Height: | Size: 554 KiB |
After Width: | Height: | Size: 4.3 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 4.5 KiB |
After Width: | Height: | Size: 3.2 KiB |
After Width: | Height: | Size: 5.5 KiB |
After Width: | Height: | Size: 2.8 KiB |
After Width: | Height: | Size: 4.4 KiB |
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 3.3 KiB |
After Width: | Height: | Size: 4.2 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 4.5 KiB |
After Width: | Height: | Size: 3.2 KiB |
After Width: | Height: | Size: 4.2 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 4.4 KiB |
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 4.3 KiB |
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 3.3 KiB |
After Width: | Height: | Size: 3.3 KiB |
After Width: | Height: | Size: 3.2 KiB |
After Width: | Height: | Size: 87 KiB |
After Width: | Height: | Size: 88 KiB |
After Width: | Height: | Size: 6.9 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 4.8 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 5.8 KiB |
After Width: | Height: | Size: 1.9 KiB |