diff options
author | Roman Birg <roman@cyngn.com> | 2016-07-06 13:32:16 -0700 |
---|---|---|
committer | Roman Birg <roman@cyngn.com> | 2016-07-25 08:44:46 -0700 |
commit | 705716fc60c62d9036e8c8e804c4110231c804d9 (patch) | |
tree | 2fa65cf5a0c1b6891e81cfeafd6f48d8e2d1c2b3 /cm | |
parent | 86cae92291f728d0eca637573ecbe6e0a53ccf1a (diff) | |
download | vendor_cmsdk-705716fc60c62d9036e8c8e804c4110231c804d9.zip vendor_cmsdk-705716fc60c62d9036e8c8e804c4110231c804d9.tar.gz vendor_cmsdk-705716fc60c62d9036e8c8e804c4110231c804d9.tar.bz2 |
cmsdk: Broker out CMAudioService
Change-Id: Ia1205ad67d524ebf379085a6287993b8b82bc76f
Signed-off-by: Roman Birg <roman@cyngn.com>
Diffstat (limited to 'cm')
-rw-r--r-- | cm/jni/Android.mk | 1 | ||||
-rw-r--r-- | cm/jni/src/onload.cpp | 2 | ||||
-rw-r--r-- | cm/jni/src/org_cyanogenmod_platform_internal_CMAudioService.cpp | 170 | ||||
-rw-r--r-- | cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioService.java | 161 | ||||
-rw-r--r-- | cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioServiceBroker.java | 130 | ||||
-rw-r--r-- | cm/res/AndroidManifest.xml | 10 | ||||
-rw-r--r-- | cm/res/res/values/config.xml | 2 | ||||
-rw-r--r-- | cm/res/res/values/strings.xml | 4 |
8 files changed, 144 insertions, 336 deletions
diff --git a/cm/jni/Android.mk b/cm/jni/Android.mk index 3e8aae4..ed4896e 100644 --- a/cm/jni/Android.mk +++ b/cm/jni/Android.mk @@ -17,7 +17,6 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - src/org_cyanogenmod_platform_internal_CMAudioService.cpp \ src/org_cyanogenmod_platform_internal_PerformanceManagerService.cpp \ src/onload.cpp diff --git a/cm/jni/src/onload.cpp b/cm/jni/src/onload.cpp index 1ee8fda..07413b2 100644 --- a/cm/jni/src/onload.cpp +++ b/cm/jni/src/onload.cpp @@ -21,7 +21,6 @@ namespace android { -int register_org_cyanogenmod_platform_internal_CMAudioService(JNIEnv* env); int register_org_cyanogenmod_platform_internal_PerformanceManagerService(JNIEnv* env); }; @@ -39,7 +38,6 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) } ALOG_ASSERT(env, "Could not retrieve the env!"); - register_org_cyanogenmod_platform_internal_CMAudioService(env); register_org_cyanogenmod_platform_internal_PerformanceManagerService(env); return JNI_VERSION_1_4; diff --git a/cm/jni/src/org_cyanogenmod_platform_internal_CMAudioService.cpp b/cm/jni/src/org_cyanogenmod_platform_internal_CMAudioService.cpp deleted file mode 100644 index 9c1cc35..0000000 --- a/cm/jni/src/org_cyanogenmod_platform_internal_CMAudioService.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* -** -** Copyright 2016, The CyanogenMod 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. -*/ - -//#define LOG_NDEBUG 0 - -#define LOG_TAG "CMAudioService-JNI" - -#include <utils/Log.h> - -#include <JNIHelp.h> -#include <jni.h> -#include "core_jni_helpers.h" -#include "android_media_AudioErrors.h" - -#include <media/AudioSystem.h> -#include <media/AudioSession.h> - -#include <system/audio.h> -#include <utils/threads.h> - -// ---------------------------------------------------------------------------- - -namespace android { - -static const char* const kClassPathName = "org/cyanogenmod/platform/internal/CMAudioService"; - -static jclass gArrayListClass; -static struct { - jmethodID add; - jmethodID toArray; -} gArrayListMethods; - -static struct { - jmethodID postAudioSessionEventFromNative; -} gAudioSessionEventHandlerMethods; - -static jclass gAudioSessionInfoClass; -static jmethodID gAudioSessionInfoCstor; - -static jobject gThiz; - -static Mutex gCallbackLock; - -// ---------------------------------------------------------------------------- - -static void -org_cyanogenmod_platform_internal_CMAudioService_session_info_callback(int event, - sp<AudioSessionInfo>& info, bool added) -{ - AutoMutex _l(gCallbackLock); - - JNIEnv *env = AndroidRuntime::getJNIEnv(); - if (env == NULL) { - return; - } - - jobject jSession = env->NewObject(gAudioSessionInfoClass, gAudioSessionInfoCstor, - info->mSessionId, info->mStream, info->mFlags, info->mChannelMask, info->mUid); - - env->CallVoidMethod(gThiz, - gAudioSessionEventHandlerMethods.postAudioSessionEventFromNative, - event, jSession, added); - - env->DeleteLocalRef(jSession); -} - -static void -org_cyanogenmod_platform_internal_CMAudioService_registerAudioSessionCallback( - JNIEnv *env, jobject thiz, jboolean enabled) -{ - if (gThiz == NULL) { - gThiz = env->NewGlobalRef(thiz); - } - - AudioSystem::setAudioSessionCallback( enabled ? - org_cyanogenmod_platform_internal_CMAudioService_session_info_callback : NULL); -} - -static jint -org_cyanogenmod_platform_internal_CMAudioService_listAudioSessions(JNIEnv *env, jobject thiz, - jint streams, jobject jSessions) -{ - ALOGV("listAudioSessions"); - - if (jSessions == NULL) { - ALOGE("listAudioSessions NULL arraylist"); - return (jint)AUDIO_JAVA_BAD_VALUE; - } - if (!env->IsInstanceOf(jSessions, gArrayListClass)) { - ALOGE("listAudioSessions not an arraylist"); - return (jint)AUDIO_JAVA_BAD_VALUE; - } - - status_t status; - Vector< sp<AudioSessionInfo>> sessions; - - status = AudioSystem::listAudioSessions((audio_stream_type_t)streams, sessions); - if (status != NO_ERROR) { - ALOGE("AudioSystem::listAudioSessions error %d", status); - } else { - ALOGV("AudioSystem::listAudioSessions count=%zu", sessions.size()); - } - - jint jStatus = nativeToJavaStatus(status); - if (jStatus != AUDIO_JAVA_SUCCESS) { - goto exit; - } - - for (size_t i = 0; i < sessions.size(); i++) { - const sp<AudioSessionInfo>& s = sessions.itemAt(i); - - jobject jSession = env->NewObject(gAudioSessionInfoClass, gAudioSessionInfoCstor, - s->mSessionId, s->mStream, s->mFlags, s->mChannelMask, s->mUid); - - if (jSession == NULL) { - jStatus = (jint)AUDIO_JAVA_ERROR; - goto exit; - } - - env->CallBooleanMethod(jSessions, gArrayListMethods.add, jSession); - env->DeleteLocalRef(jSession); - } - -exit: - return jStatus; -} - - -// ---------------------------------------------------------------------------- - -static JNINativeMethod gMethods[] = { - {"native_listAudioSessions", "(ILjava/util/ArrayList;)I", - (void *)org_cyanogenmod_platform_internal_CMAudioService_listAudioSessions}, - {"native_registerAudioSessionCallback", "(Z)V", - (void *)org_cyanogenmod_platform_internal_CMAudioService_registerAudioSessionCallback}, -}; - -int register_org_cyanogenmod_platform_internal_CMAudioService(JNIEnv *env) -{ - jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList"); - gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass); - gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z"); - gArrayListMethods.toArray = GetMethodIDOrDie(env, arrayListClass, "toArray", "()[Ljava/lang/Object;"); - - jclass audioSessionInfoClass = FindClassOrDie(env, "cyanogenmod/media/AudioSessionInfo"); - gAudioSessionInfoClass = MakeGlobalRefOrDie(env, audioSessionInfoClass); - gAudioSessionInfoCstor = GetMethodIDOrDie(env, audioSessionInfoClass, "<init>", "(IIIII)V"); - - gAudioSessionEventHandlerMethods.postAudioSessionEventFromNative = - GetMethodIDOrDie(env, env->FindClass(kClassPathName), - "audioSessionCallbackFromNative", "(ILcyanogenmod/media/AudioSessionInfo;Z)V"); - - return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); -} - -} /* namespace android */ diff --git a/cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioService.java b/cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioService.java deleted file mode 100644 index 6cebb8e..0000000 --- a/cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioService.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2016 The CyanogenMod 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 org.cyanogenmod.platform.internal; - -import android.content.Context; -import android.content.Intent; -import android.os.Binder; -import android.os.IBinder; -import android.os.RemoteException; -import android.os.UserHandle; -import android.util.Log; - -import com.android.server.SystemService; - -import java.io.FileDescriptor; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; - -import cyanogenmod.app.CMContextConstants; -import cyanogenmod.media.AudioSessionInfo; -import cyanogenmod.media.CMAudioManager; -import cyanogenmod.media.ICMAudioService; -import cyanogenmod.platform.Manifest; - -public class CMAudioService extends CMSystemService { - - private static final String TAG = "CMAudioService"; - private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); - - private final Context mContext; - - private static final int AUDIO_STATUS_OK = 0; - - //keep in sync with include/media/AudioPolicy.h - private final static int AUDIO_OUTPUT_SESSION_EFFECTS_UPDATE = 10; - - public CMAudioService(Context context) { - super(context); - - mContext = context; - } - - @Override - public String getFeatureDeclaration() { - return CMContextConstants.Features.AUDIO; - } - - @Override - public void onStart() { - if (!NativeHelper.isNativeLibraryAvailable()) { - Log.wtf(TAG, "CM Audio service started by system server by native library is" + - "unavailable. Service will be unavailable."); - return; - } - publishBinderService(CMContextConstants.CM_AUDIO_SERVICE, mBinder); - } - - @Override - public void onBootPhase(int phase) { - if (phase == PHASE_BOOT_COMPLETED) { - if (NativeHelper.isNativeLibraryAvailable()) { - native_registerAudioSessionCallback(true); - } - } - } - - private final IBinder mBinder = new ICMAudioService.Stub() { - - @Override - public List<AudioSessionInfo> listAudioSessions(int streamType) throws RemoteException { - final ArrayList<AudioSessionInfo> sessions = new ArrayList<AudioSessionInfo>(); - if (!NativeHelper.isNativeLibraryAvailable()) { - // no sessions for u - return sessions; - } - - int status = native_listAudioSessions(streamType, sessions); - if (status != AUDIO_STATUS_OK) { - Log.e(TAG, "Error retrieving audio sessions! status=" + status); - } - - return sessions; - } - - @Override - public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { - mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); - - pw.println(); - pw.println("CMAudio Service State:"); - try { - List<AudioSessionInfo> sessions = listAudioSessions(-1); - if (sessions.size() > 0) { - pw.println(" Audio sessions:"); - for (AudioSessionInfo info : sessions) { - pw.println(" " + info.toString()); - } - } else { - pw.println(" No active audio sessions"); - } - } catch (RemoteException e) { - // nothing - } - } - }; - - private void broadcastSessionChanged(boolean added, AudioSessionInfo sessionInfo) { - Intent i = new Intent(CMAudioManager.ACTION_AUDIO_SESSIONS_CHANGED); - i.putExtra(CMAudioManager.EXTRA_SESSION_INFO, sessionInfo); - i.putExtra(CMAudioManager.EXTRA_SESSION_ADDED, added); - - sendBroadcastToAll(i, Manifest.permission.OBSERVE_AUDIO_SESSIONS); - } - - private void sendBroadcastToAll(Intent intent, String receiverPermission) { - intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); - - final long ident = Binder.clearCallingIdentity(); - try { - if (DEBUG) Log.d(TAG, "Sending broadcast: " + intent.toString()); - - mContext.sendBroadcastAsUser(intent, UserHandle.ALL, receiverPermission); - } finally { - Binder.restoreCallingIdentity(ident); - } - } - - /* - * Handles events from JNI - */ - private synchronized void audioSessionCallbackFromNative(int event, - AudioSessionInfo sessionInfo, boolean added) { - - switch (event) { - case AUDIO_OUTPUT_SESSION_EFFECTS_UPDATE: - broadcastSessionChanged(added, sessionInfo); - break; - default: - Log.e(TAG, "Unknown event " + event); - } - } - - private native final void native_registerAudioSessionCallback(boolean enabled); - - private native final int native_listAudioSessions( - int stream, ArrayList<AudioSessionInfo> sessions); -} diff --git a/cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioServiceBroker.java b/cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioServiceBroker.java new file mode 100644 index 0000000..65ee040 --- /dev/null +++ b/cm/lib/main/java/org/cyanogenmod/platform/internal/CMAudioServiceBroker.java @@ -0,0 +1,130 @@ +/** + * Copyright (C) 2016 The CyanogenMod 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 org.cyanogenmod.platform.internal; + +import org.cyanogenmod.platform.internal.common.BrokeredServiceConnection; + +import android.annotation.NonNull; +import android.annotation.SdkConstant; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Log; +import android.util.Slog; + +import java.io.FileDescriptor; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import cyanogenmod.app.CMContextConstants; +import cyanogenmod.media.AudioSessionInfo; +import cyanogenmod.media.ICMAudioService; + +public class CMAudioServiceBroker extends BrokerableCMSystemService<ICMAudioService> { + + private static final String TAG = "CMAudioServiceBroker"; + private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); + + private final Context mContext; + + private static final ComponentName TARGET_IMPLEMENTATION_COMPONENT = + new ComponentName("org.cyanogenmod.cmaudio.service", + "org.cyanogenmod.cmaudio.service.CMAudioService"); + + public CMAudioServiceBroker(Context context) { + super(context); + mContext = context; + } + + @Override + public void onBootPhase(int phase) { + super.onBootPhase(phase); + if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { + publishBinderService(CMContextConstants.CM_AUDIO_SERVICE, new BinderService()); + } + } + + @Override + public void onStart() { + if (DEBUG) Slog.d(TAG, "service started"); + } + + @Override + protected ICMAudioService getIBinderAsIInterface(@NonNull IBinder service) { + return ICMAudioService.Stub.asInterface(service); + } + + @Override + protected ICMAudioService getDefaultImplementation() { + return mServiceStubForFailure; + } + + @Override + protected ComponentName getServiceComponent() { + return TARGET_IMPLEMENTATION_COMPONENT; + } + + private void checkPermission() { + mContext.enforceCallingOrSelfPermission( + cyanogenmod.platform.Manifest.permission.OBSERVE_AUDIO_SESSIONS, null); + } + + @Override + public String getFeatureDeclaration() { + return CMContextConstants.Features.AUDIO; + } + + private final ICMAudioService mServiceStubForFailure = new ICMAudioService.Stub() { + @Override + public List<AudioSessionInfo> listAudioSessions(int streamType) throws RemoteException { + checkPermission(); + return Collections.emptyList(); + } + }; + + private final class BinderService extends ICMAudioService.Stub { + @Override + public List<AudioSessionInfo> listAudioSessions(int streamType) throws RemoteException { + checkPermission(); + return getBrokeredService().listAudioSessions(streamType); + } + + @Override + public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); + + pw.println(); + pw.println("CMAudio Service State:"); + try { + List<AudioSessionInfo> sessions = listAudioSessions(-1); + if (sessions.size() > 0) { + pw.println(" Audio sessions:"); + for (AudioSessionInfo info : sessions) { + pw.println(" " + info.toString()); + } + } else { + pw.println(" No active audio sessions"); + } + } catch (RemoteException e) { + // nothing + } + } + } +} diff --git a/cm/res/AndroidManifest.xml b/cm/res/AndroidManifest.xml index de6414f..c0e0e94 100644 --- a/cm/res/AndroidManifest.xml +++ b/cm/res/AndroidManifest.xml @@ -24,7 +24,8 @@ <protected-broadcast android:name="cyanogenmod.intent.action.SCREEN_CAMERA_GESTURE" /> <protected-broadcast android:name="cyanogenmod.intent.action.INITIALIZE_CM_HARDWARE" /> - <protected-broadcast android:name="cyanogenmod.intent.action.ACTION_AUDIO_SESSIONS_CHANGED" /> + <protected-broadcast android:name="cyanogenmod.intent.action.ACTION_AUDIO_SESSIONS_CHANGED" + android:permission="cyanogenmod.permission.MANAGE_AUDIO_SESSIONS" /> <!-- Must be required by an, to ensure that only the system can bind to it. @hide --> @@ -220,6 +221,13 @@ android:description="@string/permdesc_observe_audio_sessions" android:protectionLevel="normal"/> + <!-- Allows an application to post system-wide changes to audio sessions + @hide --> + <permission android:name="cyanogenmod.permission.MANAGE_AUDIO_SESSIONS" + android:label="@string/permlab_manage_audio_sessions" + android:description="@string/permdesc_manage_audio_sessions" + android:protectionLevel="signature|privileged"/> + <!-- Allows an application to access the weather service. <p>Although the protection is normal, this permission should be required ONLY by those apps meant to do something meaningful with the data provided by the service (LockClock, SysUI)--> diff --git a/cm/res/res/values/config.xml b/cm/res/res/values/config.xml index 9592bf1..8533767 100644 --- a/cm/res/res/values/config.xml +++ b/cm/res/res/values/config.xml @@ -112,7 +112,7 @@ <item>org.cyanogenmod.platform.internal.LiveLockScreenServiceBroker</item> <item>org.cyanogenmod.platform.internal.CMWeatherManagerService</item> <item>org.cyanogenmod.platform.internal.display.LiveDisplayService</item> - <item>org.cyanogenmod.platform.internal.CMAudioService</item> + <item>org.cyanogenmod.platform.internal.CMAudioServiceBroker</item> </string-array> <!-- The CMSystemServer class that is invoked from Android's SystemServer --> diff --git a/cm/res/res/values/strings.xml b/cm/res/res/values/strings.xml index 1551adf..19e5569 100644 --- a/cm/res/res/values/strings.xml +++ b/cm/res/res/values/strings.xml @@ -218,6 +218,10 @@ <string name="permlab_observe_audio_sessions">observe audio session changes</string> <string name="permdesc_observe_audio_sessions">Allows an app to observe audio streams being created and destroyed.</string> + <!-- CMAudioService - observe session changes permission --> + <string name="permlab_manage_audio_sessions">manage audio session changes</string> + <string name="permdesc_manage_audio_sessions">Allows an app to send audio stream updates.</string> + <!-- QuickSettings: Themes tile --> <string name="qs_themes_label">Themes</string> <string name="qs_themes_content_description">Customize your theme</string> |