diff --git a/src/test.txt b/src/test.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/widget-provider.txt b/src/widget-provider.txt new file mode 100644 index 0000000..81ed6ae --- /dev/null +++ b/src/widget-provider.txt @@ -0,0 +1,156 @@ +/* + * 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. + */ +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区(www.micode.net) + * + * 根据 Apache 许可证 2.0 版本("许可证")获得许可; + * 除非符合许可证,否则您不得使用此文件。 + * 您可以在以下网址获取许可证的副本 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,否则根据许可证分发的软件 + * 是基于"按原样"的基础分发的,不提供任何形式的担保或条件, + * 无论是明示的还是暗示的。请参阅许可证以获取特定的语言权限 + * 和限制。 + */ +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; + +// 这个类是 NoteWidgetProvider 的一个抽象基类。它扩展了 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 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 并设置其属性。 + 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)); + // 创建一个待定意图,以在隐私模式下启动 NotesListActivity。 + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( + context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); + } else { + // 使用笔记片段设置文本。 + rv.setTextViewText(R.id.widget_text, snippet); + // 创建一个待定意图,以启动 NoteEditActivity 以编辑笔记。 + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + // 设置小部件文本的点击行为。 + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); + // 更新小部件实例。 + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); + } + } + } + // 抽象方法,根据背景 ID 获取背景图片资源的资源 ID。 + protected abstract int getBgResourceId(int bgId); + // 抽象方法,获取小部件的布局 ID。 + protected abstract int getLayoutId(); + // 抽象方法,获取小部件的类型。 + protected abstract int getWidgetType(); +} diff --git a/src/widget-provider_2X.txt b/src/widget-provider_2X.txt new file mode 100644 index 0000000..1f2c6a8 --- /dev/null +++ b/src/widget-provider_2X.txt @@ -0,0 +1,68 @@ +/* + * 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. + */ + +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区(www.micode.net) + * + * 根据 Apache 许可证 2.0 版本("许可证")获得许可; + * 除非符合许可证,否则您不得使用此文件。 + * 您可以在以下网址获取许可证的副本 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,否则根据许可证分发的软件 + * 是基于"按原样"的基础分发的,不提供任何形式的担保或条件, + * 无论是明示的还是暗示的。请参阅许可证以获取特定的语言权限 + * 和限制。 + */ +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 小部件。 + +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + // 当小部件需要更新时调用此方法。 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.update(context, appWidgetManager, appWidgetIds); + } + // 获取 2x 小部件的布局 ID。 + @Override + protected int getLayoutId() { + return R.layout.widget_2x; + } + // 根据背景 ID 获取 2x 小部件背景图片资源的资源 ID。 + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + // 获取小部件的类型,这里表示 2x 小部件。 + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; + } +} +//NoteWidgetProvider_2x 类继承自 NoteWidgetProvider,它是处理 2x 小部件的具体实现。 +//onUpdate 方法在小部件需要更新时被调用,它调用了基类的 update 方法来执行小部件的更新。 +//getLayoutId 方法返回 2x 小部件的布局资源 ID。 +//getBgResourceId 方法根据给定的背景 ID 获取 2x 小部件背景图片资源的资源 ID。 +//getWidgetType 方法返回小部件的类型,这里表示 2x 小部件。 \ No newline at end of file diff --git a/src/widget-provider_4X.txt b/src/widget-provider_4X.txt new file mode 100644 index 0000000..c132928 --- /dev/null +++ b/src/widget-provider_4X.txt @@ -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. + */ + +/* + * 版权所有 (c) 2010-2011,MiCode 开源社区(www.micode.net) + * + * 根据 Apache 许可证 2.0 版本("许可证")获得许可; + * 除非符合许可证,否则您不得使用此文件。 + * 您可以在以下网址获取许可证的副本 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,否则根据许可证分发的软件 + * 是基于"按原样"的基础分发的,不提供任何形式的担保或条件, + * 无论是明示的还是暗示的。请参阅许可证以获取特定的语言权限 + * 和限制。 + */ +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 是 NoteWidgetProvider 的子类,用于处理 4x 小部件。 +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + + // 当小部件需要更新时调用此方法。 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用基类的 update 方法以更新小部件。 + super.update(context, appWidgetManager, appWidgetIds); + } + + // 获取 4x 小部件的布局 ID。 + protected int getLayoutId() { + return R.layout.widget_4x; + } + + // 根据背景 ID 获取 4x 小部件背景图片资源的资源 ID。 + @Override + protected int getBgResourceId(int bgId) { + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + // 获取小部件的类型,这里表示 4x 小部件。 + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; + } +} + //NoteWidgetProvider_4x 类继承自 NoteWidgetProvider,它是处理 4x 小部件的具体实现。 + //onUpdate 方法在小部件需要更新时被调用,它调用了基类的 update 方法来执行小部件的更新。 + //getLayoutId 方法返回 4x 小部件的布局资源 ID。 + //getBgResourceId 方法根据给定的背景 ID 获取 4x 小部件背景图片资源的资源 ID。 + //getWidgetType 方法返回小部件的类型,这里表示 4x 小部件。 \ No newline at end of file