PackageManager.getApplicationEnabledSetting

Please refer to the PackageManager.getPackageInfo/packageName hook documentation.

CollectionFif
GroupGet.Applications
NamePackageManager.getApplicationEnabledSetting
AuthorFif_
Version2
Updated (UTC)2018-10-11 03:45:25
Created (UTC)2018-10-11 03:43:27
Downloads1081
Class nameandroid.content.pm.PackageManager
Method namegetApplicationEnabledSetting
Parameter typesjava.lang.String
Return typeint
Min SDK1
Max SDK999
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