Android Launcher桌面图标显示数字

来源:互联网 发布:阿里云账号可以注销吗 编辑:程序博客网 时间:2024/04/27 03:35
之前一直以为是应用本身在对图标进行修改,看了源码之后发现其实主要的工作并不是应用自己完成的,主要的工作在是launcher里面完成的.

关于系统里面类似未读短信的具体处理流程如下:



 原理

一个应用要实现这个效果,就要在自己有未读的消息的时候发送一个广播告诉系统我有未处理的事件了(例如:短信,电话和邮件等),同时将相关的信息进行保存,比如应用的名称(这里指的是ComponentName)和未处理事件的数量.系统将提升用户有待处理的事件交给Launcher去处理,launcher会接收到对应的广播,不是随便一个应用都有这个待遇的,launcher接到广播之后会先解析数据,看当前的应用有没有这个待遇也就是是否支持显示这个待处理事件的通知.如果通过launcher就会调用相关方法去重绘应用的icon.
效果图:

实现过程分析

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

/** M: send new Calls broadcast to luancher to update unread icon @{ */    public static final void notifyNewCallsCount(SQLiteDatabase db, Context context) {        Cursor c = null;       …..        //send count=0 to clear the unread icon        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));        }    }

LauncherApplication.java(Launcher注册对应的广播接收器)

packages/apps/Launcher2/src/com/android/launcher2/LauncherApplication.java
public void onCreate() {       ........        /// M: register unread broadcast.        if (FeatureOption.MTK_LAUNCHER_UNREAD_SUPPORT) {            mUnreadLoader = new MTKUnreadLoader(getApplicationContext());            // Register unread change broadcast.            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) { //workspace 里面的快捷方式                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) { //all app 里面的应用icon                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);            /// M: Draw unread text.            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>


原文:http://862123204-qq-com.iteye.com/blog/2000310

0 0
原创粉丝点击