效果图: 实现过程分析
Intent.java(系统增加3个常量)
frameworks/base/core/java/android/content/Intent.java
- public static final String MTK_ACTION_UNREAD_CHANGED = "com.mediatek.action.UNREAD_CHANGED";
- public static final String MTK_EXTRA_UNREAD_COMPONENT = "com.mediatek.intent.extra.UNREAD_COMPONENT";
- public static final String MTK_EXTRA_UNREAD_NUMBER = "com.mediatek.intent.extra.UNREAD_NUMBER";
-
系统里面有这个功能几个应用相关的代码路径
日历 packages/apps/Calendar/src/com/mediatek/calendar/MTKUtils.java
邮件 :packages/apps/Email/src/com/android/email/NotificationController.java
电话 :packages/providers/ContactsProvider/src/com/android/providers/contacts/CallLogProvider.java
短信 :packages/providers/TelephonyProvider/src/com/android/providers/telephony/MmsSmsProvider.java
这里以电话为例:
假设当前有人打电话进来,系统会保存改记录到数据库,根据事件是否已经处理来判断是否发送广播
CallLogProvider.java(发送有待处理事件的广播)
packages/providers/ContactsProvider/src/com/android/providers/contacts/CallLogProvider.java
- public static final void notifyNewCallsCount(SQLiteDatabase db, Context context) {
- Cursor c = null;
- …..
-
- if (newCallsCount >= 0) {
- Intent newIntent = new Intent(Intent.MTK_ACTION_UNREAD_CHANGED);
- newIntent.putExtra(Intent.MTK_EXTRA_UNREAD_NUMBER, newCallsCount);
- newIntent.putExtra(Intent.MTK_EXTRA_UNREAD_COMPONENT, new ComponentName(Constants.CONTACTS_PACKAGE,
- Constants.CONTACTS_DIALTACTS_ACTIVITY));
- context.sendBroadcast(newIntent);
- android.provider.Settings.System.putInt(context.getContentResolver(), Constants.CONTACTS_UNREAD_KEY, Integer
- .valueOf(newCallsCount));
- }
- }
MTKUnreadLoader.java(Launcher接收到应用发送的广播,进行判断改应用是否可以显示有未处理事件的图标)
packages/apps/Launcher2/src/com/android/launcher2/MTKUnreadLoader.java
- public void onReceive(final Context context, final Intent intent) {
- final String action = intent.getAction();
-
- if (Intent.MTK_ACTION_UNREAD_CHANGED.equals(action)) {
- final ComponentName componentName = (ComponentName) intent.getExtra(Intent.MTK_EXTRA_UNREAD_COMPONENT);
- final int unreadNum = intent.getIntExtra(Intent.MTK_EXTRA_UNREAD_NUMBER, -1);
-
- if (mCallbacks != null && componentName != null && unreadNum != -1) {
-
- final int index = supportUnreadFeature(componentName);
- if (index >= 0) {
- boolean ret = setUnreadNumberAt(index, unreadNum);
- if (ret) {
- final UnreadCallbacks callbacks = mCallbacks.get();
- if (callbacks != null) {
- callbacks.bindComponentUnreadChanged(componentName, unreadNum);
- }
- .........
- }
LauncherApplication.java(Launcher注册对应的广播接收器)
packages/apps/Launcher2/src/com/android/launcher2/LauncherApplication.java
- public void onCreate() {
- ........
-
- if (FeatureOption.MTK_LAUNCHER_UNREAD_SUPPORT) {
- mUnreadLoader = new MTKUnreadLoader(getApplicationContext());
-
- filter = new IntentFilter();
- filter.addAction(Intent.MTK_ACTION_UNREAD_CHANGED);
- registerReceiver(mUnreadLoader, filter);
- }
- ..............
- }
MTKUnreadLoader.java(处理应用的图标显示未处理事件的数字)
packages/apps/Launcher2/src/com/android/launcher2/MTKUnreadLoader.java
- static void drawUnreadEventIfNeed(Canvas canvas, View icon) {
- ItemInfo info = (ItemInfo)icon.getTag();
- if (info != null && info.unreadNum > 0) {
- Resources res = icon.getContext().getResources();
- ..........
- if (info.unreadNum > Launcher.MAX_UNREAD_COUNT) {
- unreadTextNumber = String.valueOf(Launcher.MAX_UNREAD_COUNT);
- unreadTextPlusPaint.getTextBounds(unreadTextPlus, 0, unreadTextPlus.length(), unreadTextPlusBounds);
- } else {
- unreadTextNumber = String.valueOf(info.unreadNum);
- }
- unreadTextNumberPaint.getTextBounds(unreadTextNumber, 0, unreadTextNumber.length(), unreadTextNumberBounds);
- int textHeight = unreadTextNumberBounds.height();
- int textWidth = unreadTextNumberBounds.width() + unreadTextPlusBounds.width();
-
-
- NinePatchDrawable unreadBgNinePatchDrawable = (NinePatchDrawable)res.getDrawable(R.drawable.ic_newevents_numberindication);
- .........
- Rect unreadBgBounds = new Rect(0, 0, unreadBgWidth, unreadBgHeight);
- unreadBgNinePatchDrawable.setBounds(unreadBgBounds);
-
- int unreadMarginTop = 0;
- int unreadMarginRight = 0;
- if (info instanceof ShortcutInfo) {
- if (info.container == (long)LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
- unreadMarginTop = (int)res.getDimension(R.dimen.hotseat_unread_margin_top);
- unreadMarginRight = (int)res.getDimension(R.dimen.hotseat_unread_margin_right);
- } else if (info.container == (long)LauncherSettings.Favorites.CONTAINER_DESKTOP) {
- unreadMarginTop = (int)res.getDimension(R.dimen.workspace_unread_margin_top);
- unreadMarginRight = (int)res.getDimension(R.dimen.workspace_unread_margin_right);
- } else {
- unreadMarginTop = (int)res.getDimension(R.dimen.folder_unread_margin_top);
- unreadMarginRight = (int)res.getDimension(R.dimen.folder_unread_margin_right);
- }
- } else if (info instanceof FolderInfo) {
- if (info.container == (long)LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
- unreadMarginTop = (int)res.getDimension(R.dimen.hotseat_unread_margin_top);
- unreadMarginRight = (int)res.getDimension(R.dimen.hotseat_unread_margin_right);
- } else if (info.container == (long)LauncherSettings.Favorites.CONTAINER_DESKTOP) {
- unreadMarginTop = (int)res.getDimension(R.dimen.workspace_unread_margin_top);
- unreadMarginRight = (int)res.getDimension(R.dimen.workspace_unread_margin_right);
- }
- }
- else if (info instanceof ApplicationInfo) {
- unreadMarginTop = (int)res.getDimension(R.dimen.app_list_unread_margin_top);
- unreadMarginRight = (int)res.getDimension(R.dimen.app_list_unread_margin_right);
- }
-
- int unreadBgPosX = icon.getScrollX() + icon.getWidth() - unreadBgWidth - unreadMarginRight;
- int unreadBgPosY = icon.getScrollY() + unreadMarginTop;
-
- canvas.save();
- canvas.translate(unreadBgPosX, unreadBgPosY);
-
- unreadBgNinePatchDrawable.draw(canvas);
-
-
- Paint.FontMetrics fontMetrics = unreadTextNumberPaint.getFontMetrics();
- if (info.unreadNum > Launcher.MAX_UNREAD_COUNT) {
- canvas.drawText(unreadTextNumber,
- (unreadBgWidth - unreadTextPlusBounds.width()) / 2,
- (unreadBgHeight + textHeight) / 2,
- unreadTextNumberPaint);
- canvas.drawText(unreadTextPlus,
- (unreadBgWidth + unreadTextNumberBounds.width()) / 2,
- (unreadBgHeight + textHeight) / 2 + fontMetrics.ascent / 2,
- unreadTextPlusPaint);
- } else {
- .....
- }
- }
unread_support_shortcuts.xml(配置哪些应用可以显示待处理的事件)
packages/apps/Launcher2/res/xml/unread_support_shortcuts.xml
- <unreadshortcuts xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher">
- <shortcut
- launcher:unreadPackageName="com.android.contacts"
- launcher:unreadClassName="com.android.contacts.activities.DialtactsActivity"
- launcher:unreadType="0"
- launcher:unreadKey="com_android_contacts_mtk_unread"
- />
- <shortcut
- launcher:unreadPackageName="com.android.mms"
- launcher:unreadClassName="com.android.mms.ui.BootActivity"
- launcher:unreadType="0"
- launcher:unreadKey="com_android_mms_mtk_unread"
- />
- <shortcut
- launcher:unreadPackageName="com.android.email"
- launcher:unreadClassName="com.android.email.activity.Welcome"
- launcher:unreadType="0"
- launcher:unreadKey="com_android_email_mtk_unread"
- />
- ................
- </unreadshortcuts>