Android P Launcher3中添加未读信息角标

Launcher3中添加未读信息角标

添加未读信息部分参考Android之前版本即可

首先在源码中加入对应的监听(公司使用GMS项目,因此修改的是GMS包下的overlay)

vendor/partner_gms/products/gms_overlay/frameworks/base/core/res/res/values/config.xml

-     com.google.android.setupwizard:com.google.android.apps.restore
+    com.android.launcher3:com.google.android.setupwizard:com.google.android.apps.restore

在对应的文件中注释掉Notification的拦截和低内存设备的判定

frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java


+                //if (mAllowedManagedServicePackages.test(listener.getPackageName())) {
                mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(),
                        userId, false, granted);
                mListeners.setPackageOrComponentEnabled(listener.flattenToString(),
                        userId, true, granted);

                getContext().sendBroadcastAsUser(new Intent(
                        NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
                                .setPackage(listener.getPackageName())
                                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                        UserHandle.of(userId), null);

                  savePolicyFile();
+                //}

packages/apps/Settings/src/com/android/settings/notification/ZenAccessSettings.java

// if (!ActivityManager.isLowRamDeviceStatic()) {
        reloadList();
        getContentResolver().registerContentObserver(
                Secure.getUriFor(Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES), false,
                mObserver);
        getContentResolver().registerContentObserver(
                Secure.getUriFor(Secure.ENABLED_NOTIFICATION_LISTENERS), false,
                mObserver);
        // } else {
        //     setEmptyText(R.string.disabled_low_ram_device);
        // }

packages/apps/Settings/src/com/android/settings/utils/ManagedServiceSettings.java

// if (!ActivityManager.isLowRamDeviceStatic()) {
        mServiceListing.reload();
        mServiceListing.setListening(true);
        // } else {
        //     setEmptyText(R.string.disabled_low_ram_device);
        // }

在Launcher3中修改对应文件的函数,使其满足传递对应的未读数量

packages/apps/Launcher3/src/com/android/launcher3/badge/BadgeRenderer.java

public void draw(
-            Canvas canvas, int color, Rect iconBounds, float badgeScale, Point spaceForOffset) {
+            Canvas canvas, int color, @Nullable BadgeInfo badgeInfo,
+            Rect iconBounds, float badgeScale, Point spaceForOffset) {

...
+        String notificationCount = badgeInfo == null ? "0"
+                : String.valueOf(badgeInfo.getNotificationCount());
+        int numChars = notificationCount.length();
         canvas.save();
+
+        boolean isText = !DOTS_ONLY && badgeInfo != null && badgeInfo.getNotificationCount() != 0;

 

-        mCirclePaint.setColor(Color.BLACK);
-        canvas.drawBitmap(mBackgroundWithShadow, mBitmapOffset, mBitmapOffset, mCirclePaint);
-        mCirclePaint.setColor(color);
-        canvas.drawCircle(0, 0, mCircleRadius, mCirclePaint);
+        if (isText) {
+            mTextPaint.setColor(Color.RED);
+            canvas.drawBitmap(mBackgroundWithShadow, mBitmapOffset,
+                    mBitmapOffset, mTextPaint);
+            mTextPaint.setColor(Color.WHITE);
+            canvas.drawText(notificationCount, 0, mTextHeight / 2, mTextPaint);
+        } else {
+            mCirclePaint.setColor(Color.BLACK);
+            canvas.drawBitmap(mBackgroundWithShadow, mBitmapOffset, mBitmapOffset, mCirclePaint);
+            mCirclePaint.setColor(color);
+            canvas.drawCircle(0, 0, mCircleRadius, mCirclePaint);
+        }

 

将调用BadgeRenderer中draw方法的地方,修改:

packages/apps/Launcher3/src/com/android/launcher3/BubbleTextView.java

-            mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
-                    mTempSpaceForBadgeOffset);
+            mBadgeRenderer.draw(canvas, mBadgeColor, mBadgeInfo, mTempIconBounds,
+                    mBadgeScale, mTempSpaceForBadgeOffset);

 

packages/apps/Launcher3/src/com/android/launcher3/folder/FolderIcon.java

-            mBadgeRenderer.draw(canvas, mBackground.getBadgeColor(), mTempBounds,
-                    badgeScale, mTempSpaceForBadgeOffset);
+            mBadgeRenderer.draw(canvas, mBackground.getBadgeColor(), mBadgeInfo, mTempBounds,
+                   badgeScale, mTempSpaceForBadgeOffset);

注意:FolderBadgeInfo中默认返回的数量为0,需要手动修改

packages/apps/Launcher3/src/com/android/launcher3/badge/FolderBadgeInfo.java

public int getNotificationCount() {
         // This forces the folder badge to always show up as a dot.
-        return 0;
+        return mNumNotifications; 
     }

你可能感兴趣的:(android,Launcher3)