Update, install, hide and unhide APK

1, The apk installed by the user has been updated

public void registerReceiver(Context context) {
        Slog.d("PMSdddd", "systemReady1");
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addDataScheme("package");
        BroadcastReceiver packgeMonitorReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String action = intent.getAction();
                final String packageName =
intent.getData().getSchemeSpecificPart();
                final boolean replacing =
intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
                final boolean dataRemoved =
intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false);

                Slog.d("PMSdddd", "action: " + action + " packageName:"
+ packageName + "  replacing:" + replacing
                        + "  dataRemoved:" + dataRemoved);
            }
        };
        context.registerReceiver(packgeMonitorReceiver, filter);
    }

Step 1, install apk

adb install apk

Line 52701: 02-01 21:26:23.213  1727  1727 D PMSdddd : action: android.intent.action.PACKAGE_ADDED  packageName:com.adobe.reader  replacing:false  dataRemoved:false

Step 2: hide this apk

adb shell pm hide apk

Line 53895: 02-01 21:28:22.043  1727  1727 D PMSdddd : action: android.intent.action.PACKAGE_REMOVED  packageName:com.adobe.reader  replacing:false  dataRemoved:false

Step 3, adb install update
adb install -r apk

Line 54477: 02-01 21:29:41.550  1727  1727 D PMSdddd : action: android.intent.action.PACKAGE_REMOVED  packageName:com.adobe.reader  replacing:true  dataRemoved:false


Line 54512: 02-01 21:29:41.648  1727  1727 D PMSdddd : action: android.intent.action.PACKAGE_ADDED  packageName:com.adobe.reader  replacing:true  dataRemoved:false

Step 4, unhide the apk

adb shell pm unhide apk

Line 55640: 02-01 21:32:25.651  1727  1727 D PMSdddd : action: android.intent.action.PACKAGE_ADDED  packageName:com.adobe.reader  replacing:false  dataRemoved:false

Step 5: adb uninstall uninstalls apk

Line 61310: 02-01 21:52:36.395  1727  1727 D PMSdddd : action: android.intent.action.PACKAGE_REMOVED  packageName:com.adobe.reader  replacing:false  dataRemoved:true

Summary:
hide command: the system will send the broadcast Android intent. action. PACKAGE_ Removed, the data is not deleted
unhide command: the system will send the broadcast Android intent. action. PACKAGE_ Add, data will not be deleted

When updating apk:
The system first sends the broadcast Android intent. action. PACKAGE_ REMOVED,intent. The value obtained by getboolean extra (intent.extra_replacing, false) is true

The system then sends the broadcast Android intent. action. PACKAGE_ ADDED,intent. The value obtained by getboolean extra (intent.extra_replacing, false) is also true

When uninstalling apk:
The system sends broadcast Android intent. action. PACKAGE_ REMOVED,intent. The value obtained by getboolean extra (intent.extra_data_removed, false) is true

2, The system apk (located in the system/app directory) is updated

Step 1: preset apk(v1.0) to system/app

adb push apk system/app

Then restore the factory settings
Step 2: adb install-r updates apk to v2.0 0

Line 8118: 02-02 02:55:59.428  1130  1130 D PMSdddd : action: android.intent.action.PACKAGE_REMOVED  packageName:com.example.ddd  replacing:true  dataRemoved:false


Line 8134: 02-02 02:55:59.501  1130  1130 D PMSdddd : action: android.intent.action.PACKAGE_ADDED  packageName:com.example.ddd  replacing:true  dataRemoved:false


Line 8281: 02-02 02:55:59.903  1130  1130 D PMSdddd : action: android.intent.action.PACKAGE_REPLACED  packageName:com.example.ddd  replacing:true  dataRemoved:false

The updated apk is installed in the data/app Directory:

    Line 8550: 02-02 05:12:10.806  1132  1228 I PackageManager: Update system package com.example.ddd code path from /system/operator/app/AndroidDemoV1.0.apk to /data/app/~~IYd549AfrGeHUc-lWfY8kg==/com.example.ddd-o9yTi7O1l3Cm3EhqTvf2Rw==; Retain data and using new
    Line 8550: 02-02 05:12:10.806  1132  1228 I PackageManager: Update system package com.example.ddd code path from /system/operator/app/AndroidDemoV1.0.apk to /data/app/~~IYd549AfrGeHUc-lWfY8kg==/com.example.ddd-o9yTi7O1l3Cm3EhqTvf2Rw==; Retain data and using new
    Line 8551: 02-02 05:12:10.808  1132  1228 I PackageManager: Update system package com.example.ddd resource path from /system/operator/app/AndroidDemoV1.0.apk to /data/app/~~IYd549AfrGeHUc-lWfY8kg==/com.example.ddd-o9yTi7O1l3Cm3EhqTvf2Rw==; Retain data and using new
    Line 8551: 02-02 05:12:10.808  1132  1228 I PackageManager: Update system package com.example.ddd resource path from /system/operator/app/AndroidDemoV1.0.apk to /data/app/~~IYd549AfrGeHUc-lWfY8kg==/com.example.ddd-o9yTi7O1l3Cm3EhqTvf2Rw==; Retain data and using new

Step 3: the Settings "application information" interface cannot be uninstalled, and there are no uninstall buttons (open, disable and forced stop)

Summary:

When the system apk is updated, the system sends the broadcast: Action: Android intent. action. PACKAGE_ REMOVED,action: android.intent.action.PACKAGE_ADDED,action: android.intent.action.PACKAGE_REPLACED

And intent The value obtained by getboolean extra (intent.extra_replacing, false) is true

Android monitors multi-user switching, hides and disables the specified Apk

IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_USER_SWITCHED);
        filter.addAction(Intent.ACTION_USER_ADDED);
        BroadcastReceiver mUserSwitchedReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String action = intent.getAction();
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
                if (userId == UserHandle.USER_NULL) {
                    Slog.e("PMSdddd", "received an invalid EXTRA_USER_HANDLE");
                    return;
                }
                if (Intent.ACTION_USER_SWITCHED.equals(action) && userId > 0) {
                    Slog.d("PMSdddd", "User switched to userId " + userId);
                    AsyncTask.execute(new Runnable() {
                        @Override
                        public void run() {
                            Slog.d("PMSdddd", "install start11");
                            Sbbbbbbbbbbb.hideOtherBrandAppWhenUserSwitched();
                        }
                    });
                } else if (Intent.ACTION_USER_ADDED.equals(action) && userId > 0) {
                    Slog.d("PMSdddd", "Added User  userId " + userId);
                    AsyncTask.execute(new Runnable() {
                        @Override
                        public void run() {
                            Slog.d("PMSdddd", "install start11");
Sbbbbbbbbbbb
.hideOtherBrandAppWhenUserSwitched(); } }); } } }; mContext.registerReceiver(mUserSwitchedReceiver, filter);

Hide and disable apk

/**
     * only hide app but don't delete user data
     *
     * @param pkgName
     */
    private void hide(String pkgName) {
        final PackageManager pm = mContext.getPackageManager();
        int userId = ActivityManager.getCurrentUser();
        pm.setApplicationHiddenSettingAsUser(pkgName, true, new UserHandle(userId));
        disableApplication(pkgName);
    }

    private void unhide(String pkgName) {
        final PackageManager pm = mContext.getPackageManager();
        int userId = ActivityManager.getCurrentUser();
        pm.setApplicationHiddenSettingAsUser(pkgName, false, new UserHandle(userId));
        enableApplication(pkgName);
    }

    private void disableApplication(String pkgName) {
        final PackageManager pm = mContext.getPackageManager();
        try {
            int state = pm.getApplicationEnabledSetting(pkgName);
            Slog.d(TAG, "disableApplication state: " + state + " pkgName:" + pkgName);
            if (state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED)
                return;
            pm.setApplicationEnabledSetting(pkgName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
        } catch (IllegalArgumentException exeption) {
            Slog.w(TAG, "disableApplication error:" + exeption.getMessage());
        }
    }

    private void enableApplication(String pkgName) {
        final PackageManager pm = mContext.getPackageManager();
        try {
            int state = pm.getApplicationEnabledSetting(pkgName);
            Slog.d(TAG, "enableApplication state: " + state + " pkgName:" + pkgName);
            if (state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
                return;
            pm.setApplicationEnabledSetting(pkgName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
        } catch (IllegalArgumentException exeption) {
            Slog.w(TAG, "enableApplication error:" + exeption.getMessage());
        }
    }

Hidden apk or disable d apk. The information is saved in / data / system / users / 0 / package restrictions XML file

<pkg name="com.google.android.apps.maps" ceDataInode="4439" enabled="3" enabledCaller="com.android.settings" domainVerificationStatus="2" app-link-generation="4">

<pkg name="com.google.android.apps.maps" ceDataInode="4439" enabled="2" enabledCaller="android" domainVerificationStatus="2" app-link-generation="4">
<pkg name="com.amazon.appmanager" ceDataInode="4472" hidden="true" enabled="2" enabledCaller="android" />

<pkg name="com.google.android.apps.maps" ceDataInode="4439" enabled="3" enabledCaller="com.android.settings" domainVerificationStatus="2" app-link-generation="4">

Information saved after apk is hidden: hidden="true"

After apk is disable d, the value of enabled = will be different according to the constant parameters passed in setApplicationEnabledSetting, as described below:

public static final int COMPONENT_ENABLED_STATE_DISABLED = 2;  // When disabling an application, if this parameter is passed in, the application has no icon on the desktop, and there is no icon in the setting interface and application list (the application is completely hidden)

public static final int COMPONENT_ENABLED_STATE_DISABLED_USER = 3;  // When disabling an application, if this parameter is passed in, the application has no icon on the desktop, but there are icons in the setting interface and application list (only the desktop icon is hidden, the icon can still be seen in the setting, and the user can enable the application again)

In addition: ADB shell PM disable user application package name: equivalent to the passed in parameter is COMPONENT_ENABLED_STATE_DISABLED.

How to check which apps on the phone are disable d:

Method 1: adb shell pm list packages -d

C:\Users\zzz>adb shell pm list packages -d
package:com.facebook.services
package:com.google.android.videos
package:com.facebook.appmanager

Method 2: ADB shell PM dump packages > Desktop / log2 txt

Enabled = 2 ---- > indicates that the application is in the disable state. The corresponding value is COMPONENT_ENABLED_STATE_DISABLED

Package [com.facebook.appmanager] (a926214):
    userId=10098
    pkg=Package{9ae0abd com.facebook.appmanager}
    codePath=/system/app/appmanager
    resourcePath=/system/app/appmanager
    legacyNativeLibraryDir=/system/app/appmanager/lib
    primaryCpuAbi=arm64-v8a
    secondaryCpuAbi=null
    versionCode=277606887 minSdk=21 targetSdk=30
    versionName=67.3.0
    splits=[base]
    apkSigningVersion=2
    applicationInfo=ApplicationInfo{9ae0abd com.facebook.appmanager}
    flags=[ SYSTEM HAS_CODE ALLOW_CLEAR_USER_DATA ]
    privateFlags=[ PRIVATE_FLAG_ACTIVITIES_RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION ALLOW_AUDIO_PLAYBACK_CAPTURE HAS_DOMAIN_URLS PRIVATE_FLAG_ALLOW_NATIVE_HEAP_POINTER_TAGGING ]
    forceQueryable=false
    queriesIntents=[Intent { act=com.facebook.secure.packagefinder.intent.ACTION_QUERY_PACKAGES }]
    dataDir=/data/user/0/com.facebook.appmanager
    supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
    timeStamp=2021-07-12 23:30:37
    firstInstallTime=2021-07-12 23:30:37
    lastUpdateTime=2021-07-12 23:30:37
    signatures=PackageSignatures{18e58b2 version:2, signatures:[c4e416cc], past signatures:[]}
    installPermissionsFixed=true
    pkgFlags=[ SYSTEM HAS_CODE ALLOW_CLEAR_USER_DATA ]
    declared permissions:
      com.facebook.appmanager.ACCESS: prot=signature, INSTALLED
      com.facebook.appmanager.API_ACCESS: prot=normal, INSTALLED
    install permissions:
      android.permission.DOWNLOAD_WITHOUT_NOTIFICATION: granted=true
      android.permission.FOREGROUND_SERVICE: granted=true
      android.permission.RECEIVE_BOOT_COMPLETED: granted=true
      android.permission.INTERNET: granted=true
      android.permission.GET_PACKAGE_SIZE: granted=true
      com.facebook.appmanager.ACCESS: granted=true
      android.permission.ACCESS_NETWORK_STATE: granted=true
      android.permission.ACCESS_WIFI_STATE: granted=true
      android.permission.WAKE_LOCK: granted=true
    User 0: ceDataInode=4785 installed=true hidden=false suspended=false distractionFlags=0 stopped=false notLaunched=false enabled=2 instant=false virtual=false
    overlay paths:
      /product/overlay/NavigationBarModeGestural/NavigationBarModeGesturalOverlay.apk
      lastDisabledCaller: android
      gids=[3003]

Check which apps belong to the system signature?

Method: ADB shell PM dump packages > Desktop / log2 txt

The apk with the package name "android" belongs to the platform apk and must be the system signature. First, take a look at the signature information of the apk with the package name "android":

 Package [android] (ad28bfa):
    userId=1000
    sharedUser=SharedUserSetting{769a3df android.uid.system/1000}
    pkg=Package{6103b22 android}
    codePath=/system/framework/framework-res.apk
    resourcePath=/system/framework/framework-res.apk
    legacyNativeLibraryDir=/system/lib64/framework-res
    primaryCpuAbi=arm64-v8a
    secondaryCpuAbi=null
    versionCode=30 minSdk=30 targetSdk=30
    versionName=11
    splits=[base]
    apkSigningVersion=3
    applicationInfo=ApplicationInfo{6103b22 android}
    flags=[ SYSTEM PERSISTENT ALLOW_BACKUP ]
    privateFlags=[ PRIVATE_FLAG_ACTIVITIES_RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION ALLOW_AUDIO_PLAYBACK_CAPTURE DEFAULT_TO_DEVICE_PROTECTED_STORAGE DIRECT_BOOT_AWARE PRIVILEGED PRIVATE_FLAG_ALLOW_NATIVE_HEAP_POINTER_TAGGING ]
    forceQueryable=true
    queriesPackages=[]
    dataDir=/data/system
    supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
    timeStamp=2021-07-12 23:27:41
    firstInstallTime=2021-07-12 23:27:41
    lastUpdateTime=2021-07-12 23:27:41
    signatures=PackageSignatures{539b9b3 version:3, signatures:[a0521abc], past signatures:[]}
    installPermissionsFixed=true
    pkgFlags=[ SYSTEM PERSISTENT ALLOW_BACKUP ]
    declared permissions:
      android.permission.READ_CONTACTS: prot=dangerous, INSTALLED
      android.permission.WRITE_CONTACTS: prot=dangerous, INSTALLED
      ........

    User 0: ceDataInode=4316 installed=true hidden=false suspended=false distractionFlags=0 stopped=false notLaunched=false enabled=0 instant=false virtual=false
    overlay paths:
      /product/overlay/NavigationBarModeGestural/NavigationBarModeGesturalOverlay.apk
      

From the above information, you can see the signature information: signatures:[a0521abc], and then in log2 Txt file global search "a0521abc", you can list all applications signed by the system.

Conclusion: the follow-up will be continuously updated. If you like, please pay attention to it.
Related video
[Android advanced] reinforcement and optimization of APK

Keywords: Android apk

Added by risi on Mon, 13 Dec 2021 06:17:17 +0200