Reset SELinux to Enforcing after installing magisk module - LG V30 Questions & Answers

I'm running lineageos 17.1 and was selinux enforcing, that is until I flashed this mod for Gcam to enable the wide lens. I'm trying to get netflix to work in widevine L1 and was seeing some promising results in enforcing mode, (the modded apk I previously used on a verizon stock rom now only plays audio, no video), but after flashing that aux camera enabler (and uninstalling it) I cant get selinux back to enforcing. I've tried "setenforce enforcing" but after reboot "getenforcing" returns "Permissive".
I did some research on selinux and I saw some concerns that, even if I did get enforcing reset, the policies changed by the aux mod might cause issues if not reversed. These are the policy statements contained in the aforementioned mod:
Code:
allow { untrusted_app } persist_camera_prop file { read open getattr map }
allow { untrusted_app } vendor_default_prop file { map }
allow { untrusted_app } priv_app_tmpfs fil { read }
allow { untrusted_app } rootfs file { read getattr open }
allow { untrusted_app } vendor_camera_prop file { read open getattr map }
allow { system_server } vendor_camera_prop file { read open getattr map }
allow { hal_camera_default } default_prop file { read open getattr map }
allow { untrusted_app_25 } vendor_camera_prop file { read open getattr map }
allow { system_server } vendor_default_prop file { read open getattr map }
allow { platform_app } vendor_default_prop file { read open getattr map }
Any ideas where I should go from here? I realize wiping and restarting is probably the simplest solution, but my dumb ass didn't make a backup before installing the aux mod, and I would really like to avoid spending another weekend setting up my phone again.

Stuckurface said:
I'm running lineageos 17.1 and was selinux enforcing, that is until I flashed this mod for Gcam to enable the wide lens. I'm trying to get netflix to work in widevine L1 and was seeing some promising results in enforcing mode, (the modded apk I previously used on a verizon stock rom now only plays audio, no video), but after flashing that aux camera enabler (and uninstalling it) I cant get selinux back to enforcing. I've tried "setenforce enforcing" but after reboot "getenforcing" returns "Permissive".
I did some research on selinux and I saw some concerns that, even if I did get enforcing reset, the policies changed by the aux mod might cause issues if not reversed. These are the policy statements contained in the aforementioned mod:
Code:
allow { untrusted_app } persist_camera_prop file { read open getattr map }
allow { untrusted_app } vendor_default_prop file { map }
allow { untrusted_app } priv_app_tmpfs fil { read }
allow { untrusted_app } rootfs file { read getattr open }
allow { untrusted_app } vendor_camera_prop file { read open getattr map }
allow { system_server } vendor_camera_prop file { read open getattr map }
allow { hal_camera_default } default_prop file { read open getattr map }
allow { untrusted_app_25 } vendor_camera_prop file { read open getattr map }
allow { system_server } vendor_default_prop file { read open getattr map }
allow { platform_app } vendor_default_prop file { read open getattr map }
Any ideas where I should go from here? I realize wiping and restarting is probably the simplest solution, but my dumb ass didn't make a backup before installing the aux mod, and I would really like to avoid spending another weekend setting up my phone again.
Click to expand...
Click to collapse
All Android 10 AOSP ROMs are supposed to be run on permissive and are permissive by default.
So you were probably never on selinux enforcing

tech_infinity said:
All Android 10 AOSP ROMs are supposed to be run on permissive and are permissive by default.
So you were probably never on selinux enforcing
Click to expand...
Click to collapse
Typically, yes, but I suppose this one (lineage 17) is enforcing by default:
https://forum.xda-developers.com/lg-v30/development/rom-lineageos-16-0-lineageos-17-0-wip-t4042333
Previously, I showed as enforcing under the "Trust" page (current picture attached for context). Furthermore, drm info and Netflix, previous to flashing that aux mod, showed my widevine status as L1, both of which now show L3.
Also, I went ahead and tried to use the kernel se policy patcher mentioned in the thread above to set to enforcing, but it just bootloops until I flash again and set back to permissive. I'm guessing that's due to the policies set by that aux mod, but idk for certain.
Oh well, even if I did get selinux enforcing again, it's unlikely that I could get Netflix to work in L1 ¯\_(ツ)_/¯

Stuckurface said:
Typically, yes, but I suppose this one (lineage 17) is enforcing by default:
https://forum.xda-developers.com/lg-v30/development/rom-lineageos-16-0-lineageos-17-0-wip-t4042333
Previously, I showed as enforcing under the "Trust" page (current picture attached for context). Furthermore, drm info and Netflix, previous to flashing that aux mod, showed my widevine status as L1, both of which now show L3.
Also, I went ahead and tried to use the kernel se policy patcher mentioned in the thread above to set to enforcing, but it just bootloops until I flash again and set back to permissive. I'm guessing that's due to the policies set by that aux mod, but idk for certain.
Oh well, even if I did get selinux enforcing again, it's unlikely that I could get Netflix to work in L1 ¯\_(ツ)_/¯
Click to expand...
Click to collapse
Nope. ALL android 10 ROMs are permissive including this one.
you're getting bootloops because the ROM isn't made to be run on enforcing.
Also the latest build had some issues with Netflix, might wanna try a different rom for netflix to work

Stuckurface said:
I'm running lineageos 17.1 and was selinux enforcing, that is until I flashed this mod for Gcam to enable the wide lens. I'm trying to get netflix to work in widevine L1 and was seeing some promising results in enforcing mode, (the modded apk I previously used on a verizon stock rom now only plays audio, no video), but after flashing that aux camera enabler (and uninstalling it) I cant get selinux back to enforcing. I've tried "setenforce enforcing" but after reboot "getenforcing" returns "Permissive".
I did some research on selinux and I saw some concerns that, even if I did get enforcing reset, the policies changed by the aux mod might cause issues if not reversed. These are the policy statements contained in the aforementioned mod:
Code:
allow { untrusted_app } persist_camera_prop file { read open getattr map }
allow { untrusted_app } vendor_default_prop file { map }
allow { untrusted_app } priv_app_tmpfs fil { read }
allow { untrusted_app } rootfs file { read getattr open }
allow { untrusted_app } vendor_camera_prop file { read open getattr map }
allow { system_server } vendor_camera_prop file { read open getattr map }
allow { hal_camera_default } default_prop file { read open getattr map }
allow { untrusted_app_25 } vendor_camera_prop file { read open getattr map }
allow { system_server } vendor_default_prop file { read open getattr map }
allow { platform_app } vendor_default_prop file { read open getattr map }
Any ideas where I should go from here? I realize wiping and restarting is probably the simplest solution, but my dumb ass didn't make a backup before installing the aux mod, and I would really like to avoid spending another weekend setting up my phone again.
Click to expand...
Click to collapse
Have you found a complete solution to the problem?

Redy321 said:
Have you found a complete solution to the problem?
Click to expand...
Click to collapse
If you're talking about "problem" of ROM being permissive then no. Because the Devs will have to make the ROMs boot with Enforcing, nothing the user can do here.
And if you're talking about Netflix, The latest build had some issues with Netflix, might wanna try a different rom or build for netflix to work

Redy321 said:
Have you found a complete solution to the problem?
Click to expand...
Click to collapse
Technically, yes. I bought a Pixel 5.
With the death of custom ROMS looming on all but at&t, and with all the time I spend trying to get pixel exclusive features working on my phone, I thought it was time to join cstark on pixel.
In unrelated news, I'm getting ready to sell a 2yo carrier and bootloader unlocked us998 (good condition, it lived its life in an OtterBox). I'll was planning on selling it on ebay after resetting to stock and re-locking the bootloader (because of the "scary" warning on boot), but if someone on the forum is interested, DM me for details.

tech_infinity said:
If you're talking about "problem" of ROM being permissive then no. Because the Devs will have to make the ROMs boot with Enforcing, nothing the user can do here.
And if you're talking about Netflix, The latest build had some issues with Netflix, might wanna try a different rom or build for netflix to work
Click to expand...
Click to collapse
Yeah, DRM content on the whole is a problem for the lineage 17.1 ROM (not just Netflix, but also HBO). Strangely, it reports itself as L1, but unless you can force widevine to drop to L3, perhaps through installing some magisk module like I did (which will result in SD quality), video playback won't work.

Related

[Q] Xposed developing for 5.1 cm12.1

Hello
My phone is the moto x 2014 running CM12.1 nightly.
i was trying to learn how to develop modules for my rom using this tut.
Everything was fine using this code
Code:
package de.robv.android.xposed.mods.tutorial;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
public class Tutorial implements IXposedHookLoadPackage {
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.android.systemui"))
return;
});
}
}
but when i add the hook method
Code:
findAndHookMethod("com.android.systemui.statusbar.policy.Clock", lpparam.classLoader, "updateClock", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
// this will be called before the clock was updated by the original method
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
// this will be called after the clock was updated by the original method
}
System UI crashes on startup and i need to reboot to recovery and disable xposed.
The Question​what is going wrong? is this tutorial outdated for 5.1 am i missing something?
logs are in attachement, for more info plz don't hesitate to ask.
Use latest build
http://forum.xda-developers.com/xposed/super-alpha-posted-permission-xposed-t3072979
For me it doesn't work.
I flashed the zip with twrp, the phone rebooted successfully but I can't find the xposed program from the app menu. Maybe I missed something?
I am on g3 with 5.1.1 cyanidel.
Thanks!
half cooked
Stefano Bottari said:
For me it doesn't work.
I flashed the zip with twrp, the phone rebooted successfully but I can't find the xposed program from the app menu. Maybe I missed something?
I am on g3 with 5.1.1 cyanidel.
Thanks!
Click to expand...
Click to collapse
Your food is half cooked....
After flashing it through twrp you have to install xposed 3.0 alpha apk .

Help with Audio_effects.conf

Can someone please explain how the audio_effects.conf file works. Especially the effects UUID part. I really need help. Maybe from people like @PDesire who makes audio mods.
Thanks in advance.
See this:
1) struct effect_descriptor_s : https://github.com/CyanogenMod/andr...b/cm-11.0/include/hardware/audio_effect.h#L70
2) as example library reverb (libreverbwrapper.so) : https://github.com/CyanogenMod/andr...fects/lvm/wrapper/Reverb/EffectReverb.cpp#L78
remittor said:
See this:
1) struct effect_descriptor_s : https://github.com/CyanogenMod/andr...b/cm-11.0/include/hardware/audio_effect.h#L70
2) as example library reverb (libreverbwrapper.so) : https://github.com/CyanogenMod/andr...fects/lvm/wrapper/Reverb/EffectReverb.cpp#L78
Click to expand...
Click to collapse
Thanks alot for the links! I will continue reading it to try understand everything but how does one make a custom effect. Like this:
Code:
libraries {
yume_audio {
path /system/lib/soundfx/libyumeaudio.so
}
}
effects {
yume_effect {
library yume_audio
uuid 6723dd80-f0b7-11e0-98a2-0002a5d5c51b
}
}

[APP] UnrecoverableKeyException after software or security updates on Samsung

Hello!
I am not allowed to post under Software forum yet, hence posting this question here. Sorry for that.
We have developed a support of biometric authentication in our mobile app using BiometricPrompt API. We decided to go with CryptoObjects as it allows us to comply with regulations.
After implementation we are facing an issue that on Samsung devices after some device software updates and after almost every Security Patch our Key is getting corrupted/invalidated. This forces us generating a new key pair, leading to a terrible user experience (having to re-enable a feature again and again).
Error log:
Caused by java.security.UnrecoverableKeyException: Failed to obtain information about key
at android.security.keystore.AndroidKeyStoreProvider.getKeyCharacteristics(AndroidKeyStoreProvider.java:238)
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyFromKeystore(AndroidKeyStoreProvider.java:360)
at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:116)
at java.security.KeyStore.getKey(KeyStore.java:1062)
at com.app_name.mobile.data.crypto.CryptoRepositoryImpl.getSignature(CryptoRepositoryImpl.java:527)
at com.app_name.mobile.business.biometric.authentication.BiometricAuthenticationPromptInteractorImpl$getSignatureForAuthentication$1.subscribe(BiometricAuthenticationPromptInteractorImpl.java:52)
Caused by android.security.KeyStoreException: User authentication required
at android.security.KeyStore.getKeyStoreException(KeyStore.java:1151)
at android.security.keystore.AndroidKeyStoreProvider.getKeyCharacteristics(AndroidKeyStoreProvider.java:240)
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyFromKeystore(AndroidKeyStoreProvider.java:360)
at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:116)
at java.security.KeyStore.getKey(KeyStore.java:1062)
Click to expand...
Click to collapse
Has anyone experienced similar issues? How to prevent our key getting invalidated/corrupted after Samsung security patches? Is it actually a normal behavior and outcome of Security patches?
We are generating a key pair using following code (fallback is used as we discovered that cheaper Samsung devices not supporting EC, hence we use RSA on such devices) :
Code:
@RequiresApi(Build.VERSION_CODES.M)
override fun createSigningKey(keyBaseName: KeyBaseName, useFallbackAlgorithm: Boolean): Either<Throwable, JavaPublicKey> = try {
deleteKey(keyBaseName)
when {
useFallbackAlgorithm -> KeyPairGenerator
.getInstance(KEY_ALGORITHM_RSA, ANDROID_KEY_STORE_PROVIDER)
.apply {
initialize(KeyGenParameterSpec.Builder(keyBaseName, PURPOSE_SIGN or PURPOSE_VERIFY)
.setAlgorithmParameterSpec(RSAKeyGenParameterSpec(KEY_SIZE, RSAKeyGenParameterSpec.F4))
.setDigests(DIGEST_SHA256, DIGEST_SHA512)
.setSignaturePaddings(SIGNATURE_PADDING_RSA_PKCS1)
.setUserAuthenticationRequired(true)
.build())
}
else -> KeyPairGenerator
.getInstance(KEY_ALGORITHM_EC, ANDROID_KEY_STORE_PROVIDER)
.apply {
initialize(KeyGenParameterSpec.Builder(keyBaseName, PURPOSE_SIGN or PURPOSE_VERIFY)
.setDigests(DIGEST_SHA256, DIGEST_SHA512)
.setUserAuthenticationRequired(true)
.build())
}
}.generateKeyPair()
.public
.right()
} catch (e: Exception) {
firebaseRepository.logException(RuntimeException(“createSigningKey”, e))
e.left()
}
Thank you!

How to make your own music player using HMS Audio Kit: Extensive Tutorial Part 1

More information like this, you can visit HUAWEI Developer Forum
Original link:https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201327649924660033&fid=0101187876626530001
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
If you have always wanted to make your own music player somewhere inside of you, this tutorial is for you. We will use Huawei’s relatively new AudioKit to develop one and I will show the steps one by one so that you can develop a music player on your own.
Do not rely on copy-pastes though, I am here to guide you, not let you copy-paste my code. To do that, you do not have to read this extensive tutorial, just click here to reach the full source code. Keep in mind that this source code contains three other HMS kits and their imperfect implementations, it is not a dedicated AudioKit-only app. However, if you are here to learn for real, let’s start below.
First of all, make sure you have everything you needed before starting to develop your app.
Hardware Requirements
A computer (desktop or laptop) running Windows 7 or Windows 10
A Huawei phone (with the USB cable), which is used for debugging
Software Requirements
Java JDK (JDK 1.7 is recommended.)
Android Studio 3.X
SDK Platform 19 or later
Gradle 4.6 or later
HMS Core (APK) 5.0.0.300 or later
Required Knowledge
Android app development basics
Android app development multithreading
Secondly, you should integrate Huawei HMS Core to your app. Details are listed here, in the official documentation. You should complete #3 and #4 until “Writing the Code” part. From now on, I will assume that you have prepared your application and ready to go with necessary devices attached for running.
Apart from this tutorial, you can always use sample app provided by Huawei, by clicking here. Source code gives idea about most concepts but it is not well-explained and the code is kind of confusing. My tutorial will also use that sample app but it will not be the same thing at all.
For app resources like play button, pause button, skip button etc., please find your own resources or use the resources provided by Huawei sample app that I provided the link above. If you already have resources available, you can use them too because it is very important to have a responsive UI to track music changes, but ultimately it does not matter how they actually look. From now on, I will also assume that you have necessary drawables/images/buttons for a standard music player. If you are going to use the app for commercial purposes, make sure the resources you use have no copyright issues.
End Product
If you want to know what kind of product we will be having in the end, look no further. The below screenshot roughly describes our end product after this relatively long tutorial. Of course, all customizations and UI elements (including colors) are up to you, so it could differ from my product.
Also, as you can see my app contains AdsKit and Account Kit usages, which will not be included in this tutorial. That’s why, I will not give you the XML codes of my application (I already shared the my GitHub project link, if you are so inclined).
Remark: I mostly use the words audio file, music file and song(s) interchangably. Please do not get confused.
Let’s Plan!
Let’s plan together. We want to develop a music player application, so it should has some basic features that we want to support, such as play/pause, next/previous audio and seekbar updates. Seekbar will be showing us the progress of the music and it should be updated real time so that our music playback is always synced with life cycles of the app as well as the seekbar of our app. If you want to go further, we should add play modes, shuffle audio files, normal mode (sequential playback), loop the audio and loop the playlist. Of course, we want to render our cover image to the screen for a nice user experience and print the details of the audio file below it. And since we are only implementing the core features, we should have a playlist icon to open up the playlist and we should be able to choose an audio file from it. For the sake of simplicity, all browsed audio files from the device will be added to one single playlist for users to choose from. For the scope of this tutorial, creating new playlists, adding/removing files to those lists etc. are not supported. As can be predicted, our code will include a lot of button clicks.
I suggest you start with design on your own. Assuming that you have button/image resources you can imitate the design of my app (screenshot above) or your favorite music player. I placed the playlist button right above and clicking on it should open the playlist, and re-clicking should close it back. You can ignore the test ad below the playback buttons and AccountKit related parts above the cover image.
Let me remind you that I use view binding, so it is natural that you will not see much “findViewById”s. For official documentation of what I do, follow here.
Let’s code!
We should initialize our AudioKit managers to manage the playback later. Then, we should browse the local storage and get the audio files. After also completing listeners and notifications, we should implement button clicks so that users could have a smooth experience. Let’s start with a custom method called initializeManagerAndGetPlayList(…) to do the actual work, it is to be called in onCreate of the activity.
Code:
@SuppressLint("StaticFieldLeak")
public void initializeManagerAndGetPlayList(final Context context) {
new AsyncTask() {
@Override
protected Void doInBackground(Void... voids) {
HwAudioPlayerConfig hwAudioPlayerConfig = new HwAudioPlayerConfig(context);
HwAudioManagerFactory.createHwAudioManager(hwAudioPlayerConfig, new HwAudioConfigCallBack() {
@RequiresApi(api = Build.VERSION_CODES.R)
@Override
public void onSuccess(HwAudioManager hwAudioManager) {
try {
mHwAudioManager = hwAudioManager;
mHwAudioPlayerManager = hwAudioManager.getPlayerManager();
mHwAudioConfigManager = hwAudioManager.getConfigManager();
mHwAudioQueueManager = hwAudioManager.getQueueManager();
playList = getLocalPlayList(MainActivity.this);
if (playList.size() > 0) {
Collections.sort(playList, new Comparator() {
@Override
public int compare(final HwAudioPlayItem object1, final HwAudioPlayItem object2) {
return object1.getAudioTitle().compareTo(object2.getAudioTitle());
}
});
}
doListenersAndNotifications(MainActivity.this);
} catch (Exception e) {
Log.e("TAG", "player init fail", e);
}
}
@Override
public void onError(int errorCode) {
Log.e("TAG", "init err:" + errorCode);
}
});
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
PlaylistAdapter playlistAdapter = new PlaylistAdapter(playList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
binding.playlistRecyclerView.setLayoutManager(layoutManager);
playlistAdapter.setOnItemClickListener(MainActivity.this);
binding.playlistRecyclerView.setAdapter(playlistAdapter);
super.onPostExecute(aVoid);
}
}.execute();
}
Let me explain what we do here. We need a thread operation to create the managers and get the configuration in AudioKit. Thus, I used AsyncTask to do just that.
Remark: AsyncTask is currently deprecated so I would suggest you to use other methods if you care about modernity.
If AsyncTask succeeds, then I get my HwAudioManager instance and set it to my global variable. After that, using that manager instance, I get my player and queue managers. (and config manager as well but it will be used less frequently)
After getting my managers, I get my local playlist by a method I will share below. The collections code is just to sort the list alphabetically, you do not have to use it. Later, there is another method called doListenersAndNotifications(…), to set the notification bar and to attach the listeners, which is a crucial part in playback management.
In onPostExecute method, now that my managers are ready, I set my adapter for my playlist. However, we will get back to this later on. You do not need to worry about for now.
Let’s see how getLocalPlayList(…) works.
Beware: In order for below method to work, you must ask for the storage permission from the user explicitly. How to deal with it is your own responsibility. You must consider all cases (like user not giving consent etc.). The below method will work only after the storage permission is granted.
Code:
public List getLocalPlayList(Context context) {
List playItemList = new ArrayList<>();
Cursor cursor = null;
try {
ContentResolver contentResolver = context.getContentResolver();
if (contentResolver == null) {
return playItemList;
}
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
selection,
null,
MediaStore.Audio.Media.TITLE);
HwAudioPlayItem songItem;
if (cursor != null) {
if(!cursor.moveToNext()){
//there is no music, do sth
return playItemList; //return empty list for now
}
else{
while(cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
if (new File(path).exists()) {
songItem = new HwAudioPlayItem();
songItem.setAudioTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
songItem.setAudioId(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)) + "");
songItem.setFilePath(path);
songItem.setOnline(0);
songItem.setIsOnline(0);
songItem.setDuration(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
songItem.setSinger(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
playItemList.add(songItem);
}
}
}
}
else{
Toast.makeText(this, "We have a serious cursor problem here!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("TAG,", "EXCEPTION", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
Log.i("LOCALITEMSIZE", "getLocalPlayList: " + playItemList.size());
return playItemList;
}
HwAudioPlayItem is the AudioKit’s POJO class for audio files. It contains the basic attributes of an audio file that developers can set, to use them later. Please note that, as of the publish time of this article, HwAudioPlayItem does not contain enough methods to initialize. Thus, some of the fields you consider you would use may be lacking. For example, there is no field for album name and thus my screenshot above always displays “Album Unknown”.
By this method, we browse the local music files, retrieve them in the format of HwAudioPlayItem and add them to a local list. Before returning, see the size of it in the logs. Our playlist is therefore ready, after this operation.
Now let’s see how I set listeners and implement notifications.
Code:
private List mTempListeners = new CopyOnWriteArrayList<>(); //a global variable
private void doListenersAndNotifications(final Context context) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
for (HwAudioStatusListener listener : mTempListeners) {
try {
mHwAudioManager.addPlayerStatusListener(listener);
} catch (RemoteException e) {
Log.e("TAG", "TAG", e);
}
}
mHwAudioConfigManager.setSaveQueue(true);
mHwAudioConfigManager.setNotificationFactory(new INotificationFactory() {
@Override
public Notification createNotification(NotificationConfig notificationConfig) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
builder = new NotificationCompat.Builder(getApplication(), null);
RemoteViews remoteViews = new RemoteViews(getApplication().getPackageName(), R.layout.notification_player);
builder.setContent(remoteViews);
builder.setSmallIcon(R.drawable.icon_notifaction_music);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCustomBigContentView(remoteViews);
NotificationUtils.addChannel(getApplication(), NotificationUtils.NOTIFY_CHANNEL_ID_PLAY, builder);
boolean isQueueEmpty = mHwAudioManager.getQueueManager().isQueueEmpty();
Bitmap bitmap;
bitmap = notificationConfig.getBitmap();
setBitmap(remoteViews, bitmap);
boolean isPlaying = mHwAudioManager.getPlayerManager().isPlaying() && !isQueueEmpty;
remoteViews.setImageViewResource(R.id.image_toggle, isPlaying ? R.drawable.ic_notification_stop : R.drawable.ic_notification_play);
HwAudioPlayItem playItem = mHwAudioManager.getQueueManager().getCurrentPlayItem();
remoteViews.setTextViewText(R.id.text_song, playItem.getAudioTitle());
remoteViews.setTextViewText(R.id.text_artist, playItem.getSinger());
remoteViews.setImageViewResource(R.id.image_last, R.drawable.ic_notification_before);
remoteViews.setImageViewResource(R.id.image_next, R.drawable.ic_notification_next);
remoteViews.setOnClickPendingIntent(R.id.image_last, notificationConfig.getPrePendingIntent());
remoteViews.setOnClickPendingIntent(R.id.image_toggle, notificationConfig.getPlayPendingIntent());
remoteViews.setOnClickPendingIntent(R.id.image_next, notificationConfig.getNextPendingIntent());
remoteViews.setOnClickPendingIntent(R.id.image_close, getCancelPendingIntent());
remoteViews.setOnClickPendingIntent(R.id.layout_content, getMainIntent());
return builder.build();
}
else{
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplication(), null);
RemoteViews remoteViews = new RemoteViews(getApplication().getPackageName(), R.layout.statusbar);
builder.setContent(remoteViews);
builder.setSmallIcon(R.drawable.icon_notifaction_music);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCustomBigContentView(remoteViews);
NotificationUtils.addChannel(getApplication(), NotificationUtils.NOTIFY_CHANNEL_ID_PLAY, builder);
boolean isQueueEmpty = mHwAudioManager.getQueueManager().isQueueEmpty();
Bitmap bitmap;
bitmap = notificationConfig.getBitmap();
setBitmap(remoteViews, bitmap);
boolean isPlaying = mHwAudioManager.getPlayerManager().isPlaying() && !isQueueEmpty;
remoteViews.setImageViewResource(R.id.widget_id_control_play,
isPlaying ? R.drawable.notify_btn_pause_selector : R.drawable.notify_btn_play_selector);
HwAudioPlayItem playItem = mHwAudioManager.getQueueManager().getCurrentPlayItem();
remoteViews.setTextViewText(R.id.trackname, playItem.getAudioTitle());
remoteViews.setTextViewText(R.id.artistalbum, playItem.getSinger());
remoteViews.setImageViewResource(R.id.widget_id_control_prev, R.drawable.notify_btn_close_selector);
remoteViews.setImageViewResource(R.id.widget_id_control_next, R.drawable.notify_btn_next_selector);
remoteViews.setOnClickPendingIntent(R.id.widget_id_control_prev, getCancelPendingIntent());
remoteViews.setOnClickPendingIntent(R.id.widget_id_control_play, notificationConfig.getPlayPendingIntent());
remoteViews.setOnClickPendingIntent(R.id.widget_id_control_next, notificationConfig.getNextPendingIntent());
remoteViews.setOnClickPendingIntent(R.id.statusbar_layout, getMainIntent());
return builder.build();
}
}
});
}
});
}
private void setBitmap(RemoteViews remoteViews, Bitmap bitmap) {
HwAudioPlayItem tmpItem = mHwAudioQueueManager.getCurrentPlayItem();
Bitmap imageCoverOfMusic = getBitmapOfCover(tmpItem);
if(imageCoverOfMusic != null){
Log.i("TAG", "Notification bitmap not empty");
remoteViews.setImageViewBitmap(R.id.image_cover, imageCoverOfMusic);
}
else{
if (bitmap != null) {
Log.i("TAG", "Notification bitmap not empty");
remoteViews.setImageViewBitmap(R.id.image_cover, bitmap);
} else {
Log.w("TAG", "Notification bitmap is null");
remoteViews.setImageViewResource(R.id.image_cover, R.drawable.icon_notifaction_default);
}
}
}
private Bitmap getAlbumImage(String path) {
try{
android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(path);
byte[] data = mmr.getEmbeddedPicture();
if (data != null)
return BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
public Bitmap getBitmapOfCover(HwAudioPlayItem currItem){
if(currItem != null) {
String currentSongPath = currItem.getFilePath();
if (currentSongPath != null) {
Bitmap tmpMap = getAlbumImage(currentSongPath);
binding.albumPictureImageView.setImageBitmap(tmpMap);
return tmpMap;
}
}
return null;
}
private PendingIntent getCancelPendingIntent() {
Log.i("TAG", "getCancelPendingIntent");
Intent closeIntent = new Intent("com.menes.audiokittryoutapp.cancel_notification");
closeIntent.setPackage(getApplication().getPackageName());
return PendingIntent.getBroadcast(getApplication(), 2, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent getMainIntent() {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setClass(getApplication().getBaseContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
return PendingIntent.getActivity(getApplication(), 0, intent, 0);
}
Let me explain here. I know it looks complicated but most of it is almost copy-paste boiler plate code. First we attach listeners in a looper, HwAudioStatusListener is AudioKit’s listener class. After that we save our queue and set the notification bar. I use this code with else part almost commented out due to support issues but you may copy and paste it to try it out in lower versions of Android. That being said, it should be clear that you should update the resource names as per your needs/wants.
setBitmap(…) method is to render the cover image in notification bar layout. (You can find the customized layout in GitHub link I shared). You cannot find this method in this way in sample apps. It is a cool and important feature, so I suggest you use it. Also, there is a NotificationUtils class for additional notification bar related codes, and you may also find it in the GitHub. You do not have to put them separately, you can copy those code to MainActivity and still use them, if you think that that will not be overcomplicating your code.
Also the other Bitmap methods will let you use the setBitmap(…) method correctly. Also, they will be useful when rendering the cover image to main screen too. Since HwAudioPlayItem class do not have a field for image paths for easy renders, I had to use more than 2 methods to use the path of the song to retrieve the cover image and then use it to get the Bitmap of it.
Other intent methods are to implement the feature of close button to cancel the notification and for the main intent.
The End for now!
The rest of the application will be talked about in Part 2 of this series. We will be implementing onCreate method, button clicks and listeners in detail. See you there.

How to Quickly Build an Audio Editor with UI

Audio is the soul of media, and for mobile apps in particular, it engages with users more, adds another level of immersion, and enriches content.
This is a major driver of my obsession for developing audio-related functions. In my recent post that tells how I developed a portrait retouching function for a live-streaming app, I mentioned that I wanted to create a solution that can retouch music. I know that a technology called spatial audio can help with this, and — guess what — I found a synonymous capability in HMS Core Audio Editor Kit, which can be integrated independently, or used together with other capabilities in the UI SDK of this kit.
I chose to integrate the UI SDK into my demo first, which is loaded with not only the kit's capabilities, but also a ready-to-use UI. This allows me to give the spatial audio capability a try and frees me from designing the UI. Now let's dive into the development procedure of the demo.
Development Procedure​Preparations​1. Prepare the development environment, which has requirements on both software and hardware. These are:
Software requirements:
JDK version: 1.8 or later
Android Studio version: 3.X or later
minSdkVersion: 24 or later
targetSdkVersion: 33 (recommended)
compileSdkVersion: 30 (recommended)
Gradle version: 4.6 or later (recommended)
Hardware requirements: a phone running EMUI 5.0 or later, or a phone running Android whose version ranges from Android 7.0 to Android 13.
2. Configure app information in a platform called AppGallery Connect, and go through the process of registering as a developer, creating an app, generating a signing certificate fingerprint, configuring the signing certificate fingerprint, enabling the kit, and managing the default data processing location.
3. Integrate the HMS Core SDK.
4. Add necessary permissions in the AndroidManifest.xml file, including the vibration permission, microphone permission, storage write permission, storage read permission, Internet permission, network status access permission, and permission to obtaining the changed network connectivity state.
When the app's Android SDK version is 29 or later, add the following attribute to the application element, which is used for obtaining the external storage permission.
Code:
<application
android:requestLegacyExternalStorage="true"
…… >
SDK Integration​1. Initialize the UI SDK and set the app authentication information. If the information is not set, this may affect some functions of the SDK.
Code:
// Obtain the API key from the agconnect-services.json file.
// It is recommended that the key be stored on cloud, which can be obtained when the app is running.
String api_key = AGConnectInstance.getInstance().getOptions().getString("client/api_key");
// Set the API key.
HAEApplication.getInstance().setApiKey(api_key);
2. Create AudioFilePickerActivity, which is a customized activity used for audio file selection.
Code:
/**
* Customized activity, used for audio file selection.
*/
public class AudioFilePickerActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
performFileSearch();
}
private void performFileSearch() {
// Select multiple audio files.
registerForActivityResult(new ActivityResultContracts.GetMultipleContents(), new ActivityResultCallback<List<Uri>>() {
@Override
public void onActivityResult(List<Uri> result) {
handleSelectedAudios(result);
finish();
}
}).launch("audio/*");
}
/**
* Process the selected audio files, turning the URIs into paths as needed.
*
* @param uriList indicates the selected audio files.
*/
private void handleSelectedAudios(List<Uri> uriList) {
// Check whether the audio files exist.
if (uriList == null || uriList.size() == 0) {
return;
}
ArrayList<String> audioList = new ArrayList<>();
for (Uri uri : uriList) {
// Obtain the real path.
String filePath = FileUtils.getRealPath(this, uri);
audioList.add(filePath);
}
// Return the audio file path to the audio editing UI.
Intent intent = new Intent();
// Use HAEConstant.AUDIO_PATH_LIST that is provided by the SDK.
intent.putExtra(HAEConstant.AUDIO_PATH_LIST, audioList);
// Use HAEConstant.RESULT_CODE as the result code.
this.setResult(HAEConstant.RESULT_CODE, intent);
finish();
}
}
The FileUtils utility class is used for obtaining the real path, which is detailed here. Below is the path to this class.
Code:
app/src/main/java/com/huawei/hms/audioeditor/demo/util/FileUtils.java
3. Add the action value to AudioFilePickerActivity in AndroidManifest.xml. The SDK would direct to a screen according to this action.
Code:
<activity
android:name=".AudioFilePickerActivity"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.hms.audioeditor.chooseaudio" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
4. Launch the audio editing screen via either:
Mode 1: Launch the screen without input parameters. In this mode, the default configurations of the SDK are used.
Code:
HAEUIManager.getInstance().launchEditorActivity(this);
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Audio editing screens
Mode 2: Launch the audio editing screen with input parameters. This mode lets you set the menu list and customize the path for an output file. On top of this, the mode also allows for specifying the input audio file paths, setting the draft mode, and more.
Launch the screen with the menu list and customized output file path:
Code:
// List of level-1 menus. Below are just some examples:
ArrayList<Integer> menuList = new ArrayList<>();
// Add audio.
menuList.add(MenuCommon.MAIN_MENU_ADD_AUDIO_CODE);
// Record audio.
menuList.add(MenuCommon.MAIN_MENU_AUDIO_RECORDER_CODE);
// List of level-2 menus, which are displayed after audio files are input and selected.
ArrayList<Integer> secondMenuList = new ArrayList<>();
// Split audio.
secondMenuList.add(MenuCommon.EDIT_MENU_SPLIT_CODE);
// Delete audio.
secondMenuList.add(MenuCommon.EDIT_MENU_DEL_CODE);
// Adjust the volume.
secondMenuList.add(MenuCommon.EDIT_MENU_VOLUME2_CODE);
// Customize the output file path.
String exportPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath() + "/";
AudioEditorLaunchOption.Builder audioEditorLaunch = new AudioEditorLaunchOption.Builder()
// Set the level-1 menus.
.setCustomMenuList(menuList)
// Set the level-2 menus.
.setSecondMenuList(secondMenuList)
// Set the output file path.
.setExportPath(exportPath);
// Launch the audio editing screen with the menu list and customized output file path.
try {
HAEUIManager.getInstance().launchEditorActivity(this, audioEditorLaunch.build(), new LaunchCallback() {
@Override
public void onFailed(int errCode, String errMsg) {
Toast.makeText(mContext, errMsg, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
Level-1 menus
Level-2 menus
Launch the screen with the specified input audio file paths:
Code:
// Set the input audio file paths.
ArrayList<AudioInfo> audioInfoList = new ArrayList<>();
// Example of an audio file path:
String audioPath = "/storage/emulated/0/Music/Dream_It_Possible.flac";
// Create an instance of AudioInfo and pass the audio file path.
AudioInfo audioInfo = new AudioInfo(audioPath);
// Set the audio name.
audioInfo.setAudioName("Dream_It_Possible");
audioInfoList.add(audioInfo);
AudioEditorLaunchOption.Builder audioEditorLaunch = new AudioEditorLaunchOption.Builder()
// Set the input audio file paths.
.setFilePaths(audioInfoList);
// Launch the audio editing screen with the specified input audio file paths.
try {
HAEUIManager.getInstance().launchEditorActivity(this, audioEditorLaunch.build(), new LaunchCallback() {
@Override
public void onFailed(int errCode, String errMsg) {
Toast.makeText(mContext, errMsg, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
Launch the screen with drafts:
Code:
// Obtain the draft list. For example:
List<DraftInfo> draftList = HAEUIManager.getInstance().getDraftList();
// Specify the first draft in the draft list.
String draftId = null;
if (!draftList.isEmpty()) {
draftId = draftList.get(0).getDraftId();
}
AudioEditorLaunchOption.Builder audioEditorLaunch = new AudioEditorLaunchOption.Builder()
// Set the draft ID, which can be null.
.setDraftId(draftId)
// Set the draft mode. NOT_SAVE is the default value, which indicates not to save a project as a draft.
.setDraftMode(AudioEditorLaunchOption.DraftMode.SAVE_DRAFT);
// Launch the audio editing screen with drafts.
try {
HAEUIManager.getInstance().launchEditorActivity(this, audioEditorLaunch.build(), new LaunchCallback() {
@Override
public void onFailed(int errCode, String errMsg) {
Toast.makeText(mContext, errMsg, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
And just like that, SDK integration is complete, and the prototype of the audio editing app I want is ready to use.
Not bad. It has all the necessary functions of an audio editing app, and best of all, it's pretty easy to develop, thanks to the all-in-one and ready-to-use SDK.
Anyway, I tried the spatial audio function preset in the SDK and I found I could effortlessly add more width to a song. However, I also want a customized UI for my app, instead of simply using the one provided by the UI SDK. So my next step is to create a demo with the UI that I have designed and the spatial audio function.
Afterthoughts​Truth to be told, the integration process wasn't as smooth as it seemed. I encountered two issues, but luckily, after doing some of my own research and contacting the kit's technical support team, I was able to fix the issues.
The first issue I came across was that after touching the Add effects and AI dubbing buttons, the UI displayed The token has expired or is invalid, and the Android Studio console printed the HAEApplication: please set your app apiKey log. The reason for this was that the app's authentication information was not configured. There are two ways of configuring this. The first was introduced in the first step of SDK Integration of this post, while the second was to use the app's access token, which had the following code:
Code:
HAEApplication.getInstance().setAccessToken("your access token");
The second issue — which is actually another result of unconfigured app authentication information — is the Something went wrong error displayed on the screen after an operation. To solve it, first make sure that the app authentication information is configured. Once this is done, go to AppGallery Connect to check whether Audio Editor Kit has been enabled for the app. If not, enable it. Note that because of caches (of either the mobile phone or server), it may take a while before the kit works for the app.
Also, in the Preparations part, I skipped the step for configuring obfuscation scripts before adding necessary permissions. This step is, according to technical support, necessary for apps that aim to be officially released. The app I have covered in this post is just a demo, so I just skipped this step.
Takeaway​No app would be complete with audio, and with spatial audio, you can deliver an even more immersive audio experience to your users.
Developing a spatial audio function for a mobile app can be a piece of cake thanks to HMS Core Audio Editor Kit. The spatial audio capability can be integrated either independently or together with other capabilities via the UI SDK, which delivers a ready-to-use UI, so that you can skip the tricky bits and focus more on what matters to users.

Categories

Resources