clearCallingIdentity及restoreCallingIdentity用途

本文详细解析了Android系统服务中clearCallingIdentity和restoreCallingIdentity的作用,通过实例分析了这两个方法如何在权限检查和跨进程通信中确保正确处理调用者的身份。在处理如设置WiFi热点等敏感操作时,这些方法对于维持权限管理和AppOps策略的合规性至关重要。

lollipop-mstar-master/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java 中api setWifiApEnabled实现中

有以下语句

......

          if (! mSettingsStore.handleWifiApToggled(enabled)) {                                                                                                                                               

                return;

            } else {

                Slog.d(TAG, "handleWifiApToggled " + enabled);

            }    

 

handleWifiApToggled的实现在WifiSettingsStore.java文件中如下:

 

    synchronized boolean handleWifiApToggled(boolean wifiApEnabled) {

        if (mAirplaneModeOn && !isAirplaneToggleable()) {

            return false;

        }

 

        if (wifiApEnabled) {

            if (mAirplaneModeOn) {

                persistWifiApState(WIFI_ENABLED_AIRPLANE_OVERRIDE);

            } else {

                persistWifiApState(WIFI_AP_ENABLED);

            }

        } else {

            persistWifiApState(WIFI_AP_DISABLED);

        }                                                                                                                                                                                                      

        return true;

    }

===》

 

   private void persistWifiApState(int state) {                                                                                                                                                               

        final ContentResolver cr

void tempAllowlistTargetPkgIfPossible(int targetUid, String targetPackage, 914 int callingPid, int callingUid, String callingPackage, String reason) { 915 final long token = Binder.clearCallingIdentity(); 916 try { 917 MediaServerUtils.enforcePackageName(mContext, callingPackage, callingUid); 918 if (targetUid != callingUid) { 919 boolean canAllowWhileInUse = 920 mActivityManagerInternal.canAllowWhileInUsePermissionInFgs( 921 callingPid, callingUid, callingPackage); 922 boolean canStartFgs = 923 canAllowWhileInUse 924 || mActivityManagerInternal.canStartForegroundService( 925 callingPid, callingUid, callingPackage); 926 Log.i(TAG, "tempAllowlistTargetPkgIfPossible callingPackage:" 927 + callingPackage + " targetPackage:" + targetPackage 928 + " reason:" + reason 929 + (canAllowWhileInUse ? " [WIU]" : "") 930 + (canStartFgs ? " [FGS]" : "")); 931 if (canAllowWhileInUse) { 932 mActivityManagerInternal.tempAllowWhileInUsePermissionInFgs( 933 targetUid, 934 MediaSessionDeviceConfig 935 .getMediaSessionCallbackFgsWhileInUseTempAllowDurationMs()); 936 } 937 if (canStartFgs) { 938 final Context userContext = mContext.createContextAsUser( 939 UserHandle.of(UserHandle.getUserId(targetUid)), /* flags= */ 0); 940 final PowerExemptionManager powerExemptionManager = 941 userContext.getSystemService( 942 PowerExemptionManager.class); 943 powerExemptionManager.addToTemporaryAllowList(targetPackage, 944 PowerExemptionManager.REASON_MEDIA_SESSION_CALLBACK, reason, 945 MediaSessionDeviceConfig.getMediaSessionCallbackFgsAllowlistDurationMs()); 946 } 947 } 948 } finally { 949 Binder.restoreCallingIdentity(token); 950 } 951 }深度解析
最新发布
12-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值