Java Code Examples for android.content.Intent#ShortcutIconResource

The following examples show how to use android.content.Intent#ShortcutIconResource . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileManager.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public Intent createShortcutIntent(Context context, File file) {
    // create shortcut if requested
    Intent.ShortcutIconResource icon =
            Intent.ShortcutIconResource.fromContext(context, R.mipmap.ic_launcher);

    Intent intent = new Intent();

    Intent launchIntent = new Intent(context, SplashScreenActivity.class);
    launchIntent.putExtra(CompileManager.FILE_PATH, file.getPath());
    launchIntent.setAction("run_from_shortcut");

    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, file.getName());
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    return intent;
}
 
Example 2
Source File: TaskerQuickActionsActivity.java    From SecondScreen with Apache License 2.0 6 votes vote down vote up
private void doLauncherStuff(String key, String value) {
    // The meat of our shortcut
    Intent shortcutIntent = new Intent (this, TaskerQuickActionsActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.putExtra(U.KEY, key);
    shortcutIntent.putExtra(U.VALUE, value);
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher);

    // The result we are passing back from this activity
    Intent resultIntent = new Intent();
    resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
    resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, U.generateBlurb(this, key, value, false));

    setResult(RESULT_OK, resultIntent);

    finish();
}
 
Example 3
Source File: LauncherIconCreator.java    From ActivityLauncher with ISC License 6 votes vote down vote up
@TargetApi(14)
private static void doCreateShortcut(Context context, String appName, Intent intent, String iconResourceName) {
    Intent shortcutIntent = new Intent();
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
    if (iconResourceName != null) {
        Intent.ShortcutIconResource ir = new Intent.ShortcutIconResource();
        if (intent.getComponent() == null) {
            ir.packageName = intent.getPackage();
        } else {
            ir.packageName = intent.getComponent().getPackageName();
        }
        ir.resourceName = iconResourceName;
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ir);
    }
    shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(shortcutIntent);
}
 
Example 4
Source File: ShortcutInfo.java    From LB-Launcher with Apache License 2.0 6 votes vote down vote up
public ShortcutInfo(Context context, ShortcutInfo info) {
    super(info);
    title = info.title.toString();
    intent = new Intent(info.intent);
    if (info.iconResource != null) {
        iconResource = new Intent.ShortcutIconResource();
        iconResource.packageName = info.iconResource.packageName;
        iconResource.resourceName = info.iconResource.resourceName;
    }
    mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
    customIcon = info.customIcon;
    flags = info.flags;
    firstInstallTime = info.firstInstallTime;
    user = info.user;
    status = info.status;
}
 
Example 5
Source File: ShortcutInfo.java    From Trebuchet with GNU General Public License v3.0 6 votes vote down vote up
public ShortcutInfo(Context context, ShortcutInfo info) {
    super(info);
    title = Utilities.trim(info.title);
    intent = new Intent(info.intent);
    if (info.iconResource != null) {
        iconResource = new Intent.ShortcutIconResource();
        iconResource.packageName = info.iconResource.packageName;
        iconResource.resourceName = info.iconResource.resourceName;
    }
    mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
    customIcon = info.customIcon;
    flags = info.flags;
    firstInstallTime = info.firstInstallTime;
    user = info.user;
    status = info.status;
}
 
Example 6
Source File: ShortCutUtils.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
/**
 * 为程序创建桌面快捷方式
 *
 * @param activity Activity
 * @param res      res
 */
public static void addShortcut(Activity activity, int res) {

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    // 快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name));
    shortcut.putExtra("duplicate", false); // 不允许重复创建
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName(activity, activity.getClass().getName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // 快捷方式的图标
    Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(activity, res);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

    activity.sendBroadcast(shortcut);
}
 
Example 7
Source File: PhonkScriptHelper.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
public static void addShortcut(Context c, String folder, String name) {
    Project p = new Project(folder, name);

    Intent.ShortcutIconResource icon;
    icon = Intent.ShortcutIconResource.fromContext(c, R.drawable.app_icon);

    if (ShortcutManagerCompat.isRequestPinShortcutSupported(c)) {
        Intent shortcutIntent = new Intent(c, AppRunnerActivity.class);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcutIntent.putExtra(Project.NAME, p.getName());
        shortcutIntent.putExtra(Project.FOLDER, p.getFolder());
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(c, folder + "/" + name)
                .setIntent(shortcutIntent) // !!! intent's action must be set on oreo
                .setShortLabel(name)
                .setIcon(IconCompat.createWithResource(c, R.drawable.app_icon))
                .build();
        ShortcutManagerCompat.requestPinShortcut(c, shortcutInfo, null);
    }
}
 
Example 8
Source File: ShortCutUtil.java    From styT with Apache License 2.0 6 votes vote down vote up
/**
 * 为程序创建桌面快捷方式
 *
 * @param activity Activity
 * @param res      res
 */
public static void addShortcut(Activity activity, int res) {

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    // 快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name));
    // 不允许重复创建
    shortcut.putExtra("duplicate", false);
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName(activity, activity.getClass().getName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // 快捷方式的图标
    Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(activity, res);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

    activity.sendBroadcast(shortcut);
}
 
Example 9
Source File: FloatingStylishCreateShortCutActivity.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
private void onSuccess() {
    Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_round);

    Intent intent = new Intent();
    Intent launchIntent = new Intent(this, FloatingStylishOpenShortCutActivity.class);

    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

    setResult(RESULT_OK, intent);
    finish();
}
 
Example 10
Source File: BroadcastIntent.java    From container with GNU General Public License v3.0 5 votes vote down vote up
private Intent handleInstallShortcutIntent(Intent intent) {
    Intent shortcut = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    if (shortcut != null) {
        ComponentName component = shortcut.resolveActivity(VirtualCore.getPM());
        if (component != null) {
            String pkg = component.getPackageName();
            Intent newShortcutIntent = new Intent();
            newShortcutIntent.setClassName(getHostPkg(), Constants.SHORTCUT_PROXY_ACTIVITY_NAME);
            newShortcutIntent.addCategory(Intent.CATEGORY_DEFAULT);
            newShortcutIntent.putExtra("_VA_|_intent_", shortcut);
            newShortcutIntent.putExtra("_VA_|_uri_", shortcut.toUri(0));
            newShortcutIntent.putExtra("_VA_|_user_id_", VUserHandle.myUserId());
            intent.removeExtra(Intent.EXTRA_SHORTCUT_INTENT);
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, newShortcutIntent);

            Intent.ShortcutIconResource icon = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
            if (icon != null && !TextUtils.equals(icon.packageName, getHostPkg())) {
                try {
                    Resources resources = VirtualCore.get().getResources(pkg);
                    if (resources != null) {
                        int resId = resources.getIdentifier(icon.resourceName, "drawable", pkg);
                        if (resId > 0) {
                            Drawable iconDrawable = resources.getDrawable(resId);
                            Bitmap newIcon = BitmapUtils.drawableToBitmap(iconDrawable);
                            if (newIcon != null) {
                                intent.removeExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
                                intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newIcon);
                            }
                        }
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return intent;
}
 
Example 11
Source File: FloatingCodecCreateShortCutActivity.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
private void onSuccess() {
    Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_round);

    Intent intent = new Intent();
    Intent launchIntent = new Intent(this, FloatingCodecOpenShortCutActivity.class);

    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

    setResult(RESULT_OK, intent);
    finish();
}
 
Example 12
Source File: CreateShortcutActivity.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
private void onSuccess() {
    Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, getShortcutIcon());

    Intent intent = new Intent();

    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getOpenShortcutActivityIntent());
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getShortcutName());
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

    setResult(RESULT_OK, intent);
    finish();
}
 
Example 13
Source File: ShortcutInfo.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
public ShortcutInfo(Context context, ShortcutInfo info) {
    super(info);
    title = info.title.toString();
    intent = new Intent(info.intent);
    if (info.iconResource != null) {
        iconResource = new Intent.ShortcutIconResource();
        iconResource.packageName = info.iconResource.packageName;
        iconResource.resourceName = info.iconResource.resourceName;
    }
    mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
    customIcon = info.customIcon;
    initFlagsAndFirstInstallTime(
            getPackageInfo(context, intent.getComponent().getPackageName()));
}
 
Example 14
Source File: InstallShortcutReceiver.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
public void onReceive(Context context, Intent data) {
    if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
        return;
    }

    if (DBG) Log.d(TAG, "Got INSTALL_SHORTCUT: " + data.toUri(0));

    Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    if (intent == null) {
        return;
    }

    // This name is only used for comparisons and notifications, so fall back to activity name
    // if not supplied
    String name = ensureValidName(context, intent,
            data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME)).toString();
    Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
    Intent.ShortcutIconResource iconResource =
        data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);

    // Queue the item up for adding if launcher has not loaded properly yet
    LauncherAppState.setApplicationContext(context.getApplicationContext());
    LauncherAppState app = LauncherAppState.getInstance();
    boolean launcherNotLoaded = (app.getDynamicGrid() == null);

    PendingInstallShortcutInfo info = new PendingInstallShortcutInfo(data, name, intent);
    info.icon = icon;
    info.iconResource = iconResource;

    String spKey = LauncherAppState.getSharedPreferencesKey();
    SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
    addToInstallQueue(sp, info);
    if (!mUseInstallQueue && !launcherNotLoaded) {
        flushInstallQueue(context);
    }
}
 
Example 15
Source File: InstallShortcutReceiver.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
private static ShortcutInfo createShortcutInfo(Intent data, LauncherAppState app) {
    Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);

    if (intent == null) {
        // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
        return null;
    }

    final ShortcutInfo info = new ShortcutInfo();

    // Only support intents for current user for now. Intents sent from other
    // users wouldn't get here without intent forwarding anyway.
    info.user = Process.myUserHandle();

    if (bitmap instanceof Bitmap) {
        info.iconBitmap = LauncherIcons.createIconBitmap((Bitmap) bitmap, app.getContext());
    } else {
        Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
        if (extra instanceof Intent.ShortcutIconResource) {
            info.iconResource = (Intent.ShortcutIconResource) extra;
            info.iconBitmap = LauncherIcons.createIconBitmap(info.iconResource, app.getContext());
        }
    }
    if (info.iconBitmap == null) {
        info.iconBitmap = app.getIconCache().getDefaultIcon(info.user);
    }

    info.title = Utilities.trim(name);
    info.contentDescription = UserManagerCompat.getInstance(app.getContext())
            .getBadgedLabelForUser(info.title, info.user);
    info.intent = intent;
    return info;
}
 
Example 16
Source File: BaseActivity.java    From prayer-times-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocaleUtils.init(this);
    //AppRatingDialog.increaseAppStarts();

    if (Preferences.SHOW_INTRO.get() || Preferences.CHANGELOG_VERSION.get() < BuildConfig.CHANGELOG_VERSION) {
        Module.INTRO.launch(this);
    }

    super.setContentView(R.layout.activity_base);
    mToolbar = findViewById(R.id.toolbar);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);
        mToolbar.setBackgroundResource(R.color.colorPrimary);
        mToolbar.setNavigationIcon(
                MaterialDrawableBuilder.with(this).setIcon(MaterialDrawableBuilder.IconValue.MENU).setColorResource(R.color.white)
                        .setToActionbarSize().build());
    }


    mDrawerLayout = findViewById(R.id.drawer);
    mNav = mDrawerLayout.findViewById(R.id.base_nav);
    View header = LayoutInflater.from(this).inflate(R.layout.drawer_header, mNav, false);
    try {
        PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        ((TextView) header.findViewById(R.id.version)).setText(pInfo.versionName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    mNav.addHeaderView(header);
    ArrayAdapter<Module> list = buildNavAdapter(this);
    mNav.setAdapter(list);
    mNav.setOnItemClickListener(this);
    mNav.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mNav.getHeight() < ((View) mNav.getParent()).getHeight()) {
                int diff = ((View) mNav.getParent()).getHeight() - mNav.getHeight();
                mNav.setDividerHeight(mNav.getDividerHeight() + diff / mNav.getAdapter().getCount() + 1);
            } else {
                mNav.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }

    });

    mDrawerLayout.post(() -> {
        if (mToolbar != null) {
            mToolbar.setTitle(mTitleRes);
        }
    });

    getSupportFragmentManager().addOnBackStackChangedListener(this);

    if (savedInstanceState != null)
        mNavPos = savedInstanceState.getInt("navPos", 0);

    String comp = getIntent().getComponent().getClassName();
    for (int i = 0; i < Module.values().length; i++) {
        if (comp.contains(Module.values()[i].getKey())) {
            mNavPos = i;
        }
    }

    if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
        Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, mIconRes);

        Intent intent = new Intent();
        Intent launchIntent = new Intent(this, BaseActivity.class);
        launchIntent.setComponent(getIntent().getComponent());
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        launchIntent.putExtra("duplicate", false);

        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(mTitleRes));
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

        setResult(RESULT_OK, intent);
        finish();
    }

    moveToFrag(mDefaultFragment);
}
 
Example 17
Source File: BaseActivity.java    From prayer-times-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocaleUtils.init(this);
    //AppRatingDialog.increaseAppStarts();

    if (Preferences.SHOW_INTRO.get() || Preferences.CHANGELOG_VERSION.get() < BuildConfig.CHANGELOG_VERSION) {
        Module.INTRO.launch(this);
    }

    super.setContentView(R.layout.activity_base);
    mToolbar = findViewById(R.id.toolbar);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);
        mToolbar.setBackgroundResource(R.color.colorPrimary);
        mToolbar.setNavigationIcon(
                MaterialDrawableBuilder.with(this).setIcon(MaterialDrawableBuilder.IconValue.MENU).setColorResource(R.color.white)
                        .setToActionbarSize().build());
    }


    mDrawerLayout = findViewById(R.id.drawer);
    mNav = mDrawerLayout.findViewById(R.id.base_nav);
    View header = LayoutInflater.from(this).inflate(R.layout.drawer_header, mNav, false);
    try {
        PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        ((TextView) header.findViewById(R.id.version)).setText(pInfo.versionName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    mNav.addHeaderView(header);
    ArrayAdapter<Module> list = buildNavAdapter(this);
    mNav.setAdapter(list);
    mNav.setOnItemClickListener(this);
    mNav.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mNav.getHeight() < ((View) mNav.getParent()).getHeight()) {
                int diff = ((View) mNav.getParent()).getHeight() - mNav.getHeight();
                mNav.setDividerHeight(mNav.getDividerHeight() + diff / mNav.getAdapter().getCount() + 1);
            } else {
                mNav.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }

    });

    mDrawerLayout.post(() -> {
        if (mToolbar != null) {
            mToolbar.setTitle(mTitleRes);
        }
    });

    getSupportFragmentManager().addOnBackStackChangedListener(this);

    if (savedInstanceState != null)
        mNavPos = savedInstanceState.getInt("navPos", 0);

    String comp = getIntent().getComponent().getClassName();
    for (int i = 0; i < Module.values().length; i++) {
        if (comp.contains(Module.values()[i].getKey())) {
            mNavPos = i;
        }
    }

    if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
        Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, mIconRes);

        Intent intent = new Intent();
        Intent launchIntent = new Intent(this, BaseActivity.class);
        launchIntent.setComponent(getIntent().getComponent());
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        launchIntent.putExtra("duplicate", false);

        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(mTitleRes));
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

        setResult(RESULT_OK, intent);
        finish();
    }

    moveToFrag(mDefaultFragment);
}
 
Example 18
Source File: InstallShortcutReceiver.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
String encodeToString() {
    try {
        if (activityInfo != null) {
            // If it a launcher target, we only need component name, and user to
            // recreate this.
            return new JSONStringer()
                .object()
                .key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
                .key(APP_SHORTCUT_TYPE_KEY).value(true)
                .key(USER_HANDLE_KEY).value(UserManagerCompat.getInstance(mContext)
                        .getSerialNumberForUser(user))
                .endObject().toString();
        } else if (shortcutInfo != null) {
            // If it a launcher target, we only need component name, and user to
            // recreate this.
            return new JSONStringer()
                    .object()
                    .key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
                    .key(DEEPSHORTCUT_TYPE_KEY).value(true)
                    .key(USER_HANDLE_KEY).value(UserManagerCompat.getInstance(mContext)
                            .getSerialNumberForUser(user))
                    .endObject().toString();
        } else if (providerInfo != null) {
            // If it a launcher target, we only need component name, and user to
            // recreate this.
            return new JSONStringer()
                    .object()
                    .key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
                    .key(APP_WIDGET_TYPE_KEY).value(true)
                    .key(USER_HANDLE_KEY).value(UserManagerCompat.getInstance(mContext)
                            .getSerialNumberForUser(user))
                    .endObject().toString();
        }

        if (launchIntent.getAction() == null) {
            launchIntent.setAction(Intent.ACTION_VIEW);
        } else if (launchIntent.getAction().equals(Intent.ACTION_MAIN) &&
                launchIntent.getCategories() != null &&
                launchIntent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
            launchIntent.addFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        }

        // This name is only used for comparisons and notifications, so fall back to activity
        // name if not supplied
        String name = ensureValidName(mContext, launchIntent, label).toString();
        Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
        Intent.ShortcutIconResource iconResource =
            data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);

        // Only encode the parameters which are supported by the API.
        JSONStringer json = new JSONStringer()
            .object()
            .key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
            .key(NAME_KEY).value(name);
        if (icon != null) {
            byte[] iconByteArray = Utilities.flattenBitmap(icon);
            json = json.key(ICON_KEY).value(
                    Base64.encodeToString(
                            iconByteArray, 0, iconByteArray.length, Base64.DEFAULT));
        }
        if (iconResource != null) {
            json = json.key(ICON_RESOURCE_NAME_KEY).value(iconResource.resourceName);
            json = json.key(ICON_RESOURCE_PACKAGE_NAME_KEY)
                    .value(iconResource.packageName);
        }
        return json.endObject().toString();
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 19
Source File: PanelShortcutActivity.java    From Noyze with Apache License 2.0 3 votes vote down vote up
/**
 * This function creates a shortcut and returns it to the caller.  There are actually two
 * intents that you will send back.
 *
 * The first intent serves as a container for the shortcut and is returned to the launcher by
 * setResult().  This intent must contain three fields:
 *
 * <ul>
 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
 * the shortcut.</li>
 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
 * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
 * a drawable resource.</li>
 * </ul>
 *
 * If you use a simple drawable resource, note that you must wrapper it using
 * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so
 * that the launcher can access resources that are stored in your application's .apk file.  If
 * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
 * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
 *
 * The shortcut intent can be any intent that you wish the launcher to send, when the user
 * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW}
 * with an appropriate Uri for your content, but any Intent will work here as long as it
 * triggers the desired action within your Activity.
 */
private void setupShortcut() {
    LOGD(TAG, "setupShortcut()");

    // First, set up the shortcut intent.  For this example, we simply create an intent that
    // will bring us directly back to this activity.  A more typical implementation would use a
    // data Uri in order to display a more specific result, or a custom action in order to
    // launch a specific operation.

    // NOTE: Only contain primitive extras or the Intent might not be saved as a Uri!
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClass(getApplicationContext(), getClass());
    shortcutIntent.putExtra("show", true);

    Intent.ShortcutIconResource icon =
            Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);

    // Then, set up the container intent (the response to the caller)
    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getTitle());
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

    // Now, return the result to the launcher
    setResult(RESULT_OK, intent);
    finish();
}
 
Example 20
Source File: PanelShortcutActivity.java    From Noyze with Apache License 2.0 3 votes vote down vote up
/**
 * This function creates a shortcut and returns it to the caller.  There are actually two
 * intents that you will send back.
 *
 * The first intent serves as a container for the shortcut and is returned to the launcher by
 * setResult().  This intent must contain three fields:
 *
 * <ul>
 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
 * the shortcut.</li>
 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
 * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
 * a drawable resource.</li>
 * </ul>
 *
 * If you use a simple drawable resource, note that you must wrapper it using
 * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so
 * that the launcher can access resources that are stored in your application's .apk file.  If
 * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
 * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
 *
 * The shortcut intent can be any intent that you wish the launcher to send, when the user
 * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW}
 * with an appropriate Uri for your content, but any Intent will work here as long as it
 * triggers the desired action within your Activity.
 */
private void setupShortcut() {
    LOGD(TAG, "setupShortcut()");

    // First, set up the shortcut intent.  For this example, we simply create an intent that
    // will bring us directly back to this activity.  A more typical implementation would use a
    // data Uri in order to display a more specific result, or a custom action in order to
    // launch a specific operation.

    // NOTE: Only contain primitive extras or the Intent might not be saved as a Uri!
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClass(getApplicationContext(), getClass());
    shortcutIntent.putExtra("show", true);

    Intent.ShortcutIconResource icon =
            Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);

    // Then, set up the container intent (the response to the caller)
    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getTitle());
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

    // Now, return the result to the launcher
    setResult(RESULT_OK, intent);
    finish();
}