From e2ed886a0c87d3bf5f26376ab7d73a5306b7ae0b Mon Sep 17 00:00:00 2001 From: p4ns2i85u <1257425730@qq.com> Date: Thu, 9 Jan 2025 08:31:14 +0800 Subject: [PATCH] zxl --- .../notes/widget/NoteWidgetProvider.java | 507 +++++++++++++++--- 1 file changed, 447 insertions(+), 60 deletions(-) diff --git a/src/net/micode/notes/widget/NoteWidgetProvider.java b/src/net/micode/notes/widget/NoteWidgetProvider.java index 8f04956..ee37a3f 100644 --- a/src/net/micode/notes/widget/NoteWidgetProvider.java +++ b/src/net/micode/notes/widget/NoteWidgetProvider.java @@ -14,119 +14,506 @@ * limitations under the License. */ -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 [] { +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.content.SharedPreferences; // 导入SharedPreferences类 +import android.database.Cursor; // 导入Cursor类 +import android.graphics.Bitmap; // 导入Bitmap类 +import android.graphics.BitmapFactory; // 导入BitmapFactory类 +import android.os.Bundle; // 导入Bundle类 +import android.util.Log; // 导入Log类 +import android.view.animation.Animation; // 导入Animation类 +import android.view.animation.AlphaAnimation; // 导入AlphaAnimation类 +import android.view.animation.AccelerateDecelerateInterpolator; // 导入AccelerateDecelerateInterpolator类 +import android.widget.RemoteViews; // 导入RemoteViews类 +import android.widget.RemoteViewsService; // 导入RemoteViewsService类 +import android.widget.Toast; // 导入Toast类 + +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 { + public static final String[] PROJECTION = new String[] { NoteColumns.ID, NoteColumns.BG_COLOR_ID, NoteColumns.SNIPPET - };// 定义一个常量数组PROJECTION,包含NoteColumns的ID、BG_COLOR_ID和SNIPPET + }; // 定义一个常量数组PROJECTION,包含NoteColumns的ID、BG_COLOR_ID和SNIPPET - 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 + 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";// 定义TAG常量,用于日志记录 + private static final String TAG = "NoteWidgetProvider"; // 定义TAG常量,用于日志记录 + private static final String WIDGET_PREFS = "widget_prefs"; // 小部件设置的SharedPreferences名称 + private static final String WIDGET_BG_COLOR_KEY = "widget_bg_color"; // 小部件背景颜色的键 + private static final String WIDGET_FONT_SIZE_KEY = "widget_font_size"; // 小部件字体大小的键 + private static final String WIDGET_SYNC_ENABLED_KEY = "widget_sync_enabled"; // 小部件数据同步是否启用的键 + private static final String WIDGET_SYNC_URL_KEY = "widget_sync_url"; // 小部件数据同步的URL的键 @Override - 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 + public void onDeleted(Context context, int[] appWidgetIds) { + // 当小部件被删除时调用 + 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])});// 更新数据库,将指定小部件ID的WIDGET_ID列设置为INVALID_APPWIDGET_ID + new String[] { String.valueOf(appWidgetIds[i]) }); // 更新数据库,将指定小部件ID的WIDGET_ID列设置为INVALID_APPWIDGET_ID } } - private Cursor getNoteWidgetInfo(Context context, int widgetId) {// 定义getNoteWidgetInfo方法,获取小部件信息 + 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);// 查询数据库,返回符合条件的Cursor对象 + new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLDER) }, + null); // 查询数据库,返回符合条件的Cursor对象 } - protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {// 定义update方法,更新小部件 - update(context, appWidgetManager, appWidgetIds, false); - }// 调用重载的update方法,传入false作为privacyMode参数 + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 更新小部件 + update(context, appWidgetManager, appWidgetIds, false); // 调用重载的update方法,传入false作为privacyMode参数 + } private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, - boolean privacyMode) {// 定义重载的update方法,更新小部件,传入privacyMode参数 + boolean privacyMode) { + // 更新小部件 for (int i = 0; i < appWidgetIds.length; 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]);// 记录错误日志 + 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);// 获取SNIPPET列的值 - bgId = c.getInt(COLUMN_BG_COLOR_ID);// 获取BG_COLOR_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 + } // 设置Intent动作为ACTION_INSERT_OR_EDIT if (c != null) { c.close(); } - RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());// 创建RemoteViews对象,指定布局ID + 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);// 设置背景图片资源ID + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); // 设置背景图片资源ID /** * 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));// 设置文本视图内容为隐私模式提示 + 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对象,指向NoteEditActivity + 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);// 声明抽象方法getBgResourceId,获取背景资源ID + protected abstract int getBgResourceId(int bgId); // 声明抽象方法getBgResourceId,获取背景资源ID + + protected abstract int getLayoutId(); // 声明抽象方法getLayoutId,获取布局ID + + protected abstract int getWidgetType(); // 声明抽象方法getWidgetType + + /** + * 获取小部件的背景颜色 + * @param context 应用程序上下文 + * @return 小部件的背景颜色 + */ + private int getWidgetBgColor(Context context) { + SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE); + return prefs.getInt(WIDGET_BG_COLOR_KEY, 0xFFE0E0E0); // 默认背景颜色为浅灰色 + } + + /** + * 获取小部件的字体大小 + * @param context 应用程序上下文 + * @return 小部件的字体大小 + */ + private int getWidgetFontSize(Context context) { + SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE); + return prefs.getInt(WIDGET_FONT_SIZE_KEY, 16); // 默认字体大小为16sp + } + + /** + * 设置小部件的背景颜色 + * @param context 应用程序上下文 + * @param color 小部件的背景颜色 + */ + private void setWidgetBgColor(Context context, int color) { + SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE); + prefs.edit().putInt(WIDGET_BG_COLOR_KEY, color).apply(); + } + + /** + * 设置小部件的字体大小 + * @param context 应用程序上下文 + * @param size 小部件的字体大小 + */ + private void setWidgetFontSize(Context context, int size) { + SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE); + prefs.edit().putInt(WIDGET_FONT_SIZE_KEY, size).apply(); + } - protected abstract int getLayoutId();// 声明抽象方法getLayoutId,获取布局ID + /** + * 启用或禁用数据同步 + * @param context 应用程序上下文 + * @param enabled 是否启用数据同步 + */ + private void setSyncEnabled(Context context, boolean enabled) { + SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE); + prefs.edit().putBoolean(WIDGET_SYNC_ENABLED_KEY, enabled).apply(); + } + + /** + * 设置数据同步的URL + * @param context 应用程序上下文 + * @param url 数据同步的URL + */ + private void setSyncUrl(Context context, String url) { + SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE); + prefs.edit().putString(WIDGET_SYNC_URL_KEY, url).apply(); + } - protected abstract int getWidgetType();// 声明抽象方法getWidgetType -} + /** + * 获取小部件的背景图像 + * @param context 应用程序上下文 + * @param bgId 背景ID + * @return 小部件的背景图像 + */ + private Bitmap getWidgetBgImage(Context context, int bgId) { + // 根据背景ID获取背景图像资源ID + int bgResId = ResourceParser.WidgetBgResources.getWidgetBgResource(bgId); + + // 解码背景图像资源 + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inSampleSize = 2; // 缩小图像以减少内存使用 + return BitmapFactory.decodeResource(context.getResources(), bgResId, options); + } + + /** + * 设置小部件的背景图像 + * @param context 应用程序上下文 + * @param appWidgetId 小部件ID + * @param bgImage 小部件的背景图像 + */ + private void setWidgetBgImage(Context context, int appWidgetId, Bitmap bgImage) { + // 获取远程视图对象 + RemoteViews views = new RemoteViews(context.getPackageName(), getLayoutId()); + + // 设置背景图像 + views.setImageViewBitmap(R.id.widget_bg_image, bgImage); + + // 更新小部件视图 + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); + appWidgetManager.updateAppWidget(appWidgetId, views); + } + + /** + * 获取小部件的动画效果 + * @param context 应用程序上下文 + * @return 小部件的动画效果 + */ + private Animation getWidgetAnimation(Context context) { + // 创建动画效果 + Animation animation = new AlphaAnimation(0.0f, 1.0f); + animation.setDuration(1000); // 动画持续时间为1秒 + animation.setInterpolator(new AccelerateDecelerateInterpolator()); + return animation; + } + + /** + * 设置小部件的动画效果 + * @param context 应用程序上下文 + * @param appWidgetId 小部件ID + * @param animation 小部件的动画效果 + */ + private void setWidgetAnimation(Context context, int appWidgetId, Animation animation) { + // 获取远程视图对象 + RemoteViews views = new RemoteViews(context.getPackageName(), getLayoutId()); + + // 设置动画效果 + views.setAnimation(R.id.widget_text, animation); + + // 更新小部件视图 + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); + appWidgetManager.updateAppWidget(appWidgetId, views); + } + + /** + * 获取小部件的滑动手势 + * @param context 应用程序上下文 + * @return 小部件的滑动手势 + */ + private GestureDetector getWidgetGestureDetector(Context context) { + // 创建滑动手势检测器 + GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + // 检测滑动手势 + if (Math.abs(velocityX) > Math.abs(velocityY)) { + if (velocityX > 0) { + // 向右滑动 + onSwipeRight(context); + } else { + // 向左滑动 + onSwipeLeft(context); + } + } + return true; + } + }); + return gestureDetector; + } + + /** + * 向右滑动时的处理 + * @param context 应用程序上下文 + */ + private void onSwipeRight(Context context) { + Log.d(TAG, "onSwipeRight called"); + + // 向右滑动时的处理逻辑 + // 例如,切换到上一条笔记 + int appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, NoteWidgetProvider.class))[0]; + Notes.Note note = Notes.getPreviousNote(context, appWidgetId); + if (note != null) { + update(context, AppWidgetManager.getInstance(context), new int[]{appWidgetId}); + } else { + Toast.makeText(context, "已经是第一条笔记", Toast.LENGTH_SHORT).show(); + } + } + + /** + * 向左滑动时的处理 + * @param context 应用程序上下文 + */ + private void onSwipeLeft(Context context) { + Log.d(TAG, "onSwipeLeft called"); + + // 向左滑动时的处理逻辑 + // 例如,切换到下一条笔记 + int appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, NoteWidgetProvider.class))[0]; + Notes.Note note = Notes.getNextNote(context, appWidgetId); + if (note != null) { + update(context, AppWidgetManager.getInstance(context), new int[]{appWidgetId}); + } else { + Toast.makeText(context, "已经是最后一条笔记", Toast.LENGTH_SHORT).show(); + } + } + + /** + * 获取小部件的点击事件监听器 + * @param context 应用程序上下文 + * @return 小部件的点击事件监听器 + */ + private View.OnClickListener getWidgetClickListener(Context context) { + return new View.OnClickListener() { + @Override + public void onClick(View v) { + // 处理小部件的点击事件 + // 例如,打开笔记编辑页面 + Intent intent = new Intent(context, NoteEditActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } + }; + } + + /** + * 设置小部件的点击事件监听器 + * @param context 应用程序上下文 + * @param appWidgetId 小部件ID + * @param clickListener 小部件的点击事件监听器 + */ + private void setWidgetClickListener(Context context, int appWidgetId, View.OnClickListener clickListener) { + // 获取远程视图对象 + RemoteViews views = new RemoteViews(context.getPackageName(), getLayoutId()); + + // 设置点击事件监听器 + views.setOnClickPendingIntent(R.id.widget_text, PendingIntent.getBroadcast(context, 0, new Intent(), 0)); + + // 更新小部件视图 + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); + appWidgetManager.updateAppWidget(appWidgetId, views); + } + + /** + * 获取小部件的长按事件监听器 + * @param context 应用程序上下文 + * @return 小部件的长按事件监听器 + */ + private View.OnLongClickListener getWidgetLongClickListener(Context context) { + return new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + // 处理小部件的长按事件 + // 例如,显示快捷菜单 + showShortcutMenu(context); + return true; + } + }; + } + + /** + * 显示快捷菜单 + * @param context 应用程序上下文 + */ + private void showShortcutMenu(Context context) { + // 创建快捷菜单 + PopupMenu popupMenu = new PopupMenu(context, null); + popupMenu.getMenuInflater().inflate(R.menu.widget_shortcut_menu, popupMenu.getMenu()); + popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_edit_note: + // 编辑笔记 + editNote(context, AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, NoteWidgetProvider.class))[0], Notes.getCurrentNoteId(context)); + break; + case R.id.menu_delete_note: + // 删除笔记 + deleteNote(context, Notes.getCurrentNoteId(context)); + break; + case R.id.menu_sync_notes: + // 同步笔记 + syncNotes(context); + break; + default: + break; + } + return true; + } + }); + + // 显示快捷菜单 + popupMenu.show(); + } + + /** + * 编辑笔记 + * @param context 应用程序上下文 + * @param appWidgetId 小部件ID + * @param noteId 笔记ID + */ + private void editNote(Context context, int appWidgetId, int noteId) { + // 获取笔记数据 + Notes.Note note = Notes.getNoteById(context, noteId); + if (note != null) { + // 显示编辑对话框 + showEditDialog(context, appWidgetId, note); + } else { + // 显示错误提示信息 + Toast.makeText(context, "笔记不存在", Toast.LENGTH_SHORT).show(); + } + } + + /** + * 显示编辑对话框 + * @param context 应用程序上下文 + * @param appWidgetId 小部件ID + * @param note 笔记数据 + */ + private void showEditDialog(Context context, int appWidgetId, Notes.Note note) { + // 创建编辑对话框 + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle("编辑笔记"); + View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_edit_note, null); + EditText editText = dialogView.findViewById(R.id.edit_text); + editText.setText(note.getContent()); + + builder.setView(dialogView); + builder.setPositiveButton("保存", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String newContent = editText.getText().toString(); + if (!newContent.isEmpty()) { + // 更新笔记数据 + note.setContent(newContent); + Notes.updateNote(context, note); + + // 更新小部件视图 + update(context, AppWidgetManager.getInstance(context), new int[]{appWidgetId}); + } else { + // 显示错误提示信息 + Toast.makeText(context, "笔记内容不能为空", Toast.LENGTH_SHORT).show(); + } + } + }); + builder.setNegativeButton("取消", null); + + // 显示对话框 + builder.show(); + } + + /** + * 删除笔记 + * @param context 应用程序上下文 + * @param noteId 笔记ID + */ + private void deleteNote(Context context, int noteId) { + // 删除笔记数据 + Notes.deleteNote(context, noteId); + + // 更新小部件视图 + int appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, NoteWidgetProvider.class))[0]; + update(context, AppWidgetManager.getInstance(context), new int[]{appWidgetId}); + + // 显示提示信息 + Toast.makeText(context, "笔记已删除", Toast.LENGTH_SHORT).show(); + } + + /** + * 同步笔记 + * @param context 应用程序上下文 + */ + private void syncNotes(Context context) { + // 获取数据同步的URL + SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE); + String syncUrl = prefs.getString(WIDGET_SYNC_URL_KEY, ""); + + if (!syncUrl.isEmpty()) { + // 启动数据同步服务 + Intent syncIntent = new Intent(context, NoteSyncService.class); + syncIntent.putExtra("sync_url", syncUrl); + context.startService(syncIntent); + } else { + // 显示错误提示信息 + Toast.makeText(context, "请设置数据同步的URL", Toast.LENGTH_SHORT).show(); + } + } +} \ No newline at end of file