src/net/micode/notes/widget #13

Merged
pskuv5h87 merged 1 commits from branch2 into main 10 months ago

@ -0,0 +1,131 @@
/*
* 便
* 便
*/
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 {
// 数据库查询投影指定需要获取的列便签ID、背景颜色ID和内容片段
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) {
// 当小部件被删除时将关联便签的WIDGET_ID设为无效值解除绑定
ContentValues values = new ContentValues();
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
for (int appWidgetId : appWidgetIds) {
context.getContentResolver().update(
Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[]{String.valueOf(appWidgetId)}
);
}
}
// 查询指定widgetId关联的便签信息排除废纸篓中的便签
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);
}
// 核心更新逻辑遍历所有小部件ID更新每个小部件的内容和样式
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
boolean privacyMode) {
for (int appWidgetId : appWidgetIds) {
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) continue;
int bgId = ResourceParser.getDefaultBgId(context); // 默认背景资源
String snippet = "";
Intent intent = new Intent(context, NoteEditActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // 单例模式启动Activity
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetId);
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
// 从数据库获取关联的便签数据
try (Cursor cursor = getNoteWidgetInfo(context, appWidgetId)) {
if (cursor != null && cursor.moveToFirst()) {
if (cursor.getCount() > 1) {
Log.e(TAG, "Multiple notes with same widget id: " + appWidgetId);
continue; // 异常情况:多个便签关联同一小部件
}
snippet = cursor.getString(COLUMN_SNIPPET);
bgId = cursor.getInt(COLUMN_BG_COLOR_ID);
intent.putExtra(Intent.EXTRA_UID, cursor.getLong(COLUMN_ID));
intent.setAction(Intent.ACTION_VIEW); // 查看现有便签
} else {
snippet = context.getString(R.string.widget_havenot_content);
intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // 创建新便签
}
}
// 构建RemoteViews并设置内容
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
// 根据隐私模式设置不同内容和点击行为
PendingIntent pendingIntent;
if (privacyMode) {
rv.setTextViewText(R.id.widget_text, context.getString(R.string.widget_under_visit_mode));
pendingIntent = PendingIntent.getActivity(
context, appWidgetId,
new Intent(context, NotesListActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
} else {
rv.setTextViewText(R.id.widget_text, snippet);
pendingIntent = PendingIntent.getActivity(
context, appWidgetId, intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
}
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, rv); // 更新小部件视图
}
}
/* 抽象方法:子类需实现具体逻辑 */
protected abstract int getBgResourceId(int bgId); // 根据颜色ID获取实际资源ID
protected abstract int getLayoutId(); // 获取小部件布局文件ID
protected abstract int getWidgetType(); // 获取小部件类型标识
}

@ -0,0 +1,57 @@
/*
* 2x便便
* 2x
*/
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 {
/**
*
* @param context
* @param appWidgetManager
* @param appWidgetIds ID
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 调用父类通用更新逻辑,保持功能一致性
super.update(context, appWidgetManager, appWidgetIds);
}
/**
* 2xID
* @return R.layout.widget_2x
*/
@Override
protected int getLayoutId() {
// 指定2x尺寸小部件的专属布局文件
return R.layout.widget_2x;
}
/**
* 2x
* @param bgId
* @return ID
*/
@Override
protected int getBgResourceId(int bgId) {
// 通过资源解析工具获取2x专属背景资源
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
}
/**
*
* @return 2xTYPE_WIDGET_2X
*/
@Override
protected int getWidgetType() {
// 在数据库和业务逻辑中用于标识2x小部件类型
return Notes.TYPE_WIDGET_2X;
}
}

@ -0,0 +1,57 @@
/*
* 4x便4x
* 4x4x4x
*/
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 {
/**
*
* @param context 访
* @param appWidgetManager
* @param appWidgetIds ID
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// 复用父类通用更新逻辑,保持各尺寸小部件行为一致性
super.update(context, appWidgetManager, appWidgetIds);
}
/**
* 4x
* @return 4xwidget_4x.xmlID
*/
@Override
protected int getLayoutId() {
// 定义4x尺寸特有的UI布局结构
return R.layout.widget_4x;
}
/**
*
* @param bgId
* @return 4xID
*/
@Override
protected int getBgResourceId(int bgId) {
// 通过资源解析工具获取4x专用背景图
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
}
/**
*
* @return TYPE_WIDGET_4X
*/
@Override
protected int getWidgetType() {
// 标识当前为4x规格小部件区别于2x/其他尺寸
return Notes.TYPE_WIDGET_4X;
}
}
Loading…
Cancel
Save