Include home app when searching installed apps
Change-Id: I4677307bb23f03ed7fb224c32312be7cb114763d Fix: 62221174 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -37,6 +37,7 @@ import com.android.settings.applications.PackageManagerWrapper;
|
||||
import com.android.settings.dashboard.SiteMapManager;
|
||||
import com.android.settings.utils.AsyncLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -55,7 +56,7 @@ public class InstalledAppResultLoader extends AsyncLoader<Set<? extends SearchRe
|
||||
private final String mQuery;
|
||||
private final UserManager mUserManager;
|
||||
private final PackageManagerWrapper mPackageManager;
|
||||
|
||||
private final List<ResolveInfo> mHomeActivities = new ArrayList<>();
|
||||
|
||||
public InstalledAppResultLoader(Context context, PackageManagerWrapper pmWrapper,
|
||||
String query, SiteMapManager mapManager) {
|
||||
@@ -71,6 +72,9 @@ public class InstalledAppResultLoader extends AsyncLoader<Set<? extends SearchRe
|
||||
final Set<AppSearchResult> results = new HashSet<>();
|
||||
final PackageManager pm = mPackageManager.getPackageManager();
|
||||
|
||||
mHomeActivities.clear();
|
||||
mPackageManager.getHomeActivities(mHomeActivities);
|
||||
|
||||
for (UserInfo user : getUsersToCount()) {
|
||||
final List<ApplicationInfo> apps =
|
||||
mPackageManager.getInstalledApplicationsAsUser(
|
||||
@@ -106,11 +110,18 @@ public class InstalledAppResultLoader extends AsyncLoader<Set<? extends SearchRe
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the candidate should be included in candidate list
|
||||
* <p/>
|
||||
* This method matches logic in {@code ApplicationState#FILTER_DOWNLOADED_AND_LAUNCHER}.
|
||||
*/
|
||||
private boolean shouldIncludeAsCandidate(ApplicationInfo info, UserInfo user) {
|
||||
// Not system app
|
||||
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0
|
||||
|| (info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
return true;
|
||||
}
|
||||
// Shows up in launcher
|
||||
final Intent launchIntent = new Intent(LAUNCHER_PROBE)
|
||||
.setPackage(info.packageName);
|
||||
final List<ResolveInfo> intents = mPackageManager.queryIntentActivitiesAsUser(
|
||||
@@ -119,7 +130,11 @@ public class InstalledAppResultLoader extends AsyncLoader<Set<? extends SearchRe
|
||||
| PackageManager.MATCH_DIRECT_BOOT_AWARE
|
||||
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
|
||||
user.id);
|
||||
return intents != null && intents.size() != 0;
|
||||
if (intents != null && intents.size() != 0) {
|
||||
return true;
|
||||
}
|
||||
// Is launcher app itself
|
||||
return isPackageInList(mHomeActivities, info.packageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -194,6 +209,15 @@ public class InstalledAppResultLoader extends AsyncLoader<Set<? extends SearchRe
|
||||
return NAME_NO_MATCH;
|
||||
}
|
||||
|
||||
private boolean isPackageInList(List<ResolveInfo> resolveInfos, String pkg) {
|
||||
for (ResolveInfo info : resolveInfos) {
|
||||
if (TextUtils.equals(info.activityInfo.packageName, pkg)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<String> getBreadCrumb() {
|
||||
if (mBreadcrumb == null || mBreadcrumb.isEmpty()) {
|
||||
final Context context = getContext();
|
||||
@@ -206,6 +230,7 @@ public class InstalledAppResultLoader extends AsyncLoader<Set<? extends SearchRe
|
||||
|
||||
/**
|
||||
* A temporary ranking scheme for installed apps.
|
||||
*
|
||||
* @param wordDiff difference between query length and app name length.
|
||||
* @return the ranking.
|
||||
*/
|
||||
|
@@ -19,6 +19,7 @@ package com.android.settings.search;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.UserManager;
|
||||
@@ -28,8 +29,6 @@ import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.applications.PackageManagerWrapper;
|
||||
import com.android.settings.dashboard.SiteMapManager;
|
||||
import com.android.settings.search.InstalledAppResultLoader;
|
||||
import com.android.settings.search.SearchResult;
|
||||
import com.android.settings.testutils.ApplicationTestUtils;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
@@ -39,6 +38,8 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -52,6 +53,7 @@ import static android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyList;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -166,6 +168,36 @@ public class InstalledAppResultLoaderTest {
|
||||
assertThat(mLoader.loadInBackground().size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void query_matchingQuery_shouldReturnSystemAppIfHomeApp() {
|
||||
when(mPackageManagerWrapper.getInstalledApplicationsAsUser(anyInt(), anyInt()))
|
||||
.thenReturn(Arrays.asList(
|
||||
ApplicationTestUtils.buildInfo(0 /* uid */, "app1", FLAG_SYSTEM,
|
||||
0 /* targetSdkVersion */)));
|
||||
when(mPackageManagerWrapper.queryIntentActivitiesAsUser(
|
||||
any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(null);
|
||||
|
||||
when(mPackageManagerWrapper.getHomeActivities(anyList())).thenAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
final List<ResolveInfo> list = (List<ResolveInfo>) invocation.getArguments()[0];
|
||||
final ResolveInfo info = new ResolveInfo();
|
||||
info.activityInfo = new ActivityInfo();
|
||||
info.activityInfo.packageName = "app1";
|
||||
list.add(info);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
final String query = "app";
|
||||
|
||||
mLoader = new InstalledAppResultLoader(mContext, mPackageManagerWrapper, query,
|
||||
mSiteMapManager);
|
||||
|
||||
assertThat(mLoader.loadInBackground().size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void query_matchingQuery_shouldNotReturnSystemAppIfNotLaunchable() {
|
||||
when(mPackageManagerWrapper.getInstalledApplicationsAsUser(anyInt(), anyInt()))
|
||||
|
Reference in New Issue
Block a user