Merge "Extract counting of installed apps into helper class"

This commit is contained in:
TreeHugger Robot
2016-11-18 01:14:34 +00:00
committed by Android (Google) Code Review
9 changed files with 433 additions and 42 deletions

View File

@@ -17,44 +17,36 @@ package com.android.settings.applications;
import android.app.AppGlobals;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.content.pm.UserInfo;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import java.util.List;
public abstract class AppCounter extends AsyncTask<Void, Void, Integer> {
protected final PackageManager mPm;
protected final IPackageManager mIpm;
protected final PackageManagerWrapper mPm;
protected final UserManager mUm;
public AppCounter(Context context) {
mPm = context.getPackageManager();
mIpm = AppGlobals.getPackageManager();
public AppCounter(Context context, PackageManagerWrapper packageManager) {
mPm = packageManager;
mUm = UserManager.get(context);
}
@Override
protected Integer doInBackground(Void... params) {
int count = 0;
for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) {
try {
@SuppressWarnings("unchecked")
ParceledListSlice<ApplicationInfo> list =
mIpm.getInstalledApplications(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| (user.isAdmin() ? PackageManager.GET_UNINSTALLED_PACKAGES : 0),
user.id);
for (ApplicationInfo info : list.getList()) {
if (includeInCount(info)) {
count++;
}
for (UserInfo user : getUsersToCount()) {
final List<ApplicationInfo> list =
mPm.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| (user.isAdmin() ? PackageManager.GET_UNINSTALLED_PACKAGES : 0),
user.id);
for (ApplicationInfo info : list) {
if (includeInCount(info)) {
count++;
}
} catch (RemoteException e) {
}
}
return count;
@@ -66,5 +58,6 @@ public abstract class AppCounter extends AsyncTask<Void, Void, Integer> {
}
protected abstract void onCountComplete(int num);
protected abstract List<UserInfo> getUsersToCount();
protected abstract boolean includeInCount(ApplicationInfo info);
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.android.settings.applications;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import java.util.List;
public abstract class InstalledAppCounter extends AppCounter {
public InstalledAppCounter(Context context, PackageManagerWrapper packageManager) {
super(context, packageManager);
}
@Override
protected boolean includeInCount(ApplicationInfo info) {
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
return true;
}
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
return true;
}
Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
.addCategory(Intent.CATEGORY_LAUNCHER)
.setPackage(info.packageName);
int userId = UserHandle.getUserId(info.uid);
List<ResolveInfo> intents = mPm.queryIntentActivitiesAsUser(
launchIntent,
PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
userId);
return intents != null && intents.size() != 0;
}
}

View File

@@ -22,7 +22,7 @@ import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.icu.text.AlphabeticIndex;
import android.os.Bundle;
import android.os.Environment;
@@ -1259,7 +1259,8 @@ public class ManageApplications extends InstrumentedFragment
@Override
public void setListening(boolean listening) {
if (listening) {
new AppCounter(mContext) {
new InstalledAppCounter(mContext,
new PackageManagerWrapperImpl(mContext.getPackageManager())) {
@Override
protected void onCountComplete(int num) {
mLoader.setSummary(SummaryProvider.this,
@@ -1267,23 +1268,8 @@ public class ManageApplications extends InstrumentedFragment
}
@Override
protected boolean includeInCount(ApplicationInfo info) {
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
return true;
} else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
return true;
}
Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
.addCategory(Intent.CATEGORY_LAUNCHER)
.setPackage(info.packageName);
int userId = UserHandle.getUserId(info.uid);
List<ResolveInfo> intents = mPm.queryIntentActivitiesAsUser(
launchIntent,
PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
userId);
return intents != null && intents.size() != 0;
protected List<UserInfo> getUsersToCount() {
return mUm.getProfiles(UserHandle.myUserId());
}
}.execute();
}

View File

@@ -17,10 +17,17 @@ package com.android.settings.applications;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.R;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.notification.NotificationBackend;
import java.util.List;
/**
* Extension of ManageApplications with no changes other than having its own
* SummaryProvider.
@@ -42,12 +49,18 @@ public class NotificationApps extends ManageApplications {
@Override
public void setListening(boolean listening) {
if (listening) {
new AppCounter(mContext) {
new AppCounter(mContext,
new PackageManagerWrapperImpl(mContext.getPackageManager())) {
@Override
protected void onCountComplete(int num) {
updateSummary(num);
}
@Override
protected List<UserInfo> getUsersToCount() {
return mUm.getProfiles(UserHandle.myUserId());
}
@Override
protected boolean includeInCount(ApplicationInfo info) {
return mNotificationBackend.getNotificationsBanned(info.packageName,

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import java.util.List;
/**
* This interface replicates a subset of the android.content.pm.PackageManager (PM). The interface
* exists so that we can use a thin wrapper around the PM in production code and a mock in tests.
* We cannot directly mock or shadow the PM, because some of the methods we rely on are newer than
* the API version supported by Robolectric.
*/
public interface PackageManagerWrapper {
/**
* Calls {@code PackageManager.getInstalledApplicationsAsUser()}.
*
* @see android.content.pm.PackageManager.PackageManager#getInstalledApplicationsAsUser
*/
List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId);
/**
* Calls {@code PackageManager.queryIntentActivitiesAsUser()}.
*
* @see android.content.pm.PackageManager.PackageManager#queryIntentActivitiesAsUser
*/
List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId);
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import java.util.List;
public class PackageManagerWrapperImpl implements PackageManagerWrapper {
private final PackageManager mPm;
public PackageManagerWrapperImpl(PackageManager pm) {
mPm = pm;
}
@Override
public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
return mPm.getInstalledApplicationsAsUser(flags, userId);
}
@Override
public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId) {
return mPm.queryIntentActivitiesAsUser(intent, flags, userId);
}
}