PackageManager.getPackageInfo/packageName

This hook complements the built-in PackageManager XPrivacyLua hooks. ★★★ Please make sure all hooks in the set are the same version! ★★★ ★★★ Please read the changelog below when upgrading to version 2! ★★★ Instructions: 1. Download these hooks: - PackageManager.getApplicationEnabledSetting (all Android versions) - PackageManager.getApplicationInfo (Marshmallow 6.0 and earlier) - PackageManager.getApplicationInfoAsUser (Nougat 7.0 and later) - PackageManager.getPackageInfo/packageName (Marshmallow 6.0 and earlier) - PackageManager.getPackageInfoAsUser (Nougat 7.0 and later) - PackageManager.getPackageInfo/versionedPackage (Oreo 8.0 and later) - PackageManager.getPackageGids (Marshmallow 6.0 and earlier) - PackageManager.getPackageGids/flags (Nougat 7.0 and later) - PackageManager.getPackageUid (Marshmallow 6.0 and earlier - PackageManager.getPackageUidAsUser (Nougat 7.0 and later) Note that it won't hurt and won't cause unnecessary overhead to download hooks for the wrong Android version, they'll be greyed out in "Manage hook defininitions". 2. Make sure that the Fif collection is enabled in XPL Pro. 3. For any app for which you want the hooks to apply: 3.a. Toggle the "Get Applications" category until it's fully enabled (red) in XPL. 3.b. Or, alternately, enable in XPL Pro all the hooks listed above that apply to your Android version. Changelog: v2 - 2018-10-10 Fix exception in getPackageUid/Gids hooks. Added getApplicationEnabledSetting, getApplicationInfo, getApplicationInfoAsUser, getPackageInfoAsUser and getPackageUidAsUser hooks. Made getPackageInfo/packageName, getPackageGids and getPackageUid available on MM and earlier only. Note hook users: if you are running Nougat 7.0 and later and had assigments for getPackageInfo/packageName, getPackageGids or getPackageUid you will have to reassign them respectively to getPackageInfoAsUser, getPackageGids/flags and getPackageUidAsUser when upgrading to version 2. v1 - 2018-10-04 First public release.

CollectionFif
GroupGet.Applications
NamePackageManager.getPackageInfo/packageName
AuthorFif_
Version2
Updated (UTC)2018-10-11 04:07:24
Created (UTC)2018-10-05 06:45:49
Downloads1422
Class nameandroid.content.pm.PackageManager
Method namegetPackageInfo
Parameter typesjava.lang.String, int
Return typeandroid.content.pm.PackageInfo
Min SDK1
Max SDK23
Min APK0
Max APK2147483647
Excluded packages-
EnabledYes
OptionalNo
UsageYes
NotifyNo
Settings-
-- Fif.PackageManager.get* is a Lua hook set designed to work with XPrivacyLua.
-- Copyright (C) 2018 Philippe Troin (Fif_ on XDA)

-- Fif.PackageManager.get* is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- Fif.PackageManager.get* is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with XPrivacyLua.  If not, see <http://www.gnu.org/licenses/>.

function after(hook, param)
    local result = param:getResult()
    if result == nil then
        return false
    end

    local scope = param:getApplicationContext()
    local applicationInfo, packageName
    local hookName = hook:getName()
    local hookNameGetPackageInfo = 'PackageManager.getPackageInfo'
    local hookNameGetPackage = 'PackageManager.getPackage'
    local hookNameGetApplication = 'PackageManager.getApplication'

    if hookName:sub(1, hookNameGetPackage:len()) == hookNameGetPackage then
        local packageInfo
        if hookName:sub(1, hookNameGetPackageInfo:len()) == hookNameGetPackageInfo then
            if (hookName == 'PackageManager.getPackageInfo/packageName' 
                    or hookName == 'PackageManager.getPackageInfoAsUser' )
                    and param:getValue('Fif.PackageManager.getPackage.selfCall.package.' .. param:getArgument(0), scope) == '1' then
                return false
            end
            packageInfo = result
        else
            local pm = param:getThis()
            -- Ugh, our hooked version of getPackageInfo can throw and exception
            -- which will show up as an error (even though the hooks together 
            -- performed as expected).  Signal to the other hook this is us calling ourselves.
            param:putValue('Fif.PackageManager.getPackage.selfCall.package.' .. param:getArgument(0), '1', scope)
            packageInfo = pm:getPackageInfo(param:getArgument(0), 0)
            param:putValue('Fif.PackageManager.getPackage.selfCall.package.' .. param:getArgument(0), nil, scope)
        end
        applicationInfo = packageInfo.applicationInfo
        packageName = packageInfo.packageName
    elseif hookName:sub(1, hookNameGetApplication:len()) == hookNameGetApplication then
        if hookName == 'PackageManager.getApplicationInfo' or hookName == 'PackageManager.getApplicationInfoAsUser' then
            if param:getValue('Fif.PackageManager.getPackage.selfCall.application.' .. param:getArgument(0), scope) == '1' then
                return false
            end
            applicationInfo = result
        else
            local pm = param:getThis()
            -- Ugh, our hooked version of getApplicationInfo can throw and exception
            -- which will show up as an error (even though the hooks together 
            -- performed as expected).  Signal to the other hook this is us calling ourselves.
            param:putValue('Fif.PackageManager.getPackage.selfCall.application.' .. param:getArgument(0), '1', scope)
            applicationInfo = pm:getApplicationInfo(param:getArgument(0), 0)
            param:putValue('Fif.PackageManager.getPackage.selfCall.application.' .. param:getArgument(0), nil, scope)
        end
        packageName = param:getArgument(0)
    else
        local crash = "Unexpected hookName=" .. hookName
        crash.crash()
    end

    if bit32.band(applicationInfo.flags, applicationInfo.FLAG_SYSTEM) ~= 0 then
        log('Allow ' .. packageName .. ' (system)')
        return false
    end

    local cuid = param:getUid()
    -- if hookName:sub(-6) == 'AsUser' then
    --     cuid = param:getArgument(2)
    -- end

    if cuid == applicationInfo.uid then
        log('Allow ' .. packageName .. ' (same uid)')
        return false
    end

    local clsException
    if hookName == 'PackageManager.getApplicationEnabledSetting' then
        clsException = luajava.bindClass('java.lang.IllegalArgumentException ')
    else
        clsException = luajava.bindClass('android.content.pm.PackageManager$NameNotFoundException')
    end
    local fake = luajava.new(clsException, packageName)
    param:setResult(fake)
    log('Deny ' .. packageName)
    return true, packageName, tostring(fake)
end