|
|
|
|
@ -1,20 +1,18 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
* 遵循Apache许可证2.0版本("许可证");
|
|
|
|
|
* 除非遵守许可证,否则不得使用本文件。
|
|
|
|
|
* 您可以在以下地址获取许可证副本:
|
|
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
|
@ -32,101 +30,178 @@ import net.micode.notes.tool.ResourceParser;
|
|
|
|
|
import net.micode.notes.ui.NoteEditActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 便签桌面小部件的基类(抽象类)
|
|
|
|
|
* 继承自AppWidgetProvider,处理小部件的创建、更新、删除等生命周期事件
|
|
|
|
|
*/
|
|
|
|
|
public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
|
|
|
|
public static final String [] PROJECTION = new String [] {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.BG_COLOR_ID,
|
|
|
|
|
NoteColumns.SNIPPET
|
|
|
|
|
|
|
|
|
|
// 数据库查询投影(指定需要查询的字段)
|
|
|
|
|
public static final String[] PROJECTION = new String[]{
|
|
|
|
|
NoteColumns.ID, // 便签ID
|
|
|
|
|
NoteColumns.BG_COLOR_ID, // 背景颜色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;
|
|
|
|
|
// 投影字段对应的列索引(便于代码中通过索引访问字段)
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当小部件被删除时调用(批量删除)
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param appWidgetIds 被删除的小部件ID数组
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
|
|
|
|
// 创建内容值,将便签关联的Widget ID重置为无效值
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
|
|
|
|
|
|
|
|
|
// 遍历删除的小部件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])});
|
|
|
|
|
context.getContentResolver().update(
|
|
|
|
|
Notes.CONTENT_NOTE_URI, // 便签内容URI
|
|
|
|
|
values, // 要更新的内容值
|
|
|
|
|
NoteColumns.WIDGET_ID + "=?", // 条件:Widget ID等于当前ID
|
|
|
|
|
new String[]{String.valueOf(appWidgetIds[i])} // 参数值
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取与小部件关联的便签信息(从数据库查询)
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param widgetId 小部件ID
|
|
|
|
|
* @return 包含便签信息的Cursor(需手动关闭)
|
|
|
|
|
*/
|
|
|
|
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
|
|
|
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
PROJECTION,
|
|
|
|
|
return context.getContentResolver().query(
|
|
|
|
|
Notes.CONTENT_NOTE_URI, // 便签内容URI
|
|
|
|
|
PROJECTION, // 查询字段
|
|
|
|
|
// 条件:Widget ID匹配且不属于回收站(PARENT_ID不等于回收站ID)
|
|
|
|
|
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
|
|
|
|
|
new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) },
|
|
|
|
|
null);
|
|
|
|
|
new String[]{
|
|
|
|
|
String.valueOf(widgetId),
|
|
|
|
|
String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
|
},
|
|
|
|
|
null // 排序方式(默认无)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新小部件(公共入口,隐私模式默认为false)
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param appWidgetManager 小部件管理器
|
|
|
|
|
* @param appWidgetIds 要更新的小部件ID数组
|
|
|
|
|
*/
|
|
|
|
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
|
|
|
update(context, appWidgetManager, appWidgetIds, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新小部件(私有方法,支持隐私模式)
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param appWidgetManager 小部件管理器
|
|
|
|
|
* @param appWidgetIds 要更新的小部件ID数组
|
|
|
|
|
* @param privacyMode 是否为隐私模式(显示占位文本)
|
|
|
|
|
*/
|
|
|
|
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
|
|
|
|
boolean privacyMode) {
|
|
|
|
|
boolean privacyMode) {
|
|
|
|
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
|
|
|
|
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
|
|
|
|
|
int widgetId = appWidgetIds[i];
|
|
|
|
|
if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
|
|
|
|
|
// 默认背景ID和摘要
|
|
|
|
|
int bgId = ResourceParser.getDefaultBgId(context);
|
|
|
|
|
String snippet = "";
|
|
|
|
|
|
|
|
|
|
// 创建点击小部件时启动的Intent(编辑便签或新建便签)
|
|
|
|
|
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());
|
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 设置启动模式为singleTop
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, widgetId); // 传递小部件ID
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); // 传递小部件类型(由子类实现)
|
|
|
|
|
|
|
|
|
|
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
|
|
|
|
|
// 查询与小部件关联的便签信息
|
|
|
|
|
Cursor c = getNoteWidgetInfo(context, widgetId);
|
|
|
|
|
if (c != null && c.moveToFirst()) {
|
|
|
|
|
// 检查是否存在多条记录(异常情况,日志警告)
|
|
|
|
|
if (c.getCount() > 1) {
|
|
|
|
|
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
|
|
|
|
|
Log.e(TAG, "Multiple message with same widget id:" + widgetId);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 读取便签数据
|
|
|
|
|
snippet = c.getString(COLUMN_SNIPPET); // 摘要
|
|
|
|
|
bgId = c.getInt(COLUMN_BG_COLOR_ID); // 背景颜色ID
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); // 传递便签ID
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW); // 设置Intent动作为查看
|
|
|
|
|
} else {
|
|
|
|
|
// 无关联便签时,显示提示文本,并设置动作为新建/编辑
|
|
|
|
|
snippet = context.getResources().getString(R.string.widget_havenot_content);
|
|
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭Cursor(避免资源泄漏)
|
|
|
|
|
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;
|
|
|
|
|
// 创建RemoteViews对象(加载小部件布局)
|
|
|
|
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); // 布局ID由子类实现
|
|
|
|
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); // 设置背景图片(背景资源ID由子类实现)
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); // 传递背景ID到Intent
|
|
|
|
|
|
|
|
|
|
// 生成点击事件的PendingIntent
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
|
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);
|
|
|
|
|
// 隐私模式下显示占位文本,点击跳转到便签列表
|
|
|
|
|
rv.setTextViewText(R.id.widget_text, context.getString(R.string.widget_under_visit_mode));
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(
|
|
|
|
|
context,
|
|
|
|
|
widgetId,
|
|
|
|
|
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 = PendingIntent.getActivity(
|
|
|
|
|
context,
|
|
|
|
|
widgetId,
|
|
|
|
|
intent,
|
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 为小部件文本视图设置点击事件
|
|
|
|
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
|
|
|
|
// 更新小部件界面
|
|
|
|
|
appWidgetManager.updateAppWidget(widgetId, rv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------- 抽象方法(由子类实现具体逻辑)---------------------
|
|
|
|
|
/**
|
|
|
|
|
* 获取背景资源ID(根据背景颜色ID解析为图片资源)
|
|
|
|
|
* @param bgId 背景颜色ID
|
|
|
|
|
* @return 背景图片资源ID
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getBgResourceId(int bgId);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取小部件布局ID
|
|
|
|
|
* @return 布局资源ID(如R.layout.widget_2x)
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取小部件类型(用于区分不同尺寸的小部件)
|
|
|
|
|
* @return 小部件类型常量(如Notes.TYPE_WIDGET_2X)
|
|
|
|
|
*/
|
|
|
|
|
protected abstract int getWidgetType();
|
|
|
|
|
}
|
|
|
|
|
}
|