<?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 Lukáš Sztefek on Medium]]></title>
        <description><![CDATA[Stories by Lukáš Sztefek on Medium]]></description>
        <link>https://medium.com/@skywall?source=rss-cd4f6a5bd795------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Akr_hLVbTUu48dO8ihNkZg.jpeg</url>
            <title>Stories by Lukáš Sztefek on Medium</title>
            <link>https://medium.com/@skywall?source=rss-cd4f6a5bd795------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 15:33:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@skywall/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[Integration testing with Dagger]]></title>
            <link>https://medium.com/@skywall/integration-testing-with-dagger-b568d298b0b?source=rss-cd4f6a5bd795------2</link>
            <guid isPermaLink="false">https://medium.com/p/b568d298b0b</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[dagger]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Lukáš Sztefek]]></dc:creator>
            <pubDate>Mon, 23 Nov 2020 13:26:02 GMT</pubDate>
            <atom:updated>2020-12-03T15:24:23.171Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZwG0zljRUGjzF9PtbbY35w.png" /></figure><p>Dagger is a superb universal dependency injection library. One of the reasons we use DI in our applications is that it forces us to decouple the whole system into single-purpose components. These components are combined together, and as a result of this process, we end up with a single object that represents the whole system. A “side effect” of this approach is great testability.</p><p>I&#39;m not going to cover single component unit testing since it is straightforward and described by plenty of other articles. Integration testing, when we test the whole system or a group of components, is, on the other hand, a bit more challenging and not that well described. The source code related to this article is available in the <a href="https://github.com/skywall/dagger-testing">GitHub repository</a>.</p><p>Dagger is widely used on Android and many articles about Dagger &amp; testing were written from Android&#39;s point of view. That&#39;s a reason why I explicitly avoid the Android perspective (bye-bye ApplicationComponent) and deal with Dagger as a Java (Kotlin) library.</p><h3>Our TicketMachine</h3><p>Let&#39;s begin with a system we want to test – a ticket machine. Just describe it quickly. At first, we need three machine subparts — a display, a printer, and a coin counter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf24716f11ce808d18758ac1bfc6f693/href">https://medium.com/media/bf24716f11ce808d18758ac1bfc6f693/href</a></iframe><p>Let&#39;s combine them into a complete ticket machine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cde2783e63f1927d24e7e8d8a2243527/href">https://medium.com/media/cde2783e63f1927d24e7e8d8a2243527/href</a></iframe><p>We need to instruct Dagger on how to provide us an instance of a brand new ticket machine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2532ba9e50ecf4ff79f95488ff559f14/href">https://medium.com/media/2532ba9e50ecf4ff79f95488ff559f14/href</a></iframe><p>Our machine is ready, let&#39;s try it.</p><pre>val ticketMachine = DaggerTicketMachineComponent<br>    .create()<br>    .ticketMachine<br><br>ticketMachine.selectTicket(TicketType.ADULT)<br>ticketMachine.insertCoin()<br>ticketMachine.insertCoin()</pre><pre>--- OUTPUT ----</pre><pre>&gt; Select ticket:<br>&gt; Ticket selected: ADULT. Insert coins: 2<br>&gt; Insert coins: 1<br>&gt; Ticket printed: ADULT<br>&gt; Select ticket:</pre><p>Great, we have our ticket machine working. Test time has come! At first, we should define what do we want to test. I recommend the given — when — then template to specify a test name. Let&#39;s begin with a test given ticket machine when adult ticket selected and paid then ticket is printed. To create mocks and run verifications, I’m going to use the superb mocking framework <a href="http://mockk.io/">MockK</a>.</p><h3>The naive approach</h3><p>The naive approach is based on manual object construction. We create all the parts of the tested object, combine them, and finally instantiate object. As you might see, the printer is a mocked instance, thus we can do some verifications, concretely whether a print of a ticket was requested.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7b0cf97a56c7d703487243c07a00c7e/href">https://medium.com/media/b7b0cf97a56c7d703487243c07a00c7e/href</a></iframe><p>You might have already realized what could go wrong here. A tested object might have plenty of dependencies, and those dependencies have other dependencies and so on. You can mock some of them, but your test will slowly change from an integration test into a unit test.</p><p>Another challenge lies in dependency instantiation itself. You might not be able to instantiate it on your own — construction is, for example, restricted to some module, and in the real implementation, you get instance provided by dependency injection. You can solve this issue with interface extraction and following the <a href="https://en.wikipedia.org/wiki/Facade_pattern">facade design pattern</a>, but that&#39;s a different story.</p><h3>Extended Module approach</h3><p>Can we somehow use the power of Dagger to build a tested object? The answer is yes! This can be done quite easily.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0abb714a53913baefb60005797868bca/href">https://medium.com/media/0abb714a53913baefb60005797868bca/href</a></iframe><p>Now we face a tricky problem. We aren&#39;t able to verify if a ticket print was requested since we cannot replace a real printer implementation with our mock. One of the solutions is to extract the printer provider into a separate module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64650a7edf8b7f5874d0010c0d2ae7cc/href">https://medium.com/media/64650a7edf8b7f5874d0010c0d2ae7cc/href</a></iframe><p>A new PrinterModule is declared in modules of TicketMachineComponent so all of the dependencies are henceforth satisfied. It&#39;s also possible to provide the printer via module to a Dagger object builder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cf416f53b3a407b45cb81ea62d7a8af/href">https://medium.com/media/9cf416f53b3a407b45cb81ea62d7a8af/href</a></iframe><p>Sadly, we can&#39;t still provide our mocked instance because it is hidden in the module implementation. That&#39;s the reason why is the PrinterModule class defined as open. It allows us to create a custom TestPrinterModule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70c067f5d975477ca5e46401f12e1fd8/href">https://medium.com/media/70c067f5d975477ca5e46401f12e1fd8/href</a></iframe><p>We can use this test module instead of the real module and provide a printer mock through its constructor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9bfc65c5c1ccb024d7223569e5a3f68/href">https://medium.com/media/f9bfc65c5c1ccb024d7223569e5a3f68/href</a></iframe><p>This approach is <strong>not suitable</strong> for every use case and also brings a few limitations. You have to override all of the types provided by the extended module, you can&#39;t add new ones, and the module itself must be declared open. Can we do better?</p><h3>Custom test Component &amp; Module approach</h3><p>Custom test component implementation is the clearest solution. Firstly, there is no need to have open class modules in production, and we can change their inner implementation. We declare a new, independent, TestPrinterModule and use it in new TestTicketMachineComponent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e05f19040df73bb5b6f5e44bfd66932/href">https://medium.com/media/8e05f19040df73bb5b6f5e44bfd66932/href</a></iframe><p>The final test is going to be almost identical to the previous one. It differs only in the method .testPrinterModule() where we provide our test module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b79ae2b78b2d845dac42dd31ba86958/href">https://medium.com/media/4b79ae2b78b2d845dac42dd31ba86958/href</a></iframe><h4>Satisfying dependencies</h4><p>Lastly, let me also introduce one interesting fact. Dagger is based on satisfying dependencies. It doesn&#39;t matter <strong>where</strong> the dependency is provided. What really matters <strong>if</strong> it is provided. With that in mind, we can completely omit TestPrinterModule and create a custom component factory where we pass the requested Printer instance with @BindsInstance annotation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1897494f8f0101a33040d254499f2d2/href">https://medium.com/media/d1897494f8f0101a33040d254499f2d2/href</a></iframe><p>The final test is the most straightforward of all solutions mentioned in this article.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbd6d5fea609994ec7099e7903d2abd7/href">https://medium.com/media/bbd6d5fea609994ec7099e7903d2abd7/href</a></iframe><h3>Conclusion</h3><p>One of the well-known rules in software development is <a href="https://en.wikipedia.org/wiki/KISS_principle">KISS</a>. Keep it simple, stupid. Dagger modules are not an exception. I saw plenty of Android apps based on a single ApplicationComponent and a single ApplicationModule. I recommend avoiding these god modules and separating them into smaller, logical, single-purpose parts. It improves not only the testability of your application but also architectural design.</p><p>All code presented in this article is available at the <a href="https://github.com/skywall/dagger-testing">GitHub repository</a>. Feel free to send me a DM on <a href="https://twitter.com/lukassztefek">Twitter</a> if you have further questions or ideas.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b568d298b0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dagger 2 in Android Dynamic Multimodule project]]></title>
            <link>https://medium.com/@skywall/dagger-2-in-android-dynamic-multimodule-project-c90954630615?source=rss-cd4f6a5bd795------2</link>
            <guid isPermaLink="false">https://medium.com/p/c90954630615</guid>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[dagger-android]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[multi-module-project]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Lukáš Sztefek]]></dc:creator>
            <pubDate>Thu, 29 Oct 2020 07:35:33 GMT</pubDate>
            <atom:updated>2020-10-29T07:35:33.956Z</atom:updated>
            <content:encoded><![CDATA[<p>Multimodule projects are being quickly adopted in the last generation of Android applications. Gradle modules may depend on each other in multiple ways. This article focuses on the approach based on the dynamic-feature modules, where the feature module depends on the main application module and not vice versa. Dependency injection in a multimodule world is not that straightforward as one might expect, and you have to deal with plenty of obstacles.</p><p>Source code related to this article is available in the following <a href="https://github.com/skywall/android-dagger-multimodule">GitHub repository</a>. Since there is a name collision between the Gradle module and the Dagger module, when talking about the Dagger module, an uppercase variant will be used — Module.</p><h3>Project structure</h3><p>The module hierarchy in the described project can be divided into three groups. The first one, <strong>libraries</strong>, is the place for the Android and Kotlin libraries, which are meant to be used in application and feature modules.</p><p><strong>Application</strong> group takes care of the Application class instance and main Activity. Finally, the <strong>features</strong> group contains dynamic modules with UI related logic — Fragments, ViewModels, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*cE9enhpsk3uCqU_byAAE5w.png" /><figcaption>Gradle modules hierarchy</figcaption></figure><p>The :network and :database modules contain IO related logic. They are combined together with :common (strings/dimensions/etc.) into a single data source in the :core module. The module :service takes care of simple Android Service started in feature modules.</p><h3>Dagger Components hierarchy</h3><p>Let&#39;s take a look at hierarchy from Dagger&#39;s perspective. In the center, there is CoreComponent. It combines Modules from library modules and provides library instances into ApplicationComponent, ServiceComponent and feature components, for example HomeComponent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*CLdokAp7tE9eJ2CYgbMqsg.png" /><figcaption>Dagger component hierarchy</figcaption></figure><p>CoreComponent is singleton scoped, which means there is only one instance of this component within the whole application. Since unscoped components are not allowed to depend on scoped component, Application/Service/Feature components are also scoped. Let&#39;s jump to the implementation.</p><h3>Database module</h3><p>This is going to be easy. TheDatabase class itself is just a dummy class, so don&#39;t bother about implementation. There are actually two essential parts. At first, the Dagger Module itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d836f7ca4fce342d0b9169f174262bd/href">https://medium.com/media/2d836f7ca4fce342d0b9169f174262bd/href</a></iframe><p>Next, we need to create a Module&#39;s contract, an interface that tells us what dependencies this particular module provides. We&#39;re going to need this later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54cfc41fbbccc58f17ad8a97c5ba5ac7/href">https://medium.com/media/54cfc41fbbccc58f17ad8a97c5ba5ac7/href</a></iframe><p>Since the network module is almost identical to the database one, we&#39;ll skip it.</p><h3>CoreComponent to the scene</h3><p>CoreComponent combines repository-like Modules within a single component. It is a pretty standard component, with one difference. CoreComponent implements contracts I&#39;ve mentioned before. Why?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac3fcfb06eb410ac304d93adec67662/href">https://medium.com/media/7ac3fcfb06eb410ac304d93adec67662/href</a></iframe><p>When one component depends on another, only types explicitly defined in the component&#39;s interface are provided out of the component. Since HomeComponent depends on CoreComponent, we must provide Database through a method of a contract; otherwise, we will not be able to inject Database instance with HomeComponent. Contracts also have one nice side effect. You can hide some of the provided types if you don&#39;t define them in the contract interface.</p><p>We have our component defined. Now we need to instantiate it and store it somewhere. We need to access CoreComponent from multiple places (Application/feature module/…), so we store it in a Kotlin object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1630c0f14239621edd96db3b061dd85/href">https://medium.com/media/a1630c0f14239621edd96db3b061dd85/href</a></iframe><p>The component is lazily created when requested for the first time, and the instance is kept in the SingletonHolder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1250a535a0e3ea6f216551be339c0a45/href">https://medium.com/media/1250a535a0e3ea6f216551be339c0a45/href</a></iframe><h3>Home (dynamic-feature) module</h3><p>Let&#39;s begin with HomeComponent which connects types provided by CoreComponent and HomeModule. You might notice it doesn&#39;t include a standard inject() method. There is instead the plus() method to create a subcomponent related to some specific fragment, HomeFragment in this case. You can imagine subcomponent as an extension of parent component, so it provides everything that parent does but may add something more. All of the fragments are injected through their own subcomponents and never by HomeComponent itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26ca5c90f5ee536c946db29223f5afe4/href">https://medium.com/media/26ca5c90f5ee536c946db29223f5afe4/href</a></iframe><p>We again face a problem where to store our HomeComponent. Feature modules take care of UI, and they are basically a set of Fragments with related logic. With that in mind, we can hide the component into the base fragment. This solution&#39;s downside is that we need to define a base fragment class for every single feature module, but it is inevitable because feature modules do not depend on each other. Our feature modules are also dynamic, which means you can anytime decide not to include a module in a final APK.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd160ed427ce4dfefad215ef5716295e/href">https://medium.com/media/cd160ed427ce4dfefad215ef5716295e/href</a></iframe><h4>HomeFragment</h4><p>We are almost done. Let&#39;s start with injection. As mentioned before, HomeFragment has its own subcomponent with the inject() method and own Module. HomeFragmentModule provides fragment-related attributes, for example, data from intent, which can be afterward injected by the view model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0ce1bac3d65f153b00c04fe57f8d588/href">https://medium.com/media/a0ce1bac3d65f153b00c04fe57f8d588/href</a></iframe><p>The only missing piece is our HomeFragment. As you might see, we can inject dependencies from all of the modules.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3286169a08cd5e2c414637273a9728ac/href">https://medium.com/media/3286169a08cd5e2c414637273a9728ac/href</a></iframe><h3>Conclusion</h3><p>Dagger is a powerful but complex tool. It might bring some boilerplate but also order into applications we create. I hope this article gave you an overview that dependency injection in a dynamic multimodule project is possible and, at the end of the day, not that hard.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c90954630615" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inject JS into Android’s WebView]]></title>
            <link>https://medium.com/@skywall/inject-js-into-androids-webview-8845fb5902b7?source=rss-cd4f6a5bd795------2</link>
            <guid isPermaLink="false">https://medium.com/p/8845fb5902b7</guid>
            <category><![CDATA[android-trick]]></category>
            <category><![CDATA[webview]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-tracking]]></category>
            <dc:creator><![CDATA[Lukáš Sztefek]]></dc:creator>
            <pubDate>Fri, 27 Dec 2019 20:03:20 GMT</pubDate>
            <atom:updated>2023-01-01T19:40:21.816Z</atom:updated>
            <content:encoded><![CDATA[<p>WebView gives developers an easy way to embed a web-based application into an Android app with almost no code. Just give it some URL, and it works. Things become interesting when you want to track a user’s interaction within the context of WebView.</p><p>This article describes how to implement a mechanism to get notified when the user clicks on the elements of a web page inside WebView. Let’s imagine the following use case. A user of your app can see the documentation of Vuetify framework, and you want to track, how often the user clicks on the Help button on this site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*l32K6RYakSNkv5mzC0uHcQ.gif" /></figure><p>Implementation of the application which displays the predefined web page in a WebView is relatively straightforward:</p><pre>class MainActivity : AppCompatActivity() {<br><br>  override fun onCreate(savedInstanceState: Bundle?) {<br>    super.onCreate(savedInstanceState)<br><br>    val webViewClient = WebViewClient()<br>    val webView = WebView(this)<br><br>    webView.webViewClient = webViewClient<br>    webView.settings.javaScriptEnabled = true<br><br>    setContentView(webView)<br>    webView.loadUrl(&quot;https://vuetifyjs.com/en/getting-started/quick-start&quot;)<br>  }<br>}</pre><h3>The easy way</h3><p>There are two scenarios of how a web app might react to a user click event. The first one is redirection. WebView client has quite an easy way to listen for redirects. You just need to override its method shouldOverrideUrlLoading() .</p><pre>val webViewClient = object : WebViewClient() {<br>  override fun shouldOverrideUrlLoading(<br>    view: WebView, <br>    request: WebResourceRequest<br>  ): Boolean {<br>    // log click event<br>    return super.shouldOverrideUrlLoading(view, request)<br>  }<br>}</pre><p>Unfortunately, this is not the case addressed above. No redirection happens when the Help button is clicked. In this particular case, the button click is processed by the client’s side JavaScript code. Let’s start with some basics.</p><h3>Exposing JS function calls out of WebView</h3><p>There is a way to bridge JavaScript methods called in WebView and your Java/Kotlin code. Let’s consider you have the following HTML code within the shown page:</p><pre>&lt;button onclicked=&quot;Android.onClicked()&quot;&gt;</pre><p>The JavaScript function onClicked() is called on the global object Android, when the button is clicked. From Kotlin code perspective, this function might be binded by addJavaScriptInterface() method:</p><pre>object AndroidJSInterface {<br>    <br>  @JavascriptInterface<br>  fun onClicked() {<br>    Log.d(TAG, &quot;Help button clicked&quot;)<br>  }<br>}<br><br>fun bind(webView: WebView) {<br>  webView.addJavascriptInterface(AndroidJSInterface, &quot;Android&quot;)<br>}</pre><p>Remember that Help button in Vuetify documentation example at the beginning? There is no Android.onHelpClicked() method call within the web page so there is nothing we can bind to. Let&#39;s figure this out.</p><h3>Accessing and modifying DOM</h3><p>In case you are not familiar with <a href="https://www.w3schools.com/whatis/whatis_htmldom.asp">DOM</a>, it stands for Document Object Model. It is an object representation of a whole HTML page as a tree of its elements. You can iterate over these elements, access or modify them:</p><pre>function f() {<br>  var btns = document.getElementsByTagName(&#39;button&#39;);<br>  for (var i = 0, n = btns.length; i &lt; n; i++) {<br>    if (btns[i].getAttribute(&#39;aria-label&#39;) === &#39;Support&#39;) {<br>      btns[i].setAttribute(&#39;onclick&#39;, &#39;Android.onClicked()&#39;);<br>    }<br>  }<br>}</pre><p>The function above finds all &lt;button&gt; elements, and it adds them a new attribute onclick=&#39;Android.onClick()&#39; if that element also has the aria-label=&#39;Support&#39; attribute. The aria-label attribute with the value Support hasn&#39;t been chosen at random. This attribute uniquely identifies the Help button from the very beginning of this article:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*Gfm9Gt0u2AiZ4xxg0JIL4g.png" /></figure><h3>Run JS in WebView</h3><p>The last missing part is to run DOM modifying JavaScript within WebView. Better start with a simple example demonstrating how to call empty JS function:</p><pre>webView.loadUrl(&quot;javascript:(function f() { } )()&quot;)</pre><p>This code will execute JavaScript function f() immediately when loadUrl() is called. Calling loadUrl() repeatedly doesn&#39;t reload WebView&#39;s content, as you might instinctively suggest from a method name.</p><p>In situations where you want to execute JS code when the page is fully loaded, which is almost all of the time, you end up listening to the onPageFinished() callback:</p><pre>val webViewClient = object : WebViewClient() {<br><br>  override fun onPageFinished(view: WebView, url: String) {<br>    view.loadUrl(<br>      &quot;&quot;&quot;javascript:(function f() {<br>        var btns = document.getElementsByTagName(&#39;button&#39;);<br>        for (var i = 0, n = allElements.length; i &lt; n; i++) {<br>          if (btns[i].getAttribute(&#39;aria-label&#39;) === &#39;Support&#39;) {<br>            btns[i].setAttribute(&#39;onclick&#39;, &#39;Android.onClicked()&#39;);<br>          }<br>        }<br>      })()&quot;&quot;&quot;<br>    )<br>  }<br>}</pre><h3>Let’s sum this up</h3><p>That’s it. With this last piece of the puzzle, we are now able to modify HTML code of the viewed page and listen for injected callbacks:</p><pre>class MainActivity : AppCompatActivity() {<br>  object AndroidJSInterface {<br><br>    @JavascriptInterface<br>    fun onClicked() {<br>      Log.d(&quot;HelpButton&quot;, &quot;Help button clicked&quot;)<br>    }<br>  }<br><br>  override fun onCreate(savedInstanceState: Bundle?) {<br>    super.onCreate(savedInstanceState)<br><br>    val webViewClient = object : WebViewClient() {<br>      override fun onPageFinished(view: WebView, url: String) {<br>        loadJs(view)<br>      }<br>    }<br><br>    val webView = WebView(this)<br>    webView.webViewClient = webViewClient<br><br>    webView.settings.javaScriptEnabled = true<br>    webView.addJavascriptInterface(AndroidJSInterface, &quot;Android&quot;)<br><br>    setContentView(webView)<br>    webView.loadUrl(&quot;https://vuetifyjs.com/en/getting-started/quick-start&quot;)<br>  }<br><br>  private fun loadJs(webView: WebView) {<br>    webView.loadUrl(<br>      &quot;&quot;&quot;javascript:(function f() {<br>        var btns = document.getElementsByTagName(&#39;button&#39;);<br>        for (var i = 0, n = btns.length; i &lt; n; i++) {<br>          if (btns[i].getAttribute(&#39;aria-label&#39;) === &#39;Support&#39;) {<br>            btns[i].setAttribute(&#39;onclick&#39;, &#39;Android.onClicked()&#39;);<br>          }<br>        }<br>      })()&quot;&quot;&quot;<br>    )<br>  }<br>}</pre><h3>Final word</h3><p>WebView brings developers many possibilities and power. With immense power also comes big responsibility. The approach demonstrated in this article might potentially create vulnerabilities to the XSS attacks. Make sure you know what you are doing. Also, changes by the owner of a web page might result in your application misbehaving or not working at all.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8845fb5902b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android minSdkVersion = 21 becomes reality]]></title>
            <link>https://medium.com/@skywall/android-minsdkversion-21-becomes-reality-7232f14d9cc2?source=rss-cd4f6a5bd795------2</link>
            <guid isPermaLink="false">https://medium.com/p/7232f14d9cc2</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Lukáš Sztefek]]></dc:creator>
            <pubDate>Tue, 23 Apr 2019 20:20:33 GMT</pubDate>
            <atom:updated>2019-04-23T20:20:33.021Z</atom:updated>
            <content:encoded><![CDATA[<p>In terms of Android Development minSdkVersion = 21 means a lot. From developers perspective it&#39;s almost a holy grail because things start to work without compromises and hours of debugging. Even well-known Twitter account <a href="https://twitter.com/minSdkVersion">minSdkVersion</a> informed that it&#39;s time to switch almost a year ago.</p><h3>minSdkVersion on Twitter</h3><p>21</p><h3>build.gradle updated, what&#39;s next?</h3><p>The most important part behind us, fun ahead. I&#39;m going to list all of the improvements and new APIs we figured out since we finally decided to switch to 21. Let&#39;s start with some UI stuff.</p><h3>(Animated) Vector Drawables</h3><p>You might say “Wait what? Vector Drawables and its animated variants are backported to Android 2.3, aren&#39;t they?“ and you are right. There are indeed few things you need to keep in mind when working with these:</p><ol><li>TextView compound drawables like android:drawableLeft can’t be vectors. You must use android:drawableLeftCompat or set drawables in Java/Kotlin code.</li><li>You can&#39;t use vector directly via itsR.drawable.xxx reference in methods .setSmallIcon() and .setLargeIcon() in NotificationBuilder . Instead of this you have to use VectorDrawableCompat for drawable instance creation.</li><li>You have to enable vector drawables in build.gradle file with <strong>vectorDrawables.useSupportLibrary = true </strong>flag</li></ol><p>Since Android 5.1 there is no longer need to take care of those things.</p><h3>Shared Element Transition</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*2ZIhnFigCJEdJsxxPNTjEw.gif" /><figcaption>Source: android-developers.googleblog.com</figcaption></figure><p>Framework to enable fluid user attention transition from one screen into an another. It is the most commonly used for transition from some sort of list item into an item detail. You can define multiple elements to be translated at once. Shared Element Transition was first introduced <a href="https://android-developers.googleblog.com/2018/02/continuous-shared-element-transitions.html">in this article</a>.</p><p>Prior to Android 5.1 there is no backward compatibility and usage of this framework has no effects in your application&#39;s user interface.</p><h3>Elevation</h3><p><a href="https://material.io/design/environment/elevation.html">Elevation</a> as a principle is part of the Material Design specification. It enables developers to place their UI components on imaginary layers (along z-axis). Elevation defines how layers overlaps and what shadow they cast. There are some backports to older versions, for example<a href="https://developer.android.com/reference/android/support/v7/widget/CardView">CardView</a> , but they aren&#39;t applicable in all cases.</p><pre>&lt;View<br>    android:layout_gravity=&quot;center&quot;<br>    android:layout_width=&quot;30dp&quot;<br>    android:layout_height=&quot;30dp&quot;<br>    android:background=&quot;@color/colorPrimary&quot;<br>    <strong>android:elevation=&quot;10dp&quot;</strong>/&gt;</pre><p>Elevation <em>10dp </em>produces shadow you can see on following image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*jmF5ho-qKCPSW1RzfnUwDQ.png" /><figcaption>View with elevation (left) and without elevation (right)</figcaption></figure><h3>Tint</h3><p>Tinting is simply said applying some colour mask onto some other image, mostly drawable. Result of this process is drawable with all non-transparent pixels coloured with mask colour. Prior to API 21 there are a lot of news in this area. You might specify background tint at every View through method <a href="https://developer.android.com/reference/android/view/View.html#attr_android:backgroundTint">setBackgroundImageTintList()</a> or sibling attribute android:backgroundTint respectively. There are no more issues with tints specified as ColorStateList (or colour selector) especially in case of ImageView .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*ugBd_PXPkvd9Gb9hsdFWXg.png" /></figure><h3>Ripple effect</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*eITcRWVsArN8X1SGAKJccA.gif" /></figure><p>Ripple works! There is no longer need to define multiple drawable xml selectors for your buttons in res/drawable-v21/ and res/drawable/ . There is also a new way to create <a href="https://developer.android.com/reference/android/graphics/drawable/RippleDrawable">RippleDrawable</a> dynamically in Java/Kotlin.</p><pre>val color : ColorStateList = ColorStateList.valueOf(Color.<em>BLUE</em>)<br>val content : Drawable? = null<br>val mask : Drawable? = null<br>textView.<em>background </em>= RippleDrawable(color, content, mask)</pre><h3>Camera2 API</h3><p>Brand new camera handling API is ready to be used. In case you come into contact with the old one, you might remember how fragile and buggy Camera API was. New API solves many of these issues and brings cool new features like support for burst capture mode, native RAW images capture, auto-white balance (AWB) control, exposure control etc. Nathan Vogt has written <a href="https://willowtreeapps.com/ideas/camera2-and-you-leveraging-android-lollipops-new-camera">very nice summary</a> about Camera2 API.</p><h3>Bluetooth LE</h3><p>Bluetooth Low Energy was in theory supported since Android 4.3, but there was basically single framework to handle both Bluetooth Classic and BLE. <a href="https://developer.android.com/reference/android/bluetooth/le/package-summary">New package</a> com.android.bluetooth.le contains BLE related logic separated into the new module and takes care of scan and advertise phases of a communication. There is no longer need to parse advertising packets on your own etc.</p><p>Prior API version 21 we can truly mark BLE as supported and relatively easy to use. Besides that things gets even better since API 23, but thats another story.</p><h3>Conclusion</h3><p>This article gives you brief summary of developer related changes after minSdkVersion change to 21 (Android 5.1). List provided here obviously don&#39;t contain all of the changes, but focuses on most significant ones.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7232f14d9cc2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dismiss Activity like a Pro with Hauler]]></title>
            <link>https://medium.com/@skywall/dismiss-activity-like-a-pro-with-hauler-f91e67b5def5?source=rss-cd4f6a5bd795------2</link>
            <guid isPermaLink="false">https://medium.com/p/f91e67b5def5</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[gesture-recognition]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Lukáš Sztefek]]></dc:creator>
            <pubDate>Fri, 30 Nov 2018 15:51:38 GMT</pubDate>
            <atom:updated>2023-01-01T19:43:10.455Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mf_6GCty9DLSL000O4qW2g.png" /></figure><p>Responsive and animated UI elements are more and more significant attributes of nowadays apps. We have recently faced a task: <strong>Implementing swipe to dismiss gesture</strong> of an Activity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/213/1*UhXA27O9vmkyRJQNf3zpKg.gif" /></figure><p>Our solution is based on application called <a href="https://github.com/nickbutcher/plaid">Plaid</a>. It&#39;s an app developed by Googlers which serves as a showcase for Material Design, solid architecture, modularisation etc.</p><p>We were so satisfied with our solution, that we made it <a href="https://github.com/thefuntasty/hauler">open source</a> and <strong>available for everyone </strong>as a standalone component. <a href="https://github.com/thefuntasty/hauler#download">Download instructions</a> are available in our Github repository. How can you use it in your project?</p><h3>HaulerView on a stage</h3><p>Most of the dismiss gesture work runs inView called HaulerView . It is basically FrameLayout that handles nested scroll events from its direct child. This child can be NestedScrollView or RecyclerView . The layout of an Activity you want to dismiss might look like this:</p><pre>&lt;com.thefuntasty.hauler.HaulerView<br>        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>        android:id=&quot;@+id/haulerView&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;&gt;<br><br>    &lt;androidx.core.widget.NestedScrollView<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:fillViewport=&quot;true&quot;&gt;<br>            <br>            &lt;!-- your layout--&gt;<br>            <br>    &lt;/androidx.core.widget.NestedScrollView&gt;<br>&lt;/com.thefuntasty.hauler.HaulerView&gt;</pre><p>Our layout is ready, let&#39;s move on and make our Activity floating and translucent. We can achieve this with the following theme attributes of the Activity :</p><pre>&lt;style name=&quot;AppTheme.Draggable&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;<br>       &lt;item name=&quot;android:colorBackgroundCacheHint&quot;&gt;@null&lt;/item&gt;<br>       &lt;item name=&quot;android:windowContentOverlay&quot;&gt;@null&lt;/item&gt;<br>       &lt;item name=&quot;android:windowIsFloating&quot;&gt;false&lt;/item&gt;<br>       &lt;item name=&quot;android:windowIsTranslucent&quot;&gt;true&lt;/item&gt;<br>       &lt;item name=&quot;android:windowNoTitle&quot;&gt;true&lt;/item&gt;<br>       &lt;item name=&quot;android:windowBackground&quot;&gt;#99323232&lt;/item&gt;<br>&lt;/style&gt;</pre><p>Do not forget to apply the theme via android:theme attribute to the Activity which is meant to be dragged in AndroidManifest.xml file.</p><pre>&lt;activity<br>        android:name=&quot;.draggable.SimpleUsageActivity&quot;<br>        android:theme=&quot;@style/AppTheme.Draggable&quot;/&gt;</pre><p>We are almost there. Gesture and Activity animations work as expected, but nothing happens when dismiss is performed. Simply, set OnDragDismissedListener on your HaulerView and we are done:</p><pre>override fun onCreate(savedInstanceState: Bundle?) {<br>        // ...<br><br>        haulerView.setOnDragDismissedListener {<br>            finish() // finish activity when dismiss performed<br>        }<br>}</pre><h3>What&#39;s next?</h3><p>This article gives you a basic understanding of how to integrate HaulerView to your project. Moreover, you can customize dismiss animation scale, elasticity and distance through a few predefined XML attributes. Take a look at our <a href="https://github.com/thefuntasty/hauler/tree/master/sample">sample application</a> or <a href="https://github.com/thefuntasty/hauler">repository</a> if you want to learn more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f91e67b5def5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Design — Quick intro to VectorDrawables]]></title>
            <link>https://medium.com/@skywall/android-design-quick-intro-to-vectordrawables-a7ad708d6f03?source=rss-cd4f6a5bd795------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7ad708d6f03</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Lukáš Sztefek]]></dc:creator>
            <pubDate>Thu, 09 Feb 2017 10:13:25 GMT</pubDate>
            <atom:updated>2023-01-01T19:46:17.681Z</atom:updated>
            <content:encoded><![CDATA[<p>We have recently started to use <strong>VectorDrawable</strong> in our projects. There are plenty of articles about it, but if you’re starting from scratch, you might not know where to begin. This article give you minimal knowledge you need to have, if you want to use them in your project.</p><p>VectorDrawables are fully supported since Lollipop (API 21). Minimal SDK version specified in your project’s build.gradle file probably contains value 16 (95% of devices). What does it cost to use vectors on Jelly Bean devices and newer?</p><h3>Gradle Project Setup</h3><p>At first, add these statements to your module <strong>build.gradle</strong> file:</p><pre>android {<br>  defaultConfig {<br>    vectorDrawables.useSupportLibrary = true<br>    vectorDrawables.generatedDensities = []<br>  }<br> <br>  aaptOptions {<br>    additionalParameters “ — no-version-vectors”<br>  }<br>}</pre><pre>dependencies {<br>  compile “com.android.support:support-vector-drawable:25.1.1”<br>}</pre><h3>The designer sent me the icon.SVG file. Sooo ….</h3><p><strong>SVG</strong> files are not directly supported (except 3rd party libraries) and they must be converted to <strong>Android-friendly XML variant</strong>. Android Studio has a tool to import SVG files and you can find it here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*sGIhLvUFQyJtZFox16zr6A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*BUn8NSaUj1Z36AFvNPA-hA.png" /><figcaption>Tool to import SVG files and generate particular XML</figcaption></figure><p>All generated files are automatically placed to common <strong>/res/drawable</strong> folder, no matter what screen density they belong to (because now they belong to all of them!).</p><p>At this moment, you have your <strong>ic_icon.xml</strong> in project. It should look like this:</p><pre>&lt;?xml version=”1.0&quot; encoding=”utf-8&quot;?&gt;<br>&lt;vector<br>   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>   android:width=&quot;24dp&quot;<br>   android:height=&quot;24dp&quot;<br>   android:viewportHeight=&quot;24&quot;<br>   android:viewportWidth=&quot;24&quot;&gt;<br>   &lt;path<br>      android:fillColor=&quot;#8998a7&quot;<br>      android:pathData=&quot;M10,6L8.59,7.41 13.17,12l-4.58,4.59L10,18l6,-6z&quot;/&gt;<br>&lt;/vector&gt;</pre><h3>Graphics imported, now I want to …</h3><h4>… set icon.xml to ImageView in XML</h4><pre>&lt;ImageView<br>   android:layout_width=”wrap_content”<br>   android:layout_height=”wrap_content”<br>   app:srcCompat=”@drawable/ic_icon”/&gt;</pre><p>That was quick.</p><h4>… change color of ImageView icon in XML</h4><pre>&lt;ImageView<br>   android:layout_width=”wrap_content”<br>   android:layout_height=”wrap_content”<br>   app:srcCompat=”@drawable/ic_icon”<br>   android:tint=”@color/yellow”/&gt;</pre><p>Every colored part of your icon except fully transparent places now has color specified in <strong>android:tint</strong> attribute.</p><h4>… load VectorDrawable in Java code</h4><pre>Drawable vectorDrawable = VectorDrawableCompat.create(<br>                              getResources(), <br>                              R.drawable.ic_icon, <br>                              null);</pre><h4>… change color of VectorDrawable in Java</h4><pre>Drawable drawable = DrawableCompat.wrap(vectorDrawable);<br>DrawableCompat.setTint(drawable.mutate(), Color.WHITE);<br>imageView.setImageDrawable(drawable);</pre><p>Two things to explain. What is <em>wrap()</em>? This makes your drawable available for tint on different API levels.</p><p>And what about <em>mutate()</em>? Drawables are cached across application. If you load one of your icons at two places and tint one of them, both Drawables will be affected by the new color. To suppress this behavior, you need to say “I want to change only this instance of a Drawable” and that’s basically what <em>mutate()</em> does.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*tYObiuyEeCV35khlpfOdeA.png" /></figure><h4>… change the color of ImageView icon in Java</h4><p>Since ImageView methods <em>setImageTint()</em> and <em>setImageTintMode()</em> are available from Lollipop (API 21), we have to use another way:</p><pre>ImageView iv = (ImageView) findViewById(R.id.image_view);<br>iv.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN);</pre><p>Second argument of <em>setColorFilter()</em> method <strong>PortDuff.Mode</strong> defines how is specified color combined with the actual icon in ImageView. In most cases, you’re fine with <strong>SRC_IN</strong> mode.</p><p>If you are not familiar with PorterDuff.Mode at all, you should definitely take a look at <a href="http://ssp.impulsetrain.com/porterduff.html">this great article</a> from Soren Sandmann.</p><h3>Pains you should be aware of</h3><h4>fill-rule=”evenodd”</h4><p>Your designer probably uses Photoshop or Sketch to create a designs. He can specify fill rule of a shape to <strong>nonzero</strong> or <strong>even-odd</strong>. This settings causes self-intersecting shapes render differently. Unfortunately, even-odd rule is supported since Marshmallow (API 23).</p><p>Luckily, there is one <a href="http://a-student.github.io/SvgToVectorDrawableConverter.Web/">online tool</a> what allows you to convert SVG icons based on even-odd rule. Just check ” — <strong>fix-fill-type”</strong> flag on the top of the tool page. This solution uses some <em>complex</em> math to generate vectors without mentioned unsupported rule.</p><p>If you want to know more about this problem <a href="https://medium.com/sketch-app-sources/exporting-svgs-in-sketch-for-android-169ee8dae603#.7n3cr2yy0">you can continue here</a>.</p><h4>ColorStateList (color selectors) in android:tint attribute</h4><p>It is working since Lollipop (API 21). In case you want to change drawable tint based on View’s state (<em>selected</em>, <em>pressed</em>, …) you need to care of it for yourself eg. by switching different drawables via <strong>app:srcCompat</strong> attribute or applying tint directly in Java code.</p><h3>Is it worth it?</h3><p>Definitely. Once you go vector, there is no way back. You get smaller APK size, perfectly sharp icons, no more mess with multiple <strong>PNG</strong>’s for different densities and some great animations, but that is different story…</p><h3>Read more</h3><ul><li><a href="http://stackoverflow.com/questions/33696488/getting-bitmap-from-vector-drawable">http://stackoverflow.com/questions/33696488/getting-bitmap-from-vector-drawable</a></li><li><a href="https://commonsware.com/Android/previews/vector-drawables">https://commonsware.com/Android/previews/vector-drawables</a></li><li><a href="https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html">https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7ad708d6f03" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Testing — Spy object]]></title>
            <link>https://medium.com/@skywall/android-testing-spy-object-2e161e2011d0?source=rss-cd4f6a5bd795------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e161e2011d0</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-testing]]></category>
            <dc:creator><![CDATA[Lukáš Sztefek]]></dc:creator>
            <pubDate>Mon, 05 Dec 2016 12:25:49 GMT</pubDate>
            <atom:updated>2016-12-05T16:05:08.705Z</atom:updated>
            <content:encoded><![CDATA[<p>You are probably familiar with mock objects. If not here is a quick brief. These objects has no real implementation, they just represents the skeleton of an object (public methods) as they are seen by other objects. To use this in your tests, just add <strong>@Mock</strong> annotation before the object declaration.</p><p>Imagine a simple presenter test:</p><pre>@RunWith(MockitoJUnitRunner.class)<br>public class ProfilePresenterTest {</pre><pre>   @Mock User mockUser;</pre><pre>   private ProfilePresenter profilePresenter;</pre><pre>   @Test<br>   public void presenter_withShowUser_avatarRequested() {<br>      profilePresenter = new ProfilePresenter(mockUser);<br>      profilePresenter.showUser();<br>      <em>verify</em>(mockUser).getAvatar();<br>   }<br>}</pre><p>In this test we are able to verify if the method <em>getAvatar()</em> has been called during the execution of the <em>showUser()</em> on the presenter. This approach is quite satisfying in most cases.</p><p>But, what to do if you want to run test on real User object, but still do some verifications? You can’t simply verify real object method calls via <em>verify(realUser).getAvatar(); </em>Attempting to do this leads to exception:</p><pre>org.mockito.exceptions.misusing.NotAMockException: <br>Argument passed to verify() is of type User and is not a mock!</pre><h3>Time for Spies</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*jUW3h57ySA67su6L2nCsQg.jpeg" /></figure><p>Spy creates a wrapper around a real object instance and you can continue in doing verifications that you need. Similarly as Mock, Spy object can be created with <strong>@Spy</strong> annotation or via Mockito static method <em>spy().</em></p><pre>@RunWith(MockitoJUnitRunner.class)<br>public class ProfilePresenterTest {</pre><pre>   @Spy User spyUser = User.create();</pre><pre>   private ProfilePresenter profilePresenter;</pre><pre>   @Test<br>   public void presenter_withShowUser_avatarRequested() {<br>      profilePresenter = new ProfilePresenter(spyUser);<br>      profilePresenter.showUser();<br>      <em>verify</em>(spyUser).getAvatar();<br>   }<br>}</pre><p>Interesting, isn’t it? And that’s not all. A spy allows you to override (stub) some of the real object methods. Let’s say you want to return your own Avatar object when <em>getAvatar()</em> is called:</p><pre>Mockito.doReturn(Avatar.custom()).when(spyUser).getAvatar();</pre><p>Rest of the object preserves its default (real) behavior.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e161e2011d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>