/* * 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 //这是一个抽象类NoteWidgetProvider,继承自AppWidgetProvider基类,重写了onDeleted和updateAppWidget方法。 // PROJECTION是一个常量数组,包含笔记的ID、背景颜色和摘要。COLUMN_ID、COLUMN_BG_COLOR_ID和COLUMN_SNIPPET是对应的下标。 // getNoteWidgetInfo方法根据WIDGET_ID从内容提供者查询笔记的信息,updateAppWidget方法设置了widget的背景颜色和文本内容, // 并更新widget。onDeleted方法在小部件被删除时调用,将widget_id字段设置为无效值(AppWidgetManager.INVALID_APPWIDGET_ID) // 并将该值更新到所有与此小部件相关的笔记中。 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); } //这段代码实现了一个更新 widget 的方法。具体来说,它接收四个参数:context、appWidgetManager、appWidgetIds 和 privacyMode。 // 其中,context 表示上下文环境,appWidgetManager 是管理 widget 的对象,appWidgetIds 是一个整数数组,表示需要更新的 widget 的 id 列表, // privacyMode 表示是否启用隐私模式。 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); } } } // 该方法使用 for 循环遍历 appWidgetIds 数组中的所有元素,对于每个元素,如果其值不等于 AppWidgetManager.INVALID_APPWIDGET_ID,则进一步处理。 //首先,调用 ResourceParser.getDefaultBgId(context) 方法获取默认的背景 id。然后,将 snippet 变量设为空字符串,并构建一个指向 NoteEditActivity 的 intent 对象,并设置其 FLAG_ACTIVITY_SINGLE_TOP 标志和一些附加信息,如 widget id 和 widget type。接下来,调用 getNoteWidgetInfo(context, appWidgetIds[i]) 方法获取与指定 widget 相关联的笔记信息。 // 如果获取到了一个有效的 Cursor 对象 c,并且该 Cursor 对象中至少包含一条数据,则进一步处理。如果 c 中包含多条记录,则输出一条错误日志并关闭 Cursor 对象;否则,从 c 中读取笔记信息,并将其设置为 snippet 变量的值。最后,利用 appWidgetManager 对象更新 widget 的内容。 protected abstract int getBgResourceId(int bgId); protected abstract int getLayoutId(); protected abstract int getWidgetType(); }