How handle shortcut Uri from intent.getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST)

48 views Asked by At

I'm building a custom launcher. I'm currently stuck with this kind of shortcut intent. I'm able to get icon drawable and label but not the Uri which I need to save in my database.

Below is what I have been doing so far

// where I handle the initial intent
public void addShortcut(Intent intent, int position) { 
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        launcher.launch(intent);
    }

// Activity result launcher
private final ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                int resultCode = result.getResultCode();
                Intent data = result.getData();

                if (resultCode == RESULT_OK && data != null) {
                    this.completeAddShortcut(data);
                } else {
                    Utils.an_error_has_occurred(getApplicationContext());
                }
            }
    );

private void completeAddShortcut(Intent intent) {
if (intent.getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST) != null) {
            intent.setPackage(getApplicationContext().getPackageName());
            launcher.launch(intent);
}

On manifest file:

<activity
    android:name=".HandleShortcutActivity"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.content.pm.action.CONFIRM_PIN_SHORTCUT" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Then HandleShortcutActivity.class

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        LauncherApps launcherApps =  getApplicationContext().getSystemService(LauncherApps.class);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LauncherApps.PinItemRequest mRequest = launcherApps.getPinItemRequest(intent);

            if (mRequest != null && mRequest.getRequestType() == LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT) {
                ShortcutInfo si = mRequest.getShortcutInfo();
                Drawable iconDrawable = launcherApps.getShortcutIconDrawable(si, 0);
                Bitmap icon = drawableToBitmap(iconDrawable);

                String label = null;
                if (si.getShortLabel() != null) {
                    label = si.getShortLabel().toString();

                    CharSequence longlabel = si.getLongLabel();
                    if (longlabel != null) {
                        if (longlabel.toString().startsWith(label)) {
                            label = longlabel.toString();
                        } else {
                            label += " " + longlabel;
                        }
                    }
                }
                mRequest.accept();
            }
        }
        finish();
    }

when I try adding a shortcut I get the following errors:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=270011744, result=-1, data=Intent { (has extras) }} to activity {com.launcher/com.launcher.activities.EditorActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
 

I'm feeling very confused and can't think logically. I think the problem is coming from the way I handle intents with their results activites but not sure how to fix.

0

There are 0 answers