他のアプリケーションからのショートカットの作成ブロードキャストを受信
2967 ワード
public class InstallShortcutReceiver extends BroadcastReceiver {
private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_INSTALL_SHORTCUT)) {
String name = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Intent extraIntent = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
if (name == null) {
try {
PackageManager pm = context.getPackageManager();
ActivityInfo info = pm.getActivityInfo(intent.getComponent(), 0);
name = info.loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException nnfe) {
return;
}
}
ComponentName component = null;
Bitmap bitmap = null;
ShortcutIconResource iconResource = null;
Parcelable bitmapParcelable = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
if (bitmapParcelable != null && bitmapParcelable instanceof Bitmap) {
bitmap = (Bitmap) bitmapParcelable;
} else {
Parcelable extra = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
if (extra != null && extra instanceof ShortcutIconResource) {
try {
iconResource = (ShortcutIconResource) extra;
PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication(iconResource.packageName);
int id = resources.getIdentifier(iconResource.resourceName, null, null);
Drawable drawable = resources.getDrawable(id);
bitmap = Util.drawableToBitmap(drawable);
} catch (Exception e) {
// TODO: handle exception
}
}
}
if (extraIntent == null) {
return;
} else {
if (extraIntent.getAction() == null) {
extraIntent.setAction(Intent.ACTION_VIEW);
} else if (extraIntent.getAction().equals(Intent.ACTION_MAIN) && extraIntent.getCategories() != null
&& extraIntent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
extraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
}
component = extraIntent.getComponent();
if (component == null) {
if (iconResource != null) {
component = new ComponentName(iconResource.packageName, iconResource.resourceName);
} else {
component = new ComponentName("com.joy.other", "com.joy.otherActivity");
}
}
TrinityButtonInfo buttonInfo = new TrinityButtonInfo(new ButtonMetaData());
buttonInfo.mComponentName = component;
buttonInfo.mTitle = name;
buttonInfo.mBitmap = BitmapHelper.createIconBitmap(bitmap, LauncherApplication.mContext, null, true);
buttonInfo.mIntent = extraIntent;
buttonInfo.mShortcutTitle = name;
buttonInfo.mShortcutBitmap = bitmap;
addButtonsToCurrentHomePage(buttonInfo);
}
}
}
}