Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c028a7d9 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Resolve intent before launching from DashboardFragment." into oc-dev

parents 2fc1fa6c 3c7ca105
Loading
Loading
Loading
Loading
+12 −9
Original line number Diff line number Diff line
@@ -20,9 +20,9 @@ import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.text.TextUtils;
import android.util.Log;
@@ -51,22 +51,17 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
    private static final String DASHBOARD_TILE_PREF_KEY_PREFIX = "dashboard_tile_pref_";
    private static final String META_DATA_KEY_INTENT_ACTION = "com.android.settings.intent.action";


    protected final Context mContext;

    private final MetricsFeatureProvider mMetricsFeatureProvider;
    private final CategoryManager mCategoryManager;
    private final PackageManager mPackageManager;

    public DashboardFeatureProviderImpl(Context context) {
        mContext = context.getApplicationContext();
        mCategoryManager = CategoryManager.get(context, getExtraIntentAction());
        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
    }

    @VisibleForTesting
    DashboardFeatureProviderImpl(Context context, CategoryManager categoryManager) {
        mContext = context.getApplicationContext();
        mCategoryManager = categoryManager;
        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
        mPackageManager = context.getPackageManager();
    }

    @Override
@@ -208,6 +203,10 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {

    private void launchIntentOrSelectProfile(Activity activity, Tile tile, Intent intent,
            int sourceMetricCategory) {
        if (!isIntentResolvable(intent)) {
            Log.w(TAG, "Cannot resolve intent, skipping. " + intent);
            return;
        }
        ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
        if (tile.userHandle == null) {
            mMetricsFeatureProvider.logDashboardStartIntent(mContext, intent, sourceMetricCategory);
@@ -219,4 +218,8 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
            ProfileSelectDialog.show(activity.getFragmentManager(), tile);
        }
    }

    private boolean isIntentResolvable(Intent intent) {
        return mPackageManager.resolveActivity(intent, 0) != null;
    }
}
+32 −3
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;

import java.util.ArrayList;

@@ -293,9 +294,35 @@ public class DashboardFeatureProviderImplTest {
                .isEqualTo(MetricsProto.MetricsEvent.SETTINGS_GESTURES);
    }

    @Test
    public void clickPreference_withUnresolvableIntent_shouldNotLaunchAnything() {
        ReflectionHelpers.setField(
                mImpl, "mPackageManager", RuntimeEnvironment.getPackageManager());
        Activity activity = Robolectric.buildActivity(Activity.class).get();
        final ShadowApplication application = ShadowApplication.getInstance();
        final Preference preference = new Preference(application.getApplicationContext());
        final Tile tile = new Tile();
        tile.key = "key";
        tile.intent = new Intent();
        tile.intent.setComponent(new ComponentName("pkg", "class"));
        tile.metaData = new Bundle();
        tile.metaData.putString("com.android.settings.intent.action", "TestAction");
        tile.userHandle = null;

        mImpl.bindPreferenceToTile(activity, MetricsProto.MetricsEvent.SETTINGS_GESTURES,
                preference, tile, "123", Preference.DEFAULT_ORDER);
        preference.performClick();

        final ShadowActivity.IntentForResult launchIntent =
                shadowOf(activity).getNextStartedActivityForResult();

        assertThat(launchIntent).isNull();
    }

    @Test
    public void getPreferences_noCategory_shouldReturnNull() {
        mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
        mImpl = new DashboardFeatureProviderImpl(mActivity);
        ReflectionHelpers.setField(mImpl, "mCategoryManager", mCategoryManager);
        when(mCategoryManager.getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE))
                .thenReturn(null);

@@ -306,7 +333,8 @@ public class DashboardFeatureProviderImplTest {

    @Test
    public void getPreferences_noTileForCategory_shouldReturnNull() {
        mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
        mImpl = new DashboardFeatureProviderImpl(mActivity);
        ReflectionHelpers.setField(mImpl, "mCategoryManager", mCategoryManager);
        when(mCategoryManager.getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE))
                .thenReturn(new DashboardCategory());

@@ -317,7 +345,8 @@ public class DashboardFeatureProviderImplTest {

    @Test
    public void getPreferences_hasTileForCategory_shouldReturnPrefList() {
        mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
        mImpl = new DashboardFeatureProviderImpl(mActivity);
        ReflectionHelpers.setField(mImpl, "mCategoryManager", mCategoryManager);
        final DashboardCategory category = new DashboardCategory();
        category.tiles.add(new Tile());
        when(mCategoryManager