Skip to content

Commit

Permalink
Merge pull request leolin310148#51 from MajeurAndroid/master
Browse files Browse the repository at this point in the history
Fixed context leak issue
  • Loading branch information
leolin310148 committed Jan 7, 2016
2 parents e2ab8e9 + 73d4928 commit e1e4d23
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 172 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ USAGE
<br/>3. Add the codes below:

int badgeCount = 1;
ShortcutBadger.with(getApplicationContext()).count(badgeCount);
ShortcutBadger.applyCount(context, badgeCount);
<br/>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);
<br/>
<br/>
<br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
}
});

Expand Down
6 changes: 6 additions & 0 deletions SampleApp/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@
android:text="Set badge"
android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>

<Button android:id="@+id/btnRemoveBadge"
android:layout_below="@id/btnSetBadge"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Remove badge"
android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>


</RelativeLayout>
25 changes: 25 additions & 0 deletions ShortcutBadger/src/main/java/me/leolin/shortcutbadger/Badger.java
Original file line number Diff line number Diff line change
@@ -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<String>} containing supported launchers package names
*/
List<String> getSupportLaunchers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<? extends ShortcutBadger>> BADGERS = new LinkedList<Class<? extends ShortcutBadger>>();
private static final List<Class<? extends Badger>> BADGERS = new LinkedList<Class<? extends Badger>>();

static {
BADGERS.add(AdwHomeBadger.class);
Expand All @@ -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
Expand All @@ -66,62 +101,30 @@ 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;
}
}
} catch (Exception e) {
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<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down
Loading

0 comments on commit e1e4d23

Please sign in to comment.