From 7c0c0df54bbc7773bf75f25d9ef438a024c9ae4b Mon Sep 17 00:00:00 2001 From: Oowisho <145649282+Oowisho@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:50:28 +0800 Subject: [PATCH] src --- src/BackupUtils.java | 417 +++++++++++++++++++++++++++++++++ src/MainActivity.java | 42 ++++ src/NoteWidgetProvider.java | 185 +++++++++++++++ src/NoteWidgetProvider_2x.java | 71 ++++++ src/NoteWidgetProvider_4x.java | 71 ++++++ 5 files changed, 786 insertions(+) create mode 100644 src/BackupUtils.java create mode 100644 src/MainActivity.java create mode 100644 src/NoteWidgetProvider.java create mode 100644 src/NoteWidgetProvider_2x.java create mode 100644 src/NoteWidgetProvider_4x.java diff --git a/src/BackupUtils.java b/src/BackupUtils.java new file mode 100644 index 0000000..05da13e --- /dev/null +++ b/src/BackupUtils.java @@ -0,0 +1,417 @@ +/* + * 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.tool; + +import android.content.Context; +import android.database.Cursor; +import android.os.Environment; +import android.text.TextUtils; +import android.text.format.DateFormat; +import android.util.Log; + +import net.micode.notes.R; +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 java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +/** + * 笔记数据备份工具类 + * 功能:将应用内的笔记数据导出为文本文件,支持文件夹和笔记内容的结构化导出 + * 设计模式:单例模式,确保全局唯一实例 + */ +public class BackupUtils { + private static final String TAG = "BackupUtils"; + // 单例实例引用 + private static BackupUtils sInstance; + + /** + * 获取单例实例 + * + * @param context 应用上下文,用于初始化内部组件 + * @return BackupUtils实例 + */ + public static synchronized BackupUtils getInstance(Context context) { + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + + /** + * 备份/恢复操作的状态码定义 + * 用于标识操作过程中的不同状态或错误类型 + */ + // SD卡未挂载 + public static final int STATE_SD_CARD_UNMOUONTED = 0; + // 备份文件不存在 + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; + // 数据格式损坏(可能被其他程序修改) + public static final int STATE_DATA_DESTROIED = 2; + // 系统运行时异常 + public static final int STATE_SYSTEM_ERROR = 3; + // 操作成功 + public static final int STATE_SUCCESS = 4; + + // 文本导出组件引用 + private TextExport mTextExport; + + /** + * 私有构造函数,确保单例模式 + * + * @param context 应用上下文 + */ + private BackupUtils(Context context) { + mTextExport = new TextExport(context); + } + + /** + * 检查外部存储(SD卡)是否可用 + * + * @return true-已挂载且可读写,false-不可用 + */ + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + /** + * 执行笔记数据导出为文本文件 + * + * @return 操作状态码(参考STATE_*常量) + */ + public int exportToText() { + return mTextExport.exportToText(); + } + + /** + * 获取导出的文本文件名 + * + * @return 文件名(含扩展名) + */ + public String getExportedTextFileName() { + return mTextExport.mFileName; + } + + /** + * 获取导出文件的存储目录 + * + * @return 目录路径字符串 + */ + public String getExportedTextFileDir() { + return mTextExport.mFileDirectory; + } + + /** + * 文本导出内部类 + * 负责具体的笔记数据导出逻辑,包括数据库查询和文件写入 + */ + private static class TextExport { + // 笔记查询的列投影(指定查询哪些字段) + private static final String[] NOTE_PROJECTION = { + NoteColumns.ID, // 笔记ID + NoteColumns.MODIFIED_DATE, // 最后修改时间 + NoteColumns.SNIPPET, // 笔记摘要(文件夹名称) + NoteColumns.TYPE // 笔记类型(普通笔记/文件夹) + }; + + // 笔记查询结果集中各列的索引 + private static final int NOTE_COLUMN_ID = 0; + private static final int NOTE_COLUMN_MODIFIED_DATE = 1; + private static final int NOTE_COLUMN_SNIPPET = 2; + + // 笔记数据(内容)查询的列投影 + private static final String[] DATA_PROJECTION = { + DataColumns.CONTENT, // 内容主体 + DataColumns.MIME_TYPE, // 内容类型(文本/通话记录等) + DataColumns.DATA1, // 扩展数据1(通话时间) + DataColumns.DATA2, // 扩展数据2 + DataColumns.DATA3, // 扩展数据3 + DataColumns.DATA4, // 扩展数据4(电话号码) + }; + + // 数据查询结果集中各列的索引 + private static final int DATA_COLUMN_CONTENT = 0; + private static final int DATA_COLUMN_MIME_TYPE = 1; + private static final int DATA_COLUMN_CALL_DATE = 2; + private static final int DATA_COLUMN_PHONE_NUMBER = 4; + + // 导出文本的格式模板(从资源文件获取) + private final String[] TEXT_FORMAT; + // 格式模板索引定义 + private static final int FORMAT_FOLDER_NAME = 0; // 文件夹名称格式 + private static final int FORMAT_NOTE_DATE = 1; // 笔记日期格式 + private static final int FORMAT_NOTE_CONTENT = 2; // 笔记内容格式 + + // 上下文、文件名、文件目录引用 + private Context mContext; + private String mFileName; + private String mFileDirectory; + + /** + * 构造函数 + * + * @param context 应用上下文 + */ + public TextExport(Context context) { + // 从资源文件获取导出格式模板 + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + mContext = context; + } + + /** + * 获取指定索引的格式模板 + * + * @param id 格式模板索引(参考FORMAT_*常量) + * @return 格式字符串 + */ + private String getFormat(int id) { + return TEXT_FORMAT[id]; + } + + /** + * 将指定文件夹及其包含的笔记导出到文本流 + * + * @param folderId 文件夹ID + * @param ps 打印流(用于写入文件) + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于该文件夹的所有笔记 + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { folderId }, null); + + if (notesCursor != null) { + if (notesCursor.moveToFirst()) { + do { + // 打印笔记最后修改日期(使用指定格式) + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // 获取当前笔记ID并导出其内容 + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + notesCursor.close(); + } + } + + /** + * 将指定笔记的内容导出到文本流 + * + * @param noteId 笔记ID + * @param ps 打印流(用于写入文件) + */ + private void exportNoteToText(String noteId, PrintStream ps) { + // 查询该笔记的具体内容数据 + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { noteId }, null); + + if (dataCursor != null) { + if (dataCursor.moveToFirst()) { + do { + String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + if (DataConstants.CALL_NOTE.equals(mimeType)) { + // 处理通话记录类型的笔记 + String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); + long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); + String location = dataCursor.getString(DATA_COLUMN_CONTENT); + + // 打印电话号码 + if (!TextUtils.isEmpty(phoneNumber)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); + } + // 打印通话时间 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat + .format(mContext.getString(R.string.format_datetime_mdhm), callDate))); + // 打印通话相关位置信息 + if (!TextUtils.isEmpty(location)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); + } + } else if (DataConstants.NOTE.equals(mimeType)) { + // 处理普通文本笔记 + String content = dataCursor.getString(DATA_COLUMN_CONTENT); + if (!TextUtils.isEmpty(content)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), content)); + } + } + } while (dataCursor.moveToNext()); + } + dataCursor.close(); + } + // 在笔记之间添加分隔符 + try { + ps.write(new byte[] { Character.LINE_SEPARATOR, Character.LETTER_NUMBER }); + } catch (IOException e) { + Log.e(TAG, e.toString()); + } + } + + /** + * 执行完整的笔记数据导出流程 + * + * @return 导出操作状态码(参考STATE_*常量) + */ + public int exportToText() { + // 检查SD卡是否可用 + if (!externalStorageAvailable()) { + Log.d(TAG, "Media was not mounted"); + return STATE_SD_CARD_UNMOUONTED; + } + + // 获取文件打印流 + PrintStream ps = getExportToTextPrintStream(); + if (ps == null) { + Log.e(TAG, "get print stream error"); + return STATE_SYSTEM_ERROR; + } + + // 第一步:导出所有文件夹及其笔记 + Cursor folderCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, + null, null); + + if (folderCursor != null) { + if (folderCursor.moveToFirst()) { + do { + // 打印文件夹名称(通话记录文件夹使用固定名称) + String folderName = ""; + if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { + folderName = mContext.getString(R.string.call_record_folder_name); + } else { + folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); + } + if (!TextUtils.isEmpty(folderName)) { + ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); + } + // 导出该文件夹下的所有笔记 + String folderId = folderCursor.getString(NOTE_COLUMN_ID); + exportFolderToText(folderId, ps); + } while (folderCursor.moveToNext()); + } + folderCursor.close(); + } + + // 第二步:导出根目录下的普通笔记 + Cursor noteCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + "=0", + null, null); + + if (noteCursor != null) { + if (noteCursor.moveToFirst()) { + do { + // 打印笔记修改日期 + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // 导出该笔记的内容 + String noteId = noteCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (noteCursor.moveToNext()); + } + noteCursor.close(); + } + // 关闭打印流 + ps.close(); + + return STATE_SUCCESS; + } + + /** + * 获取用于导出文本的打印流 + * + * @return 打印流对象,失败时返回null + */ + private PrintStream getExportToTextPrintStream() { + // 生成SD卡上的目标文件 + File file = generateFileMountedOnSDcard(mContext, R.string.file_path, + R.string.file_name_txt_format); + if (file == null) { + Log.e(TAG, "create file to exported failed"); + return null; + } + // 记录文件名和目录 + mFileName = file.getName(); + mFileDirectory = mContext.getString(R.string.file_path); + + // 创建打印流 + PrintStream ps = null; + try { + FileOutputStream fos = new FileOutputStream(file); + ps = new PrintStream(fos); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + return ps; + } + } + + /** + * 在SD卡上生成用于存储导出数据的文件 + * + * @param context 应用上下文 + * @param filePathResId 目录路径资源ID + * @param fileNameFormatResId 文件名格式资源ID + * @return 生成的文件对象,失败时返回null + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + // 构建文件路径(SD卡根目录+应用自定义路径) + StringBuilder sb = new StringBuilder(); + sb.append(Environment.getExternalStorageDirectory()); + sb.append(context.getString(filePathResId)); + File filedir = new File(sb.toString()); + + // 构建文件名(含日期后缀) + sb.append(context.getString( + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), + System.currentTimeMillis()))); + File file = new File(sb.toString()); + + // 创建目录和文件(如果不存在) + try { + if (!filedir.exists()) { + filedir.mkdir(); + } + if (!file.exists()) { + file.createNewFile(); + } + return file; + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } +} \ No newline at end of file diff --git a/src/MainActivity.java b/src/MainActivity.java new file mode 100644 index 0000000..7bb128f --- /dev/null +++ b/src/MainActivity.java @@ -0,0 +1,42 @@ +package net.micode.notes; + +import android.os.Bundle; + +import androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +/** + * 应用的主活动类,负责设置全屏沉浸体验并处理窗口安全区域 + * 通过使用 EdgeToEdge API 和 WindowInsets 实现状态栏和导航栏的透明效果 + */ +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // 启用边缘到边缘显示模式(全屏沉浸效果) + // 这会使内容延伸到状态栏和导航栏下方 + EdgeToEdge.enable(this); + + // 设置活动的布局文件 + setContentView(R.layout.activity_main); + + // 为主要内容视图设置窗口安全区域的处理逻辑 + // 确保内容不会被状态栏、导航栏或其他系统UI元素遮挡 + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + // 获取系统栏(状态栏和导航栏)的安全区域内边距 + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + + // 为视图应用内边距,确保内容在安全区域内显示 + // 这样可以避免内容被系统UI遮挡,同时保持全屏沉浸的视觉效果 + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + + // 返回处理后的窗口插入,传递给后续的处理程序 + return insets; + }); + } +} \ No newline at end of file diff --git a/src/NoteWidgetProvider.java b/src/NoteWidgetProvider.java new file mode 100644 index 0000000..ef74b97 --- /dev/null +++ b/src/NoteWidgetProvider.java @@ -0,0 +1,185 @@ +/* + * 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; + +/** + * 便签桌面小部件的基类,提供通用功能实现 + 部件(如 1x1、4x1 尺寸 + + bstract class NoteWidgetProvider extends AppWidgetProvider { + // 查询便签信息的列投影(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"; + + /** + * 当小部件被删除时调用 + * - 将对应便签的 widget_id 重置为 INVALID_APPWIDGET_ID + * - 防止数据库中残留无效的小部件关联 + */ + @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])}); + } + } + + /** + * 查询与小部件关联的便签信息 + * - 过滤条件:widget_id 匹配且便签不在回收站 + * - 返回 Cursor 用于读取便签数据(ID、背景色、摘要) + */ + 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 i = 0; i < appWidgetIds.length; i++) { + if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_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()); + + // 查询关联便签信息 + Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); + if (c != null && c.moveToFirst()) { + // 检查是否存在多个相同 widget_id 的便签(异常情况) + 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); + + /** + * 设置小部件点击行为 + * - 隐私模式:显示提示文本,点击跳转到便签列表 + * - 普通模式:显示便签内容,点击跳转到编辑界面 + */ + 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); + } + } + } + + /** + * 获取背景资源 ID(子类必须实现) + * @param bgId 背景色 ID + * @return 对应的 Drawable 资源 ID + */ + protected abstract int getBgResourceId(int bgId); + + /** + * 获取小部件布局 ID(子类必须实现) + * @return 布局资源 ID(如 R.layout.widget_1x1) + */ + protected abstract int getLayoutId(); + + /** + * 获取小部件类型(子类必须实现) + * @return 小部件类型常量(如 Notes.TYPE_WIDGET_1X1) + */ + protected abstract int getWidgetType(); +} \ No newline at end of file diff --git a/src/NoteWidgetProvider_2x.java b/src/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..d11c2fe --- /dev/null +++ b/src/NoteWidgetProvider_2x.java @@ -0,0 +1,71 @@ +/* + * 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; + + * 2x 尺寸便签桌面小部件的具体实现类 + * - 继承自 NoteWidgetProvider 基类 + * - 提供 2x 尺寸小部件的布局、背景和类型定义 + */ +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + + /** + * 响应小部件更新事件 + * - 调用父类的 update 方法处理内容更新逻辑 + * - 保持与基类统一的更新机制 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取 2x 尺寸小部件的布局资源 ID + * - 返回 R.layout.widget_2x 布局文件 + * - 定义小部件的界面结构和元素位置 + */ + @Override + protected int getLayoutId() { + return R.layout.widget_2x; + } + + /** + * 获取 2x 尺寸小部件的背景资源 ID + * - 根据背景色 ID (bgId) 从资源解析器获取对应背景图 + * - 支持不同主题的背景样式(如浅色、深色、彩色) + */ + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + /** + * 获取小部件类型标识 + * - 返回 Notes.TYPE_WIDGET_2X (值为 2) + * - 用于在数据库中区分不同尺寸的小部件 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; + } +} \ No newline at end of file diff --git a/src/NoteWidgetProvider_4x.java b/src/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..76c037d --- /dev/null +++ b/src/NoteWidgetProvider_4x.java @@ -0,0 +1,71 @@ +/* + * 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; + + * 4x 尺寸便签桌面小部件的具体实现类 + * - 继承自 NoteWidgetProvider 基类 + * - 提供 4x 尺寸小部件的布局、背景和类型定义 + */ +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + + /** + * 响应小部件更新事件 + * - 调用父类的 update 方法处理内容更新逻辑 + * - 保持与基类统一的更新机制 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取 4x 尺寸小部件的布局资源 ID + * - 返回 R.layout.widget_4x 布局文件 + * - 定义更大尺寸的便签显示界面 + */ + @Override + protected int getLayoutId() { + return R.layout.widget_4x; + } + + /** + * 获取 4x 尺寸小部件的背景资源 ID + * - 根据背景色 ID (bgId) 从资源解析器获取对应背景图 + * - 适配更大尺寸的便签背景显示 + */ + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + /** + * 获取小部件类型标识 + * - 返回 Notes.TYPE_WIDGET_4X (值为 4) + * - 用于在数据库中区分不同尺寸的小部件 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; + } +} \ No newline at end of file