├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── why │ │ └── project │ │ └── shortcutbadgerdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── why │ │ │ └── project │ │ │ └── shortcutbadgerdemo │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── why │ └── project │ └── shortcutbadgerdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── shortcutbadger ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── me │ │ └── leolin │ │ └── shortcutbadger │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── me │ │ │ └── leolin │ │ │ └── shortcutbadger │ │ │ ├── BadgeIntentService.java │ │ │ ├── Badger.java │ │ │ ├── ShortcutBadgeException.java │ │ │ ├── ShortcutBadger.java │ │ │ ├── impl │ │ │ ├── AdwHomeBadger.java │ │ │ ├── ApexHomeBadger.java │ │ │ ├── AsusHomeBadger.java │ │ │ ├── DefaultBadger.java │ │ │ ├── EverythingMeHomeBadger.java │ │ │ ├── HuaweiHomeBadger.java │ │ │ ├── IntentConstants.java │ │ │ ├── LGHomeBadger.java │ │ │ ├── NewHtcHomeBadger.java │ │ │ ├── NovaHomeBadger.java │ │ │ ├── OPPOHomeBader.java │ │ │ ├── SamsungHomeBadger.java │ │ │ ├── SonyHomeBadger.java │ │ │ ├── VivoHomeBadger.java │ │ │ ├── XiaomiHomeBadger.java │ │ │ ├── ZTEHomeBadger.java │ │ │ └── ZukHomeBadger.java │ │ │ └── util │ │ │ ├── BroadcastHelper.java │ │ │ └── CloseHelper.java │ └── res │ │ ├── drawable-hdpi │ │ └── badger_notification_icon.png │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── me │ └── leolin │ └── shortcutbadger │ └── ExampleUnitTest.java └── 安装包 └── app-debug.apk /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches/build_file_checksums.ser 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ShortcutBadgerDemo 2 | [ShortcutBadgerDemo【安卓应用角标(badge)实现方案】](https://www.cnblogs.com/whycxb/p/10081963.html) 3 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.why.project.shortcutbadgerdemo" 7 | minSdkVersion 16 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | 29 | //引用shortcutbadger 30 | implementation project(':shortcutbadger') 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/why/project/shortcutbadgerdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.why.project.shortcutbadgerdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.why.project.shortcutbadgerdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/why/project/shortcutbadgerdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.why.project.shortcutbadgerdemo; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.widget.Toast; 7 | 8 | import me.leolin.shortcutbadger.ShortcutBadgeException; 9 | import me.leolin.shortcutbadger.ShortcutBadger; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | private Context mContext; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_main); 19 | 20 | mContext = this; 21 | 22 | //一般设备上的显示角标的代码--可以结合RomUtil进行机型判断 23 | try { 24 | ShortcutBadger.applyCountOrThrow(mContext, 9); 25 | } catch (ShortcutBadgeException e) { 26 | e.printStackTrace(); 27 | //如果还没有提示过toast,则进行提示 28 | Toast.makeText(mContext,"本APP暂时无法在该设备上实现应用角标功能",Toast.LENGTH_SHORT).show(); 29 | } 30 | 31 | //下面是小米设备上的显示角标的关键代码--可以结合RomUtil进行机型判断 32 | /*finish();//在小米设备上APP打开着的情况下,是不显示角标的,只有APP关闭了才会显示角标 33 | startService(new Intent(MainActivity.this, BadgeIntentService.class).putExtra("badgeCount", 9));*/ 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ShortcutBadgerDemo 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/why/project/shortcutbadgerdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.why.project.shortcutbadgerdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.2.1' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':shortcutbadger' 2 | -------------------------------------------------------------------------------- /shortcutbadger/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /shortcutbadger/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | minSdkVersion 16 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled true 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | 28 | implementation 'com.android.support:appcompat-v7:28.0.0' 29 | testImplementation 'junit:junit:4.12' 30 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 32 | } 33 | -------------------------------------------------------------------------------- /shortcutbadger/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | #https://github.com/leolin310148/ShortcutBadger/issues/46 2 | -keep class me.leolin.shortcutbadger.impl.** { (...); } 3 | -------------------------------------------------------------------------------- /shortcutbadger/src/androidTest/java/me/leolin/shortcutbadger/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("me.leolin.shortcutbadger.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/BadgeIntentService.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.IntentService; 5 | import android.app.Notification; 6 | import android.app.NotificationChannel; 7 | import android.app.NotificationManager; 8 | import android.content.Context; 9 | import android.content.Intent; 10 | import android.os.Build; 11 | 12 | public class BadgeIntentService extends IntentService { 13 | 14 | private static final String NOTIFICATION_CHANNEL = "me.leolin.shortcutbadger.example"; 15 | 16 | private int notificationId = 0; 17 | 18 | public BadgeIntentService() { 19 | super("BadgeIntentService"); 20 | } 21 | 22 | private NotificationManager mNotificationManager; 23 | 24 | @Override 25 | public void onCreate() { 26 | super.onCreate(); 27 | mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 28 | } 29 | 30 | @Override 31 | public void onStart(Intent intent, int startId) { 32 | super.onStart(intent, startId); 33 | } 34 | 35 | @Override 36 | protected void onHandleIntent(Intent intent) { 37 | if (intent != null) { 38 | int badgeCount = intent.getIntExtra("badgeCount", 0); 39 | 40 | mNotificationManager.cancel(notificationId); 41 | notificationId++; 42 | 43 | Notification.Builder builder = new Notification.Builder(getApplicationContext()) 44 | .setContentTitle("") 45 | .setContentText("") 46 | .setSmallIcon(R.drawable.badger_notification_icon); 47 | 48 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 49 | setupNotificationChannel(); 50 | 51 | builder.setChannelId(NOTIFICATION_CHANNEL); 52 | } 53 | 54 | Notification notification = builder.build(); 55 | ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); 56 | mNotificationManager.notify(notificationId, notification); 57 | } 58 | } 59 | 60 | @TargetApi(Build.VERSION_CODES.O) 61 | private void setupNotificationChannel() { 62 | NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, "ShortcutBadger Sample", 63 | NotificationManager.IMPORTANCE_DEFAULT); 64 | 65 | mNotificationManager.createNotificationChannel(channel); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/Badger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | 6 | import java.util.List; 7 | 8 | public interface Badger { 9 | 10 | /** 11 | * Called when user attempts to update notification count 12 | * @param context Caller context 13 | * @param componentName Component containing package and class name of calling application's 14 | * launcher activity 15 | * @param badgeCount Desired notification count 16 | * @throws ShortcutBadgeException 17 | */ 18 | void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException; 19 | 20 | /** 21 | * Called to let {@link ShortcutBadger} knows which launchers are supported by this badger. It should return a 22 | * @return List containing supported launchers package names 23 | */ 24 | List getSupportLaunchers(); 25 | } 26 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/ShortcutBadgeException.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger; 2 | 3 | public class ShortcutBadgeException extends Exception { 4 | public ShortcutBadgeException(String message) { 5 | super(message); 6 | } 7 | 8 | public ShortcutBadgeException(String message, Exception e) { 9 | super(message, e); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/ShortcutBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger; 2 | 3 | import android.app.Notification; 4 | import android.content.ComponentName; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.pm.PackageManager; 8 | import android.content.pm.ResolveInfo; 9 | import android.os.Build; 10 | import android.util.Log; 11 | 12 | import java.lang.reflect.Field; 13 | import java.lang.reflect.Method; 14 | import java.util.LinkedList; 15 | import java.util.List; 16 | 17 | import me.leolin.shortcutbadger.impl.AdwHomeBadger; 18 | import me.leolin.shortcutbadger.impl.ApexHomeBadger; 19 | import me.leolin.shortcutbadger.impl.AsusHomeBadger; 20 | import me.leolin.shortcutbadger.impl.DefaultBadger; 21 | import me.leolin.shortcutbadger.impl.EverythingMeHomeBadger; 22 | import me.leolin.shortcutbadger.impl.HuaweiHomeBadger; 23 | import me.leolin.shortcutbadger.impl.NewHtcHomeBadger; 24 | import me.leolin.shortcutbadger.impl.NovaHomeBadger; 25 | import me.leolin.shortcutbadger.impl.OPPOHomeBader; 26 | import me.leolin.shortcutbadger.impl.SamsungHomeBadger; 27 | import me.leolin.shortcutbadger.impl.SonyHomeBadger; 28 | import me.leolin.shortcutbadger.impl.VivoHomeBadger; 29 | import me.leolin.shortcutbadger.impl.ZTEHomeBadger; 30 | import me.leolin.shortcutbadger.impl.ZukHomeBadger; 31 | 32 | 33 | /** 34 | * @author Leo Lin 35 | */ 36 | public final class ShortcutBadger { 37 | 38 | private static final String LOG_TAG = "ShortcutBadger"; 39 | private static final int SUPPORTED_CHECK_ATTEMPTS = 3; 40 | 41 | private static final List> BADGERS = new LinkedList>(); 42 | 43 | private volatile static Boolean sIsBadgeCounterSupported; 44 | private final static Object sCounterSupportedLock = new Object(); 45 | 46 | static { 47 | BADGERS.add(AdwHomeBadger.class); 48 | BADGERS.add(ApexHomeBadger.class); 49 | BADGERS.add(DefaultBadger.class); 50 | BADGERS.add(NewHtcHomeBadger.class); 51 | BADGERS.add(NovaHomeBadger.class); 52 | BADGERS.add(SonyHomeBadger.class); 53 | BADGERS.add(AsusHomeBadger.class); 54 | BADGERS.add(HuaweiHomeBadger.class); 55 | BADGERS.add(OPPOHomeBader.class); 56 | BADGERS.add(SamsungHomeBadger.class); 57 | BADGERS.add(ZukHomeBadger.class); 58 | BADGERS.add(VivoHomeBadger.class); 59 | BADGERS.add(ZTEHomeBadger.class); 60 | BADGERS.add(EverythingMeHomeBadger.class); 61 | } 62 | 63 | private static Badger sShortcutBadger; 64 | private static ComponentName sComponentName; 65 | 66 | /** 67 | * Tries to update the notification count 68 | * 69 | * @param context Caller context 70 | * @param badgeCount Desired badge count 71 | * @return true in case of success, false otherwise 72 | */ 73 | public static boolean applyCount(Context context, int badgeCount) { 74 | try { 75 | applyCountOrThrow(context, badgeCount); 76 | return true; 77 | } catch (ShortcutBadgeException e) { 78 | if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { 79 | Log.d(LOG_TAG, "Unable to execute badge", e); 80 | } 81 | return false; 82 | } 83 | } 84 | 85 | /** 86 | * Tries to update the notification count, throw a {@link ShortcutBadgeException} if it fails 87 | * 88 | * @param context Caller context 89 | * @param badgeCount Desired badge count 90 | */ 91 | public static void applyCountOrThrow(Context context, int badgeCount) throws ShortcutBadgeException { 92 | if (sShortcutBadger == null) { 93 | boolean launcherReady = initBadger(context); 94 | 95 | if (!launcherReady) 96 | throw new ShortcutBadgeException("No default launcher available"); 97 | } 98 | 99 | try { 100 | sShortcutBadger.executeBadge(context, sComponentName, badgeCount); 101 | } catch (Exception e) { 102 | throw new ShortcutBadgeException("Unable to execute badge", e); 103 | } 104 | } 105 | 106 | /** 107 | * Tries to remove the notification count 108 | * 109 | * @param context Caller context 110 | * @return true in case of success, false otherwise 111 | */ 112 | public static boolean removeCount(Context context) { 113 | return applyCount(context, 0); 114 | } 115 | 116 | /** 117 | * Tries to remove the notification count, throw a {@link ShortcutBadgeException} if it fails 118 | * 119 | * @param context Caller context 120 | */ 121 | public static void removeCountOrThrow(Context context) throws ShortcutBadgeException { 122 | applyCountOrThrow(context, 0); 123 | } 124 | 125 | /** 126 | * Whether this platform launcher supports shortcut badges. Doing this check causes the side 127 | * effect of resetting the counter if it's supported, so this method should be followed by 128 | * a call that actually sets the counter to the desired value, if the counter is supported. 129 | */ 130 | public static boolean isBadgeCounterSupported(Context context) { 131 | // Checking outside synchronized block to avoid synchronization in the common case (flag 132 | // already set), and improve perf. 133 | if (sIsBadgeCounterSupported == null) { 134 | synchronized (sCounterSupportedLock) { 135 | // Checking again inside synch block to avoid setting the flag twice. 136 | if (sIsBadgeCounterSupported == null) { 137 | String lastErrorMessage = null; 138 | for (int i = 0; i < SUPPORTED_CHECK_ATTEMPTS; i++) { 139 | try { 140 | Log.i(LOG_TAG, "Checking if platform supports badge counters, attempt " 141 | + String.format("%d/%d.", i + 1, SUPPORTED_CHECK_ATTEMPTS)); 142 | if (initBadger(context)) { 143 | sShortcutBadger.executeBadge(context, sComponentName, 0); 144 | sIsBadgeCounterSupported = true; 145 | Log.i(LOG_TAG, "Badge counter is supported in this platform."); 146 | break; 147 | } else { 148 | lastErrorMessage = "Failed to initialize the badge counter."; 149 | } 150 | } catch (Exception e) { 151 | // Keep retrying as long as we can. No need to dump the stack trace here 152 | // because this error will be the norm, not exception, for unsupported 153 | // platforms. So we just save the last error message to display later. 154 | lastErrorMessage = e.getMessage(); 155 | } 156 | } 157 | 158 | if (sIsBadgeCounterSupported == null) { 159 | Log.w(LOG_TAG, "Badge counter seems not supported for this platform: " 160 | + lastErrorMessage); 161 | sIsBadgeCounterSupported = false; 162 | } 163 | } 164 | } 165 | } 166 | return sIsBadgeCounterSupported; 167 | } 168 | 169 | /** 170 | * @param context Caller context 171 | * @param notification 172 | * @param badgeCount 173 | */ 174 | public static void applyNotification(Context context, Notification notification, int badgeCount) { 175 | if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) { 176 | try { 177 | Field field = notification.getClass().getDeclaredField("extraNotification"); 178 | Object extraNotification = field.get(notification); 179 | Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); 180 | method.invoke(extraNotification, badgeCount); 181 | } catch (Exception e) { 182 | if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { 183 | Log.d(LOG_TAG, "Unable to execute badge", e); 184 | } 185 | } 186 | } 187 | } 188 | 189 | // Initialize Badger if a launcher is availalble (eg. set as default on the device) 190 | // Returns true if a launcher is available, in this case, the Badger will be set and sShortcutBadger will be non null. 191 | private static boolean initBadger(Context context) { 192 | Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); 193 | if (launchIntent == null) { 194 | Log.e(LOG_TAG, "Unable to find launch intent for package " + context.getPackageName()); 195 | return false; 196 | } 197 | 198 | sComponentName = launchIntent.getComponent(); 199 | 200 | Intent intent = new Intent(Intent.ACTION_MAIN); 201 | intent.addCategory(Intent.CATEGORY_HOME); 202 | List resolveInfos = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); 203 | 204 | for (ResolveInfo resolveInfo : resolveInfos) { 205 | String currentHomePackage = resolveInfo.activityInfo.packageName; 206 | 207 | for (Class badger : BADGERS) { 208 | Badger shortcutBadger = null; 209 | try { 210 | shortcutBadger = badger.newInstance(); 211 | } catch (Exception ignored) { 212 | } 213 | if (shortcutBadger != null && shortcutBadger.getSupportLaunchers().contains(currentHomePackage)) { 214 | sShortcutBadger = shortcutBadger; 215 | break; 216 | } 217 | } 218 | if (sShortcutBadger != null) { 219 | break; 220 | } 221 | } 222 | 223 | if (sShortcutBadger == null) { 224 | if (Build.MANUFACTURER.equalsIgnoreCase("ZUK")) 225 | sShortcutBadger = new ZukHomeBadger(); 226 | else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO")) 227 | sShortcutBadger = new OPPOHomeBader(); 228 | else if (Build.MANUFACTURER.equalsIgnoreCase("VIVO")) 229 | sShortcutBadger = new VivoHomeBadger(); 230 | else if (Build.MANUFACTURER.equalsIgnoreCase("ZTE")) 231 | sShortcutBadger = new ZTEHomeBadger(); 232 | else 233 | sShortcutBadger = new DefaultBadger(); 234 | } 235 | 236 | return true; 237 | } 238 | 239 | // Avoid anybody to instantiate this class 240 | private ShortcutBadger() { 241 | 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/AdwHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import me.leolin.shortcutbadger.Badger; 11 | import me.leolin.shortcutbadger.ShortcutBadgeException; 12 | import me.leolin.shortcutbadger.util.BroadcastHelper; 13 | 14 | /** 15 | * @author Gernot Pansy 16 | */ 17 | public class AdwHomeBadger implements Badger { 18 | 19 | public static final String INTENT_UPDATE_COUNTER = "org.adw.launcher.counter.SEND"; 20 | public static final String PACKAGENAME = "PNAME"; 21 | public static final String CLASSNAME = "CNAME"; 22 | public static final String COUNT = "COUNT"; 23 | 24 | @Override 25 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 26 | Intent intent = new Intent(INTENT_UPDATE_COUNTER); 27 | intent.putExtra(PACKAGENAME, componentName.getPackageName()); 28 | intent.putExtra(CLASSNAME, componentName.getClassName()); 29 | intent.putExtra(COUNT, badgeCount); 30 | 31 | BroadcastHelper.sendIntentExplicitly(context, intent); 32 | } 33 | 34 | @Override 35 | public List getSupportLaunchers() { 36 | return Arrays.asList( 37 | "org.adw.launcher", 38 | "org.adwfreak.launcher" 39 | ); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/ApexHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import me.leolin.shortcutbadger.Badger; 11 | import me.leolin.shortcutbadger.ShortcutBadgeException; 12 | import me.leolin.shortcutbadger.util.BroadcastHelper; 13 | 14 | /** 15 | * @author Gernot Pansy 16 | */ 17 | public class ApexHomeBadger implements Badger { 18 | 19 | private static final String INTENT_UPDATE_COUNTER = "com.anddoes.launcher.COUNTER_CHANGED"; 20 | private static final String PACKAGENAME = "package"; 21 | private static final String COUNT = "count"; 22 | private static final String CLASS = "class"; 23 | 24 | @Override 25 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 26 | Intent intent = new Intent(INTENT_UPDATE_COUNTER); 27 | intent.putExtra(PACKAGENAME, componentName.getPackageName()); 28 | intent.putExtra(COUNT, badgeCount); 29 | intent.putExtra(CLASS, componentName.getClassName()); 30 | 31 | BroadcastHelper.sendIntentExplicitly(context, intent); 32 | } 33 | 34 | @Override 35 | public List getSupportLaunchers() { 36 | return Arrays.asList("com.anddoes.launcher"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/AsusHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import me.leolin.shortcutbadger.Badger; 11 | import me.leolin.shortcutbadger.ShortcutBadgeException; 12 | import me.leolin.shortcutbadger.util.BroadcastHelper; 13 | 14 | /** 15 | * @author leolin 16 | */ 17 | public class AsusHomeBadger implements Badger { 18 | 19 | private static final String INTENT_ACTION = IntentConstants.DEFAULT_INTENT_ACTION; 20 | private static final String INTENT_EXTRA_BADGE_COUNT = "badge_count"; 21 | private static final String INTENT_EXTRA_PACKAGENAME = "badge_count_package_name"; 22 | private static final String INTENT_EXTRA_ACTIVITY_NAME = "badge_count_class_name"; 23 | 24 | @Override 25 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 26 | Intent intent = new Intent(INTENT_ACTION); 27 | intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount); 28 | intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName()); 29 | intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName()); 30 | intent.putExtra("badge_vip_count", 0); 31 | 32 | BroadcastHelper.sendDefaultIntentExplicitly(context, intent); 33 | } 34 | 35 | @Override 36 | public List getSupportLaunchers() { 37 | return Arrays.asList("com.asus.launcher"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/DefaultBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.os.Build; 7 | 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | import me.leolin.shortcutbadger.Badger; 12 | import me.leolin.shortcutbadger.ShortcutBadgeException; 13 | import me.leolin.shortcutbadger.util.BroadcastHelper; 14 | 15 | /** 16 | * @author leolin 17 | */ 18 | public class DefaultBadger implements Badger { 19 | private static final String INTENT_ACTION = IntentConstants.DEFAULT_INTENT_ACTION; 20 | private static final String INTENT_EXTRA_BADGE_COUNT = "badge_count"; 21 | private static final String INTENT_EXTRA_PACKAGENAME = "badge_count_package_name"; 22 | private static final String INTENT_EXTRA_ACTIVITY_NAME = "badge_count_class_name"; 23 | 24 | @Override 25 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 26 | Intent intent = new Intent(INTENT_ACTION); 27 | intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount); 28 | intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName()); 29 | intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName()); 30 | 31 | BroadcastHelper.sendDefaultIntentExplicitly(context, intent); 32 | } 33 | 34 | @Override 35 | public List getSupportLaunchers() { 36 | return Arrays.asList( 37 | "fr.neamar.kiss", 38 | "com.quaap.launchtime", 39 | "com.quaap.launchtime_official" 40 | ); 41 | } 42 | 43 | boolean isSupported(Context context) { 44 | Intent intent = new Intent(INTENT_ACTION); 45 | return BroadcastHelper.resolveBroadcast(context, intent).size() > 0 46 | || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O 47 | && BroadcastHelper.resolveBroadcast(context, new Intent(IntentConstants.DEFAULT_OREO_INTENT_ACTION)).size() > 0); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/EverythingMeHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.ContentValues; 5 | import android.content.Context; 6 | import android.net.Uri; 7 | 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | import me.leolin.shortcutbadger.Badger; 12 | import me.leolin.shortcutbadger.ShortcutBadgeException; 13 | 14 | 15 | /** 16 | * @author Radko Roman 17 | * @since 13.04.17. 18 | */ 19 | public class EverythingMeHomeBadger implements Badger { 20 | 21 | private static final String CONTENT_URI = "content://me.everything.badger/apps"; 22 | private static final String COLUMN_PACKAGE_NAME = "package_name"; 23 | private static final String COLUMN_ACTIVITY_NAME = "activity_name"; 24 | private static final String COLUMN_COUNT = "count"; 25 | 26 | @Override 27 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 28 | ContentValues contentValues = new ContentValues(); 29 | contentValues.put(COLUMN_PACKAGE_NAME, componentName.getPackageName()); 30 | contentValues.put(COLUMN_ACTIVITY_NAME, componentName.getClassName()); 31 | contentValues.put(COLUMN_COUNT, badgeCount); 32 | context.getContentResolver().insert(Uri.parse(CONTENT_URI), contentValues); 33 | } 34 | 35 | @Override 36 | public List getSupportLaunchers() { 37 | return Arrays.asList("me.everything.launcher"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/HuaweiHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.net.Uri; 6 | import android.os.Bundle; 7 | 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | import me.leolin.shortcutbadger.Badger; 12 | import me.leolin.shortcutbadger.ShortcutBadgeException; 13 | 14 | /** 15 | * @author Jason Ling 16 | */ 17 | public class HuaweiHomeBadger implements Badger { 18 | 19 | @Override 20 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 21 | Bundle localBundle = new Bundle(); 22 | localBundle.putString("package", context.getPackageName()); 23 | localBundle.putString("class", componentName.getClassName()); 24 | localBundle.putInt("badgenumber", badgeCount); 25 | context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, localBundle); 26 | } 27 | 28 | @Override 29 | public List getSupportLaunchers() { 30 | return Arrays.asList( 31 | "com.huawei.android.launcher" 32 | ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/IntentConstants.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | public interface IntentConstants { 4 | String DEFAULT_INTENT_ACTION = "android.intent.action.BADGE_COUNT_UPDATE"; 5 | String DEFAULT_OREO_INTENT_ACTION = "me.leolin.shortcutbadger.BADGE_COUNT_UPDATE"; 6 | } 7 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/LGHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import me.leolin.shortcutbadger.Badger; 11 | import me.leolin.shortcutbadger.ShortcutBadgeException; 12 | import me.leolin.shortcutbadger.util.BroadcastHelper; 13 | 14 | /** 15 | * @author Leo Lin 16 | * Deprecated, LG devices will use DefaultBadger 17 | */ 18 | @Deprecated 19 | public class LGHomeBadger implements Badger { 20 | 21 | private static final String INTENT_ACTION = IntentConstants.DEFAULT_INTENT_ACTION; 22 | private static final String INTENT_EXTRA_BADGE_COUNT = "badge_count"; 23 | private static final String INTENT_EXTRA_PACKAGENAME = "badge_count_package_name"; 24 | private static final String INTENT_EXTRA_ACTIVITY_NAME = "badge_count_class_name"; 25 | 26 | @Override 27 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 28 | Intent intent = new Intent(INTENT_ACTION); 29 | intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount); 30 | intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName()); 31 | intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName()); 32 | 33 | BroadcastHelper.sendDefaultIntentExplicitly(context, intent); 34 | } 35 | 36 | @Override 37 | public List getSupportLaunchers() { 38 | return Arrays.asList( 39 | "com.lge.launcher", 40 | "com.lge.launcher2" 41 | ); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/NewHtcHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | import java.util.Collections; 8 | import java.util.List; 9 | 10 | import me.leolin.shortcutbadger.Badger; 11 | import me.leolin.shortcutbadger.ShortcutBadgeException; 12 | import me.leolin.shortcutbadger.util.BroadcastHelper; 13 | 14 | /** 15 | * @author Leo Lin 16 | */ 17 | public class NewHtcHomeBadger implements Badger { 18 | 19 | public static final String INTENT_UPDATE_SHORTCUT = "com.htc.launcher.action.UPDATE_SHORTCUT"; 20 | public static final String INTENT_SET_NOTIFICATION = "com.htc.launcher.action.SET_NOTIFICATION"; 21 | public static final String PACKAGENAME = "packagename"; 22 | public static final String COUNT = "count"; 23 | public static final String EXTRA_COMPONENT = "com.htc.launcher.extra.COMPONENT"; 24 | public static final String EXTRA_COUNT = "com.htc.launcher.extra.COUNT"; 25 | 26 | @Override 27 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 28 | 29 | Intent intent1 = new Intent(INTENT_SET_NOTIFICATION); 30 | boolean intent1Success; 31 | 32 | intent1.putExtra(EXTRA_COMPONENT, componentName.flattenToShortString()); 33 | intent1.putExtra(EXTRA_COUNT, badgeCount); 34 | 35 | Intent intent = new Intent(INTENT_UPDATE_SHORTCUT); 36 | boolean intentSuccess; 37 | 38 | intent.putExtra(PACKAGENAME, componentName.getPackageName()); 39 | intent.putExtra(COUNT, badgeCount); 40 | 41 | try { 42 | BroadcastHelper.sendIntentExplicitly(context, intent1); 43 | intent1Success = true; 44 | } catch (ShortcutBadgeException e) { 45 | intent1Success = false; 46 | } 47 | 48 | try { 49 | BroadcastHelper.sendIntentExplicitly(context, intent); 50 | intentSuccess = true; 51 | } catch (ShortcutBadgeException e) { 52 | intentSuccess = false; 53 | } 54 | 55 | if (!intent1Success && !intentSuccess) { 56 | throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString()); 57 | } 58 | } 59 | 60 | @Override 61 | public List getSupportLaunchers() { 62 | return Collections.singletonList("com.htc.launcher"); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/NovaHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.ContentValues; 5 | import android.content.Context; 6 | import android.net.Uri; 7 | 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | import me.leolin.shortcutbadger.Badger; 12 | import me.leolin.shortcutbadger.ShortcutBadgeException; 13 | 14 | /** 15 | * Shortcut Badger support for Nova Launcher. 16 | * TeslaUnread must be installed. 17 | * User: Gernot Pansy 18 | * Date: 2014/11/03 19 | * Time: 7:15 20 | */ 21 | public class NovaHomeBadger implements Badger { 22 | 23 | private static final String CONTENT_URI = "content://com.teslacoilsw.notifier/unread_count"; 24 | private static final String COUNT = "count"; 25 | private static final String TAG = "tag"; 26 | 27 | @Override 28 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 29 | ContentValues contentValues = new ContentValues(); 30 | contentValues.put(TAG, componentName.getPackageName() + "/" + componentName.getClassName()); 31 | contentValues.put(COUNT, badgeCount); 32 | context.getContentResolver().insert(Uri.parse(CONTENT_URI), contentValues); 33 | } 34 | 35 | @Override 36 | public List getSupportLaunchers() { 37 | return Arrays.asList("com.teslacoilsw.launcher"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/OPPOHomeBader.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.ComponentName; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.pm.ProviderInfo; 8 | import android.net.Uri; 9 | import android.os.Build; 10 | import android.os.Bundle; 11 | import java.util.Collections; 12 | import java.util.List; 13 | 14 | import me.leolin.shortcutbadger.Badger; 15 | import me.leolin.shortcutbadger.ShortcutBadgeException; 16 | import me.leolin.shortcutbadger.util.BroadcastHelper; 17 | 18 | /** 19 | * Created by NingSo on 2016/10/14.上午10:09 20 | * 21 | * @author: NingSo 22 | * Email: ningso.ping@gmail.com 23 | */ 24 | 25 | public class OPPOHomeBader implements Badger { 26 | 27 | private static final String PROVIDER_CONTENT_URI = "content://com.android.badge/badge"; 28 | private static final String INTENT_ACTION = "com.oppo.unsettledevent"; 29 | private static final String INTENT_EXTRA_PACKAGENAME = "pakeageName"; 30 | private static final String INTENT_EXTRA_BADGE_COUNT = "number"; 31 | private static final String INTENT_EXTRA_BADGE_UPGRADENUMBER = "upgradeNumber"; 32 | private static final String INTENT_EXTRA_BADGEUPGRADE_COUNT = "app_badge_count"; 33 | private int mCurrentTotalCount = -1; 34 | 35 | @Override 36 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 37 | if (mCurrentTotalCount == badgeCount) { 38 | return; 39 | } 40 | mCurrentTotalCount = badgeCount; 41 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){ 42 | executeBadgeByContentProvider(context, badgeCount); 43 | } else { 44 | executeBadgeByBroadcast(context, componentName, badgeCount); 45 | } 46 | } 47 | 48 | @Override 49 | public List getSupportLaunchers() { 50 | return Collections.singletonList("com.oppo.launcher"); 51 | } 52 | 53 | private void executeBadgeByBroadcast(Context context, ComponentName componentName, 54 | int badgeCount) throws ShortcutBadgeException { 55 | if (badgeCount == 0) { 56 | badgeCount = -1; 57 | } 58 | Intent intent = new Intent(INTENT_ACTION); 59 | intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName()); 60 | intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount); 61 | intent.putExtra(INTENT_EXTRA_BADGE_UPGRADENUMBER, badgeCount); 62 | 63 | BroadcastHelper.sendIntentExplicitly(context, intent); 64 | } 65 | 66 | /** 67 | * Send request to OPPO badge content provider to set badge in OPPO home launcher. 68 | * 69 | * @param context the context to use 70 | * @param badgeCount the badge count 71 | */ 72 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 73 | private void executeBadgeByContentProvider(Context context, int badgeCount) throws ShortcutBadgeException { 74 | try { 75 | Bundle extras = new Bundle(); 76 | extras.putInt(INTENT_EXTRA_BADGEUPGRADE_COUNT, badgeCount); 77 | context.getContentResolver().call(Uri.parse(PROVIDER_CONTENT_URI), "setAppBadgeCount", null, extras); 78 | } catch (Throwable ignored) { 79 | throw new ShortcutBadgeException("Unable to execute Badge By Content Provider"); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/SamsungHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.ContentResolver; 5 | import android.content.ContentValues; 6 | import android.content.Context; 7 | import android.database.Cursor; 8 | import android.net.Uri; 9 | import android.os.Build; 10 | 11 | import java.util.Arrays; 12 | import java.util.List; 13 | 14 | import me.leolin.shortcutbadger.Badger; 15 | import me.leolin.shortcutbadger.ShortcutBadgeException; 16 | import me.leolin.shortcutbadger.util.CloseHelper; 17 | 18 | /** 19 | * @author Leo Lin 20 | */ 21 | public class SamsungHomeBadger implements Badger { 22 | private static final String CONTENT_URI = "content://com.sec.badge/apps?notify=true"; 23 | private static final String[] CONTENT_PROJECTION = new String[]{"_id", "class"}; 24 | 25 | private DefaultBadger defaultBadger; 26 | 27 | public SamsungHomeBadger() { 28 | if (Build.VERSION.SDK_INT >= 21) { 29 | defaultBadger = new DefaultBadger(); 30 | } 31 | } 32 | 33 | @Override 34 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 35 | if (defaultBadger != null && defaultBadger.isSupported(context)) { 36 | defaultBadger.executeBadge(context, componentName, badgeCount); 37 | } else { 38 | Uri mUri = Uri.parse(CONTENT_URI); 39 | ContentResolver contentResolver = context.getContentResolver(); 40 | Cursor cursor = null; 41 | try { 42 | cursor = contentResolver.query(mUri, CONTENT_PROJECTION, "package=?", new String[]{componentName.getPackageName()}, null); 43 | if (cursor != null) { 44 | String entryActivityName = componentName.getClassName(); 45 | boolean entryActivityExist = false; 46 | while (cursor.moveToNext()) { 47 | int id = cursor.getInt(0); 48 | ContentValues contentValues = getContentValues(componentName, badgeCount, false); 49 | contentResolver.update(mUri, contentValues, "_id=?", new String[]{String.valueOf(id)}); 50 | if (entryActivityName.equals(cursor.getString(cursor.getColumnIndex("class")))) { 51 | entryActivityExist = true; 52 | } 53 | } 54 | 55 | if (!entryActivityExist) { 56 | ContentValues contentValues = getContentValues(componentName, badgeCount, true); 57 | contentResolver.insert(mUri, contentValues); 58 | } 59 | } 60 | } finally { 61 | CloseHelper.close(cursor); 62 | } 63 | } 64 | } 65 | 66 | private ContentValues getContentValues(ComponentName componentName, int badgeCount, boolean isInsert) { 67 | ContentValues contentValues = new ContentValues(); 68 | if (isInsert) { 69 | contentValues.put("package", componentName.getPackageName()); 70 | contentValues.put("class", componentName.getClassName()); 71 | } 72 | 73 | contentValues.put("badgecount", badgeCount); 74 | 75 | return contentValues; 76 | } 77 | 78 | @Override 79 | public List getSupportLaunchers() { 80 | return Arrays.asList( 81 | "com.sec.android.app.launcher", 82 | "com.sec.android.app.twlauncher" 83 | ); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/SonyHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.AsyncQueryHandler; 4 | import android.content.ComponentName; 5 | import android.content.ContentValues; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.pm.ProviderInfo; 9 | import android.net.Uri; 10 | import android.os.Looper; 11 | 12 | import java.util.Arrays; 13 | import java.util.List; 14 | 15 | import me.leolin.shortcutbadger.Badger; 16 | import me.leolin.shortcutbadger.ShortcutBadgeException; 17 | 18 | 19 | /** 20 | * @author Leo Lin 21 | */ 22 | public class SonyHomeBadger implements Badger { 23 | 24 | private static final String INTENT_ACTION = "com.sonyericsson.home.action.UPDATE_BADGE"; 25 | private static final String INTENT_EXTRA_PACKAGE_NAME = "com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME"; 26 | private static final String INTENT_EXTRA_ACTIVITY_NAME = "com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME"; 27 | private static final String INTENT_EXTRA_MESSAGE = "com.sonyericsson.home.intent.extra.badge.MESSAGE"; 28 | private static final String INTENT_EXTRA_SHOW_MESSAGE = "com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE"; 29 | 30 | private static final String PROVIDER_CONTENT_URI = "content://com.sonymobile.home.resourceprovider/badge"; 31 | private static final String PROVIDER_COLUMNS_BADGE_COUNT = "badge_count"; 32 | private static final String PROVIDER_COLUMNS_PACKAGE_NAME = "package_name"; 33 | private static final String PROVIDER_COLUMNS_ACTIVITY_NAME = "activity_name"; 34 | private static final String SONY_HOME_PROVIDER_NAME = "com.sonymobile.home.resourceprovider"; 35 | private final Uri BADGE_CONTENT_URI = Uri.parse(PROVIDER_CONTENT_URI); 36 | 37 | private AsyncQueryHandler mQueryHandler; 38 | 39 | @Override 40 | public void executeBadge(Context context, ComponentName componentName, 41 | int badgeCount) throws ShortcutBadgeException { 42 | if (sonyBadgeContentProviderExists(context)) { 43 | executeBadgeByContentProvider(context, componentName, badgeCount); 44 | } else { 45 | executeBadgeByBroadcast(context, componentName, badgeCount); 46 | } 47 | } 48 | 49 | @Override 50 | public List getSupportLaunchers() { 51 | return Arrays.asList("com.sonyericsson.home", "com.sonymobile.home"); 52 | } 53 | 54 | private static void executeBadgeByBroadcast(Context context, ComponentName componentName, 55 | int badgeCount) { 56 | Intent intent = new Intent(INTENT_ACTION); 57 | intent.putExtra(INTENT_EXTRA_PACKAGE_NAME, componentName.getPackageName()); 58 | intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName()); 59 | intent.putExtra(INTENT_EXTRA_MESSAGE, String.valueOf(badgeCount)); 60 | intent.putExtra(INTENT_EXTRA_SHOW_MESSAGE, badgeCount > 0); 61 | context.sendBroadcast(intent); 62 | } 63 | 64 | /** 65 | * Send request to Sony badge content provider to set badge in Sony home launcher. 66 | * 67 | * @param context the context to use 68 | * @param componentName the componentName to use 69 | * @param badgeCount the badge count 70 | */ 71 | private void executeBadgeByContentProvider(Context context, ComponentName componentName, 72 | int badgeCount) { 73 | if (badgeCount < 0) { 74 | return; 75 | } 76 | 77 | final ContentValues contentValues = createContentValues(badgeCount, componentName); 78 | if (Looper.myLooper() == Looper.getMainLooper()) { 79 | // We're in the main thread. Let's ensure the badge update happens in a background 80 | // thread by using an AsyncQueryHandler and an async update. 81 | if (mQueryHandler == null) { 82 | mQueryHandler = new AsyncQueryHandler( 83 | context.getApplicationContext().getContentResolver()) { 84 | }; 85 | } 86 | insertBadgeAsync(contentValues); 87 | } else { 88 | // Already in a background thread. Let's update the badge synchronously. Otherwise, 89 | // if we use the AsyncQueryHandler, this thread may already be dead by the time the 90 | // async execution finishes, which will lead to an IllegalStateException. 91 | insertBadgeSync(context, contentValues); 92 | } 93 | } 94 | 95 | /** 96 | * Asynchronously inserts the badge counter. 97 | * 98 | * @param contentValues Content values containing the badge count, package and activity names 99 | */ 100 | private void insertBadgeAsync(final ContentValues contentValues) { 101 | mQueryHandler.startInsert(0, null, BADGE_CONTENT_URI, contentValues); 102 | } 103 | 104 | /** 105 | * Synchronously inserts the badge counter. 106 | * 107 | * @param context Caller context 108 | * @param contentValues Content values containing the badge count, package and activity names 109 | */ 110 | private void insertBadgeSync(final Context context, final ContentValues contentValues) { 111 | context.getApplicationContext().getContentResolver() 112 | .insert(BADGE_CONTENT_URI, contentValues); 113 | } 114 | 115 | /** 116 | * Creates a ContentValues object to be used in the badge counter update. The package and 117 | * activity names must correspond to an activity that holds an intent filter with action 118 | * "android.intent.action.MAIN" and category android.intent.category.LAUNCHER" in the manifest. 119 | * Also, it is not allowed to publish badges on behalf of another client, so the package and 120 | * activity names must belong to the process from which the insert is made. 121 | * To be able to insert badges, the app must have the PROVIDER_INSERT_BADGE 122 | * permission in the manifest file. In case these conditions are not 123 | * fulfilled, or any content values are missing, there will be an unhandled 124 | * exception on the background thread. 125 | * 126 | * @param badgeCount the badge count 127 | * @param componentName the component name from which package and class name will be extracted 128 | * 129 | */ 130 | private ContentValues createContentValues(final int badgeCount, 131 | final ComponentName componentName) { 132 | final ContentValues contentValues = new ContentValues(); 133 | contentValues.put(PROVIDER_COLUMNS_BADGE_COUNT, badgeCount); 134 | contentValues.put(PROVIDER_COLUMNS_PACKAGE_NAME, componentName.getPackageName()); 135 | contentValues.put(PROVIDER_COLUMNS_ACTIVITY_NAME, componentName.getClassName()); 136 | return contentValues; 137 | } 138 | 139 | /** 140 | * Check if the latest Sony badge content provider exists . 141 | * 142 | * @param context the context to use 143 | * @return true if Sony badge content provider exists, otherwise false. 144 | */ 145 | private static boolean sonyBadgeContentProviderExists(Context context) { 146 | boolean exists = false; 147 | ProviderInfo info = context.getPackageManager().resolveContentProvider(SONY_HOME_PROVIDER_NAME, 0); 148 | if (info != null) { 149 | exists = true; 150 | } 151 | return exists; 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/VivoHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import me.leolin.shortcutbadger.Badger; 11 | import me.leolin.shortcutbadger.ShortcutBadgeException; 12 | 13 | /** 14 | * @author leolin 15 | */ 16 | public class VivoHomeBadger implements Badger { 17 | 18 | @Override 19 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 20 | Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM"); 21 | intent.putExtra("packageName", context.getPackageName()); 22 | intent.putExtra("className", componentName.getClassName()); 23 | intent.putExtra("notificationNum", badgeCount); 24 | context.sendBroadcast(intent); 25 | } 26 | 27 | @Override 28 | public List getSupportLaunchers() { 29 | return Arrays.asList("com.vivo.launcher"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/XiaomiHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.Notification; 5 | import android.app.NotificationManager; 6 | import android.content.ComponentName; 7 | import android.content.Context; 8 | import android.content.Intent; 9 | import android.content.pm.PackageManager; 10 | import android.content.pm.ResolveInfo; 11 | import android.os.Build; 12 | 13 | import java.lang.reflect.Field; 14 | import java.lang.reflect.Method; 15 | import java.util.Arrays; 16 | import java.util.List; 17 | 18 | import me.leolin.shortcutbadger.Badger; 19 | import me.leolin.shortcutbadger.ShortcutBadgeException; 20 | import me.leolin.shortcutbadger.util.BroadcastHelper; 21 | 22 | 23 | /** 24 | * @author leolin 25 | */ 26 | @Deprecated 27 | public class XiaomiHomeBadger implements Badger { 28 | 29 | public static final String INTENT_ACTION = "android.intent.action.APPLICATION_MESSAGE_UPDATE"; 30 | public static final String EXTRA_UPDATE_APP_COMPONENT_NAME = "android.intent.extra.update_application_component_name"; 31 | public static final String EXTRA_UPDATE_APP_MSG_TEXT = "android.intent.extra.update_application_message_text"; 32 | private ResolveInfo resolveInfo; 33 | 34 | @Override 35 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 36 | try { 37 | Class miuiNotificationClass = Class.forName("android.app.MiuiNotification"); 38 | Object miuiNotification = miuiNotificationClass.newInstance(); 39 | Field field = miuiNotification.getClass().getDeclaredField("messageCount"); 40 | field.setAccessible(true); 41 | try { 42 | field.set(miuiNotification, String.valueOf(badgeCount == 0 ? "" : badgeCount)); 43 | } catch (Exception e) { 44 | field.set(miuiNotification, badgeCount); 45 | } 46 | } catch (Exception e) { 47 | Intent localIntent = new Intent( 48 | INTENT_ACTION); 49 | localIntent.putExtra(EXTRA_UPDATE_APP_COMPONENT_NAME, componentName.getPackageName() + "/" + componentName.getClassName()); 50 | localIntent.putExtra(EXTRA_UPDATE_APP_MSG_TEXT, String.valueOf(badgeCount == 0 ? "" : badgeCount)); 51 | 52 | try { 53 | BroadcastHelper.sendIntentExplicitly(context, localIntent); 54 | } catch (ShortcutBadgeException ignored) {} 55 | } 56 | if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) { 57 | tryNewMiuiBadge(context, badgeCount); 58 | } 59 | } 60 | 61 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 62 | private void tryNewMiuiBadge(Context context, int badgeCount) throws ShortcutBadgeException { 63 | if (resolveInfo == null) { 64 | Intent intent = new Intent(Intent.ACTION_MAIN); 65 | intent.addCategory(Intent.CATEGORY_HOME); 66 | resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); 67 | } 68 | 69 | if (resolveInfo != null) { 70 | NotificationManager mNotificationManager = (NotificationManager) context 71 | .getSystemService(Context.NOTIFICATION_SERVICE); 72 | Notification.Builder builder = new Notification.Builder(context) 73 | .setContentTitle("") 74 | .setContentText("") 75 | .setSmallIcon(resolveInfo.getIconResource()); 76 | Notification notification = builder.build(); 77 | try { 78 | Field field = notification.getClass().getDeclaredField("extraNotification"); 79 | Object extraNotification = field.get(notification); 80 | Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); 81 | method.invoke(extraNotification, badgeCount); 82 | mNotificationManager.notify(0, notification); 83 | } catch (Exception e) { 84 | throw new ShortcutBadgeException("not able to set badge", e); 85 | } 86 | } 87 | } 88 | 89 | @Override 90 | public List getSupportLaunchers() { 91 | return Arrays.asList( 92 | "com.miui.miuilite", 93 | "com.miui.home", 94 | "com.miui.miuihome", 95 | "com.miui.miuihome2", 96 | "com.miui.mihome", 97 | "com.miui.mihome2", 98 | "com.i.miui.launcher" 99 | ); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/ZTEHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.content.ComponentName; 4 | import android.content.Context; 5 | import android.net.Uri; 6 | import android.os.Build; 7 | import android.os.Bundle; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import me.leolin.shortcutbadger.Badger; 13 | import me.leolin.shortcutbadger.ShortcutBadgeException; 14 | 15 | public class ZTEHomeBadger implements Badger { 16 | 17 | @Override 18 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) 19 | throws ShortcutBadgeException { 20 | Bundle extra = new Bundle(); 21 | extra.putInt("app_badge_count", badgeCount); 22 | extra.putString("app_badge_component_name", componentName.flattenToString()); 23 | 24 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 25 | context.getContentResolver().call( 26 | Uri.parse("content://com.android.launcher3.cornermark.unreadbadge"), 27 | "setAppUnreadCount", null, extra); 28 | } 29 | } 30 | 31 | @Override 32 | public List getSupportLaunchers() { 33 | return new ArrayList(0); 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/impl/ZukHomeBadger.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.impl; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.ComponentName; 5 | import android.content.Context; 6 | import android.net.Uri; 7 | import android.os.Build; 8 | import android.os.Bundle; 9 | 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | import me.leolin.shortcutbadger.Badger; 14 | import me.leolin.shortcutbadger.ShortcutBadgeException; 15 | 16 | /** 17 | * Created by wuxuejian on 2016/10/9. 18 | * 需在设置 -- 通知和状态栏 -- 应用角标管理 中开启应用 19 | */ 20 | 21 | public class ZukHomeBadger implements Badger { 22 | 23 | private final Uri CONTENT_URI = Uri.parse("content://com.android.badge/badge"); 24 | 25 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 26 | @Override 27 | public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException { 28 | Bundle extra = new Bundle(); 29 | extra.putInt("app_badge_count", badgeCount); 30 | context.getContentResolver().call(CONTENT_URI, "setAppBadgeCount", null, extra); 31 | } 32 | 33 | @Override 34 | public List getSupportLaunchers() { 35 | return Collections.singletonList("com.zui.launcher"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/util/BroadcastHelper.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.util; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.content.pm.PackageManager; 6 | import android.content.pm.ResolveInfo; 7 | import android.os.Build; 8 | 9 | import java.util.Collections; 10 | import java.util.List; 11 | 12 | import me.leolin.shortcutbadger.ShortcutBadgeException; 13 | import me.leolin.shortcutbadger.impl.IntentConstants; 14 | 15 | /** 16 | * Created by mahijazi on 17/05/16. 17 | */ 18 | public class BroadcastHelper { 19 | 20 | public static List resolveBroadcast(Context context, Intent intent) { 21 | PackageManager packageManager = context.getPackageManager(); 22 | List receivers = packageManager.queryBroadcastReceivers(intent, 0); 23 | 24 | return receivers != null ? receivers : Collections.emptyList(); 25 | } 26 | 27 | public static void sendIntentExplicitly(Context context, Intent intent) throws ShortcutBadgeException { 28 | List resolveInfos = resolveBroadcast(context, intent); 29 | 30 | if (resolveInfos.size() == 0) { 31 | throw new ShortcutBadgeException("unable to resolve intent: " + intent.toString()); 32 | } 33 | 34 | for (ResolveInfo info : resolveInfos) { 35 | Intent actualIntent = new Intent(intent); 36 | 37 | if (info != null) { 38 | actualIntent.setPackage(info.resolvePackageName); 39 | context.sendBroadcast(actualIntent); 40 | } 41 | } 42 | } 43 | 44 | public static void sendDefaultIntentExplicitly(Context context, Intent intent) throws ShortcutBadgeException { 45 | boolean oreoIntentSuccess = false; 46 | 47 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 48 | Intent oreoIntent = new Intent(intent); 49 | 50 | oreoIntent.setAction(IntentConstants.DEFAULT_OREO_INTENT_ACTION); 51 | 52 | try { 53 | sendIntentExplicitly(context, oreoIntent); 54 | oreoIntentSuccess = true; 55 | } catch (ShortcutBadgeException e) { 56 | oreoIntentSuccess = false; 57 | } 58 | } 59 | 60 | if (oreoIntentSuccess) { 61 | return; 62 | } 63 | 64 | // try pre-Oreo default intent 65 | sendIntentExplicitly(context, intent); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/java/me/leolin/shortcutbadger/util/CloseHelper.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger.util; 2 | 3 | import android.database.Cursor; 4 | 5 | import java.io.Closeable; 6 | import java.io.IOException; 7 | 8 | /** 9 | * @author leolin 10 | */ 11 | public class CloseHelper { 12 | 13 | public static void close(Cursor cursor) { 14 | if (cursor != null && !cursor.isClosed()) { 15 | cursor.close(); 16 | } 17 | } 18 | 19 | 20 | public static void closeQuietly(Closeable closeable) { 21 | try { 22 | if (closeable != null) { 23 | closeable.close(); 24 | } 25 | } catch (IOException var2) { 26 | 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /shortcutbadger/src/main/res/drawable-hdpi/badger_notification_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/shortcutbadger/src/main/res/drawable-hdpi/badger_notification_icon.png -------------------------------------------------------------------------------- /shortcutbadger/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ShortcutBadger 3 | 4 | -------------------------------------------------------------------------------- /shortcutbadger/src/test/java/me/leolin/shortcutbadger/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package me.leolin.shortcutbadger; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /安装包/app-debug.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haiyuKing/ShortcutBadgerDemo/3e789fb9236c97b06b39916fec0f84c02f995090/安装包/app-debug.apk --------------------------------------------------------------------------------