如何在android4.4上添加未读信息,未接来电或日历新事件提醒等功能,android4.2上
很多商家都已实现,其实将其移植过来就可以了,不过4.4有许多类变化了,要稍作修
改,下面将列出要修改哪些类?添加些什么方法?
详细修改如下:
1.首先准备各种大小的ic_newevents_numberindication.9.png图片,这个自己去找吧
,如果你是搞开发的,在以前的项目中都可以找到的。
2.定义个未读信息支持的app事件xml,我这个支持短信,日历,电话,邮件:
b/packages/apps/Launcher3/res/xml/unread_support_shortcuts.xml
内容如下:
+
+
+
+
+ launcher:unreadClassName="com.android.dialer.DialtactsActivity"
+ launcher:unreadType="0"
+ launcher:unreadKey="com_android_contacts_unread"
+ />
+
+ launcher:unreadClassName="com.android.mms.ui.BootActivity"
+ launcher:unreadType="0"
+ launcher:unreadKey="com_android_mms_unread"
+ />
+
+ launcher:unreadClassName="com.android.email.activity.Welcome"
+ launcher:unreadType="0"
+ launcher:unreadKey="com_android_email_unread"
+ />
+
+ launcher:unreadClassName="com.android.calendar.AllInOneActivity"
+ launcher:unreadType="0"
+ launcher:unreadKey="com_android_calendar_unread"
+ />
+
+
launcher:unreadClassName="com.mediatek.rcse.activities.ChatMainActivity"
+ launcher:unreadType="0"
+ launcher:unreadKey="com_android_rcse_unread"
+ />
+
+
launcher:unreadClassName="com.android.cellbroadcastreceiver.CellBroadcastLis
tActivity"
+ launcher:unreadType="0"
+ launcher:unreadKey="com_android_calendar_unread"
+ />
+
+
launcher:unreadClassName="com.mediatek.cellbroadcastreceiver.CellBroadcastLi
stActivity"
+ launcher:unreadType="0"
+ launcher:unreadKey="com_mediatek_cellbroadcastreceiver_unread"
+ />
+
3.添加几种需要用到而之前没有的权限:
b/packages/apps/Launcher3/AndroidManifest.xml android:name="android.permission.ADVANCED_WIDGET_API"/> 4.定义要用的字符串:b/packages/apps/Launcher3/res/values/attrs.xml 5.定义显示的数字大小尺寸,这个可以自己修改到满意,这个是四行五列排列的 1080*1920屏幕大小的尺寸。 package com.android.launcher3; import android.content.BroadcastReceiver; import com.android.internal.util.XmlUtils; import com.mediatek.launcher3.ext.LauncherLog; import org.xmlpull.v1.XmlPullParser; import java.io.IOException; class UnreadSupportShortcut { keyString, int type) { ComponentName mComponent; @Override ",type = " /** things,: number, update folders in view. private static final ArrayList UNREAD_SUPPORT_SHORTCUTS = new ArrayList private static int sUnreadSupportShortcutsNum = 0; private Context mContext; private WeakReference public UnreadLoader(Context context) { @Override (Intent.XXX_EXTRA_UNREAD_NUMBER, -1); = " + componentName mCallbacks if (mCallbacks != null && componentName != null && unreadNum != -1) { (componentName, unreadNum); /** mCallbacks = " + mCallbacks); /** @Override /** android.provider.Settings.System.getInt(cr, shortcut.mKey); + shortcut.mKey SettingNotFoundException key = " getUnreadSupportShortcutInfo()); private void loadUnreadSupportShortcuts() { + start); // Clear all previous parsed unread shortcuts. try { final int depth = parser.getDepth(); int type = -1; parser.getDepth() > depth) if (type != XmlPullParser.START_TAG) { TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.UnreadShortcut); (R.styleable.UnreadShortcut_unreadPackageName), a (R.styleable.UnreadShortcut_unreadClassName), a (R.styleable.UnreadShortcut_unreadKey), a.getInt( } unread shortcuts.", e); shortcuts.", e); " ",sUnreadSupportShortcutsNum = " getUnreadSupportShortcutInfo()); /** stored lock /** component); final int size = UNREAD_SUPPORT_SHORTCUTS.size(); (component)) { return -1; /** number. { ",unreadNum = " + unreadNum { /** supported /** /** /// M: Meature sufficent width for unread text and background image (R.dimen.unread_text_number_size)); Paint unreadTextPlusPaint = new Paint(unreadTextNumberPaint); (R.dimen.unread_text_plus_size)); String unreadTextNumber; (Launcher.MAX_UNREAD_COUNT); unreadTextPlus.length(), unreadTextPlusBounds); unreadTextNumber.length(), unreadTextNumberBounds); unreadTextPlusBounds.width(); /// M: Draw unread background image. (NinePatchDrawable) res.getDrawable (R.drawable.ic_newevents_numberindication); (); unreadBgNinePatchDrawable.getIntrinsicHeight(); int unreadMinWidth = (int) res.getDimension (R.dimen.unread_minWidth); (R.dimen.unread_text_margin); unreadBgHeight); int unreadMarginTop = 0; LauncherSettings.Favorites.CONTAINER_HOTSEAT) { (R.dimen.hotseat_unread_margin_top); (R.dimen.hotseat_unread_margin_right); LauncherSettings.Favorites.CONTAINER_DESKTOP) { (R.dimen.workspace_unread_margin_top); (R.dimen.workspace_unread_margin_right); (R.dimen.folder_unread_margin_top); (R.dimen.folder_unread_margin_right); LauncherSettings.Favorites.CONTAINER_HOTSEAT) { (R.dimen.hotseat_unread_margin_top); (R.dimen.hotseat_unread_margin_right); LauncherSettings.Favorites.CONTAINER_DESKTOP) { (R.dimen.workspace_unread_margin_top); (R.dimen.workspace_unread_margin_right); (R.dimen.app_list_unread_margin_top); (R.dimen.app_list_unread_margin_right); int unreadBgPosX = icon.getScrollX() + icon.getWidth() - unreadBgWidth - unreadMarginRight; canvas.save(); unreadBgNinePatchDrawable.draw(canvas); /// M: Draw unread text. unreadTextNumberPaint.getFontMetrics(); ()) / 2, unreadTextNumberBounds.width()) / 2, fontMetrics.ascent / 2, canvas.restore(); public interface UnreadCallbacks { and unreadNum); /** used to PageViewCellLayout,不过这里要改为AppInfo和AppsCustomizeCellLayout: 在 AppsCustomizePagedView.java中增加 paged view number. paged unreadNum) { component mNumAppsPages); getPageAt(i); = " + component appIcon = " + appIcon); (component)) { + /** UnreadLoader. + mNumAppsPages); getPageAt(i); UnreadLoader.getUnreadNumberOfComponent(appInfo.componentName); + ", appInfo = " appInfo.unreadNum); + /** unreadNum) { + /** UnreadLoader. apps) { (appInfo.componentName); 8.为文件夹添加未读事件 mInfo); mContent.getChildAt(i, j); 9.定义未读变量:ItemInfo.java类中 串可能不一样,XXX是平台名,这个字符串之后也一样的换成自己的就好了 final int unreadNum) { component component = " + component start); unreadNum); (component, unreadNum); current time = " + /** mBindingWorkspaceFinished = " Thread.currentThread()); + /** start = " + start); current time = " + /** + start); time = " 11.接下来在LauncherAppState.java中添加一些方法,主要是注册广播,4.2上没这个 类,是在LauncherApplication.java中添加,这是4.4上新增的,基本上代替了 LauncherApplication.java 在相应的地方注册广播 hotseat. this); getAllShortcutAndWidgetContainers(); tag = " + tag + ", j = " intent.getComponent(); MTKUnreadLoader.getUnreadNumberOfComponent(componentName); + /** hotseat unreadNum) { + component getAllShortcutAndWidgetContainers(); component = " + component view); intent.getComponent(); find component = " ",componentName = " + componentName); (component)) { find component = " + ",cellX = " unreadNum); 2)其次在BubbleTextView.java类中draw方法中的 (android.R.color.transparent))改条件中增加: 3)然后在FolderIcon.java中也要画: 在onAdd(ShortcutInfo item)方法中 update 在onRemove(ShortcutInfo item)方法中添加 to update 不过还得写两个方法: value. unreadNum + ", mInfo = " + mInfo + /** unread number once. ArrayList (componentName); (components.get(j))) { unreadNumTotal = " + unreadNumTotal components.size()); = " + unreadNumTotal); + /** information. unreadNum) { ArrayList unreadNumTotal = " + unreadNumTotal components.size()); = " + unreadNumTotal); 唉终于是添加完了,好多内容啊,自己做的时候没发现,原理添加了这么多东西,难怪 花了我两天时间。接下来编译就好了,希望各位能一次通过,我当初可是出了七十多个 errors呢!
+
+
+
+
+
+
+
+
+
+
+
b/packages/apps/Launcher3/res/values/dimens.xml
+
+
+
+
+
+
+
+
+
+
+
+
+
6.接下来就是写一个类了,这里命名为UnreadLoader.java:
b/packages/apps/Launcher3/src/com/android/launcher3/UnreadLoader.java
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.Canvas;
import android.graphics.drawable.NinePatchDrawable;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.SuperscriptSpan;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.View;
import com.android.launcher3.R;
import org.xmlpull.v1.XmlPullParserException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
public UnreadSupportShortcut(String pkgName, String clsName, String
mComponent = new ComponentName(pkgName, clsName);
mKey = keyString;
mShortcutType = type;
mUnreadNum = 0;
}
String mKey;
int mShortcutType;
int mUnreadNum;
public String toString() {
return "{UnreadSupportShortcut[" + mComponent + "], key = " + mKey +
+ mShortcutType + ",unreadNum = " + mUnreadNum + "}";
}
}
* M: This class is a util class, implemented to do the following two
*
* 1.Read config xml to get the shortcuts which support displaying unread
* then get the initial value of the unread number of each component and
* shortcuts and folders through callbacks implemented in Launcher.
*
* 2. Receive unread broadcast sent by application, update shortcuts and
* workspace, hot seat and update application icons in app customize paged
*/
public class UnreadLoader extends BroadcastReceiver {
private static final String TAG = "UnreadLoader";
private static final String TAG_UNREADSHORTCUTS = "unreadshortcuts";
private static final Object LOG_LOCK = new Object();
mContext = context;
}
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if (Intent.XXX_ACTION_UNREAD_CHANGED.equals(action)) {
final ComponentName componentName = (ComponentName) intent
.getExtra(Intent.XXX_EXTRA_UNREAD_COMPONENT);
final int unreadNum = intent.getIntExtra
if (LauncherLog.DEBUG) {
LauncherLog.d(TAG, "Receive unread broadcast: componentName
+ ", unreadNum = " + unreadNum + ", mCallbacks = " +
+ getUnreadSupportShortcutInfo());
}
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
}
}
}
}
}
}
* Set this as the current Launcher activity object for the loader.
*/
public void initialize(UnreadCallbacks callbacks) {
mCallbacks = new WeakReference
if (LauncherLog.DEBUG_UNREAD) {
LauncherLog.d(TAG, "initialize: callbacks = " + callbacks + ",
}
}
* Load and initialize unread shortcuts.
*
* @param context
*/
void loadAndInitUnreadShortcuts() {
new AsyncTask
@Override
protected Void doInBackground(Void... unused) {
loadUnreadSupportShortcuts();
initUnreadNumberFromSystem();
return null;
}
protected void onPostExecute(final Void result) {
if (mCallbacks != null) {
UnreadCallbacks callbacks = mCallbacks.get();
if (callbacks != null) {
callbacks.bindUnreadInfoIfNeeded();
}
}
}
}.execute();
}
* Initialize unread number by querying system settings provider.
*
* @param context
*/
private void initUnreadNumberFromSystem() {
final ContentResolver cr = mContext.getContentResolver();
final int shortcutsNum = sUnreadSupportShortcutsNum;
UnreadSupportShortcut shortcut = null;
for (int i = 0; i < shortcutsNum; i++) {
shortcut = UNREAD_SUPPORT_SHORTCUTS.get(i);
try {
shortcut.mUnreadNum =
if (LauncherLog.DEBUG_UNREAD) {
LauncherLog.d(TAG, "initUnreadNumberFromSystem: key = "
+ ", unreadNum = " + shortcut.mUnreadNum);
}
} catch (android.provider.Settings.SettingNotFoundException e) {
LauncherLog.e(TAG, "initUnreadNumberFromSystem
+ shortcut.mKey + ", e = " + e.getMessage());
}
}
if (LauncherLog.DEBUG_UNREAD) {
LauncherLog.d(TAG, "initUnreadNumberFromSystem end:" +
}
}
long start = System.currentTimeMillis();
if (LauncherLog.DEBUG_PERFORMANCE) {
LauncherLog.d(TAG, "loadUnreadSupportShortcuts begin: start = "
}
UNREAD_SUPPORT_SHORTCUTS.clear();
XmlResourceParser parser = mContext.getResources().getXml(
R.xml.unread_support_shortcuts);
AttributeSet attrs = Xml.asAttributeSet(parser);
XmlUtils.beginDocument(parser, TAG_UNREADSHORTCUTS);
while (((type = parser.next()) != XmlPullParser.END_TAG ||
&& type != XmlPullParser.END_DOCUMENT) {
continue;
}
synchronized (LOG_LOCK) {
UNREAD_SUPPORT_SHORTCUTS.add(new UnreadSupportShortcut(a
.getString
.getString
.getString
R.styleable.UnreadShortcut_unreadType, 0)));
}
a.recycle();
} catch (XmlPullParserException e) {
LauncherLog.w(TAG, "Got XmlPullParserException while parsing
} catch (IOException e) {
LauncherLog.w(TAG, "Got IOException while parsing unread
}
sUnreadSupportShortcutsNum = UNREAD_SUPPORT_SHORTCUTS.size();
if (LauncherLog.DEBUG_PERFORMANCE) {
LauncherLog.d(TAG, "loadUnreadSupportShortcuts end: time used =
+ (System.currentTimeMillis() - start) +
+ sUnreadSupportShortcutsNum +
}
}
* Get unread support shortcut information, since the information are
* in an array list, we may query it and modify it at the same time, a
* is needed.
*
* @return
*/
private static String getUnreadSupportShortcutInfo() {
String info = " Unread support shortcuts are ";
synchronized (LOG_LOCK) {
info += UNREAD_SUPPORT_SHORTCUTS.toString();
}
return info;
}
* Whether the given component support unread feature.
*
* @param component
* @return
*/
static int supportUnreadFeature(ComponentName component) {
if (LauncherLog.DEBUG_UNREAD) {
LauncherLog.d(TAG, "supportUnreadFeature: component = " +
}
if (component == null) {
return -1;
}
for (int i = 0, sz = size; i < sz; i++) {
if (UNREAD_SUPPORT_SHORTCUTS.get(i).mComponent.equals
return i;
}
}
}
* Set the unread number of the item in the list with the given unread
*
* @param index
* @param unreadNum
* @return
*/
static synchronized boolean setUnreadNumberAt(int index, int unreadNum)
if (index >= 0 || index < sUnreadSupportShortcutsNum) {
if (LauncherLog.DEBUG_UNREAD) {
LauncherLog.d(TAG, "setUnreadNumberAt: index = " + index +
+ getUnreadSupportShortcutInfo());
}
if (UNREAD_SUPPORT_SHORTCUTS.get(index).mUnreadNum != unreadNum)
UNREAD_SUPPORT_SHORTCUTS.get(index).mUnreadNum = unreadNum;
return true;
}
}
return false;
}
* Get unread number of application at the given position in the
* shortcut list.
*
* @param index
* @return
*/
static synchronized int getUnreadNumberAt(int index) {
if (index < 0 || index >= sUnreadSupportShortcutsNum) {
return 0;
}
if (LauncherLog.DEBUG_UNREAD) {
LauncherLog.d(TAG, "getUnreadNumberAt: index = " + index
+ getUnreadSupportShortcutInfo());
}
return UNREAD_SUPPORT_SHORTCUTS.get(index).mUnreadNum;
}
* Get unread number for the given component.
*
* @param component
* @return
*/
static int getUnreadNumberOfComponent(ComponentName component) {
final int index = supportUnreadFeature(component);
return getUnreadNumberAt(index);
}
* Draw unread number for the given icon.
*
* @param canvas
* @param icon
* @return
*/
static void drawUnreadEventIfNeed(Canvas canvas, View icon) {
ItemInfo info = (ItemInfo)icon.getTag();
if (info != null && info.unreadNum > 0) {
Resources res = icon.getContext().getResources();
Paint unreadTextNumberPaint = new Paint();
unreadTextNumberPaint.setTextSize(res.getDimension
unreadTextNumberPaint.setTypeface(Typeface.DEFAULT_BOLD);
unreadTextNumberPaint.setColor(0xffffffff);
unreadTextNumberPaint.setTextAlign(Paint.Align.CENTER);
unreadTextPlusPaint.setTextSize(res.getDimension
String unreadTextPlus = "+";
Rect unreadTextNumberBounds = new Rect(0, 0, 0, 0);
Rect unreadTextPlusBounds = new Rect(0, 0, 0, 0);
if (info.unreadNum > Launcher.MAX_UNREAD_COUNT) {
unreadTextNumber = String.valueOf
unreadTextPlusPaint.getTextBounds(unreadTextPlus, 0,
} else {
unreadTextNumber = String.valueOf(info.unreadNum);
}
unreadTextNumberPaint.getTextBounds(unreadTextNumber, 0,
int textHeight = unreadTextNumberBounds.height();
int textWidth = unreadTextNumberBounds.width() +
NinePatchDrawable unreadBgNinePatchDrawable =
int unreadBgWidth = unreadBgNinePatchDrawable.getIntrinsicWidth
int unreadBgHeight =
if (unreadBgWidth < unreadMinWidth) {
unreadBgWidth = unreadMinWidth;
}
int unreadTextMargin = (int) res.getDimension
if (unreadBgWidth < textWidth + unreadTextMargin) {
unreadBgWidth = textWidth + unreadTextMargin;
}
if (unreadBgHeight < textHeight) {
unreadBgHeight = textHeight;
}
Rect unreadBgBounds = new Rect(0, 0, unreadBgWidth,
unreadBgNinePatchDrawable.setBounds(unreadBgBounds);
int unreadMarginRight = 0;
if (info instanceof ShortcutInfo) {
if (info.container == (long)
unreadMarginTop = (int) res.getDimension
unreadMarginRight = (int) res.getDimension
} else if (info.container == (long)
unreadMarginTop = (int) res.getDimension
unreadMarginRight = (int) res.getDimension
} else {
unreadMarginTop = (int) res.getDimension
unreadMarginRight = (int) res.getDimension
}
} else if (info instanceof FolderInfo) {
if (info.container == (long)
unreadMarginTop = (int) res.getDimension
unreadMarginRight = (int) res.getDimension
} else if (info.container == (long)
unreadMarginTop = (int) res.getDimension
unreadMarginRight = (int) res.getDimension
}
}
else if (info instanceof ApplicationInfo) {
unreadMarginTop = (int) res.getDimension
unreadMarginRight = (int) res.getDimension
}
int unreadBgPosY = icon.getScrollY() + unreadMarginTop;
canvas.translate(unreadBgPosX, unreadBgPosY);
Paint.FontMetrics fontMetrics =
if (info.unreadNum > Launcher.MAX_UNREAD_COUNT) {
canvas.drawText(unreadTextNumber,
(unreadBgWidth - unreadTextPlusBounds.width
(unreadBgHeight + textHeight) / 2,
unreadTextNumberPaint);
canvas.drawText(unreadTextPlus,
(unreadBgWidth +
(unreadBgHeight + textHeight) / 2 +
unreadTextPlusPaint);
} else {
canvas.drawText(unreadTextNumber,
unreadBgWidth / 2,
(unreadBgHeight + textHeight) / 2,
unreadTextNumberPaint);
}
}
}
/**
* Bind shortcuts and application icons with the given component,
* update folders unread which contains the given component.
*
* @param component
* @param unreadNum
*/
void bindComponentUnreadChanged(ComponentName component, int
* Bind unread shortcut information if needed, this call back is
* update shortcuts and folders when launcher first created.
*/
void bindUnreadInfoIfNeeded();
}
}
7.为桌面应用程序添加未读事件,4.2上用到的是ApplicationInfo和
添加方法:
+ /**
+ * M: Update unread number of the given component in app customize
+ * with the given value, first find the icon, and then update the
+ * NOTES: since maybe not all applications are added in the customize
+ * view, we should update the apps info at the same time.
+ *
+ * @param component
+ * @param unreadNum
+ */
+ public void updateAppsUnreadChanged(ComponentName component, int
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateAppsUnreadChanged: component = " +
+ + ",unreadNum = " + unreadNum + ",mNumAppsPages = " +
+ }
+ updateUnreadNumInAppInfo(component, unreadNum);
+ for (int i = 0; i < mNumAppsPages; i++) {
+ AppsCustomizeCellLayout cl = (AppsCustomizeCellLayout)
+ if (cl == null) {
+ return;
+ }
+ final int count = cl.getPageChildCount();
+ View appIcon = null;
+ AppInfo appInfo = null;
+ for (int j = 0; j < count; j++) {
+ appIcon = cl.getChildOnPageAt(j);
+ appInfo = (AppInfo) appIcon.getTag();
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateAppsUnreadChanged: component
+ + ", appInfo = " + appInfo.componentName + ",
+ }
+ if (appInfo != null && appInfo.componentName.equals
+ appInfo.unreadNum = unreadNum;
+ appIcon.invalidate();
+ }
+ }
+ }
+ }
+ * M: Update unread number of all application info with data in
+ */
+ public void updateAppsUnread() {
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateAppsUnreadChanged: mNumAppsPages = "
+ }
+
+ updateUnreadNumInAppInfo(mApps);
+ // Update apps which already shown in the customized pane.
+ for (int i = 0; i < mNumAppsPages; i++) {
+ AppsCustomizeCellLayout cl = (AppsCustomizeCellLayout)
+ if (cl == null) {
+ return;
+ }
+ final int count = cl.getPageChildCount();
+ View appIcon = null;
+ AppInfo appInfo = null;
+ for (int j = 0; j < count; j++) {
+ appIcon = cl.getChildOnPageAt(j);
+ appInfo = (AppInfo) appIcon.getTag();
+ appInfo.unreadNum =
+ appIcon.invalidate();
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateAppsUnreadChanged: i = " + i
+ + appInfo.componentName + ", unreadNum = " +
+ }
+ }
+ }
+ }
+ * M: Update the unread number of the app info with given component.
+ *
+ * @param component
+ * @param unreadNum
+ */
+ private void updateUnreadNumInAppInfo(ComponentName component, int
+ final int size = mApps.size();
+ AppInfo appInfo = null;
+ for (int i = 0; i < size; i++) {
+ appInfo = mApps.get(i);
+ if (appInfo.intent.getComponent().equals(component)) {
+ appInfo.unreadNum = unreadNum;
+ }
+ }
+ }
+ * M: Update unread number of all application info with data in
+ *
+ * @param apps
+ */
+ public static void updateUnreadNumInAppInfo(final ArrayList
+ final int size = apps.size();
+ AppInfo appInfo = null;
+ for (int i = 0; i < size; i++) {
+ appInfo = apps.get(i);
+ appInfo.unreadNum = UnreadLoader.getUnreadNumberOfComponent
+ }
+ }
Folder.java类
+ /**
+ * M: Update unread number of the content shortcut.
+ */
+ public void updateContentUnreadNum() {
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "Folder updateContentUnreadNum: mInfo = " +
+ }
+ final int countX = mContent.getCountX();
+ final int countY = mContent.getCountY();
+ for (int j = 0; j < countY; j++) {
+ for (int i = 0; i < countX; i++) {
+ BubbleTextView shortcut = (BubbleTextView)
+ if (shortcut != null) {
+ shortcut.invalidate();
+ }
+ }
+ }
+ }
+ /**
+ * M: The unread num of the item.
+ */
+ int unreadNum = 0;
然后在构造函数中给它初始化:
+ unreadNum = info.unreadNum;
在toString()方法中增加 该变量+ " unreadNum= " + unreadNum + ")";
其次在ShortcutInfo.java类的toString方法中也增加该变量。
10.在Launcher.java类中调用写的方法:
首先在实现类中增加implements UnreadLoader.UnreadCallbacks 。。。
private UnreadLoader mUnreadLoader;//定义UnreadLoader对象
在onCreate方法中添加
+ /// M: added for unread feature, load and bind unread info.
+ if (FeatureOption.XXX_LAUNCHER_UNREAD_SUPPORT) {//不同平台这个字符
+ mUnreadLoader = app.getUnreadLoader();
+ mUnreadLoader.loadAndInitUnreadShortcuts();
+ }
增加几个方法:
+/**
+ * M: Bind component unread information in workspace and all apps list.
+ *
+ * @param component the component name of the app.
+ * @param unreadNum the number of the unread message.
+ */
+ public void bindComponentUnreadChanged(final ComponentName component,
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "bindComponentUnreadChanged: component = " +
+ + ", unreadNum = " + unreadNum + ", this = " + this);
+ }
+ // Post to message queue to avoid possible ANR.
+ mHandler.post(new Runnable() {
+ public void run() {
+ final long start = System.currentTimeMillis();
+ if (LauncherLog.DEBUG_PERFORMANCE) {
+ LauncherLog.d(TAG, "bindComponentUnreadChanged begin:
+ + ", unreadNum = " + unreadNum + ", start = " +
+ }
+ if (mWorkspace != null) {
+ mWorkspace.updateComponentUnreadChanged(component,
+ }
+
+ if (mAppsCustomizeContent != null) {
+ mAppsCustomizeContent.updateAppsUnreadChanged
+ }
+ if (LauncherLog.DEBUG_PERFORMANCE) {
+ LauncherLog.d(TAG, "bindComponentUnreadChanged end:
+ + System.currentTimeMillis() + ", time used = "
+ + (System.currentTimeMillis() - start));
+ }
+ }
+ });
+ }
+ * M: Bind shortcuts unread number if binding process has finished.
+ */
+ public void bindUnreadInfoIfNeeded() {
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "bindUnreadInfoIfNeeded:
+ + mBindingWorkspaceFinished + ", thread = " +
+ }
+ if (mBindingWorkspaceFinished) {
+ bindWorkspaceUnreadInfo();
+ }
+
+ if (mBindingAppsFinished) {
+ bindAppsUnreadInfo();
+ }
+ mUnreadLoadCompleted = true;
+ }
+ * M: Bind unread number to shortcuts with data in UnreadLoader.
+ */
+ private void bindWorkspaceUnreadInfo() {
+ mHandler.post(new Runnable() {
+ public void run() {
+ final long start = System.currentTimeMillis();
+ if (LauncherLog.DEBUG_PERFORMANCE) {
+ LauncherLog.d(TAG, "bindWorkspaceUnreadInfo begin:
+ }
+ if (mWorkspace != null) {
+ mWorkspace.updateShortcutsAndFoldersUnread();
+ }
+ if (LauncherLog.DEBUG_PERFORMANCE) {
+ LauncherLog.d(TAG, "bindWorkspaceUnreadInfo end:
+ + System.currentTimeMillis() + ",time used = "
+ + (System.currentTimeMillis() - start));
+ }
+ }
+ });
+ }
+ * M: Bind unread number to shortcuts with data in UnreadLoader.
+ */
+ private void bindAppsUnreadInfo() {
+ mHandler.post(new Runnable() {
+ public void run() {
+ final long start = System.currentTimeMillis();
+ if (LauncherLog.DEBUG_PERFORMANCE) {
+ LauncherLog.d(TAG, "bindAppsUnreadInfo begin: start = "
+ }
+ if (mAppsCustomizeContent != null) {
+ mAppsCustomizeContent.updateAppsUnread();
+ }
+ if (LauncherLog.DEBUG_PERFORMANCE) {
+ LauncherLog.d(TAG, "bindAppsUnreadInfo end: current
+ + System.currentTimeMillis() + ",time used = "
+ + (System.currentTimeMillis() - start));
+ }
+ }
+ });
+ }
+
首先导入FeatureOption该类,这个各个平台路径可能不一样,所以自己去找吧
+ /// M: added for unread feature.
+ private UnreadLoader mUnreadLoader; //定义对象
+ /// M: register unread broadcast.
+ if (FeatureOption.XXX_LAUNCHER_UNREAD_SUPPORT) {
+ mUnreadLoader = new UnreadLoader(sContext);
+ // Register unread change broadcast.
+ filter = new IntentFilter();
+ filter.addAction(Intent.XXX_ACTION_UNREAD_CHANGED);
+ sContext.registerReceiver(mUnreadLoader, filter);
+ LauncherLog.d(TAG, "register unread broadcast successfully");
+ }
同时在相应的地方注销广播
+ /// M: added for unread feature, unregister unread receiver.
+ if (FeatureOption.XXX_LAUNCHER_UNREAD_SUPPORT) {
+ sContext.unregisterReceiver(mUnreadLoader);
+ }
在setLauncher方法中初始化未读事件
/// M: added for unread feature, initialize unread loader.
if (FeatureOption.XXX_LAUNCHER_UNREAD_SUPPORT) {
mUnreadLoader.initialize(launcher);
}
增加一个方法:
+ /**
+ * M: Get unread loader, added for unread feature.
+ */
+ public UnreadLoader getUnreadLoader() {
+ return mUnreadLoader;
+ }
13.Workspace.java类中增加方法:
+ /**
+ * M: Update unread number of shortcuts and folders in workspace and
+ */
+ public void updateShortcutsAndFoldersUnread() {
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateShortcutsAndFolderUnread: this = " +
+ }
+ final ArrayList
+ int childCount = 0;
+ View view = null;
+ Object tag = null;
+ for (ShortcutAndWidgetContainer layout : childrenLayouts) {
+ childCount = layout.getChildCount();
+ for (int j = 0; j < childCount; j++) {
+ view = layout.getChildAt(j);
+ tag = view.getTag();
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateShortcutsAndFoldersUnread:
+ + j + ", view = " + view);
+ }
+ if (tag instanceof ShortcutInfo) {
+ final ShortcutInfo info = (ShortcutInfo) tag;
+ final Intent intent = info.intent;
+ final ComponentName componentName =
+ info.unreadNum =
+ ((BubbleTextView) view).invalidate();
+ } else if (tag instanceof FolderInfo) {
+ ((FolderIcon) view).updateFolderUnreadNum();
+ ((FolderIcon) view).invalidate();
+ }
+ }
+ }
+ }
+ * M: Update unread number of shortcuts and folders in workspace and
+ * with the given component.
+ *
+ * @param component
+ * @param unreadNum
+ */
+ public void updateComponentUnreadChanged(ComponentName component, int
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateComponentUnreadChanged: component = "
+ + ", unreadNum = " + unreadNum);
+ }
+ final ArrayList
+ int childCount = 0;
+ View view = null;
+ Object tag = null;
+ for (ShortcutAndWidgetContainer layout : childrenLayouts) {
+ childCount = layout.getChildCount();
+ for (int j = 0; j < childCount; j++) {
+ view = layout.getChildAt(j);
+ tag = view.getTag();
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateComponentUnreadChanged:
+ + ",tag = " + tag + ",j = " + j + ",view = " +
+ }
+ if (tag instanceof ShortcutInfo) {
+ final ShortcutInfo info = (ShortcutInfo) tag;
+ final Intent intent = info.intent;
+ final ComponentName componentName =
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateComponentUnreadChanged 2:
+ + component + ",intent = " + intent +
+ }
+ if (componentName != null && componentName.equals
+ LauncherLog.d(TAG, "updateComponentUnreadChanged 1:
+ + component + ",tag = " + tag + ",j = " + j
+ + info.cellX + ",cellY = " + info.cellY);
+ info.unreadNum = unreadNum;
+ ((BubbleTextView) view).invalidate();
+ }
+ } else if (tag instanceof FolderInfo) {
+ ((FolderIcon) view).updateFolderUnreadNum(component,
+ ((FolderIcon) view).invalidate();
+ }
+ }
+ }
+ /// M: Update shortcut within folder if open folder exists.
+ Folder openFolder = getOpenFolder();
+ if (openFolder != null) {
+ openFolder.updateContentUnreadNum();
+ }
+ }
14.接下来就简单了,在相应的地方去画要显示的未读数字就好了
1)首先是PagedViewIcon.java中,这个画的是APP右上角的。
先导包
+// M: Add for unread event feature.
+import android.graphics.Canvas;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.NinePatchDrawable;
在其Draw()方法中最后调用:(记得加在最后哦,不让你画的东东被app图标遮住了)
+ /// M: For feature unread draw.
+ UnreadLoader.drawUnreadEventIfNeed(canvas, this);
if (getCurrentTextColor() == getResources().getColor
/// M: Draw unread event number.
UnreadLoader.drawUnreadEventIfNeed(canvas, this);
在draw方法最后也增加
/// M: Draw unread event number.
UnreadLoader.drawUnreadEventIfNeed(canvas, this);
在dispatchDraw方法的最后:
+ /// M: Draw unread event number.
+ UnreadLoader.drawUnreadEventIfNeed(canvas, this);
/// M: added for unread feature, when add a item to a folder, we need to
/// the unread num of the folder.
final ComponentName componentName = item.intent.getComponent();
updateFolderUnreadNum(componentName, item.unreadNum);
/// M: added for Unread feature, when remove a item from a folder, we need
/// the unread num of the folder
final ComponentName componentName = item.intent.getComponent();
updateFolderUnreadNum(componentName, item.unreadNum);
+ /**
+ * M: Update the unread message number of the shortcut with the given
+ *
+ * @param unreadNum the number of the unread message.
+ */
+ public void setFolderUnreadNum(int unreadNum) {
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "setFolderUnreadNum: unreadNum = " +
+ + ", this = " + this);
+ }
+
+ if (unreadNum <= 0) {
+ mInfo.unreadNum = 0;
+ } else {
+ mInfo.unreadNum = unreadNum;
+ }
+ }
+ * M: Update unread number of the folder, the number is the total
+ * of all shortcuts in folder, duplicate shortcut will be only count
+ */
+ public void updateFolderUnreadNum() {
+ final ArrayList
+ final int contentsCount = contents.size();
+ int unreadNumTotal = 0;
+ final ArrayList
+ ShortcutInfo shortcutInfo = null;
+ ComponentName componentName = null;
+ int unreadNum = 0;
+ for (int i = 0; i < contentsCount; i++) {
+ shortcutInfo = contents.get(i);
+ componentName = shortcutInfo.intent.getComponent();
+ unreadNum = MTKUnreadLoader.getUnreadNumberOfComponent
+ if (unreadNum > 0) {
+ shortcutInfo.unreadNum = unreadNum;
+ int j = 0;
+ for (j = 0; j < components.size(); j++) {
+ if (componentName != null && componentName.equals
+ break;
+ }
+ }
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateFolderUnreadNum:
+ + ", j = " + j + ", components.size() = " +
+ }
+ if (j >= components.size()) {
+ components.add(componentName);
+ unreadNumTotal += unreadNum;
+ }
+ }
+ }
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateFolderUnreadNum 1 end: unreadNumTotal
+ }
+ setFolderUnreadNum(unreadNumTotal);
+ }
+ * M: Update the unread message of the shortcut with the given
+ *
+ * @param unreadNum the number of the unread message.
+ */
+ public void updateFolderUnreadNum(ComponentName component, int
+ final ArrayList
+ final int contentsCount = contents.size();
+ int unreadNumTotal = 0;
+ ShortcutInfo appInfo = null;
+ ComponentName name = null;
+ final ArrayList
+ for (int i = 0; i < contentsCount; i++) {
+ appInfo = contents.get(i);
+ name = appInfo.intent.getComponent();
+ if (name != null && name.equals(component)) {
+ appInfo.unreadNum = unreadNum;
+ }
+ if (appInfo.unreadNum > 0) {
+ int j = 0;
+ for (j = 0; j < components.size(); j++) {
+ if (name != null && name.equals(components.get(j))) {
+ break;
+ }
+ }
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateFolderUnreadNum:
+ + ", j = " + j + ", components.size() = " +
+ }
+ if (j >= components.size()) {
+ components.add(name);
+ unreadNumTotal += appInfo.unreadNum;
+ }
+ }
+ }
+ if (LauncherLog.DEBUG_UNREAD) {
+ LauncherLog.d(TAG, "updateFolderUnreadNum 2 end: unreadNumTotal
+ }
+ setFolderUnreadNum(unreadNumTotal);
+ }
+
+ /**
+ * M: Reset the value of the variable of sStaticValuesDirty.
+ */
+ public static void resetValuesDirty() {
+ sStaticValuesDirty = true;
+ }