Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created January 10, 2014 02:47
Show Gist options
  • Select an option

  • Save shaobin0604/8346172 to your computer and use it in GitHub Desktop.

Select an option

Save shaobin0604/8346172 to your computer and use it in GitHub Desktop.
duplicate app icon
public static boolean showAppsUpdateNotification(Context context , ArrayList<String> selectedAppId) {
boolean bAppUpdateNotificationEnabled = CtsdkSetting.getInstance(context).getAppUpdateNotificationEnabled();
if(!bAppUpdateNotificationEnabled) {
return false;
}
int icon = CtsdkSetting.getInstance(context).getNotificationIconResourceId();
if (0 >= icon) {
return false;
}
boolean result = false;
Resources res = context.getResources();
String packageName = context.getPackageName();
try {
NotificationManager nm = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
int count = AppsManager.getInstance(context).getUpdateAppsCount();
if (count == 0) {
nm.cancel(NOTIFICATION_ID_UPDATE);
return false;
}
List<BaseAppInfo> updateAppNames = AppsManager.getInstance(context).getAllUpdatableAppList();
if (null == updateAppNames || updateAppNames.size() < 1) {
return false;
}
android.app.Notification n = new android.app.Notification();
String title = "";
StringBuilder description;
int oftenUseCount = MySetting.getInstance(context).getOftenUseUpdateNum();
if (oftenUseCount == 0){
title = context.getString(res.getIdentifier("update_apps_notification", "string", packageName),count);
description = new StringBuilder();
for (BaseAppInfo info : updateAppNames) {
description.append(info.getName()+",");
}
Command appUpdateList = Command.parse(ACTION_APPUPDATELIST, null);
n.icon = icon;
n.tickerText = title;
n.when = System.currentTimeMillis();
n.flags |= android.app.Notification.FLAG_AUTO_CANCEL;
n.defaults = 0; // please be quiet
n.sound = null;
n.number = count;
Log.i(TAG, "packageName:"+packageName);
RemoteViews contentViews = new RemoteViews(packageName,res.getIdentifier("update_layout", "layout", packageName));
Log.i(TAG, "layoutId:"+res.getIdentifier("update_layout", "layout", packageName));
contentViews.setTextViewText(res.getIdentifier("update_title", "id", packageName), title);
Log.i(TAG, "titleId:"+res.getIdentifier("update_title", "id", packageName));
contentViews.setTextViewText(res.getIdentifier("update_content", "id", packageName), description.subSequence(0, description.length()-1));
Log.i(TAG, "contentId:"+res.getIdentifier("update_content", "id", packageName));
if (sShowAppCountInIcon) {
contentViews.setTextViewText(res.getIdentifier("updates_num", "id", packageName), count+"");
}
n.contentView = contentViews;
n.contentIntent = buildCommandIntent(context, NOTIFICATION_ID_UPDATE, appUpdateList, 0, false);
if (Integer.parseInt(Build.VERSION.SDK) >= VERSION_USE_NEW_UI) {
contentViews.setOnClickPendingIntent(res.getIdentifier("update_action", "id", packageName), buildBackgroundCommandIntent(context, res.getIdentifier("update_action", "id", packageName), appUpdateList, 0, false,false));
Log.i(TAG, "update_action:"+res.getIdentifier("update_action", "id", packageName));
} else {
contentViews.setViewVisibility(res.getIdentifier("update_slash", "id", packageName), View.GONE);
contentViews.setViewVisibility(res.getIdentifier("update_action", "id", packageName), View.GONE);
}
} else {
int showOftenCount = Math.min(oftenUseCount,count);
title = context.getString(res.getIdentifier("update_update_often", "string", packageName),showOftenCount);
description = new StringBuilder();
for (int i = 0; i < oftenUseCount && i< count;i++) {
description.append(updateAppNames.get(i).getName()+",");
}
Command appUpdateList = Command.parse(ACTION_APPUPDATELIST, null);
n.icon = icon;
n.tickerText = title;
n.when = System.currentTimeMillis();
n.flags |= android.app.Notification.FLAG_AUTO_CANCEL;
n.defaults = 0; // please be quiet
n.sound = null;
n.number = count;
Log.i(TAG, "packageName:"+packageName);
RemoteViews contentViews = new RemoteViews(packageName,res.getIdentifier("update_layout_often", "layout", packageName));
Log.i(TAG, "layoutId:"+res.getIdentifier("update_layout_often", "layout", packageName));
contentViews.setTextViewText(res.getIdentifier("update_title", "id", packageName), title);
Log.i(TAG, "titleId:"+res.getIdentifier("update_title", "id", packageName));
boolean canDefineClickAction = false;
canDefineClickAction = Integer.parseInt(Build.VERSION.SDK) >= VERSION_USE_NEW_UI;
PackageManager manager = context.getPackageManager();
switch (showOftenCount){
case 1:
initIconView(contentViews,1,manager, context,updateAppNames.get(0).getPackageName(),updateAppNames.get(0).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,2,manager, context,null,null,null,canDefineClickAction,selectedAppId);
initIconView(contentViews,3,manager, context,null,null,null,canDefineClickAction,selectedAppId);
initIconView(contentViews,4,manager, context,null,null,null,canDefineClickAction,selectedAppId);
break;
case 2:
initIconView(contentViews,1,manager, context,updateAppNames.get(0).getPackageName(),updateAppNames.get(0).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,2,manager, context,updateAppNames.get(1).getPackageName(),updateAppNames.get(1).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,3,manager, context,null,null,null,canDefineClickAction,selectedAppId);
initIconView(contentViews,4,manager, context,null,null,null,canDefineClickAction,selectedAppId);
break;
case 3:
initIconView(contentViews,1,manager, context,updateAppNames.get(0).getPackageName(),updateAppNames.get(0).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,2,manager, context,updateAppNames.get(1).getPackageName(),updateAppNames.get(1).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,3,manager, context,updateAppNames.get(2).getPackageName(),updateAppNames.get(2).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,4,manager, context,null,null,null,canDefineClickAction,selectedAppId);
break;
case 4:
initIconView(contentViews,1,manager, context,updateAppNames.get(0).getPackageName(),updateAppNames.get(0).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,2,manager, context,updateAppNames.get(1).getPackageName(),updateAppNames.get(1).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,3,manager, context,updateAppNames.get(2).getPackageName(),updateAppNames.get(2).getId(),appUpdateList,canDefineClickAction,selectedAppId);
initIconView(contentViews,4,manager, context,updateAppNames.get(3).getPackageName(),updateAppNames.get(3).getId(),appUpdateList,canDefineClickAction,selectedAppId);
break;
default:
break;
}
contentViews.setTextViewText(res.getIdentifier("updates_num", "id", packageName), count + "");
n.contentView = contentViews;
n.contentIntent = buildCommandIntent(context, NOTIFICATION_ID_UPDATE, appUpdateList, 0, false);
if (canDefineClickAction) {
contentViews.setOnClickPendingIntent(res.getIdentifier("update_action", "id", packageName), buildBackgroundCommandIntent(context, res.getIdentifier("update_action", "id", packageName), appUpdateList, 0, false,true));
} else {
contentViews.setViewVisibility(res.getIdentifier("update_action", "id", packageName), View.GONE);
}
}
nm.notify(NOTIFICATION_ID_UPDATE, n);
PushNotificationManager.getInstance(context).setLastAppsNotificationTime(System.currentTimeMillis());
result = true;
} catch (Exception e) {
LogUtil.i(TAG, e.toString());
result = false;
}
return result;
}
public static void initIconView(RemoteViews remoteViews , int position, PackageManager manager, Context context,String iconPackageName , String appId , Command appUpdateList ,boolean canDefineClickAction , ArrayList<String> selectedAppIds){
if (null == context){
return;
}
String buttonId = "icon_" + position +"_button" ;
String iconViewId = "icon_" + position ;
String iconIndicatorId = "icon_" + position + "_indicator";
String maskId = "icon_" + position + "_mask" ;
Resources resources = context.getResources();
String packageName = context.getPackageName();
if (TextUtils.isEmpty(iconPackageName)){
remoteViews.setViewVisibility(resources.getIdentifier(iconViewId, "id", packageName), View.GONE);
remoteViews.setViewVisibility(resources.getIdentifier(iconIndicatorId, "id", packageName), View.GONE);
} else {
Bitmap icon = getBitmapFromPackageName(manager,iconPackageName);
if (null == icon){
remoteViews.setViewVisibility(resources.getIdentifier(iconViewId, "id", packageName), View.GONE);
remoteViews.setViewVisibility(resources.getIdentifier(iconIndicatorId, "id", packageName), View.GONE);
} else {
remoteViews.setViewVisibility(resources.getIdentifier(iconViewId, "id", packageName), View.VISIBLE);
remoteViews.setViewVisibility(resources.getIdentifier(iconIndicatorId, "id", packageName), View.VISIBLE);
remoteViews.setImageViewBitmap(resources.getIdentifier(iconViewId, "id", packageName),icon);
if (null != selectedAppIds){
if (selectedAppIds.contains(appId)){
remoteViews.setViewVisibility(resources.getIdentifier(iconViewId, "id", packageName), View.GONE);
remoteViews.setViewVisibility(resources.getIdentifier(iconIndicatorId, "id", packageName), View.GONE);
remoteViews.setViewVisibility(resources.getIdentifier(maskId, "id", packageName), View.VISIBLE);
remoteViews.setImageViewBitmap(resources.getIdentifier(maskId, "id", packageName),icon);
}
}
if (canDefineClickAction){
remoteViews.setOnClickPendingIntent(resources.getIdentifier(buttonId, "id", packageName), buildIconBackgroundCommandIntent(context, resources.getIdentifier(iconViewId, "id", packageName), appUpdateList, 0, false,true,appId,selectedAppIds));
}
LogUtil.i(TAG,"initIconView # packageName is " +packageName +" appId is "+appId);
}
}
}
private static Bitmap processBitmapIfNeeded(Bitmap bitmap) {
// by testing on emulator, it's confirmed that only froyo has this issue, see
//
// 1. https://code.google.com/p/android/issues/detail?id=8489
// 2. https://groups.google.com/forum/#!topic/android-developers/YX-CSbeyxUU
//
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.FROYO && bitmap != null && bitmap.getConfig() == null) {
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
}
return bitmap;
}
public static Bitmap getBitmapFromPackageName(PackageManager manager,String packageName){
if (null == manager || TextUtils.isEmpty(packageName)){
return null;
}
try {
Drawable drawable = manager.getApplicationIcon(packageName);
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return processBitmapIfNeeded(bitmapDrawable.getBitmap());
}
} catch (PackageManager.NameNotFoundException e) {
LogUtil.i(TAG, "getBitmapFromPackageName error: " + e);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment