dhc 8 months ago
parent 543278354c
commit 2d5aa76fff

@ -0,0 +1,57 @@
/*
* 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.
*/
// 包声明表明该类所属的包名为net.micode.notes.widget
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抽象类它很可能是用于实现特定规格从类名推测可能是4倍尺寸相关笔记小部件的具体功能逻辑
public class NoteWidgetProvider_4x extends NoteWidgetProvider {
// 重写父类的onUpdate方法该方法会在小部件需要进行更新时被系统调用
// 在这里它通过调用父类的update方法来执行具体的更新操作将实际的更新细节交给父类中定义的update逻辑去处理
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.update(context, appWidgetManager, appWidgetIds);
}
// 重写父类的抽象方法getLayoutId此方法的作用是返回该类型小部件所对应的布局资源ID
// 这里返回的是R.layout.widget_4x意味着该小部件在界面展示时会使用名为widget_4x的布局文件来进行布局设置
protected int getLayoutId() {
return R.layout.widget_4x;
}
// 重写父类的抽象方法getBgResourceId其功能是根据传入的背景颜色ID来获取对应的背景资源ID
// 通过调用ResourceParser.WidgetBgResources类中的静态方法getWidget4xBgResource来获取相应的背景资源ID
// 表明该4x类型小部件获取背景资源的方式与特定的4x相关逻辑紧密相关可能是适配4倍尺寸小部件的背景资源获取方式
@Override
protected int getBgResourceId(int bgId) {
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
}
// 重写父类的抽象方法getWidgetType用于返回该小部件的类型标识
// 返回值为Notes.TYPE_WIDGET_4X表示此小部件属于特定的4x类型具体该类型在Notes类中的定义决定了其确切含义和用途
@Override
protected int getWidgetType() {
return Notes.TYPE_WIDGET_4X;
}
}

@ -0,0 +1,166 @@
/*
* 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.
*/
// 包声明该类所在的包名为net.micode.notes.widget
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;
// 抽象的AppWidgetProvider子类用于为笔记相关的小部件提供功能支持
// 注意它是抽象类,意味着具体功能需要由子类去实现特定的抽象方法
public abstract class NoteWidgetProvider extends AppWidgetProvider {
// 定义查询数据库时使用的投影(即要查询的列),用于获取笔记小部件相关信息
// 包含笔记的ID、背景颜色ID以及内容摘要等列
public static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.BG_COLOR_ID,
NoteColumns.SNIPPET
};
// 定义列索引常量方便后续从游标Cursor中获取对应列的数据
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设置为无效值
// 这样表示对应的笔记不再与该小部件关联
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
ContentValues values = new ContentValues();
// 设置要更新到数据库的WIDGET_ID为无效值
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
for (int i = 0; i < appWidgetIds.length; i++) {
// 通过ContentResolver更新数据库中对应的记录
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
values,
NoteColumns.WIDGET_ID + "=?",
new String[] { String.valueOf(appWidgetIds[i])});
}
}
// 私有方法用于获取与指定小部件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);
}
// 用于更新小部件的方法调用了另一个重载的update方法并传入默认的隐私模式参数false
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++) {
// 检查小部件ID是否有效如果无效则跳过该小部件的更新
if (appWidgetIds[i]!= AppWidgetManager.INVALID_APPWIDGET_ID) {
int bgId = ResourceParser.getDefaultBgId(context);
String snippet = "";
// 创建一个启动NoteEditActivity的意图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());
// 获取与当前小部件ID相关的笔记信息游标
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
if (c!= null && c.moveToFirst()) {
// 如果查询到多条记录对应同一个小部件ID则记录错误日志并关闭游标返回不再继续更新
if (c.getCount() > 1) {
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
c.close();
return;
}
// 从游标中获取内容摘要、背景颜色ID以及笔记ID等信息并设置到相应变量和意图中
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());
// 设置小部件背景图片资源ID
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
// 根据隐私模式生成对应的PendingIntent用于处理小部件点击事件
PendingIntent pendingIntent = null;
if (privacyMode) {
// 如果是隐私模式设置特定的提示文本并创建启动NotesListActivity的PendingIntent
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 {
// 非隐私模式下设置内容摘要文本并创建基于前面创建的intent的PendingIntent
rv.setTextViewText(R.id.widget_text, snippet);
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
// 设置小部件文本的点击事件PendingIntent
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
// 通过AppWidgetManager更新小部件的显示内容
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
}
// 抽象方法由子类实现用于根据给定的背景颜色ID获取对应的背景资源ID
protected abstract int getBgResourceId(int bgId);
// 抽象方法由子类实现用于获取小部件对应的布局资源ID
protected abstract int getLayoutId();
// 抽象方法,由子类实现,用于获取小部件的类型
protected abstract int getWidgetType();
}

@ -0,0 +1,58 @@
/*
* 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.
*/
// 包声明表明该类所在的包名为net.micode.notes.widget
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抽象类用于实现特定类型可能是2倍尺寸之类从类名推测小部件的具体功能
public class NoteWidgetProvider_2x extends NoteWidgetProvider {
// 重写父类的onUpdate方法当小部件需要更新时会被调用
// 这里直接调用了父类的update方法来执行实际的更新逻辑将具体的更新操作委托给父类的相关实现
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.update(context, appWidgetManager, appWidgetIds);
}
// 重写父类的抽象方法getLayoutId用于返回该类型小部件对应的布局资源ID
// 这里返回的是R.layout.widget_2x意味着该小部件使用名为widget_2x的布局文件来进行界面展示
@Override
protected int getLayoutId() {
return R.layout.widget_2x;
}
// 重写父类的抽象方法getBgResourceId根据传入的背景颜色ID获取对应的背景资源ID
// 通过调用ResourceParser.WidgetBgResources类中的静态方法getWidget2xBgResource来获取相应资源ID
// 说明该小部件获取背景资源的方式与特定的2x相关逻辑有关可能是适配2倍尺寸小部件的背景资源
@Override
protected int getBgResourceId(int bgId) {
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
}
// 重写父类的抽象方法getWidgetType用于返回该小部件的类型标识
// 返回Notes.TYPE_WIDGET_2X表示该小部件属于特定的2x类型具体类型含义由Notes类中定义决定
@Override
protected int getWidgetType() {
return Notes.TYPE_WIDGET_2X;
}
}
Loading…
Cancel
Save