diff --git a/widget/NoteWidgetProvider.java b/widget/NoteWidgetProvider.java new file mode 100644 index 0000000..ec6f819 --- /dev/null +++ b/widget/NoteWidgetProvider.java @@ -0,0 +1,132 @@ +/* + * 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; + +public abstract class NoteWidgetProvider extends AppWidgetProvider { + 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) { + 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])}); + } + } + + 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); + } + + 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 = 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]); + 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); + /** + * 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)); + 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); + } + } + } + + protected abstract int getBgResourceId(int bgId); + + protected abstract int getLayoutId(); + + protected abstract int getWidgetType(); +} diff --git a/widget/NoteWidgetProvider_2x.java b/widget/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..fdd043d --- /dev/null +++ b/widget/NoteWidgetProvider_2x.java @@ -0,0 +1,83 @@ +/* + * 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; + +/** + * NoteWidgetProvider_2x 是 NoteWidgetProvider 的具体实现类, + * 负责管理和更新 2x 尺寸的便签桌面小部件。 + * + * 2x 小部件通常指占用 2 列 x 2 行网格空间的桌面部件, + * 适合展示中等长度的便签内容。 + */ + + +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + /** + * 当小部件更新时调用此方法。 + * 继承自 AppWidgetProvider,在此实现中调用父类的 update 方法处理更新逻辑。 + * + * @param context 应用上下文 + * @param appWidgetManager AppWidgetManager 实例,用于管理小部件 + * @param appWidgetIds 需要更新的小部件 ID 数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的 update 方法处理小部件更新逻辑 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取 2x 小部件的布局资源 ID。 + * 此方法由父类抽象方法定义,返回适用于 2x 小部件的布局文件。 + * + * @return 布局资源 ID (R.layout.widget_2x) + */ + @Override + protected int getLayoutId() { + return R.layout.widget_2x; + } + + /** + * 根据背景 ID 获取对应的 2x 小部件背景资源。 + * 此方法由父类抽象方法定义,使用 ResourceParser 工具类获取适合 2x 小部件的背景图资源。 + * + * @param bgId 背景颜色 ID + * @return 对应的背景资源 ID + */ + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + /** + * 获取当前小部件的类型。 + * 此方法由父类抽象方法定义,返回标识 2x 小部件的类型常量。 + * + * @return 小部件类型常量 (Notes.TYPE_WIDGET_2X) + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; + } +} diff --git a/widget/NoteWidgetProvider_4x.java b/widget/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..c25cff2 --- /dev/null +++ b/widget/NoteWidgetProvider_4x.java @@ -0,0 +1,76 @@ +/* + * 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; + +/* + * NoteWidgetProvider_4x 是便签应用中 4x 尺寸桌面小部件的实现类。 + * 负责处理 4x 小部件的更新、布局和样式显示逻辑。 + * + * 4x 小部件通常占用 4 列 x 2 行的桌面空间,适合展示较长内容或更丰富的便签布局。 + */ +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + /* + * 当小部件更新时调用此方法。 + * 继承自 AppWidgetProvider,实现小部件的具体更新逻辑。 + * + * @param context 应用上下文 + * @param appWidgetManager AppWidgetManager 实例 + * @param appWidgetIds 需要更新的小部件 ID 数组 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的更新方法处理通用逻辑 + super.update(context, appWidgetManager, appWidgetIds); + } + + /* + * 获取 4x 小部件的布局资源 ID。 + * + * @return 返回 4x 小部件使用的布局文件资源 ID + */ + protected int getLayoutId() { + return R.layout.widget_4x; + } + + /* + * 根据背景颜色 ID 获取对应的 4x 小部件背景资源。 + * + * @param bgId 背景颜色 ID + * @return 返回对应的背景图片资源 ID + */ + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + /* + * 获取当前小部件的类型。 + * + * @return 返回标识 4x 小部件的类型常量 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; + } +}