Search code examples
androidandroid-manifestdigital-signatureandroid-screen-pinning

Launching another activity from a 'screen pinned' activity


http://florent-dupont.blogspot.ro/2015/02/android-5-screen-pinning.html

From a pinned app, you cannot start a secondary app, unless this one has the same shared user ID (which means that the sharedUserIdis set in the AndroidManifest.xml and that second application is packaged with the same certificate). Other apps’ Activities won’t be allowed to be started and doing so (by using Context.startActivity()) will simply be ignored.

I have done just those two things above but startActivity() is still getting ignored.

From https://developer.android.com/reference/android/R.attr.html#lockTaskMode:

If the system is already in lockTask mode when a new task rooted at this activity is launched that task will or will not start depending on whether the package of this activity has been whitelisted.

It looks tome like I have taken the necessary steps for this to work.

Anybody out there who got this working ?

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
    mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

    if (!mDpm.isAdminActive(deviceAdmin)) {
        Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
    }

    if (mDpm.isDeviceOwnerApp(getPackageName()))
    {
        mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName(), "com.that.other.package"});

        try
        {

            enableKioskMode(true);

            PackageManager pm = this.getPackageManager();
            Intent it = pm.getLaunchIntentForPackage("com.that.other.package");

            if (null != it) {
                this.startActivity(it);
                Log.d(_TAG, "Started activity for com.that.other.package");
            }
        }

        catch (Exception e)
        {
            Log.e(_TAG, e.getMessage());
            finish();
        }


    } else {

        Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
    }

}

private void enableKioskMode(boolean enabled) throws Exception
{
    if (enabled)
    {
        if (mDpm.isLockTaskPermitted(this.getPackageName()))
        {
            startLockTask();
            mIsKioskEnabled = true;
            mButton.setText(getString(R.string.exit_kiosk_mode));

        } else {

            Toast.makeText(this, getString(R.string.kiosk_not_permitted), Toast.LENGTH_SHORT).show();
        }

    } else {

        stopLockTask();
        mIsKioskEnabled = false;
        mButton.setText(getString(R.string.enter_kiosk_mode));
    }
}

Solution

  • Turned out I was missing the android:taskAffinity tag. Point it to the same strings in both packages and startActivity() should start behaving.