<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Oguzhan Yigit on Medium]]></title>
        <description><![CDATA[Stories by Oguzhan Yigit on Medium]]></description>
        <link>https://medium.com/@oguzhany34?source=rss-a089100b2c5d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Oguzhan Yigit on Medium</title>
            <link>https://medium.com/@oguzhany34?source=rss-a089100b2c5d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 18:29:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@oguzhany34/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to run Java/Kotlin code with shell privilege on Android]]></title>
            <link>https://medium.com/@oguzhany34/how-to-run-java-kotlin-code-with-shell-privilege-on-android-7f5ef19b8cc?source=rss-a089100b2c5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f5ef19b8cc</guid>
            <dc:creator><![CDATA[Oguzhan Yigit]]></dc:creator>
            <pubDate>Sun, 04 Dec 2022 15:27:26 GMT</pubDate>
            <atom:updated>2022-12-04T15:27:26.586Z</atom:updated>
            <content:encoded><![CDATA[<p>Android has several protection and isolation mechanisms that stop your code do unauthorized/insecure things on system and device, one of them and most basic one is UID/GID mechanism, in Unix-like operating systems (Android is also Unix-based operating system) a value called a user identifier (UID) and a group identifier (GID) are used to determine which system resources a user or group can access. A <strong>UID (user identifier)</strong> is a number assigned by kernel to each user/process on the system and it is used to identify the user/process by the system to determine which system resources the user can access. Android reserves UIDs in range 1000-9999 for system use and 10000-19999 is reserved for the applications, UID/GID 0 is special for Linux kernel; it&#39;s the super user (Root User) and it is capable to do everything on the system</p><p>BACKGROUND</p><p>In Android, when we start our application, OS creates a Process with UID/GID value between10000-19999assigned, here you can checkout your process UID/GID with the below code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sLshJy92R-LZtE0DTFX86w.png" /></figure><p>and with its UID/GID, application is restricted to do only allowed things such as creating File/Folder in it’s own allowed directory etc. and of course there is another security measure on Android that your app MUST define permissions on the Manifest file (AndroidManifest.xml) and also you should ask to the user dynamically in order to grant it (since Android M)</p><p>This restriction and all these security measures of course are good for users (user and device security and privacy), but it comes with a price that these measures also make OS less flexible and customizable, Every major and minor new android releases, Google hardens the security on Android and as its consequences, some apps start to not work as it intended</p><p>in order to overcome these situations, developers usually write their apps for Rooted Android devices, and apps they developed execute series of shell commands under root UID/GID (which is 0) (keep in mind that root privilege have ability to do anything on the system regardless granted permissions)</p><p>But there are not many Android users worldwide who rooted their devices (and also rooting device is a bit risky operation)</p><p>There is also another method called ADB (Android Debug Bridge) which some customization apps rely on it, ADB is set of tools and shell commands that developers use while developing their applications (mostly to debug the application) and when ADB command issued by developer, it creates a Shell Process which its UID= 2000 on device, and this shell process <strong><em>has wide range of capabilities than</em></strong> normal application process (UID=10000–19999), and some of customization apps exploit this to customize device features</p><p>IMPLEMENTATION</p><p>we will use the same ADB method to run our java/kotlin code so first of all you need to create a class which contains <strong><em>Main method </em></strong>like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CKFgYYG4RWXyXimpD7C80w.png" /></figure><p>and after that compile your app, and install it into your device, after installation open a terminal section in your android studio IDE and type the following command:</p><p>adb shell pm path YOUR_APP_PACKAGE_ID</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*auCtnlp07IbJSNb_o2YT-g.png" /></figure><p>and note the output path (which usually start with /data/xxxx)</p><p>and type the following command:</p><p>adb shell CLASSPATH=OUTPUT_FROM_PREVIOUS_COMMAND app_process /system/bin com.mordred.native2quicklauncher.ExampleClass</p><p>(Notice the <strong><em>com.mordred.native2quicklauncher.ExampleClass</em></strong> it is the full path of your class containing <strong><em>Main method</em></strong>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pshF-tkNcbQVwpjsXEwXpg.png" /></figure><p>voila as you can see from the output message, we successfully executed our java code with shell privilege</p><p>But there is a one caveat that since we executed Main method through ADB shell, the created process is different from the main application process, so you cannot communicate directly with main application process created by MainActivity class</p><p>in order to make communication between main application process (a process created by user opening our app) with our ADB-created process, we need to utilize something called <strong>IPC — a.k.a Inter Process Communication</strong></p><p>There are the following 3 methods used for IPC on Android:</p><ol><li>AIDL</li><li>Messenger</li><li>Broadcast</li></ol><p>I won’t go into the details of IPC on Android (that is long and would be the subject of another article, also you can find more details on Medium)</p><p>Since we need to deal with both ADB-created process and our application process, and IPC (to communicate them), i created a helper library to handle all these complexities for the user</p><h4>SHELLJAVA</h4><p>Originally inspired by <a href="https://github.com/Chainfire/librootjava">Librootjava</a> library by Chainfire and <a href="https://github.com/topjohnwu/libsu">Libsu</a> library by Topjohnwu which you can check out also, these libraries are “Root” version of shelljava library (if you need more privilege and access than adb shell privilege, like root privilege, you can use them)</p><p>Here is the source page of the shelljava library -&gt; <a href="https://github.com/sirmordred/shelljava">Github</a></p><p>You just need to import <em>shelljava.aar</em> into your application to use it</p><p>after importing library, first you need to define a class which extends “ShellService” and then you need to define “<strong>public IBinder executeShell()</strong>” method inside, after that you can write your “code-which-will-be-executed-with-shell-privilege” into the method body</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2xAeHZflFRTuK_I64qLDVw.png" /></figure><h4><strong>Without returning value (for executing void types functions)</strong></h4><p>if your “code-which-will-be-executed-with-shell-privilege” doesn’t need to return value, you can simply return “null”</p><h4><strong>With returning value (for executing non-void type functions)</strong></h4><p>in order to return value from shell-executed-code-block you need to define an AIDL class like the following ExampleResultAIDL aidl file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*7IWkcTIYNu_H2b1-Ki-d-w.png" /></figure><p>after defining ExampleResultAIDL class, just rebuild the project (i.e compile debug apk) to generate ExampleResultAIDL.java class (THIS STEP IS CRUCIAL SINCE WE WILL NEED <strong><em>ExampleResultAIDL.java</em></strong> class IN ORDER TO COMPILE APPLICATION)</p><p>after taking build, the generated ExampleResultAIDL class should be imported into ExampleService.java, and now you can return whatever result you want from <em>executeInShell()</em> function via the help of ExampleResultAIDL class (we return myProcessUid integer value in the example via the getResult() method)</p><p>and now you need to initialize <strong><em>ShellJava</em></strong> class like the following way inside <em>onCreate()</em> method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GPgHphRe3BfR3nmJk3RFBA.png" /></figure><p>1st parameter of shelljava constructor is application context, BEWARE that you need full app context in order to create ShellJava object</p><p>2nd parameter is our custom class ExampleService.java which we created in previous step</p><p>3rd parameter <em>secretKey</em> is a key in a String type that is used while initializing ShellJava Server, and so any harmful applications or processes cannot use your server nor initiate shell commands, you also need to keep your <em>secretKey</em> secure in order to access your running ShellJava server</p><p>ShellJava constructor takes 3 argument as default but you can also initialize it with 4 parameter, the 4th parameter, boolean parameter is indicator that your code should be run immediately after user start server or not, the default value of this boolean is false so we MUST call <em>shellJava.execute() </em>function somewhere of the application lifecycle (we are calling it when user presses button in the example app)</p><p>Note that as you can see, after returning value from your shell-executed-code-block (inside ExampleService class) you can capture it on the <em>onResult() </em>method of ShellJava object (see <em>exampleResultAIDL.getResult()</em>)</p><p>after initializing ShellJava object you can execute it with <strong><em>shellJava.execute()</em></strong> method like shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*zuiECImMdQWA_B704fNQ5A.png" /><figcaption>(Here we are executing it through button click)</figcaption></figure><p>after executing it, your code will be executed (of course IF THE SERVER IS RUNNING)</p><p>IF THE SERVER IS NOT RUNNING, your user need to initiate server starter command through adb shell (often requires PC but you can use PC-less method “<a href="https://play.google.com/store/apps/details?id=com.draco.ladb&amp;hl=tr&amp;gl=US">LADB</a>” mobile app too), you can take your command through the <em>shellJava.getServerStartCmd()</em> method and display it to the user (via Textview for example)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*Conwd2yJUHz2FLeli9w7nA.png" /><figcaption>(Here i’m printing into logs)</figcaption></figure><p>Example command to start server (For the sample app):</p><p><strong><em>adb shell sh /storage/emulated/0/Android/data/com.mordred.mylibrary/files/start.sh</em></strong></p><p>Note that your user need to initiate this adb command for once only (except reboot, after rebooting device, user need to initiate the command again)</p><p>And here you can see that we successfully executed our code with shell privilege and displayed the result via the textview</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8XjjK04QKg0DSxnWmj9dKQ.jpeg" /></figure><p>Thats all, thanks for reading</p><p>Have a nice coding</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f5ef19b8cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Blocking scam phone numbers starting with specific prefixes on Android]]></title>
            <link>https://medium.com/@oguzhany34/blocking-scam-phone-numbers-starting-with-specific-prefixes-on-android-25a9e9e709c?source=rss-a089100b2c5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/25a9e9e709c</guid>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[aosp]]></category>
            <category><![CDATA[call-blocking]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Oguzhan Yigit]]></dc:creator>
            <pubDate>Fri, 24 Jun 2022 18:34:05 GMT</pubDate>
            <atom:updated>2022-06-24T18:34:05.068Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*5SA6hTjav4p4hMASrP6pvQ.jpeg" /><figcaption>image source <a href="https://calleridreputation.com/blog/how-do-call-blocking-apps-like-hiya-and-nomorobo-work/">https://calleridreputation.com/blog/how-do-call-blocking-apps-like-hiya-and-nomorobo-work/</a></figcaption></figure><p>Hello all today i want to show you my latest hobby project which is about for improving call blocking feature of android</p><h4>Problem description</h4><p>All that stuff is started with i was being called from huge number of different numbers by scammers, after being called from scammers dozen of times, i blocked the scam numbers one-by-one with using “call blocking” feature of android, it’s simple and clear, you can block any number with using this feature and you won’t get any calls/sms etc. from that number. Here you can see example screenshots of the blocking feature:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*xAR5-eT9tOuN7NqrnZZblw.png" /><figcaption>source: <a href="https://source.android.com/devices/tech/connect/block-numbers">https://source.android.com/devices/tech/connect/block-numbers</a></figcaption></figure><p>but this feature has one caveat: it just lets you block individual numbers one-by-one</p><p>unfortunately, scammers are using range of phone numbers to overcome blockage (i don’t know and wonder how they bought and get these range of numbers btw), well after i found out that blocking individual scammer numbers isn’t enough and won’t stop scammers, i tried to search an effective way to block these numbers, luckily, they are using not so much variety of numbers, nearly all scammer phones which have called me in a period of time were starting with “+90 850 484 ******”, i first looked into number of play store apps but all of them don’t do what i want (blocking the range of numbers/numbers starts with XXX specific prefixes) and as i read it through these Stackoverflow <a href="https://android.stackexchange.com/questions/149561/how-to-block-a-range-of-phone-numbers-without-any-third-party-apps">comments</a>, it is not possible on android with or without 3rd party app, after that, i tried to implement it myself, and started to digging out the AOSP (Android Open Source Project) sources especially telephony stack on android</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/0*bgk3JyAiikWVjlZG" /><figcaption>Android Telephony Stack (source <a href="https://wladimir-tm4pda.github.io/porting/telephony.html">https://wladimir-tm4pda.github.io/porting/telephony.html</a>)</figcaption></figure><p>after digging out telephony stack on android, i tried to understand native call blocking feature of android which was firstly introduced on <a href="https://source.android.com/devices/tech/connect/block-numbers">Android 7.0</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*9mN7v2rp77iBOIOkXPXrIA.png" /><figcaption>Block phone numbers data flow (source <a href="https://source.android.com/devices/tech/connect/block-numbers">https://source.android.com/devices/tech/connect/block-numbers</a>)</figcaption></figure><p>as you can see from the data flow above, native call blocking feature is handled on both Application and Framework Layer and the core component is <a href="https://developer.android.com/reference/android/provider/BlockedNumberContract"><em>BlockedNumberProvider</em></a></p><p>Any 3rd party app developer can interact with exposed public APIs and insert/delete phone numbers as blacklisted or whitelisted, but the problem is you can not insert range of phone numbers, only one-by-one. Of course looping through range of phone numbers and insert them one by one is possible but it is a bad idea and not feasible solution because it may fill SQL database where all blocked phone numbers held so we need to find another way (My way :D)</p><h4>Understanding the native call blocking feature of android</h4><p>I realized that i need 2 things to implement this feature over default native call blocking</p><p><strong>1- Modifying telephony app’s default call blocking UI and its mechanism to make it accept wildcard numbers like “+90 850 484*”</strong></p><p><strong>2- Modifying the current in call filtering mechanism to handle inserted wildcard numbers(“+850 484*”) and block all the phone numbers in a range the specified wildcard number covers</strong></p><p>NOTE: If you’re not curious about how it works behind the scenes, you can skip the rest of this section and move on to the <strong>Implementation</strong> section.</p><p><strong>1-) Let’s take a look into call-chain of functions related to first part (saving/inserting blocked number mechanism)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*yrcd6ezZEO3oQRjdkHeloQ.png" /></figure><p><strong>2- Here is the call-chain of functions related to the second part (in call filtering of blocked numbers)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/515/1*pRiUhFBoTeWIenKKza6WCg.png" /></figure><p>To sum up, as you can see through above call-chains of functions, 1st one -saving/inserting blocked phone numbers mechanism- ends up with method called <a href="https://android.googlesource.com/platform/packages/providers/BlockedNumberProvider/+/refs/heads/android12L-release/src/com/android/providers/blockednumber/BlockedNumberProvider.java#147"><em>insertBlockedNumber()</em></a><em> </em>and 2nd one -in call filtering of blocked numbers mechanism- ends up with method <a href="https://android.googlesource.com/platform/packages/providers/BlockedNumberProvider/+/refs/heads/android12L-release/src/com/android/providers/blockednumber/BlockedNumberProvider.java#415"><em>isBlocked()</em></a></p><h4>Implementation</h4><p>As can be seen through above section, all blocking operations done through <a href="https://android.googlesource.com/platform/packages/providers/BlockedNumberProvider/+/refs/heads/android12L-release/src/com/android/providers/blockednumber/BlockedNumberProvider.java"><em>BlockedNumberProvider</em></a></p><p>I edited the both 2 method to implement my feature on top of native call blocking, it can be seen through the below commit:</p><p><a href="https://github.com/sirmordred/android_packages_providers_BlockedNumberProvider/commit/1c7097429830b054c7b844bffe9602f9135779ff">https://github.com/sirmordred/android_packages_providers_BlockedNumberProvider/commit/1c7097429830b054c7b844bffe9602f9135779ff</a></p><p>in the first function (<em>insertBlockedNumber()</em>) i just added the if check to not normalize/convert wildcard typed numbers to e164 form (or it will crash the whole telephony stack), it just saves the wildcard number typed by user into database as is without converting to e164 form</p><p>in the second function (<em>isBlocked()</em>) which i edited, i added the same cursor based database search function to handle wildcard typed numbers, especially in the line 433 it checks if the incoming call number starts with wildcard typed blocked number and it’s prefix, if it’s match, it blocks call all the way, otherwise it allows</p><p>after re-compiling custom android ROM with the edited <em>BlockedPhoneNumber</em> provider package, i tried the feature i added, and yay! it worked like a charm</p><p>i blocked the numbers starts with +90 505* prefixes using my implementation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qPTAqcH78-JgmR3-Nd5fdQ.png" /></figure><p>and so i expected the system should block all the incoming calls where phone numbers starts with “+90 505” prefix and it worked as expected and here you can also see the blockage from logs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vgqg36KbHUUEEMlz8YmASQ.png" /></figure><p>log line starts with “<a href="https://github.com/sirmordred/android_packages_providers_BlockedNumberProvider/commit/1c7097429830b054c7b844bffe9602f9135779ff#diff-bed962a2a299add5a1e9e63a203c76bb05496fd850bd92daff5a1a019a5ec439R434"><strong><em>MORDRED, My block impl, blocked number: xxx</em></strong></a>” confirms our customized code works as expected</p><p>I also uploaded required change to AOSP Gerrit: <a href="https://android-review.googlesource.com/c/platform/packages/providers/BlockedNumberProvider/+/2133577">https://android-review.googlesource.com/c/platform/packages/providers/BlockedNumberProvider/+/2133577</a> let’s see how it goes</p><p>Thanks for reading, hope you enjoyed it</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25a9e9e709c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recording internal audio and device screen while executing Appium Test on Android]]></title>
            <link>https://medium.com/@oguzhany34/recording-internal-audio-and-device-screen-while-executing-appium-test-on-android-e141a1dabb29?source=rss-a089100b2c5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e141a1dabb29</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[appium-testing]]></category>
            <category><![CDATA[appium]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Oguzhan Yigit]]></dc:creator>
            <pubDate>Sun, 05 Jun 2022 13:55:17 GMT</pubDate>
            <atom:updated>2022-06-17T14:55:29.320Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2DGa1jbCCvv2jc5uiG3s5w.jpeg" /></figure><p>Appium is an open source test automation framework for use with native, hybrid and mobile web apps.<br>It drives iOS, Android, and Windows apps using the WebDriver protocol and it is used heavily in application testing field, especially android mobile application testing, it allows developers to simulate clicks, scrolls etc. simply user interactions in an automated fashion to test various functionalities of application</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*8ieKKzd5aPU_2DPtD1vY2A.png" /><figcaption>image source: <a href="https://academy.brightest.be/courses/mobile-test-automation-with-appium/">https://academy.brightest.be/courses/mobile-test-automation-with-appium/</a></figcaption></figure><p>long story short, while i was using appium in my job and also in an android development, i needed to test an app which has some audio functionalities, like producing audio song file which audio effects has been applied into, when i started to write automated test for my app, i need to somehow record the audio played by my app, but as far as i know, and as i searched it on documents of appium and net, appium currently does not have ability to record internal audio on android while executing test (see <a href="https://github.com/appium/appium/issues/13177">https://github.com/appium/appium/issues/13177</a>)</p><p>if you did not read my previous article (simultaneous internal audio/video recording on android) check out <a href="https://medium.com/@oguzhan97yigit/simultaneous-internal-audio-video-recording-on-android-72870e2c009e">here</a>, i wondered if i can somehow implement this functionality into appium itself, so i started to deep dive into appium and see how it works</p><p>since internal audio/video recording is possible on android in an application level, i need to apply my internal audio/video recorder into appium on android application level</p><p>luckily i found out that appium uses a helper android application called “io.appium.settings” while executing tests, so i targeted that application</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*eZ79spOoOXlUe8mjH8G56g.jpeg" /></figure><p>it is an open-source <a href="https://github.com/appium/io.appium.settings">application</a> that works on android during test execution, and it provides some internal device data for appium</p><p>i managed to implement my recorder code into that appium android application but i realized it needs much more work than implementing it</p><p>firstly i need to talk (start/stop recording) with application via only adb commands (adb stands for android device bridge, and appium has ability to execute adb commands) because there is no any other way than that (except hacky/tricky solutions)</p><p>so i used <em>getIntent() </em>method inside<em> onCreate() </em>method of<em> Settings </em>activity<em> </em>to capture adb commands and talk with my recorder service:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*Rowp8-OOCERQ6QNLGT9UKg.png" /></figure><p>and inside <em>handleRecording()</em> method, i parsed the intent:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5YFhNUyj2cCwu0PE2AjQRA.png" /></figure><p>With adb commands, we can open applications, start activities/fragments and also services, we can also broadcast messages into running broadcast receivers on the phone through intents</p><p>we can also include data as strings into broadcast message commands, and it can be received and accessed through intent’s extra objects (see broadcast intent extras)</p><p>so we now can start recording via the following command:</p><pre>adb shell am start -n &quot;io.appium.settings/io.appium.settings.Settings&quot; -a io.appium.settings.recording.ACTION_START --es filename abc.mp4</pre><p>to stop we can use the following command as well:</p><pre>adb shell am start -n &quot;io.appium.settings/io.appium.settings.Settings&quot; -a io.appium.settings.recording.ACTION_STOP</pre><p>one of the challenging issues that need to be solved is that appium executing test in an automated fashion and since our recorder service needs some permissions that needs to be granted by user, we need to grant them via adb command persistently and so we don’t interrupt automated test execution</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*pDrlr-pbNFOMveQ_IBA8Ew.jpeg" /></figure><p>luckily pm utility of adb (adb shell pm) and appops utility does exactly what we want</p><p>we can grant required permissions persistently with adb via the following commands:</p><pre>adb shell pm grant io.appium.settings android.permission.RECORD_AUDIO</pre><p>another obstacle is media projection permission dialog:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*fgf8KnExCe10ilhfaMO0OQ.jpeg" /></figure><p>and to overcome this and avoid the need of user interaction, we will use appops utility (see <a href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/AppOps.md">here</a> for more info)</p><pre>adb shell appops set io.appium.settings PROJECT_MEDIA allow</pre><p>perfect, we can now start internal audio/video recording seamlessly</p><p>now you can record internal audio/video during execution of appium test like the following way:</p><p>python example(java may also be used since appium supports it as well):</p><pre>test_recording_filename = &quot;MyExample.mp4&quot;</pre><pre># Start recording</pre><pre>driver.execute_script(&#39;mobile: shell&#39;, {</pre><pre>&#39;command&#39;: &#39;am&#39;,</pre><pre>&#39;args&#39;: [&#39;start&#39;, &#39;-n&#39;, &#39;io.appium.settings/io.appium.settings.Settings&#39;, &#39;-a&#39;, &#39;io.appium.settings.recording.ACTION_START&#39;, &#39;--es&#39;, &#39;filename&#39;, test_recording_filename],</pre><pre>&#39;includeStderr&#39;: True,</pre><pre>&#39;timeout&#39;: 5000</pre><pre>})<br></pre><pre># Run your test, click/scroll etc.</pre><pre>el = driver.find_element_by_class_name(&#39;UIATextField&#39;)</pre><pre>el.click()<br></pre><pre># Stop recording</pre><pre>driver.execute_script(&#39;mobile: shell&#39;, {</pre><pre>&#39;command&#39;: &#39;am&#39;,</pre><pre>&#39;args&#39;: [&#39;start&#39;, &#39;-n&#39;, &#39;io.appium.settings/io.appium.settings.Settings&#39;, &#39;-a&#39;, &#39;io.appium.settings.recording.ACTION_STOP&#39;],</pre><pre>&#39;includeStderr&#39;: True,</pre><pre>&#39;timeout&#39;: 5000</pre><pre>})<br></pre><pre># to obtain recorded mp4 file, issue the following command:</pre><pre>file_base64 = driver.pull_file(&#39;/storage/emulated/0/Android/data/io.appium.settings/files/&#39; + test_recording_filename)<br></pre><pre># Release driver instance</pre><pre>driver.quit()</pre><p>another sample snippet can be found on here <a href="https://gist.github.com/sirmordred/4c505ddce96f7513bfbe49d38ef041be">https://gist.github.com/sirmordred/4c505ddce96f7513bfbe49d38ef041be</a></p><p>Note that recorder mp4 file’s path always starts with <em>/storage/emulated/0/Android/data/io.appium.settings/files/ </em>the path is the best out of my options (it has read/write access for both app and adb, also compatible across android versions)</p><p>(EDIT: Pull Request is now merged into official Appium repository, you can use this feature on Appium started with Version 4.1.0) You need to build my custom io.appium.settings application through <a href="https://github.com/sirmordred/io.appium.settings">here</a> from source and install into your device in order to use recording features, i also opened <a href="https://github.com/appium/io.appium.settings/pull/88">PR</a> into official appium repository</p><p>Keep in mind that the above <strong>2 adb commands</strong> need to be issued first in order to use the functionality (dont worry, it is persistent across reboots)</p><p>BEWARE: this functionality can only work on android 10 and upper versions because of internal audio capturing API limitations</p><p>thanks for reading</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e141a1dabb29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simultaneous Internal Audio/Video Recording on Android]]></title>
            <link>https://medium.com/@oguzhany34/simultaneous-internal-audio-video-recording-on-android-72870e2c009e?source=rss-a089100b2c5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/72870e2c009e</guid>
            <category><![CDATA[adb]]></category>
            <category><![CDATA[audio]]></category>
            <category><![CDATA[screen-recording]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[audio-recording]]></category>
            <dc:creator><![CDATA[Oguzhan Yigit]]></dc:creator>
            <pubDate>Sat, 23 Apr 2022 15:11:59 GMT</pubDate>
            <atom:updated>2022-04-23T15:11:59.824Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Zqkucp23cYr59LgfmsFFg.jpeg" /></figure><p>Capturing internal audio in android has always been a challenge and often required root privilege which most devices don’t have and thanks Google they haven’t added this feature officially into core android base for a long time, Of course, some manufacturers (Samsung etc.) had a custom audio capture application on their phones, but pure android did not have this feature.</p><p>Unlike internal audio capture, many devices had screen recording feature as default for a long time, from the early android versions to the latest.</p><p>luckily Google added internal audio capture and its related APIs into android starting from Android 10 version</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-iS9jSIynArNh2na" /><figcaption>source: <a href="https://developer.android.com/guide/topics/media/av-capture">https://developer.android.com/guide/topics/media/av-capture</a></figcaption></figure><p>for screen recording feature, android has default built-in utility called “screenrecord” that can be executed through ADB (a.k.a Android Device Bridge) like the following way:</p><blockquote>adb shell screenrecord /sdcard/screenrecord.mp4</blockquote><p>but the problem is default screenrecord utility does not capture internal audio (even in latest android R, correct me if i’m wrong) it has only ability to capture outer sound via microphone</p><p>combining both internal audio(mp3, aac) and screen recording (mp4) is not straightforward currently (of course there are 3rd party application that can do this)</p><p>in this article, i will show you how to do this with only using public android APIs (no 3rd party library, no challenging/custom mod required)</p><p>To record/capture internal audio and screen and combine them into mp4 video, we first need service (background service) which can run even after app is closed</p><p>so we define our service:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8YO0AH8bNixI_mxk" /></figure><p>since our service will capture screen and internal audio, we also need additional attribute(<em>android:foregroundServiceType=”mediaProjection”</em>) when describing our service in <em>AndroidManifest.xml</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ELEXV27pMANnzk0N" /></figure><p>Next, for our app to work, we need to define required permissions via <em>AndroidManifest.xml</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*REIiumU8Un85PUTi" /></figure><p>for dynamically obtaining required permissions, we also define <em>verifyPermission()</em> function inside <em>MainActivity</em> (see dynamic permissions of Android M)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uigxnetuBSRAegVG" /></figure><p>We then need to create a thread that will run in our service and do our heavy operations (recording data, encoding etc.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LZbyrB1t-7gmPeAw" /></figure><p>inside RecorderThread, we need to setup audio record object with correct parameters in order to record/capture internal audio (with the help of AudioPlaybackCapture APIs)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JS7w94NM6GSnq81J" /></figure><p>after then we also need to setup required video encoder and recorder objects in order to capture device screen</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G-FRozJDGSkKH5iTtJIy-g.png" /></figure><p>we now have completed the setup for both internal audio record and screen capture, and also since audio record APIs returns audio in pcm audio format and screen recording APIs return data in chunks, we need to encode them in proper audio/video formats and then merge/combine them into single mp4 file, that’s why we also defined encoders</p><p>for data flow, audio record captures internal audio as pcm data and write that data into some sort of queue of encoder(implementation detail) via <em>queueInputBuffer</em> and audio codec APIs reads from queue with <em>dequeueInputBuffer</em></p><p>same flow also applies to video record and video codec pair as well</p><p>after encoding audio/video data in proper formats (aac, mpeg4) we now can merge them into single mp4 file, we do this with the help of MediaMuxer APIs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/0*0kGQBWCP4reu0o9W" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*KBGlX2kIEZ-DbdoOZJOPYg.png" /></figure><p>done you can now record screen with internal audio, the recorded mp4 file will be on <em>/storage/emulated/0/MyRecord.mp4</em> (you can change it through <a href="https://github.com/sirmordred/Android-AudioVideoRecorder/blob/master/app/src/main/java/com/mordred/mordredrecorder/RecorderService.java#L134">here</a>)</p><p>Note: i used a workaround <a href="https://github.com/sirmordred/Android-AudioVideoRecorder/blob/master/app/src/main/AndroidManifest.xml#L10">here</a> for faster demonstrating it on my android 10 device, but it MAY crash on upper android versions (android 11,12), this workaround simply resolves android’s latest “<a href="https://developer.android.com/about/versions/11/privacy/storage">scoped storage</a>” mess, if you experience a crash related to file read write permission denial, you can solve it through <a href="https://medium.com/microsoft-mobile-engineering/scoped-storage-in-android-10-android-11-28d58d989f3c">here</a> (even if this issue is not related with screen recording/audio recording APIs, I will also update code myself to address this issue ASAP i hope)</p><p>Beware: this app can only work on android 10(as i tested) and up versions, here also the full implementation on Github <a href="https://github.com/sirmordred/Android-AudioVideoRecorder">https://github.com/sirmordred/Android-AudioVideoRecorder</a></p><p>if you like thumbs up and share, if you have any question, you can write comment and also open issue on Github repo</p><p>thank you for reading</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72870e2c009e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>