当前位置: 首页 > article >正文

Android MediaProjection录屏权限处理

 MediaProjection服务在SystemServiceRegistry的静态块里面注册的

@frameworks/base/core/java/android/app/SystemServiceRegistry.java
public final class SystemServiceRegistry {
    private static final String TAG = "SystemServiceRegistry";

    /** @hide */
    public static boolean sEnableServiceNotFoundWtf = false;

    private static int sServiceCacheSize;

    private static volatile boolean sInitializing;

    // Not instantiable.
    private SystemServiceRegistry() { }

    static {
        registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,

申请屏幕录制权限通过启动com.android.systemui/.media.MediaProjectionPermissionActivity来获取权限

requestScreenRecordPermission
    Intent captureIntent = u3dClient.createScreenCaptureIntent();
        this.mProjectionManager.createScreenCaptureIntent();  
        //Intent { cmp=com.android.systemui/.media.MediaProjectionPermissionActivity } packagename=com.android.systemui
    startActivityForResult(captureIntent, LOCAL_REQUEST_CODE);
@@frameworks/base/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java


/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.media;

import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.media.projection.IMediaProjection;
import android.media.projection.IMediaProjectionManager;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.BidiFormatter;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.systemui.R;
import com.android.systemui.util.Utils;

public class MediaProjectionPermissionActivity extends Activity
        implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
    private static final String TAG = "MediaProjectionPermissionActivity";
    private static final float MAX_APP_NAME_SIZE_PX = 500f;
    private static final String ELLIPSIS = "\u2026";

    private String mPackageName;
    private int mUid;
    private IMediaProjectionManager mService;

    private AlertDialog mDialog;

    @Override
    public void onCreate(Bundle icicle) {

	        Log.e(TAG, "xww getprojectionperimssino 1");
        super.onCreate(icicle);

        mPackageName = getCallingPackage();
        IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
        mService = IMediaProjectionManager.Stub.asInterface(b);

        if (mPackageName == null) {
            finish();
	        Log.e(TAG, "xww getprojectionperimssino 2");
            return;
        }

        PackageManager packageManager = getPackageManager();
        ApplicationInfo aInfo;
        try {
            aInfo = packageManager.getApplicationInfo(mPackageName, 0);
            mUid = aInfo.uid;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "unable to look up package name", e);
            finish();
	        Log.e(TAG, "xww getprojectionperimssino 3");
            return;
        }

        try {
            if (mService.hasProjectionPermission(mUid, mPackageName)) {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
                finish();
	            Log.e(TAG, "xww getprojectionperimssino 4");
                return;
            } else {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
	            Log.e(TAG, "xww getprojectionperimssino 5");
                finish();
                return;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Error checking projection permissions", e);
            finish();
	        Log.e(TAG, "xww getprojectionperimssino 6");
            return;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        try {
            if (which == AlertDialog.BUTTON_POSITIVE) {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Error granting projection permission", e);
            setResult(RESULT_CANCELED);
        } finally {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            finish();
        }
    }

    private Intent getMediaProjectionIntent(int uid, String packageName)
            throws RemoteException {
        IMediaProjection projection = mService.createProjection(uid, packageName,
                 MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */);
        Intent intent = new Intent();
        intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
        return intent;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        finish();
    }
}

最后通过获取Activity的结果判断是否成功获取到屏幕录制权限

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_CANCELED) {
            Log.e(TAG, "获取到屏幕录制权限----");
            return;
        }
    }

@@frameworks/base/packages/SystemUI/src/com/android/systemui/media/MediaProjectionPermissionActivity.java


/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.media;

import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.media.projection.IMediaProjection;
import android.media.projection.IMediaProjectionManager;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.BidiFormatter;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.systemui.R;
import com.android.systemui.util.Utils;

public class MediaProjectionPermissionActivity extends Activity
        implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
    private static final String TAG = "MediaProjectionPermissionActivity";
    private static final float MAX_APP_NAME_SIZE_PX = 500f;
    private static final String ELLIPSIS = "\u2026";

    private String mPackageName;
    private int mUid;
    private IMediaProjectionManager mService;

    private AlertDialog mDialog;

    @Override
    public void onCreate(Bundle icicle) {

            Log.e(TAG, "xww getprojectionperimssino 1");
        super.onCreate(icicle);

        mPackageName = getCallingPackage();
        IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
        mService = IMediaProjectionManager.Stub.asInterface(b);

        if (mPackageName == null) {
            finish();
            Log.e(TAG, "xww getprojectionperimssino 2");
            return;
        }

        PackageManager packageManager = getPackageManager();
        ApplicationInfo aInfo;
        try {
            aInfo = packageManager.getApplicationInfo(mPackageName, 0);
            mUid = aInfo.uid;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "unable to look up package name", e);
            finish();
            Log.e(TAG, "xww getprojectionperimssino 3");
            return;
        }

        try {
            if (mService.hasProjectionPermission(mUid, mPackageName)) {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
                finish();
                Log.e(TAG, "xww getprojectionperimssino 4");
                return;
            } else {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
                Log.e(TAG, "xww getprojectionperimssino 5");
                finish();
                return;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Error checking projection permissions", e);
            finish();
            Log.e(TAG, "xww getprojectionperimssino 6");
            return;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        try {
            if (which == AlertDialog.BUTTON_POSITIVE) {
                setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName));
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Error granting projection permission", e);
            setResult(RESULT_CANCELED);
        } finally {
            if (mDialog != null) {
                mDialog.dismiss();
            }
            finish();
        }
    }

    private Intent getMediaProjectionIntent(int uid, String packageName)
            throws RemoteException {
        IMediaProjection projection = mService.createProjection(uid, packageName,
                 MediaProjectionManager.TYPE_SCREEN_CAPTURE, false /* permanentGrant */);
        Intent intent = new Intent();
        intent.putExtra(MediaProjectionManager.EXTRA_MEDIA_PROJECTION, projection.asBinder());
        return intent;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        finish();
    }
}
 


http://www.kler.cn/news/302579.html

相关文章:

  • VIT论文阅读
  • 工业相机飞拍的原理及工作原理
  • Python(TensorFlow和PyTorch)两种显微镜成像重建算法模型(显微镜学)
  • 简单计算机网络概念
  • Salting technique
  • flink中startNewChain() 的详解
  • Qt-QWidget的font属性(18)
  • 2.ChatGPT的发展历程:从GPT-1到GPT-4(2/10)
  • Linux 管道
  • vue原理分析(十一)研究new Vue()中的initRender
  • 基于深度学习的结构优化与生成
  • 深入理解Kotlin中的异步网络请求处理
  • JavaScript 将 json 美化输出
  • 前端速通面经八股系列(八)—— React篇(上)
  • 基于鸿蒙API10的RTSP播放器(八:音量和亮度调节功能的整合)
  • 数据结构之折半插入排序概念、折半插入排序的具体步骤、折半插入排序的具体代码示例
  • 摊牌了!一文教会你轻松上手豆包MarsCode 编程助手!
  • Android的内核
  • 【STM32】外部中断
  • 数据结构 - 栈
  • 多态(c++)
  • 怎样还原空白试卷?2024教你快速还原空白试卷的软件
  • Python 最小公倍数计算器:从基础到应用
  • 鸿蒙-沉浸式pc端失效
  • 深入理解全连接层:从线性代数到 PyTorch 中的 nn.Linear 和 nn.Parameter
  • Unity Shader实现简单的各向异性渲染(采用各向异性形式的GGX分布)
  • 优化销售流程:免费体验企元数智小程序合规分销系统!
  • Idea 2021.3 破解 window
  • vue3常见的bug 修复bug
  • 力扣每日一题:1372.二叉树中的最长交错路径