Android 可视化工具权限失败

Android Visualizer Permission Failure

我正在开发一个简单的可视化应用程序,它可以监听音频输出,生成 FFT(由可视化工具 API 处理),做一些数学运算,然后无线发送信息进行显示。我在 Oneplus3 的 Android 8.0 上安装 Visualizer API 运行 时遇到问题,但它在模拟的 Android 6.0 和 8.0 手机上都能正常工作。

这是我请求我认为我需要的权限的方式,首先在清单中:

<manifest>
    <application>
        ...rest of application...
    </application>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

</manifest>

接下来我在运行时请求一些权限:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    if (ContextCompat.checkSelfPermission(getApplicationContext(), permission.MODIFY_AUDIO_SETTINGS) == PackageManager.PERMISSION_DENIED)
        Log.d("App", "No MODIFY_AUDIO_SETTINGS" );
    else
        Log.d("App", "Yes MODIFY_AUDIO_SETTINGS" );
    if (ContextCompat.checkSelfPermission(getApplicationContext(), permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED)
        Log.d("App", "No RECORD_AUDIO" );
    else
        Log.d("App", "Yes RECORD_AUDIO" );

    Log.d("App","Requesting permissions" );
    ActivityCompat.requestPermissions( this, new String[]
            {
                    permission.MODIFY_AUDIO_SETTINGS,
                    permission.RECORD_AUDIO
            },1 );
    Log.d("App","Requested perms");
}

我相信这应该打印当前权限状态,然后请求我需要的所有权限。在全新安装中,它会提示输入 RECORD_AUDIO(MODIFY_AUDIO_SETTINGS 不是危险权限,似乎是自动授予的)。异步地,这被称为:

public void onRequestPermissionsResult (int requestCode,
                                 String[] permissions,
                                 int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    boolean success = true;
    for( int i = 0; i < permissions.length; ++i ) {
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
            Log.d("App",permissions[i]+" granted");
        } else {
            Log.d("App",permissions[i]+" denied");
            Toast.makeText(getApplicationContext(), "Needed " + permissions[i], Toast.LENGTH_SHORT).show();
            success = false;
        }
    }
    if( success ) {
        Log.d("App","Setting up visualizer");
    }
}

在 Android 8.0 上,日志显示我们拥有这两种权限,并且如果我们无论如何请求它们,它们都会被授予。但是当我们稍后执行时(启动可视化工具当前与按下按钮相关联),

D/App: Yes MODIFY_AUDIO_SETTINGS
D/App: Yes RECORD_AUDIO
D/App: Requesting permissions
D/App: Requested perms
D/AppTracker: App Event: start
D/OpenGLRenderer: HWUI GL Pipeline
D/AppTracker: App Event: stop
I/Adreno: QUALCOMM build                   : 08cdca0, I5f270ba9bc
          Build Date                       : 09/18/17
          OpenGL ES Shader Compiler Version: EV031.20.00.04
          Local Branch                     : 
          Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.6.5.R1.08.00.00.312.025
          Remote Branch                    : NONE
          Reconstruct Branch               : NOTHING
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
D/App: android.permission.MODIFY_AUDIO_SETTINGS granted
D/App: android.permission.RECORD_AUDIO granted
D/App: Setting up visualizer
D/AppTracker: App Event: start
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: net.rsaxvc.blequalizer, PID: 15981
                  java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
                      at android.media.audiofx.Visualizer.<init>(Visualizer.java:218)
                      at net.rsaxvc.blequalizer.MainActivity.setupVisualizerFxAndUI(MainActivity.java:133)
                      at net.rsaxvc.blequalizer.MainActivity.access[=14=]0(MainActivity.java:22)
                      at net.rsaxvc.blequalizer.MainActivity.onNavigationItemSelected(MainActivity.java:63)
                      at android.support.design.widget.BottomNavigationView.onMenuItemSelected(BottomNavigationView.java:182)
                      at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                      at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
                      at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
                      at android.support.design.internal.BottomNavigationMenuView.onClick(BottomNavigationMenuView.java:95)
                      at android.view.View.performClick(View.java:6289)
                      at android.view.View$PerformClick.run(View.java:24800)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6809)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
D/AppTracker: App Event: crash
Application terminated.

调用时

mVisualizer = new Visualizer(0);

我们看到:"E/AudioEffect: set(): AudioFlinger could not create effect, status: -1",从media/libmedia/AudioEffect.cpp,看起来audioFlinger->createEffect失败并返回-1,等于PERMISSION_DENIED,由于下面的代码services/audioflinger/AudioFlinger.cpp:

// check audio settings permission for global effects
if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
    lStatus = PERMISSION_DENIED;
    goto Exit;
}

这在 libs/audioflinger/AudioFlinger.cpp:

static bool settingsAllowed() {
#ifndef HAVE_ANDROID_OS
    return true;
#endif
#if AUDIOFLINGER_SECURITY_ENABLED
    if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
    bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"));
    if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
    return ok;
#else
    if (!checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")))
        LOGW("WARNING: Need to add android.permission.MODIFY_AUDIO_SETTINGS to manifest");
    return true;
#endif
}

我的猜测是本机代码没有获得与 Java 代码相同的权限。或者,本机代码 运行 作为与应用程序本身不同的进程。

这似乎与 不同,后者应用只是不要求运行时权限。

在开发此应用程序的过程中,我的 phone 已更新到 Android 8.0。 AFAICT,这破坏了一些东西。最后,卸载并重新安装该应用程序修复了它。