Skip to content

Commit

Permalink
Improve the enable system apps flow in the TestDPC
Browse files Browse the repository at this point in the history
Show a list of disabled system apps. When an app from the list is clicked,
it is enabled.

Bug: 19577619

Change-Id: Ib2370412941e1c47ffdcacf8814a784b1529eef6
  • Loading branch information
stevenckngaa committed Mar 13, 2015
1 parent f3dda35 commit c28f8e9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,24 @@
import java.util.List;

/**
* A simple adapter which takes a list of package name and shows their app icon and app name in a
* A simple adapter which takes a list of package names and shows their app icon and app name in a
* listview.
*/
public class AppInfoArrayAdapter extends ArrayAdapter<String> {

private PackageManager mPackageManager;
private int mAppInfoFlags = 0;

public AppInfoArrayAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
public AppInfoArrayAdapter(Context context, int resource, List<String> pkgNameList,
boolean includeDisabledApps) {
super(context, resource, pkgNameList);
mPackageManager = getContext().getPackageManager();
if (includeDisabledApps) {
mAppInfoFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
}
}

public AppInfoArrayAdapter(Context context, int resource, List<String> pkgNameList) {
this(context, resource, pkgNameList, false /* Don't include disabled apps */);
}

@Override
Expand All @@ -52,13 +60,15 @@ public View getView(int position, View convertView, ViewGroup parent) {

try {
ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(getItem(position),
0 /* No flags*/);
mAppInfoFlags);
ImageView iconImageView = (ImageView) convertView.findViewById(R.id.pkg_icon);
iconImageView.setImageDrawable(mPackageManager.getApplicationIcon(applicationInfo));
TextView pkgNameTextView = (TextView) convertView.findViewById(R.id.pkg_name);
pkgNameTextView.setText(mPackageManager.getApplicationLabel(applicationInfo));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
// Returns an empty view in case the package is not found.
return new View(getContext());
}
return convertView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

import com.google.android.testdpc.DeviceAdminReceiver;
import com.google.android.testdpc.R;
import com.google.android.testdpc.common.AppInfoArrayAdapter;
import com.google.android.testdpc.policy.accessibility.AccessibilityServiceInfoArrayAdapter;
import com.google.android.testdpc.policy.blockuninstallation.BlockUninstallationInfoArrayAdapter;
import com.google.android.testdpc.policy.inputmethod.InputMethodInfoArrayAdapter;
Expand All @@ -91,7 +92,9 @@
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Provides several device management functions.
Expand Down Expand Up @@ -161,6 +164,7 @@ public class PolicyManagementFragment extends PreferenceFragment implements
private static final String BLOCK_UNINSTALLATION_BY_PKG_KEY = "block_uninstallation_by_pkg";
private static final String BLOCK_UNINSTALLATION_LIST_KEY = "block_uninstallation_list";
private static final String DISABLE_CAMERA_KEY = "disable_camera";
private static final String ENABLE_SYSTEM_APPS_KEY = "enable_system_apps";
private static final String ENABLE_SYSTEM_APPS_BY_PACKAGE_NAME_KEY
= "enable_system_apps_by_package_name";
private static final String ENABLE_SYSTEM_APPS_BY_INTENT_KEY = "enable_system_apps_by_intent";
Expand Down Expand Up @@ -270,6 +274,7 @@ public void onCreate(Bundle savedInstanceState) {
findPreference(GET_DISABLE_ACCOUNT_MANAGEMENT_KEY).setOnPreferenceClickListener(this);
findPreference(BLOCK_UNINSTALLATION_BY_PKG_KEY).setOnPreferenceClickListener(this);
findPreference(BLOCK_UNINSTALLATION_LIST_KEY).setOnPreferenceClickListener(this);
findPreference(ENABLE_SYSTEM_APPS_KEY).setOnPreferenceClickListener(this);
findPreference(ENABLE_SYSTEM_APPS_BY_PACKAGE_NAME_KEY).setOnPreferenceClickListener(this);
findPreference(ENABLE_SYSTEM_APPS_BY_INTENT_KEY).setOnPreferenceClickListener(this);
findPreference(MANAGE_APP_RESTRICTIONS_KEY).setOnPreferenceClickListener(this);
Expand Down Expand Up @@ -360,6 +365,9 @@ public void onClick(DialogInterface dialogInterface, int i) {
case BLOCK_UNINSTALLATION_LIST_KEY:
showBlockUninstallationPrompt();
return true;
case ENABLE_SYSTEM_APPS_KEY:
showEnableSystemAppsPrompt();
return true;
case ENABLE_SYSTEM_APPS_BY_PACKAGE_NAME_KEY:
showEnableSystemAppByPackageNamePrompt();
return true;
Expand Down Expand Up @@ -1066,8 +1074,8 @@ private void showCaCertificateList() {
if (getActivity() == null || getActivity().isFinishing()) {
return;
}
// Avoid starting the same task twice.
if (mShowCaCertificateListTask != null && !mShowCaCertificateListTask.isCancelled()) {
// Cancel the previous task
mShowCaCertificateListTask.cancel(true);
}
mShowCaCertificateListTask = new ShowCaCertificateListTask();
Expand All @@ -1085,7 +1093,7 @@ private void showManagedProfileSpecificPolicyFragment() {

/**
* Displays an alert dialog that allows the user to select applications from all non-system
* applications installed on the current profile. When the user selects an app, this app can't
* applications installed on the current profile. After the user selects an app, this app can't
* be uninstallation.
*/
private void showBlockUninstallationPrompt() {
Expand Down Expand Up @@ -1124,6 +1132,51 @@ public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
.show();
}

/**
* Shows an alert dialog which displays a list of disabled system apps. Clicking an app in the
* dialog enables the app.
*/
private void showEnableSystemAppsPrompt() {
// Disabled system apps list = {All system apps} - {Enabled system apps}
final List<String> disabledSystemApps = new ArrayList<String>();
// This list contains both enabled and disabled apps.
List<ApplicationInfo> allApps = mPackageManager.getInstalledApplications(
PackageManager.GET_UNINSTALLED_PACKAGES);
// This list contains all enabled apps.
List<ApplicationInfo> enabledApps =
mPackageManager.getInstalledApplications(0 /* No flags */);
Set<String> enabledAppsPkgNames = new HashSet<String>();
for (ApplicationInfo applicationInfo : enabledApps) {
enabledAppsPkgNames.add(applicationInfo.packageName);
}
for (ApplicationInfo applicationInfo : allApps) {
// Interested in disabled system apps only.
if (!enabledAppsPkgNames.contains(applicationInfo.packageName)
&& (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
disabledSystemApps.add(applicationInfo.packageName);
}
}

if (disabledSystemApps.isEmpty()) {
showToast(R.string.no_disabled_system_apps);
} else {
AppInfoArrayAdapter appInfoArrayAdapter = new AppInfoArrayAdapter(getActivity(),
R.id.pkg_name, disabledSystemApps, true);
new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.enable_system_apps_title))
.setAdapter(appInfoArrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
String packageName = disabledSystemApps.get(position);
mDevicePolicyManager.enableSystemApp(mAdminComponentName, packageName);
showToast(R.string.enable_system_apps_by_package_name_success_msg,
packageName);
}
})
.show();
}
}

private void showToast(int msgId, Object... args) {
Activity activity = getActivity();
if (activity == null || activity.isFinishing()) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
</string>
<string name="enable_system_apps_attempt_msg">Enabled system apps with the given intent.
</string>
<string name ="no_disabled_system_apps">All system apps have been enabled.</string>
<string name="enable">Enable</string>
<string name="invalid_system_apps_action">Invalid action.</string>

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/xml/device_policy_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ limitations under the License.

<PreferenceCategory
android:title="@string/apps_management_title">
<Preference
android:key="enable_system_apps"
android:title="@string/enable_system_apps_title"/>
<Preference
android:key="enable_system_apps_by_package_name"
android:title="@string/enable_system_apps_by_package_name"/>

<Preference
android:key="enable_system_apps_by_intent"
android:title="@string/enable_system_apps_by_intent"/>

<Preference
android:key="manage_app_restrictions"
android:title="@string/manage_app_restrictions"/>
Expand Down

0 comments on commit c28f8e9

Please sign in to comment.