|
|
|
@ -14,119 +14,119 @@
|
|
|
|
|
* 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 {
|
|
|
|
|
package net.micode.notes.widget;// 包声明,指明代码属于哪个包
|
|
|
|
|
import android.app.PendingIntent;// 导入PendingIntent类
|
|
|
|
|
import android.appwidget.AppWidgetManager;// 导入AppWidgetManager类
|
|
|
|
|
import android.appwidget.AppWidgetProvider;// 导入AppWidgetProvider类
|
|
|
|
|
import android.content.ContentValues;// 导入ContentValues类
|
|
|
|
|
import android.content.Context;// 导入Context类
|
|
|
|
|
import android.content.Intent;// 导入Intent类
|
|
|
|
|
import android.database.Cursor;// 导入Cursor类
|
|
|
|
|
import android.util.Log;// 导入Log类
|
|
|
|
|
import android.widget.RemoteViews;// 导入RemoteViews类
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;// 导入R类,用于访问资源
|
|
|
|
|
import net.micode.notes.data.Notes;// 导入Notes类
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;// 导入NoteColumns类
|
|
|
|
|
import net.micode.notes.tool.ResourceParser;// 导入ResourceParser类
|
|
|
|
|
import net.micode.notes.ui.NoteEditActivity;// 导入NoteEditActivity类
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;// 导入NotesListActivity类
|
|
|
|
|
|
|
|
|
|
public abstract class NoteWidgetProvider extends AppWidgetProvider {// 声明一个抽象类NoteWidgetProvider,继承自AppWidgetProvider
|
|
|
|
|
public static final String [] PROJECTION = new String [] {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.BG_COLOR_ID,
|
|
|
|
|
NoteColumns.SNIPPET
|
|
|
|
|
};
|
|
|
|
|
};// 定义一个常量数组PROJECTION,包含NoteColumns的ID、BG_COLOR_ID和SNIPPET
|
|
|
|
|
|
|
|
|
|
public static final int COLUMN_ID = 0;
|
|
|
|
|
public static final int COLUMN_BG_COLOR_ID = 1;
|
|
|
|
|
public static final int COLUMN_SNIPPET = 2;
|
|
|
|
|
public static final int COLUMN_ID = 0;// 定义COLUMN_ID常量,值为0
|
|
|
|
|
public static final int COLUMN_BG_COLOR_ID = 1;// 定义COLUMN_BG_COLOR_ID常量,值为1
|
|
|
|
|
public static final int COLUMN_SNIPPET = 2;// 定义COLUMN_SNIPPET常量,值为2
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NoteWidgetProvider";
|
|
|
|
|
private static final String TAG = "NoteWidgetProvider";// 定义TAG常量,用于日志记录
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {// 重写onDeleted方法,当小部件被删除时调用
|
|
|
|
|
ContentValues values = new ContentValues();// 创建ContentValues对象
|
|
|
|
|
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);// 将WIDGET_ID列的值设置为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])});
|
|
|
|
|
new String[] { String.valueOf(appWidgetIds[i])});// 更新数据库,将指定小部件ID的WIDGET_ID列设置为INVALID_APPWIDGET_ID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {// 定义getNoteWidgetInfo方法,获取小部件信息
|
|
|
|
|
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);
|
|
|
|
|
null);// 查询数据库,返回符合条件的Cursor对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {// 定义update方法,更新小部件
|
|
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
|
|
}
|
|
|
|
|
}// 调用重载的update方法,传入false作为privacyMode参数
|
|
|
|
|
|
|
|
|
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
|
|
|
|
boolean privacyMode) {
|
|
|
|
|
boolean privacyMode) {// 定义重载的update方法,更新小部件,传入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]);
|
|
|
|
|
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {// 如果小部件ID有效
|
|
|
|
|
int bgId = ResourceParser.getDefaultBgId(context);// 获取默认背景ID
|
|
|
|
|
String snippet = "";// 初始化snippet字符串
|
|
|
|
|
Intent intent = new Intent(context, NoteEditActivity.class);// 创建Intent对象,指向NoteEditActivity
|
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);// 设置Intent标志,确保活动位于任务栈顶部
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);// 添加额外数据,指定小部件ID
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());// 添加额外数据,指定小部件类型
|
|
|
|
|
|
|
|
|
|
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);// 获取小部件信息的Cursor对象
|
|
|
|
|
if (c != null && c.moveToFirst()) {// 如果Cursor不为空且移动到第一条记录
|
|
|
|
|
if (c.getCount() > 1) {// 如果记录数大于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));
|
|
|
|
|
snippet = c.getString(COLUMN_SNIPPET);// 获取SNIPPET列的值
|
|
|
|
|
bgId = c.getInt(COLUMN_BG_COLOR_ID);// 获取BG_COLOR_ID列的值
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); // 添加额外数据,指定记录ID
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
|
} else {
|
|
|
|
|
snippet = context.getResources().getString(R.string.widget_havenot_content);
|
|
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
|
|
}
|
|
|
|
|
}// 设置Intent动作为ACTION_INSERT_OR_EDIT
|
|
|
|
|
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());// 创建RemoteViews对象,指定布局ID
|
|
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);// 设置背景图片资源ID
|
|
|
|
|
/**
|
|
|
|
|
* Generate the pending intent to start host for the widget
|
|
|
|
|
*/
|
|
|
|
|
PendingIntent pendingIntent = null;
|
|
|
|
|
if (privacyMode) {
|
|
|
|
|
if (privacyMode) { // 如果隐私模式
|
|
|
|
|
rv.setTextViewText(R.id.widget_text,
|
|
|
|
|
context.getString(R.string.widget_under_visit_mode));
|
|
|
|
|
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);
|
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);// 创建PendingIntent对象,指向NoteEditActivity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
|
|
|
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);// 设置文本视图的点击
|
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
protected abstract int getBgResourceId(int bgId);// 声明抽象方法getBgResourceId,获取背景资源ID
|
|
|
|
|
|
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
protected abstract int getLayoutId();// 声明抽象方法getLayoutId,获取布局ID
|
|
|
|
|
|
|
|
|
|
protected abstract int getWidgetType();
|
|
|
|
|
protected abstract int getWidgetType();// 声明抽象方法getWidgetType
|
|
|
|
|
}
|
|
|
|
|