Android Shortcut is not working in VIVO 10

795 views Asked by At

I implementing shortcut in my app but this is not working in VIVO 10( Funtouch OS). Please help me. I am already used many method but not success.

Method 1: I am using this code but not working

@SuppressLint("NewApi")
private fun shortcut(){
    val shortcutManager = getSystemService(ShortcutManager::class.java)
    val nhentaiIntent = Intent(this, MainActivity::class.java)
    nhentaiIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    nhentaiIntent.action = Intent.ACTION_VIEW
    if (shortcutManager!!.isRequestPinShortcutSupported) {
        val pinShortcutInfo = ShortcutInfo.Builder(this, "my-shortcut")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                 .setShortLabel("hello Shortcut")
                .setIntent(nhentaiIntent)
                .build()
        val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo)
        val successCallback = PendingIntent.getBroadcast(this, /* request code */ 0, pinnedShortcutCallbackIntent, /* flags */ 0)

        shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.intentSender)
    }

}

Method 2: I am also used this code but not working

Intent nhentaiIntent = new Intent(context, MainActivity.class);
    nhentaiIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    nhentaiIntent.setAction(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
        if (shortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo info = new ShortcutInfo.Builder(context, "Shortcut")
                    .setIntent(nhentaiIntent)
                    .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
                    .setShortLabel("Hello ")
                    .setLongLabel("Lable")
                    .build();
            Intent addIntent = shortcutManager.createShortcutResultIntent(info);
            shortcutManager.requestPinShortcut(info, PendingIntent.getBroadcast(context, 0, addIntent, 0).getIntentSender());
            Toast.makeText(context, "supported_launcher", Toast.LENGTH_LONG).show();
        } else {
            // TODO: Maybe implement this for launchers without pin shortcut support?
            // TODO: Should be the same with the fallback for Android < O
            // for now just show unsupported
            Toast.makeText(context, "unsupported_launcher", Toast.LENGTH_LONG).show();
        }
    }

Method 3: I am also used this code but not working

private fun addShourcut() {
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(applicationContext)) {
        val shortcutInfo =
            ShortcutInfoCompat.Builder(applicationContext, "#1")
                .setIntent(
                    Intent(applicationContext, MainActivity::class.java).setAction(
                        Intent.ACTION_MAIN
                    )
                ) // !!! intent's action must be set on oreo
                .setShortLabel("Test")
                .setIcon(
                    IconCompat.createWithResource(
                        applicationContext,
                        R.mipmap.ic_launcher
                    )
                )
                .build()
        ShortcutManagerCompat.requestPinShortcut(applicationContext, shortcutInfo, null)
    } else {
        Toast.makeText(
            this@MainActivity, "launcher does not support short cut icon",Toast.LENGTH_LONG).show()
    }
}

Please help me anyone i spent more time for this issue.

1

There are 1 answers

0
Bhavin Patel On

I found that desktop shortcut is not created in default launcher of vivo devices. When I changed default launcher to some other launcher It's let me create desktop shortcut. So as far I did R&d I found below solution it's not complete solution but you can redirect to allow Desktop shortcuts screen using below code.

val sintent = Intent()
sintent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
sintent .component = ComponentName("com.bbk.launcher2", "com.bbk.launcher2.installshortcut.PurviewActivity")
activity.startActivity(sintent )

I tried to get result if user allowed or not but it's not work for all VIVO devices.

var uri = Uri.parse("content://com.bbk.launcher2.settings/favorites")
            var query = activity.contentResolver.query(uri, null, " itemType = ?", arrayOf("30"), null);
            if (query != null && query.count > 0) {
                var idIndex = query.getColumnIndexOrThrow("_id");
                var intentIndex = query.getColumnIndexOrThrow("intent");
                var shortcutPermissionIndex = query.getColumnIndexOrThrow("shortcutPermission");
                while (query.moveToNext()) {
                    val long = query.getLong(idIndex)
                    val intent = query.getString(intentIndex)
                    val shortcutPermission = query.getInt(shortcutPermissionIndex)
                    var unflattenFromString = ComponentName.unflattenFromString(intent)
                    Log.e("bhavin->", "initView: getShortcutPerBtn id=$long packageName= ${unflattenFromString!!.packageName} shortcutPermission= $shortcutPermission")
                    if (unflattenFromString!!.packageName == activity.packageName && shortcutPermission != 0 && shortcutPermission != 16){
                        val intent = Intent()
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        intent.component = ComponentName("com.bbk.launcher2", "com.bbk.launcher2.installshortcut.PurviewActivity")
                        activity.startActivity(intent)
                        query.close()
                        return@ensureBackgroundThread
                    }
                    else{
                        Log.e("bhavin->","abcd")
                    }
                }
                query.close()
            }

For more details you can check this I found this git

Note: I found exception some devices vivo 1902 and Z3

 java.lang.SecurityException: Permission Denial: opening provider
 com.bbk.launcher2.data.LauncherProvider from ProcessRecord{f8f30af  
 16137:com.qiutinghe.change/u0a172} (pid=16137, uid=10172) requires
 com.bbk.launcher2.permission.READ_SETTINGS or
 com.bbk.launcher2.permission.WRITE_SETTINGS

If any one found how to check desktop permission is allowed in Vivo devices please add into this answer.