diff --git a/app/InCallService/Android.bp b/app/InCallService/Android.bp new file mode 100644 index 0000000..649bdc0 --- /dev/null +++ b/app/InCallService/Android.bp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 bengris32 + * Copyright (C) 2022 LineageOS + * + * 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. + */ + +android_app { + name: "MtkInCallService", + + srcs: ["src/**/*.java"], + resource_dirs: ["res"], + + certificate: "platform", + platform_apis: true, + privileged: true, + + optimize: { + enabled: false, + } +} diff --git a/app/InCallService/AndroidManifest.xml b/app/InCallService/AndroidManifest.xml new file mode 100644 index 0000000..cb5c2bc --- /dev/null +++ b/app/InCallService/AndroidManifest.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + diff --git a/app/InCallService/res/values/strings.xml b/app/InCallService/res/values/strings.xml new file mode 100644 index 0000000..2be002f --- /dev/null +++ b/app/InCallService/res/values/strings.xml @@ -0,0 +1,4 @@ + + + Mediatek In-Call Service + diff --git a/app/InCallService/src/org/lineageos/mediatek/incallservice/OnLockedBootCompleteReceiver.java b/app/InCallService/src/org/lineageos/mediatek/incallservice/OnLockedBootCompleteReceiver.java new file mode 100644 index 0000000..218f437 --- /dev/null +++ b/app/InCallService/src/org/lineageos/mediatek/incallservice/OnLockedBootCompleteReceiver.java @@ -0,0 +1,19 @@ +package org.lineageos.mediatek.incallservice; + +import android.content.BroadcastReceiver; +import android.content.Intent; +import android.content.Context; + +import android.util.Log; + +public class OnLockedBootCompleteReceiver extends BroadcastReceiver { + private static final String LOG_TAG = "MediatekInCallService"; + + @Override + public void onReceive(final Context context, Intent intent) { + Log.i(LOG_TAG, "onBoot"); + + Intent sIntent = new Intent(context, VolumeChangeService.class); + context.startService(sIntent); + } +} diff --git a/app/InCallService/src/org/lineageos/mediatek/incallservice/VolumeChangeReceiver.java b/app/InCallService/src/org/lineageos/mediatek/incallservice/VolumeChangeReceiver.java new file mode 100644 index 0000000..b95fb01 --- /dev/null +++ b/app/InCallService/src/org/lineageos/mediatek/incallservice/VolumeChangeReceiver.java @@ -0,0 +1,50 @@ +package org.lineageos.mediatek.incallservice; + +import android.content.Intent; +import android.content.Context; +import android.content.BroadcastReceiver; + +import android.media.AudioManager; +import android.media.AudioSystem; +import android.media.AudioDeviceInfo; + +import android.util.Log; + +public class VolumeChangeReceiver extends BroadcastReceiver { + public static final String LOG_TAG = "MediatekInCallService"; + + private AudioManager mAudioManager; + + public VolumeChangeReceiver(Context context) { + mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + } + + @Override + public void onReceive(Context context, Intent intent) { + int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); + if (streamType == AudioSystem.STREAM_VOICE_CALL) { + AudioDeviceInfo callDevice = mAudioManager.getCommunicationDevice(); + if (callDevice.getInternalType() != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) { + // Device is not the built in earpiece, we don't need to do anything. + return; + } + + // Start building parameters + String parameters = "volumeDevice=" + (callDevice.getId() - 1) + ";"; + int volumeIndex = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1); + if (volumeIndex < 0) { + Log.w(LOG_TAG, "Could not get volumeIndex!"); + return; + } + + // Limit volumeIndex to a max of 7 since that's the size of + // MediaTek's gain table. + parameters += "volumeIndex=" + Math.min(7, volumeIndex) + ";"; + parameters += "volumeStreamType=" + streamType; + + // Set gain parameters + Log.d(LOG_TAG, "Setting audio parameters: " + parameters); + AudioSystem.setParameters(parameters); + } + } +} diff --git a/app/InCallService/src/org/lineageos/mediatek/incallservice/VolumeChangeService.java b/app/InCallService/src/org/lineageos/mediatek/incallservice/VolumeChangeService.java new file mode 100644 index 0000000..e15f47f --- /dev/null +++ b/app/InCallService/src/org/lineageos/mediatek/incallservice/VolumeChangeService.java @@ -0,0 +1,39 @@ +package org.lineageos.mediatek.incallservice; + +import android.media.AudioManager; + +import android.content.Intent; +import android.content.IntentFilter; +import android.content.Context; +import android.app.Service; +import android.os.IBinder; + +import android.util.Log; + +public class VolumeChangeService extends Service { + public static final String LOG_TAG = "MediatekInCallService"; + + private Context mContext; + private VolumeChangeReceiver mVolumeChangeReceiver; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onDestroy() { + super.onDestroy(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startid) { + mContext = this; + mVolumeChangeReceiver = new VolumeChangeReceiver(mContext); + + Log.i(LOG_TAG, "Service is starting..."); + this.registerReceiver(mVolumeChangeReceiver, + new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION)); + return START_STICKY; + } +} diff --git a/device.mk b/device.mk index 5bd516b..1f5d8a3 100644 --- a/device.mk +++ b/device.mk @@ -58,6 +58,9 @@ PRODUCT_PACKAGES += \ libtinyxml \ tinymix +PRODUCT_PACKAGES += \ + MtkInCallService + PRODUCT_COPY_FILES += \ $(LOCAL_PATH)/configs/audio/audio_device.xml:$(TARGET_COPY_OUT_VENDOR)/etc/audio_device.xml \ $(LOCAL_PATH)/configs/audio/audio_effects.xml:$(TARGET_COPY_OUT_VENDOR)/etc/audio_effects.xml \