7

I have been writing a tasker task to enable/disable a package. I know

adb shell pm enable com.android.contacts

will enable the package and

adb shell pm disable com.android.contacts

will disable it.

I would like to know if there's any shell command that would tell me if a package is in enabled/disabled state. Thanks in advance! :)

2 Answers 2

16
pm list packages -d 2>/dev/null

will list up all disabled packages, one per line, preceded with the keyword package: – so output looks like

package:com.android.exchange
package:com.android.email

So if you take that output and "grep" it for the package name in question, the package is enabled if the output is "empty" and disabled otherwise. Alternatively, if you want a number (0/1), you can use the wc utility:

pm list packages -d 2>/dev/null | grep com.android.exchange | wc -l

would return "1" in above example, meaning the package com.android.exchange is disabled. It would, however, return "0" if I'd grep for com.foobar.app – which doesn't necessarily mean that com.foobar.app is enabled but could also mean it's simply not installed :) So if you want to cross-check, you can do so running the same command with pm list packages -e (to list enabled packages).

2

There are apps that have similar names, so just a simple grep might be problematic. Take the following apps for instance.

com.amazon.dcp
com.amazon.dcp.contracts.framework.library

If the short one is enabled and the long one is disabled, you will get a result searching the list of disabled apps. Here is a function to determine if an app is disabled.

check_apk_disabled(){ # Check if a package is disabled. Return 1 if installed else 0
            [ $# -lt 1 ] && return 2
            adb shell pm list packages -d | cut -d: -f2 | sed 's|\r||' | grep -c "^${1}$"; }

The package list is stripped of package: as well as the 0D byte. Grep can then find an exact match with ^ at start and $ at the end.

check_apk_disabled com.android.contacts will return 1 if disabled or 0 if not. It should be noted that if an app is not installed then it will not appear on the disabled list and trying to disable an app that is not installed will generate errors. A check if the app is installed should be done before assuming it is not disabled.

check_apk_installed(){ # Check if a package is installed. Return 1 if installed else 0
            [ $# -lt 1 ] && return 2
            adb shell pm list packages | cut -d: -f2 | sed 's|\r||' | grep -c "^${1}$"; }

Now with these 2 functions you can disable the app properly.

apk=com.android.contacts
[ $(check_apk_installed $apk) == "1" ] && [ $(check_apk_disabled $apk) == "0" ] && adb pm disable-user --user=0 $apk 2>/dev/null || echo "app is not installed or already disabled"

Sorry to necro the post, but the potential to disable the wrong app in the original answer scared me!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .