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

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

Merge "Fix searching work app in settings" into oc-dr1-dev

parents add67648 9f3f5f4f
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package com.android.settings.search;

import android.content.pm.ApplicationInfo;
import android.os.UserHandle;

public class AppSearchResult extends SearchResult {
    /**
@@ -30,6 +31,10 @@ public class AppSearchResult extends SearchResult {
        info = builder.mInfo;
    }

    public UserHandle getAppUserHandle() {
        return new UserHandle(UserHandle.getUserId(info.uid));
    }

    public static class Builder extends SearchResult.Builder {
        protected ApplicationInfo mInfo;

+2 −1
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import com.android.settings.utils.AsyncLoader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
@@ -99,7 +100,7 @@ public class InstalledAppResultLoader extends AsyncLoader<Set<? extends SearchRe

                final AppSearchResult.Builder builder = new AppSearchResult.Builder();
                builder.setAppInfo(info)
                        .setStableId(info.packageName.hashCode())
                        .setStableId(Objects.hash(info.packageName, user.id))
                        .setTitle(info.loadLabel(pm))
                        .setRank(getRank(wordDiff))
                        .addBreadcrumbs(getBreadCrumb())
+13 −3
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@
 */
package com.android.settings.search;

import android.content.Intent;
import android.os.UserHandle;
import android.view.View;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.logging.nano.MetricsProto;

/**
 * ViewHolder for intent based search results.
@@ -32,7 +34,7 @@ public class IntentSearchViewHolder extends SearchViewHolder {

    @Override
    public int getClickActionMetricName() {
        return MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_RESULT;
        return MetricsProto.MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_RESULT;
    }

    @Override
@@ -41,7 +43,15 @@ public class IntentSearchViewHolder extends SearchViewHolder {

        itemView.setOnClickListener(v -> {
           fragment.onSearchResultClicked(this, result);
           fragment.startActivity(result.payload.getIntent());
            final Intent intent = result.payload.getIntent();
            // Use app user id to support work profile use case.
            if (result instanceof AppSearchResult) {
                AppSearchResult appResult = (AppSearchResult) result;
                UserHandle userHandle = appResult.getAppUserHandle();
                fragment.getActivity().startActivityAsUser(intent, userHandle);
            } else {
                fragment.startActivity(intent);
            }
        });
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -18,8 +18,11 @@ package com.android.settings.search;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.IconDrawableFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
@@ -46,6 +49,7 @@ public abstract class SearchViewHolder extends RecyclerView.ViewHolder {

    protected final MetricsFeatureProvider mMetricsFeatureProvider;
    protected final SearchFeatureProvider mSearchFeatureProvider;
    private final IconDrawableFactory mIconDrawableFactory;

    public SearchViewHolder(View view) {
        super(view);
@@ -59,6 +63,7 @@ public abstract class SearchViewHolder extends RecyclerView.ViewHolder {
        breadcrumbView = view.findViewById(R.id.breadcrumb);

        mPlaceholderSummary = view.getContext().getString(R.string.summary_placeholder);
        mIconDrawableFactory = IconDrawableFactory.newInstance(view.getContext());
    }

    public abstract int getClickActionMetricName();
@@ -78,7 +83,12 @@ public abstract class SearchViewHolder extends RecyclerView.ViewHolder {
        if (result instanceof AppSearchResult) {
            AppSearchResult appResult = (AppSearchResult) result;
            PackageManager pm = fragment.getActivity().getPackageManager();
            iconView.setImageDrawable(appResult.info.loadIcon(pm));
            UserHandle userHandle = appResult.getAppUserHandle();
            Drawable badgedIcon =
                    mIconDrawableFactory.getBadgedIcon(appResult.info, userHandle.getIdentifier());
            iconView.setImageDrawable(badgedIcon);
            titleView.setContentDescription(
                    pm.getUserBadgedLabel(appResult.info.loadLabel(pm), userHandle));
        } else {
            // Valid even when result.icon is null.
            iconView.setImageDrawable(result.icon);
+39 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;

import com.android.settings.R;
@@ -47,6 +48,7 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
import static android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
@@ -387,4 +389,41 @@ public class InstalledAppResultLoaderTest {

        assertThat(mLoader.loadInBackground().size()).isEqualTo(0);
    }

    @Test
    public void query_appExistsInBothProfiles() {
        final String query = "carrot";
        final String packageName = "carrot";
        final int user1 = 0;
        final int user2 = 10;
        final int uid = 67672;
        List<UserInfo> infos = new ArrayList<>();
        infos.add(new UserInfo(user1, "user 1", 0));
        infos.add(new UserInfo(user2, "user 2", UserInfo.FLAG_MANAGED_PROFILE));

        when(mUserManager.getProfiles(anyInt())).thenReturn(infos);

        when(mPackageManagerWrapper.getInstalledApplicationsAsUser(anyInt(), eq(user1)))
                .thenReturn(Arrays.asList(
                        ApplicationTestUtils.buildInfo(UserHandle.getUid(user1, uid) /* uid */,
                                packageName, 0 /* flags */,
                                0 /* targetSdkVersion */)));
        when(mPackageManagerWrapper.getInstalledApplicationsAsUser(anyInt(), eq(user2)))
                .thenReturn(Arrays.asList(
                        ApplicationTestUtils.buildInfo(UserHandle.getUid(user2, uid) /* uid */,
                                packageName, 0 /* flags */,
                                0 /* targetSdkVersion */)));

        mLoader = new InstalledAppResultLoader(mContext, mPackageManagerWrapper, query,
                mSiteMapManager);

        Set<AppSearchResult> searchResults = (Set<AppSearchResult>) mLoader.loadInBackground();
        assertThat(searchResults).hasSize(2);

        Set<Integer> uidResults = searchResults.stream().map(result -> result.info.uid).collect(
                Collectors.toSet());
        assertThat(uidResults).containsExactly(
                UserHandle.getUid(user1, uid),
                UserHandle.getUid(user2, uid));
    }
}
Loading