diff --git a/README.md b/README.md
index 373eb13..ab6a8b6 100644
--- a/README.md
+++ b/README.md
@@ -91,14 +91,14 @@ USAGE
3. Add the codes below:
int badgeCount = 1;
- ShortcutBadger.with(getApplicationContext()).count(badgeCount);
+ ShortcutBadger.applyCount(context, badgeCount);
4. If you want to remove the badge
- ShortcutBadger.with(getApplicationContext()).remove();
+ ShortcutBadger.removeCount(context);
or
- ShortcutBadger.with(getApplicationContext()).count(0);
+ ShortcutBadger.applyCount(context, 0);
diff --git a/SampleApp/src/main/java/me/leolin/shortcutbadger/example/MainActivity.java b/SampleApp/src/main/java/me/leolin/shortcutbadger/example/MainActivity.java
index 93cdcd3..387b00e 100644
--- a/SampleApp/src/main/java/me/leolin/shortcutbadger/example/MainActivity.java
+++ b/SampleApp/src/main/java/me/leolin/shortcutbadger/example/MainActivity.java
@@ -11,18 +11,17 @@
import android.widget.TextView;
import android.widget.Toast;
import me.leolin.shortcutbadger.ShortcutBadger;
+import me.leolin.shortcutbadger.impl.ApexHomeBadger;
public class MainActivity extends Activity {
- private EditText numInput;
-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
- numInput = (EditText) findViewById(R.id.numInput);
+ final EditText numInput = (EditText) findViewById(R.id.numInput);
Button button = (Button) findViewById(R.id.btnSetBadge);
button.setOnClickListener(new View.OnClickListener() {
@@ -35,10 +34,19 @@ public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Error input", Toast.LENGTH_SHORT).show();
}
-// ShortcutBadger.setBadge(getApplicationContext(), badgeCount);
- ShortcutBadger.with(getApplicationContext()).count(badgeCount);
+ boolean success = ShortcutBadger.applyCount(MainActivity.this, badgeCount);
+
+ Toast.makeText(getApplicationContext(), "Set count=" + badgeCount + ", success=" + success, Toast.LENGTH_SHORT).show();
+ }
+ });
+
+ Button removeBadgeBtn = (Button) findViewById(R.id.btnRemoveBadge);
+ removeBadgeBtn.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ boolean success = ShortcutBadger.removeCount(MainActivity.this);
- Toast.makeText(getApplicationContext(), "Set count=" + badgeCount, Toast.LENGTH_SHORT).show();
+ Toast.makeText(getApplicationContext(), "success=" + success, Toast.LENGTH_SHORT).show();
}
});
diff --git a/SampleApp/src/main/res/layout/activity_main.xml b/SampleApp/src/main/res/layout/activity_main.xml
index fdc9b49..b069a8e 100644
--- a/SampleApp/src/main/res/layout/activity_main.xml
+++ b/SampleApp/src/main/res/layout/activity_main.xml
@@ -27,5 +27,11 @@
android:text="Set badge"
android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>
+
+
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/Badger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/Badger.java
new file mode 100644
index 0000000..89822a1
--- /dev/null
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/Badger.java
@@ -0,0 +1,25 @@
+package me.leolin.shortcutbadger;
+
+import android.content.ComponentName;
+import android.content.Context;
+
+import java.util.List;
+
+public interface Badger {
+
+ /**
+ * Called when user attempts to update notification count
+ * @param context Caller context
+ * @param componentName Component containing package and class name of calling application's
+ * launcher activity
+ * @param badgeCount Desired notification count
+ * @throws ShortcutBadgeException
+ */
+ void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException;
+
+ /**
+ * Called to let {@link ShortcutBadger} knows which launchers are supported by this badger. It should return a
+ * {@link List} containing supported launchers package names
+ */
+ List getSupportLaunchers();
+}
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/ShortcutBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/ShortcutBadger.java
index ec38ba1..1f122a9 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/ShortcutBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/ShortcutBadger.java
@@ -17,11 +17,11 @@
/**
* @author Leo Lin
*/
-public abstract class ShortcutBadger {
+public final class ShortcutBadger {
private static final String LOG_TAG = ShortcutBadger.class.getSimpleName();
- private static final List> BADGERS = new LinkedList>();
+ private static final List> BADGERS = new LinkedList>();
static {
BADGERS.add(AdwHomeBadger.class);
@@ -36,26 +36,61 @@ public abstract class ShortcutBadger {
// BADGERS.add(SamsungHomeBadger.class);
}
- private static ShortcutBadger mShortcutBadger;
+ private static Badger sShortcutBadger;
+ private static ComponentName sComponentName;
- public static ShortcutBadger with(Context context) {
- return getShortcutBadger(context);
+ /**
+ * Tries to update the notification count
+ * @param context Caller context
+ * @param badgeCount Desired badge count
+ * @return true in case of success, false otherwise
+ */
+ public static boolean applyCount(Context context, int badgeCount) {
+ try {
+ applyCountOrThrow(context, badgeCount);
+ return true;
+ } catch (ShortcutBadgeException e) {
+ Log.e(LOG_TAG, "Unable to execute badge:" + e.getMessage());
+ return false;
+ }
}
+ /**
+ * Tries to update the notification count, throw a {@link ShortcutBadgeException} if it fails
+ * @param context Caller context
+ * @param badgeCount Desired badge count
+ */
+ public static void applyCountOrThrow(Context context, int badgeCount) throws ShortcutBadgeException {
+ if (sShortcutBadger == null)
+ initBadger(context);
- public static void setBadge(Context context, int badgeCount) throws ShortcutBadgeException {
try {
- getShortcutBadger(context).executeBadge(badgeCount);
+ sShortcutBadger.executeBadge(context, sComponentName, badgeCount);
} catch (Throwable e) {
throw new ShortcutBadgeException("Unable to execute badge:" + e.getMessage());
}
+ }
+ /**
+ * Tries to remove the notification count
+ * @param context Caller context
+ * @return true in case of success, false otherwise
+ */
+ public static boolean removeCount(Context context) {
+ return applyCount(context, 0);
}
- private static ShortcutBadger getShortcutBadger(Context context) {
- if (mShortcutBadger != null) {
- return mShortcutBadger;
- }
+ /**
+ * Tries to remove the notification count, throw a {@link ShortcutBadgeException} if it fails
+ * @param context Caller context
+ */
+ public static void removeCountOrThrow(Context context) throws ShortcutBadgeException {
+ applyCountOrThrow(context, 0);
+ }
+
+ private static void initBadger(Context context) {
+ sComponentName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent();
+
Log.d(LOG_TAG, "Finding badger");
//find the home launcher Package
@@ -66,15 +101,14 @@ private static ShortcutBadger getShortcutBadger(Context context) {
String currentHomePackage = resolveInfo.activityInfo.packageName;
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
- mShortcutBadger = new XiaomiHomeBadger(context);
- return mShortcutBadger;
+ sShortcutBadger = new XiaomiHomeBadger();
+ return;
}
- for (Class extends ShortcutBadger> badger : BADGERS) {
- Constructor extends ShortcutBadger> constructor = badger.getConstructor(Context.class);
- ShortcutBadger shortcutBadger = constructor.newInstance(context);
+ for (Class extends Badger> badger : BADGERS) {
+ Badger shortcutBadger = badger.newInstance();
if (shortcutBadger.getSupportLaunchers().contains(currentHomePackage)) {
- mShortcutBadger = shortcutBadger;
+ sShortcutBadger = shortcutBadger;
break;
}
}
@@ -82,46 +116,15 @@ private static ShortcutBadger getShortcutBadger(Context context) {
Log.e(LOG_TAG, e.getMessage(), e);
}
- if (mShortcutBadger == null) {
- mShortcutBadger = new DefaultBadger(context);
- }
-
- Log.d(LOG_TAG, "Returning badger:" + mShortcutBadger.getClass().getCanonicalName());
- return mShortcutBadger;
- }
-
-
- private ShortcutBadger() {
- }
-
- protected Context mContext;
-
- protected ShortcutBadger(Context context) {
- this.mContext = context;
- }
+ if (sShortcutBadger == null)
+ sShortcutBadger = new DefaultBadger();
- protected abstract void executeBadge(int badgeCount) throws ShortcutBadgeException;
-
- protected abstract List getSupportLaunchers();
-
- protected String getEntryActivityName() {
- ComponentName componentName = mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName()).getComponent();
- return componentName.getClassName();
- }
- protected String getContextPackageName() {
- return mContext.getPackageName();
+ Log.d(LOG_TAG, "Current badger:" + sShortcutBadger.getClass().getCanonicalName());
}
- public void count(int count) {
- try {
- executeBadge(count);
- } catch (Exception e) {
- Log.e(LOG_TAG, e.getMessage(), e);
- }
- }
+ // Avoid anybody to instantiate this class
+ private ShortcutBadger() {
- public void remove() {
- count(0);
}
}
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AdwHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AdwHomeBadger.java
index 569daa8..7c5165e 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AdwHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AdwHomeBadger.java
@@ -1,7 +1,10 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
@@ -11,23 +14,19 @@
/**
* @author Gernot Pansy
*/
-public class AdwHomeBadger extends ShortcutBadger {
+public class AdwHomeBadger implements Badger {
public static final String INTENT_UPDATE_COUNTER = "org.adw.launcher.counter.SEND";
public static final String PACKAGENAME = "PNAME";
public static final String COUNT = "COUNT";
- public AdwHomeBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent = new Intent(INTENT_UPDATE_COUNTER);
- intent.putExtra(PACKAGENAME, getContextPackageName());
+ intent.putExtra(PACKAGENAME, componentName.getPackageName());
intent.putExtra(COUNT, badgeCount);
- mContext.sendBroadcast(intent);
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/ApexHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/ApexHomeBadger.java
index d9f1df7..4e0d3b7 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/ApexHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/ApexHomeBadger.java
@@ -1,8 +1,10 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
@@ -12,25 +14,21 @@
/**
* @author Gernot Pansy
*/
-public class ApexHomeBadger extends ShortcutBadger {
+public class ApexHomeBadger implements Badger {
private static final String INTENT_UPDATE_COUNTER = "com.anddoes.launcher.COUNTER_CHANGED";
private static final String PACKAGENAME = "package";
private static final String COUNT = "count";
private static final String CLASS = "class";
- public ApexHomeBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent = new Intent(INTENT_UPDATE_COUNTER);
- intent.putExtra(PACKAGENAME, getContextPackageName());
+ intent.putExtra(PACKAGENAME, componentName.getPackageName());
intent.putExtra(COUNT, badgeCount);
- intent.putExtra(CLASS, getEntryActivityName());
- mContext.sendBroadcast(intent);
+ intent.putExtra(CLASS, componentName.getClassName());
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AsusHomeLauncher.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AsusHomeLauncher.java
index 96f3bd8..58a8f6b 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AsusHomeLauncher.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/AsusHomeLauncher.java
@@ -1,7 +1,10 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
@@ -11,25 +14,21 @@
/**
* @author leolin
*/
-public class AsusHomeLauncher extends ShortcutBadger {
+public class AsusHomeLauncher implements Badger {
private static final String INTENT_ACTION = "android.intent.action.BADGE_COUNT_UPDATE";
private static final String INTENT_EXTRA_BADGE_COUNT = "badge_count";
private static final String INTENT_EXTRA_PACKAGENAME = "badge_count_package_name";
private static final String INTENT_EXTRA_ACTIVITY_NAME = "badge_count_class_name";
- public AsusHomeLauncher(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent = new Intent(INTENT_ACTION);
intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount);
- intent.putExtra(INTENT_EXTRA_PACKAGENAME, getContextPackageName());
- intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, getEntryActivityName());
+ intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
+ intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName());
intent.putExtra("badge_vip_count", 0);
- mContext.sendBroadcast(intent);
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/DefaultBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/DefaultBadger.java
index 2963b5b..993db00 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/DefaultBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/DefaultBadger.java
@@ -1,7 +1,11 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+
+import me.leolin.shortcutbadger.Badger;
+import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
import java.util.ArrayList;
@@ -10,23 +14,19 @@
/**
* @author leolin
*/
-public class DefaultBadger extends ShortcutBadger {
+public class DefaultBadger implements Badger {
private static final String INTENT_ACTION = "android.intent.action.BADGE_COUNT_UPDATE";
private static final String INTENT_EXTRA_BADGE_COUNT = "badge_count";
private static final String INTENT_EXTRA_PACKAGENAME = "badge_count_package_name";
private static final String INTENT_EXTRA_ACTIVITY_NAME = "badge_count_class_name";
- public DefaultBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent = new Intent(INTENT_ACTION);
intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount);
- intent.putExtra(INTENT_EXTRA_PACKAGENAME, getContextPackageName());
- intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, getEntryActivityName());
- mContext.sendBroadcast(intent);
+ intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
+ intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName());
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/LGHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/LGHomeBadger.java
index ae3f919..818b4ad 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/LGHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/LGHomeBadger.java
@@ -1,7 +1,11 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+
+import me.leolin.shortcutbadger.Badger;
+import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
import java.util.Arrays;
@@ -12,24 +16,20 @@
* Deprecated, LG devices will use DefaultBadger
*/
@Deprecated
-public class LGHomeBadger extends ShortcutBadger {
+public class LGHomeBadger implements Badger {
private static final String INTENT_ACTION = "android.intent.action.BADGE_COUNT_UPDATE";
private static final String INTENT_EXTRA_BADGE_COUNT = "badge_count";
private static final String INTENT_EXTRA_PACKAGENAME = "badge_count_package_name";
private static final String INTENT_EXTRA_ACTIVITY_NAME = "badge_count_class_name";
- public LGHomeBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent = new Intent(INTENT_ACTION);
intent.putExtra(INTENT_EXTRA_BADGE_COUNT, badgeCount);
- intent.putExtra(INTENT_EXTRA_PACKAGENAME, getContextPackageName());
- intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, getEntryActivityName());
- mContext.sendBroadcast(intent);
+ intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
+ intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName());
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NewHtcHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NewHtcHomeBadger.java
index 3527215..de31612 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NewHtcHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NewHtcHomeBadger.java
@@ -3,6 +3,8 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
@@ -12,7 +14,7 @@
/**
* @author Leo Lin
*/
-public class NewHtcHomeBadger extends ShortcutBadger {
+public class NewHtcHomeBadger implements Badger {
public static final String INTENT_UPDATE_SHORTCUT = "com.htc.launcher.action.UPDATE_SHORTCUT";
public static final String INTENT_SET_NOTIFICATION = "com.htc.launcher.action.SET_NOTIFICATION";
@@ -21,23 +23,18 @@ public class NewHtcHomeBadger extends ShortcutBadger {
public static final String EXTRA_COMPONENT = "com.htc.launcher.extra.COMPONENT";
public static final String EXTRA_COUNT = "com.htc.launcher.extra.COUNT";
- public NewHtcHomeBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent1 = new Intent(INTENT_SET_NOTIFICATION);
- ComponentName localComponentName = new ComponentName(getContextPackageName(), getEntryActivityName());
- intent1.putExtra(EXTRA_COMPONENT, localComponentName.flattenToShortString());
+ intent1.putExtra(EXTRA_COMPONENT, componentName.flattenToShortString());
intent1.putExtra(EXTRA_COUNT, badgeCount);
- mContext.sendBroadcast(intent1);
+ context.sendBroadcast(intent1);
Intent intent = new Intent(INTENT_UPDATE_SHORTCUT);
- intent.putExtra(PACKAGENAME, getContextPackageName());
+ intent.putExtra(PACKAGENAME, componentName.getPackageName());
intent.putExtra(COUNT, badgeCount);
- mContext.sendBroadcast(intent);
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NovaHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NovaHomeBadger.java
index af244a9..6288a6b 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NovaHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/NovaHomeBadger.java
@@ -1,8 +1,11 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
+
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
@@ -16,23 +19,19 @@
* Date: 2014/11/03
* Time: 7:15
*/
-public class NovaHomeBadger extends ShortcutBadger {
+public class NovaHomeBadger implements Badger {
private static final String CONTENT_URI = "content://com.teslacoilsw.notifier/unread_count";
private static final String COUNT = "count";
private static final String TAG = "tag";
- public NovaHomeBadger(final Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(final int badgeCount) throws ShortcutBadgeException {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
try {
ContentValues contentValues = new ContentValues();
- contentValues.put(TAG, getContextPackageName() + "/" + getEntryActivityName());
+ contentValues.put(TAG, componentName.getPackageName() + "/" + componentName.getClassName());
contentValues.put(COUNT, badgeCount);
- mContext.getContentResolver().insert(Uri.parse(CONTENT_URI), contentValues);
+ context.getContentResolver().insert(Uri.parse(CONTENT_URI), contentValues);
} catch (IllegalArgumentException ex) {
/* Fine, TeslaUnread is not installed. */
} catch (Exception ex) {
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SamsungHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SamsungHomeBadger.java
index b962858..ee4c989 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SamsungHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SamsungHomeBadger.java
@@ -1,10 +1,13 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
+
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
import me.leolin.shortcutbadger.util.CloseHelper;
@@ -17,27 +20,23 @@
* Deprecated, Samesung devices will use DefaultBadger
*/
@Deprecated
-public class SamsungHomeBadger extends ShortcutBadger {
+public class SamsungHomeBadger implements Badger {
private static final String CONTENT_URI = "content://com.sec.badge/apps?notify=true";
private static final String[] CONTENT_PROJECTION = new String[]{"_id","class"};
- public SamsungHomeBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Uri mUri = Uri.parse(CONTENT_URI);
- ContentResolver contentResolver = mContext.getContentResolver();
+ ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = null;
try {
- cursor = contentResolver.query(mUri, CONTENT_PROJECTION, "package=?", new String[]{getContextPackageName()}, null);
+ cursor = contentResolver.query(mUri, CONTENT_PROJECTION, "package=?", new String[]{componentName.getPackageName()}, null);
if (cursor != null) {
- String entryActivityName = getEntryActivityName();
+ String entryActivityName = componentName.getClassName();
boolean entryActivityExist = false;
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
- ContentValues contentValues = getContentValues(badgeCount, false);
+ ContentValues contentValues = getContentValues(componentName, badgeCount, false);
contentResolver.update(mUri, contentValues, "_id=?", new String[]{String.valueOf(id)});
if (entryActivityName.equals(cursor.getString(cursor.getColumnIndex("class")))) {
entryActivityExist = true;
@@ -45,7 +44,7 @@ protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
}
if (!entryActivityExist) {
- ContentValues contentValues = getContentValues(badgeCount, true);
+ ContentValues contentValues = getContentValues(componentName, badgeCount, true);
contentResolver.insert(mUri, contentValues);
}
}
@@ -54,11 +53,11 @@ protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
}
}
- private ContentValues getContentValues(int badgeCount, boolean isInsert) {
+ private ContentValues getContentValues(ComponentName componentName, int badgeCount, boolean isInsert) {
ContentValues contentValues = new ContentValues();
if (isInsert) {
- contentValues.put("package", getContextPackageName());
- contentValues.put("class", getEntryActivityName());
+ contentValues.put("package", componentName.getPackageName());
+ contentValues.put("class", componentName.getClassName());
}
contentValues.put("badgecount", badgeCount);
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SolidHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SolidHomeBadger.java
index 7cdd27c..80f5af6 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SolidHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SolidHomeBadger.java
@@ -1,8 +1,10 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
@@ -12,24 +14,20 @@
/**
* @author MajeurAndroid
*/
-public class SolidHomeBadger extends ShortcutBadger {
+public class SolidHomeBadger implements Badger {
- private static final String INTENT_UPDATE_COUNTER = "com.majeur.launcher.intent.action.UPDATE_BADGE";
- private static final String PACKAGENAME = "com.majeur.launcher.intent.extra.BADGE_PACKAGE";
- private static final String COUNT = "com.majeur.launcher.intent.extra.BADGE_COUNT";
- private static final String CLASS = "com.majeur.launcher.intent.extra.BADGE_CLASS";
-
- public SolidHomeBadger(Context context) {
- super(context);
- }
+ private static final String INTENT_ACTION_UPDATE_COUNTER = "com.majeur.launcher.intent.action.UPDATE_BADGE";
+ private static final String INTENT_EXTRA_PACKAGENAME = "com.majeur.launcher.intent.extra.BADGE_PACKAGE";
+ private static final String INTENT_EXTRA_COUNT = "com.majeur.launcher.intent.extra.BADGE_COUNT";
+ private static final String INTENT_EXTRA_CLASS = "com.majeur.launcher.intent.extra.BADGE_CLASS";
@Override
- protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
- Intent intent = new Intent(INTENT_UPDATE_COUNTER);
- intent.putExtra(PACKAGENAME, getContextPackageName());
- intent.putExtra(COUNT, badgeCount);
- intent.putExtra(CLASS, getEntryActivityName());
- mContext.sendBroadcast(intent);
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
+ Intent intent = new Intent(INTENT_ACTION_UPDATE_COUNTER);
+ intent.putExtra(INTENT_EXTRA_PACKAGENAME, componentName.getPackageName());
+ intent.putExtra(INTENT_EXTRA_COUNT, badgeCount);
+ intent.putExtra(INTENT_EXTRA_CLASS, componentName.getClassName());
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SonyHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SonyHomeBadger.java
index 604d778..24ef94c 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SonyHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/SonyHomeBadger.java
@@ -1,7 +1,11 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+
+import me.leolin.shortcutbadger.Badger;
+import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
import java.util.Arrays;
@@ -10,7 +14,7 @@
/**
* @author Leo Lin
*/
-public class SonyHomeBadger extends ShortcutBadger {
+public class SonyHomeBadger implements Badger {
private static final String INTENT_ACTION = "com.sonyericsson.home.action.UPDATE_BADGE";
private static final String INTENT_EXTRA_PACKAGE_NAME = "com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME";
@@ -18,19 +22,14 @@ public class SonyHomeBadger extends ShortcutBadger {
private static final String INTENT_EXTRA_MESSAGE = "com.sonyericsson.home.intent.extra.badge.MESSAGE";
private static final String INTENT_EXTRA_SHOW_MESSAGE = "com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE";
-
- public SonyHomeBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
Intent intent = new Intent(INTENT_ACTION);
- intent.putExtra(INTENT_EXTRA_PACKAGE_NAME, getContextPackageName());
- intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, getEntryActivityName());
+ intent.putExtra(INTENT_EXTRA_PACKAGE_NAME, componentName.getPackageName());
+ intent.putExtra(INTENT_EXTRA_ACTIVITY_NAME, componentName.getClassName());
intent.putExtra(INTENT_EXTRA_MESSAGE, String.valueOf(badgeCount));
intent.putExtra(INTENT_EXTRA_SHOW_MESSAGE, badgeCount > 0);
- mContext.sendBroadcast(intent);
+ context.sendBroadcast(intent);
}
@Override
diff --git a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/XiaomiHomeBadger.java b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/XiaomiHomeBadger.java
index a8efdc3..936de85 100644
--- a/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/XiaomiHomeBadger.java
+++ b/ShortcutBadger/src/main/java/me/leolin/shortcutbadger/impl/XiaomiHomeBadger.java
@@ -1,7 +1,10 @@
package me.leolin.shortcutbadger.impl;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+
+import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;
@@ -12,18 +15,14 @@
/**
* @author leolin
*/
-public class XiaomiHomeBadger extends ShortcutBadger {
+public class XiaomiHomeBadger implements Badger {
public static final String INTENT_ACTION = "android.intent.action.APPLICATION_MESSAGE_UPDATE";
public static final String EXTRA_UPDATE_APP_COMPONENT_NAME = "android.intent.extra.update_application_component_name";
public static final String EXTRA_UPDATE_APP_MSG_TEXT = "android.intent.extra.update_application_message_text";
- public XiaomiHomeBadger(Context context) {
- super(context);
- }
-
@Override
- protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
+ public void executeBadge(Context context, ComponentName componentName, int badgeCount) throws ShortcutBadgeException {
try {
Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
Object miuiNotification = miuiNotificationClass.newInstance();
@@ -33,9 +32,9 @@ protected void executeBadge(int badgeCount) throws ShortcutBadgeException {
} catch (Exception e) {
Intent localIntent = new Intent(
INTENT_ACTION);
- localIntent.putExtra(EXTRA_UPDATE_APP_COMPONENT_NAME, getContextPackageName() + "/" + getEntryActivityName());
+ localIntent.putExtra(EXTRA_UPDATE_APP_COMPONENT_NAME, componentName.getPackageName() + "/" + componentName.getClassName());
localIntent.putExtra(EXTRA_UPDATE_APP_MSG_TEXT, String.valueOf(badgeCount == 0 ? "" : badgeCount));
- mContext.sendBroadcast(localIntent);
+ context.sendBroadcast(localIntent);
}
}