<?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 Alexander Shevelev on Medium]]></title>
        <description><![CDATA[Stories by Alexander Shevelev on Medium]]></description>
        <link>https://medium.com/@al-e-shevelev?source=rss-c87d941490a7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D1wo8CEWYqpnVT68OREFOg.png</url>
            <title>Stories by Alexander Shevelev on Medium</title>
            <link>https://medium.com/@al-e-shevelev?source=rss-c87d941490a7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 22:12:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@al-e-shevelev/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[Bend me: Developing an Android app with support for foldable screens]]></title>
            <link>https://al-e-shevelev.medium.com/bend-me-developing-an-android-app-with-support-for-foldable-screens-bf54c241ebcc?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf54c241ebcc</guid>
            <category><![CDATA[foldable-smartphone]]></category>
            <category><![CDATA[foldable-phone]]></category>
            <category><![CDATA[foldable]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Sun, 01 Nov 2026 14:45:07 GMT</pubDate>
            <atom:updated>2026-11-01T14:45:07.249Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*tQayKTdqrWts9l39LdqQkg.jpeg" /></figure><p>Recently, phones with flexible screens have been becoming more and more popular (at least in the Android world). That’s exactly why I decided to include support for this feature in my latest project as well.</p><p>In this article, I’d like to explain in detail how to add support for flexible screens to your app.</p><p>Let’s try writing a simple app that consists of two areas on a single screen. The main area will take up the entire screen when it’s fully unfolded (or if the phone doesn’t have a flexible screen). In our case, it will simply display a selected color.</p><p>The second area on the screen is a control panel with three colored buttons that will switch the color of the main area. It will only be visible when the screen is in a half-folded state.</p><p>This is how it will look on a phone with a horizontal hinge (such as the Samsung Galaxy Z Flip series and others).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FRrQxGIT4uHc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fshorts%2FRrQxGIT4uHc%3Ffeature%3Dshare&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FRrQxGIT4uHc%2Fhq2.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/294c8de8c4373765be8deb4d6704aa77/href">https://medium.com/media/294c8de8c4373765be8deb4d6704aa77/href</a></iframe><p>And here’s how our app will look on a phone with a vertical hinge (for example, a phone from the Google Pixel Fold lineup).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FioSevPgtVUQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DioSevPgtVUQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FioSevPgtVUQ%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/112ff2a8e94c1cc97da44c3baafe503f/href">https://medium.com/media/112ff2a8e94c1cc97da44c3baafe503f/href</a></iframe><p>Let’s make one thing clear right away — our app will support only phones with a single hinge, since that accounts for the vast majority of models. However, you can adapt the approach described below to phones with multiple hinges — it’s not that difficult.</p><h3>The Boring Part</h3><p>So, our app will consist of two areas — one for displaying the color and the other for selecting it. Before we move on to handling the screen fold, let’s write the code for these areas.</p><h4>Color Repository</h4><p>To store information about the selected color, we’ll create a very simple repository that will be injected into both areas as a singleton via <em>Hilt</em>. Here’s the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fcceb5e138ea765b7535ae253f0375f/href">https://medium.com/media/5fcceb5e138ea765b7535ae253f0375f/href</a></iframe><p>As you can see from the code, all the repository does is store color data and update the selected color.</p><h4>Utility Code</h4><p>To make this work, we’ll need an enum with a hinge type — here it is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fc9a169460d9267c2945b34ec64a6d7/href">https://medium.com/media/4fc9a169460d9267c2945b34ec64a6d7/href</a></iframe><p>And here’s an extension to convert the color code into a Compose color:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6ec9a0dbfaec4dd7c5c885894191cf2/href">https://medium.com/media/b6ec9a0dbfaec4dd7c5c885894191cf2/href</a></iframe><h4>Color Display</h4><p>In the color display area, we’ll use a simple ViewModel. Here’s its code along with its state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/841dcd824cf0873aee5c81f2b82ce96d/href">https://medium.com/media/841dcd824cf0873aee5c81f2b82ce96d/href</a></iframe><p>All it does is convert the repository’s state into the view’s state. And here’s our view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62895412e6698e441e117542e1303b13/href">https://medium.com/media/62895412e6698e441e117542e1303b13/href</a></iframe><p>The code speaks for itself — we subscribe to the model’s state and display it on the screen.</p><h4>Color Selection</h4><p>The color selection panel is structured in a slightly more complex way. It also uses a ViewModel, which looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f5a762d0efce04d27e135b96e71f9af/href">https://medium.com/media/4f5a762d0efce04d27e135b96e71f9af/href</a></iframe><p>It converts the repository state into the view state (here we’ll also need a list of colors to display on the buttons) and updates the selected color in the repository.</p><p>And here is the view code for the panel:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aec39b682a9038fa9953c57092c402ed/href">https://medium.com/media/aec39b682a9038fa9953c57092c402ed/href</a></iframe><p>As you can see, the panel subscribes to the model’s state, displays the color selection buttons (in a column or a row, depending on whether the phone is folded horizontally or vertically), and, when a button is clicked, passes the code for the selected color to the ViewModel.</p><h3>And this is where the magic begins</h3><p>And now, finally, we’re getting to the most intriguing part — supporting the foldable screen.</p><h4>MainActivity</h4><p>The screen areas described above are hosted in our app’s MainActivity. Let’s take a look at the entire code, and then break it down line by line. For now, we can skip everything related to <em>LookaheadScope</em> — we’ll cover that in the last section of this article.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eec4de108b3a06e2367e88b2cdfeea21/href">https://medium.com/media/eec4de108b3a06e2367e88b2cdfeea21/href</a></iframe><p>So, in lines 1–5, we create rulers that our areas will use as guides when they’re laid out on the screen. The rulers will contain the values of the hinge boundaries.</p><p>Each hinge is a rectangular area on the phone’s screen. We don’t want our areas to end up inside a hinge when they’re positioned (since that looks unattractive), which is exactly why we need to know the hinge boundaries. And we’ll store the values of these boundaries in the rulers.</p><p>Since we support both horizontal and vertical bending, we need 4 rulers — two for each hinge. We’ll initialize their values later in the code.</p><p>In lines 17–23, we obtain the rectangular hinge boundaries — separately for the horizontal hinge and for the vertical one. In doing so, we select only the first seam, if it exists.</p><p>In lines 34–44, for each hinge individually, we calculate its boundaries within the layout using the <em>windowToLocal</em> function and assign the resulting values to the rulers. Later, we’ll be able to read these values from the rulers and use them to position our areas on the screen.</p><p>Finally, in lines 49–55, we call the code for our screen, which directly contains our areas. We pass data about the seam type to the screen.</p><h4>Auxiliary modifiers for positioning controls on the screen</h4><p>Before moving on to the main screen’s code, let’s create some helper modifiers that will be used to position our areas on the screen.</p><p>Let’s start with the modifier that places a visual element above a horizontal seam. Here is the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0880b341dc48030af33e2147d1f5afa/href">https://medium.com/media/b0880b341dc48030af33e2147d1f5afa/href</a></iframe><p>As you can see, we read the value of the hinge’s top border (if it exists) and calculate the position and size of the displayed area based on this data.</p><p>Similarly, to place a control below a horizontal seam:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7cc9fa48956488e76947650ee15d67b/href">https://medium.com/media/d7cc9fa48956488e76947650ee15d67b/href</a></iframe><p>To place a control to the left of a vertical seam, the code looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b6f25b539b46d2fc9123d78d043ae24/href">https://medium.com/media/8b6f25b539b46d2fc9123d78d043ae24/href</a></iframe><p>And finally, the last modifier, for placing a control to the right of a seam:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28f1474913e89740ac317e0e098ae834/href">https://medium.com/media/28f1474913e89740ac317e0e098ae834/href</a></iframe><h4>MainScreen</h4><p>Here is the main screen where our areas are placed. I’ll provide the code in its entirety, and then we’ll break it down line by line. As with MainActivity, we won’t cover anything related to <em>LookaheadScope</em> just yet — we’ll get to that a little later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6fc4b7ae6f40a5da52e3304f650ed4d/href">https://medium.com/media/d6fc4b7ae6f40a5da52e3304f650ed4d/href</a></iframe><p>In lines 7–9, we retrieve information about the phone’s screen state. This is a universal method for determining this state; it works for both phones that fold horizontally and those that fold vertically.</p><p>Using this data, in lines 16–24, we position the main area using the modifiers we wrote earlier.</p><p>Similarly, in lines 30–49, the control panel with color selection buttons is animated into display — depending on the orientation, it appears either at the bottom or on the right.</p><h3>Animated Bounds</h3><p>There’s one last point to cover. If you watched the video at the beginning of the article carefully, you should have noticed that the bottom or right border of the main area (depending on the folding type) changes its position with an animation. This is achieved by using <em>LookaheadScope</em>, which we’ve skipped over until now.</p><p>All we need to do to achieve this effect is to wrap our code in a <em>LookaheadScope</em> (see MainActivity, line 25) and pass it to the <em>animateBounds</em> modifier of the element whose bounds we want to animate (see MainScreen, lines 5 and 26).</p><h3>Conclusion</h3><p>That’s it — now you know how to implement support for foldable screens in your app.</p><p>I’d like to remind you once again that in the code above, we’re working with only one hinge, specifically the first one (fortunately, most phones have only the one). If you need support for multiple hinges, you can do so by changing the logic in MainActivity on lines 17–23 and further down in the code.</p><p>The complete source code for this article can be found <a href="http://github.com/AlShevelev/FoldableDemo">in this repository</a>.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf54c241ebcc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recognizing User Gestures on a Phone Screen]]></title>
            <link>https://al-e-shevelev.medium.com/recognizing-user-gestures-on-a-phone-screen-f150e043eb3e?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f150e043eb3e</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[gesture-recognition]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Sun, 27 Sep 2026 17:41:49 GMT</pubDate>
            <atom:updated>2026-10-08T14:40:42.760Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*q1Vk53EnVcxQSpwXsMpkYw.jpeg" /></figure><p>While working on my own indie game project, I ran into an interesting challenge: I needed to recognize user gestures on a smartphone screen. It was important not only to identify the shape of the gesture but also to accurately calculate how much the drawn gesture deviated from the ideal template. Initially, I tried to solve this problem using custom-built static neural networks. However, despite my efforts, I was unable to achieve stable and reliable results.</p><p>During my further research, I stumbled upon an algorithm called <a href="https://depts.washington.edu/acelab/proj/dollar/index.html">“$1 Unistroke Recognizer,”</a> which ultimately solved my problem elegantly and effectively. In this article, I want to share my experience and discuss this algorithm in detail from a practical perspective.</p><h3>A Brief Overview of the Algorithm</h3><p>What makes the “$1 Unistroke Recognizer” so appealing? First and foremost, it stands out for its simplicity and high reliability. Unlike complex neural networks that require large datasets, this algorithm needs only a single example to learn to recognize a specific gesture. It’s important to understand that it analyzes the dynamics of the movement itself, rather than the final image. Therefore, shapes that look exactly the same but are drawn from different starting points or in different directions will be interpreted by the algorithm as completely different gestures.</p><p>The process of working with the algorithm is divided into two main stages. The first is the training phase, which is carried out only once. At this stage, we define reference gestures, and the algorithm stores their parameters in its database. The second stage is the recognition phase itself. During this phase, the algorithm processes user input “on the fly,” compares it with the stored data, and calculates with high accuracy exactly which gesture was drawn.</p><h3>Training</h3><p>Let’s take a look at the steps of the training phase for a single reference gesture.</p><h4>Step 1</h4><p>The reference gesture is fitted into a square of a certain size (I used a 251 x 251 square), distorting its proportions if necessary.</p><h4>Step 2</h4><p>The reference gesture is traced from the starting point to the end point, and its total length is calculated.</p><h4>Step 3</h4><p>In this step, the coordinates of the points along the path are calculated (usually 32 or 64 of them) so that the starting point coincides with the starting point of the gesture, the ending point with the ending point, and the remaining points are distributed along the gesture path as evenly as possible (this is precisely why we need to know the total length of the gesture path).</p><p>The figure below shows an example of how to construct keypoints.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oK7uhpB1DQLkVlWy-bffVQ.png" /><figcaption>Example for 25 points on a reference gesture</figcaption></figure><h4>Step 4</h4><p>The coordinates of the resulting points, relative to one of the corners of the square (traditionally the top-left corner), are saved — training is complete.</p><h3>Recognition</h3><p>The recognition process also consists of several steps:</p><h4>Step 1</h4><p>The gesture drawn by the user is fitted into a square of the same size as the reference gestures (with distorted proportions if necessary).</p><h4>Step 2</h4><p>Key points are calculated for the gesture in exactly the same way as in the training phase (steps 3 and 4).</p><h4>Step 3</h4><p>In this step, the system iterates through the sets of stored reference gesture points.</p><p>For each reference gesture point, the geometric distance to its corresponding point in the gesture being recognized is calculated (remember that both gestures are inscribed in a square of the same size). These distances are added together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l9O-9vVkE74ZJAzTCpZtYg.png" /><figcaption>The figure shows an example of this process. In this case, we need to find the distance from point 1 of the reference gesture to point 1 of the recognized gesture and repeat this for all 25 points.</figcaption></figure><h4>Step 4</h4><p>The smallest of the resulting total distances is selected — it will correspond to the recognized gesture.</p><h3>Clarification</h3><p>As mentioned above, the algorithm has only two phases: training and recognition. In principle, this is sufficient if the goal is to guarantee at least some result.</p><p>The problem is that with this approach, one of the reference patterns will always match the user’s gesture, regardless of what the user actually drew.</p><p>Therefore, we need to distinguish situations where a gesture is considered unrecognized.</p><p>To solve this problem, through trial and error and the search process, I arrived at the following formula:</p><p><strong><em>F = (D / P) / (0.5 * SD)</em></strong></p><p>where:<br><strong><em>F (“fuzziness”)</em></strong> is the resulting value that indicates how accurately the gesture was recognized;<br><strong><em>D</em></strong> is the total distance between points for the selected reference;<br><strong><em>P</em></strong> is the number of points;<br><strong><em>SD</em></strong> is the length of the diagonal of the square in which the gesture was inscribed.</p><p>If <strong><em>F &gt; 0.2</em></strong>, the gesture is considered unrecognized. I obtained the coefficient of <strong><em>0.2</em></strong> through trial and error during my experiments.</p><h3>Evaluation of how closely a recognized gesture matches the reference</h3><p>For my indie project, I need not only to recognize gestures but also to quantitatively determine how closely they match the reference (certain game mechanics depend on this). For convenience in further use, this number should be in the range [0, 1], where 1 means that the gesture perfectly matches the reference.</p><p>At the same time, I need the value to be close to 1 rather than 0 in most cases — that is, I need to tolerate some user errors when drawing gestures.</p><p>As a result, I arrived at the following formula (I use Kotlin’s mathematical functions in it):</p><p><strong><em>R = (1 — (F / 0.2).coerceIn(0, 1)).pow(U)</em></strong></p><p>where:<br><strong><em>R</em></strong> is the evaluation result in the range from 0 to 1;<br><strong><em>F</em></strong> is the result from the previous formula;<br><strong><em>0.2</em></strong> is the cutoff threshold for fuzziness from the previous section;<br><strong><em>U</em></strong> is the nonlinearity coefficient. In my case, its value is <strong><em>0.25</em></strong>. The smaller this value is, the more carelessly the user can draw a gesture.</p><p>If you are not familiar with the Kotlin language, then: the <strong><em>coerceIn()</em></strong> function converts the number it is called on to a specified range; the <strong><em>pow()</em></strong> function raises the number it is called on to the power specified in brackets.</p><h3>Automating Training</h3><p>As mentioned above, for the algorithm to work, you need a set of reference gestures during the training phase. It would be convenient if these gestures could be specified in a visual way. And it would be good to automate the training process itself.</p><p>I solved the first part of the problem by representing the gestures as images like these:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CuuJoK8ALZyZgPplZo_naw.png" /></figure><p>In these images, the gestures are already inscribed within a square of the appropriate size. The red dots specify the traversal order — the images are traversed in descending order of the <strong><em>R</em></strong> value in the dot’s color.</p><p>As for automation, I achieved it simply by using AI to generate a Python script that processes these images, identifies key points, and saves them to files (more on this in the next section).</p><h3>Source Code</h3><p>To debug this approach, I used the code available in my repository <a href="https://github.com/AlShevelev/GestureRecognition">here</a>.</p><p>It consists of two folders: Parsing and Recognition.</p><p>The Parsing folder contains reference images of the gestures I need and a generic Python script that handles the training phase — it analyzes these images, calculates keypoints, and writes them to files.</p><p>The Recognition directory contains a simple Android app that, based on the data from the training phase, recognizes the user’s gestures on the phone screen using the approaches I described above.</p><p>You are free to use this code in your own projects.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f150e043eb3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Trench Warfare” is now available on Google Play]]></title>
            <link>https://al-e-shevelev.medium.com/trench-warfare-is-now-available-on-google-play-088d213d1b76?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/088d213d1b76</guid>
            <category><![CDATA[games]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Tue, 18 Aug 2026 17:38:52 GMT</pubDate>
            <atom:updated>2026-08-18T17:38:52.002Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*2pP1bRqOsSkLvw8VrdTZMA.jpeg" /></figure><p>And so, the long journey has come to an end — my game “Trench Warfare”, has been <a href="https://play.google.com/store/apps/details?id=com.shevelev.trench_warfare">released on Google Play</a>.</p><p>“Trench Warfare” is a completely free turn-based strategy game set during World War I, featuring sprite-based graphics, written in Flutter using the Flame game engine.</p><p>I developed this with two goals in mind: I wanted to learn Flutter, and it was simply a challenge — to see if I could do it at all. In the end, I succeeded and brought the project to release.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U0BbrGbxDmxSpKqB_3930g.jpeg" /><figcaption>The game board at different scales</figcaption></figure><p>The game features 13 maps depicting the most significant battles of the Great War. You can play as either side of the conflict, giving you the chance to replay history — albeit virtually. For example, playing as Germany, you might succeed in capturing Verdun, while playing as Russia, you might avoid defeat at Tannenberg.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rqShzEaVkJqm5Q8tEEZk7w.jpeg" /><figcaption>Strategic decision-making window</figcaption></figure><p>The most challenging part of development was getting the computer to play — that is, writing the AI. When I started, I didn’t even understand how to approach this task. In the end, I figured it out, wrote the code, and even <a href="https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-1-getting-started-and-misconceptions-e991cd28d4d3">published a series of four articles</a>. In reality, the computer doesn’t play particularly well, but it’s entertaining — I’ll put it that way. There are obvious flaws, but they’re not related to the AI itself, but rather to some simplifications I made in the computer’s move-making process (since I was trying to save time, and from the very beginning, the task of writing game AI seemed very difficult to me). I know what needs to be rewritten and how, but I’m not sure I’ll actually do it.</p><p>As I’ve already mentioned, I made this game as a hobby and to learn about the technology. That’s why I decided to make the source code open — you can find it <a href="https://github.com/AlShevelev/TrenchWarfare">right here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=088d213d1b76" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Drawing an animated pie chart in Jetpack Compose]]></title>
            <link>https://al-e-shevelev.medium.com/drawing-an-animated-pie-chart-in-jetpack-compose-6b89bac94071?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b89bac94071</guid>
            <category><![CDATA[diagrams]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[drawing]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Sat, 15 Nov 2025 20:48:13 GMT</pubDate>
            <atom:updated>2025-11-15T20:48:13.349Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*YPSTlhLIBFPFh0vJ4KgQhw.png" /></figure><p>A few days ago, in one of my work projects, I needed to draw an animated pie chart in Jetpack Compose. The chart contained arbitrary sectors, each of which was colored with its own color. In this article, I want to tell you how to solve this problem.</p><h4>What we want to achieve</h4><p>First, let’s take a look at what we want to achieve in the end. Here’s what we should end up with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*3pWtQdU-K0PBusvFYLaSDQ.gif" /></figure><h4>Data structure for a single sector</h4><p>Let’s define the data structure describing one of the sectors in the diagram.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08a52ff0d9daa02175c25bb5c58b8f9c/href">https://medium.com/media/08a52ff0d9daa02175c25bb5c58b8f9c/href</a></iframe><p>This is a regular data class with three properties. The <em>from</em> and <em>to</em> properties take values from 0 to 1 and set the boundaries of a specific sector on the chart. The <em>color</em> property defines the color of the sector.</p><h4>Drawing a diagram</h4><p>Let’s take a look at the code for drawing a diagram.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a91fac883e9e7bfa2ef15527da883f5/href">https://medium.com/media/7a91fac883e9e7bfa2ef15527da883f5/href</a></iframe><p>As you can see, the code is quite simple. Code in lines 9-20 creates an animation of a floating point value ranging from 0 to 1. Note that I have provided for a mode without animation, which is determined by the <em>animated</em> flag passed in line 5.</p><p>Code in lines 25 and 26 calculates and caches the radius of the chart and the coordinates of its upper left corner.</p><p>All the magic — drawing the chart — happens in the <em>onDrawBehind </em>function.</p><p>To draw the chart, each sector is rendered sequentially.</p><p>Lines 30–34 calculate the value based on which the final angle for the drawing sector will be calculated. The sectors are divided into three categories:<br>1) those that do not need to be drawn yet — they are skipped (line 31);<br>2) those that are completely drawn — line 32;<br>3) and, finally, those that are in the process of being drawn — line 33.</p><p>The sector is drawn in lines 36–44. Note that in line 38, I subtract 90 degrees from the starting angle — this is done in order to rotate the diagram by 90 degrees for a more conventional display.</p><h4>How to use it</h4><p>Finally, let’s see how to use the code from the previous section to draw the diagram from the video at the beginning of the article. It’s so simple that it doesn’t even need an explanation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2e8fedecab50872749174e503695fb0/href">https://medium.com/media/a2e8fedecab50872749174e503695fb0/href</a></iframe><p>That’s it. Now you know how to draw an animated pie chart in Jetpack Compose. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b89bac94071" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A secure way to store API keys in Android applications]]></title>
            <link>https://al-e-shevelev.medium.com/a-secure-way-to-store-api-keys-in-android-applications-238135709067?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/238135709067</guid>
            <category><![CDATA[api-key-security]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[android-ndk]]></category>
            <category><![CDATA[jni]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Sun, 29 Jun 2025 19:41:30 GMT</pubDate>
            <atom:updated>2025-06-29T19:43:08.914Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G8MbTzrm8TV4O8jD" /></figure><p>Protecting your Android app’s API keys ensures the confidentiality of user information and prevents unauthorized access to your APIs. This is an important aspect of app security.</p><blockquote>There is no such thing as absolute security, including API keys. Our job is to make it as difficult as possible for anyone trying to access the keys, and nothing more.</blockquote><p>There are two widely used and relatively secure ways to store your Android app’s API keys:</p><ul><li>Using an application properties file (usually local.properties) that is not included in the repository;</li><li>Storing keys in native code that is called from our application code via JNI.</li></ul><p>Both methods are well known and described in many articles, so I will not explain what they consist of.</p><p>In my current project, I use the Yandex search API, which is accessed via an API key, among other things. Therefore, it is important for me to ensure that this key is as secure as possible without putting in too much effort.</p><p>To do this, I decided to combine these two well-known approaches, and I think I succeeded.</p><p>The idea behind my approach is quite simple: I store the API key in encrypted form in local.properties, and I place the encryption key, as well as the algorithm itself, in the native code. Thus, in order for an attacker to gain access to my API key, they must:</p><ul><li>intercept the encrypted API key;</li><li>find the encryption key in the native code;</li><li>understand the logic of the cryptographic algorithm in the native code.</li></ul><p>These steps can be quite difficult for an ordinary fraudster.</p><p>Let’s write a small demo application that provides such protection.</p><h3>Getting ready</h3><p>First, let’s create a new application in Android Studio:<br><em>File -&gt; New -&gt; New project -&gt; Empty Activity</em></p><p>Come up with a name for the application yourself, as well as the package name. Select Kotlin DSL as the build configuration language.</p><p>After generating the application, you can delete the tests, as we won’t need them.</p><p>The generated markup in <em>MainActivity</em> can also be deleted, as it is not needed.</p><p>Let MainActivity look like this for now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4174934c91d05e394acb186cb8608b3/href">https://medium.com/media/b4174934c91d05e394acb186cb8608b3/href</a></iframe><p>The encrypted API key will be stored in the <em>local.properties</em> file. Let’s temporarily add our unencrypted API key to this file, then we will replace it with the encrypted value. For example, like this:<br><strong><em>my_api_key=In a hole in the ground there lived a hobbit.</em></strong></p><p>As the API key, I chose a phrase from a very famous book 🙂</p><p>Also, make sure that <em>local.properties</em> is in the root <em>.gitignore</em> file so that it does not end up in the repository.</p><p>It is most convenient to take the data for encryption from <em>BuildConfig</em>. Let’s transfer our API key from <em>local.properties</em> to <em>BuildConfig</em>. To do this, add these lines to the <em>build.gradle.kts</em> file for the <em>app</em> module:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/704fc7a431e365ab7bf0b348a02bc787/href">https://medium.com/media/704fc7a431e365ab7bf0b348a02bc787/href</a></iframe><p>And the final touch. We will need a class for encrypting/decrypting the API key. In my case, it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fc4047fc058e11a1df57b3b1ee5e1ab/href">https://medium.com/media/9fc4047fc058e11a1df57b3b1ee5e1ab/href</a></iframe><p>Line 4 describes the function from the native code that we will call for encryption/decryption. The type of operation is determined by the operationId parameter. For encryption, its value is 1, for decryption — 2. The second parameter — source — is the string to be processed.</p><p>The <em>getKey</em> function (lines 6–8) retrieves and returns the key by calling a native function and taking the source data from <em>BuildConfig</em>. Note that it now passes the value 1 to operationId, because we need to encrypt our API key to get started — that’s why we temporarily put it in <em>local.properties</em>.</p><p>Finally, line 12 loads the native library, which we will create in the next section.</p><p>That completes the preparation. Let’s move on to the encryption and decryption code.</p><h3>Native code for encryption and decryption</h3><p>Before writing code, you need to install NDK (Native Development Kit, a set of tools that allows you to use C and C++ code with Android) and CMake (an external build tool that works alongside Gradle to build your native library). You can do this through Android Studio settings, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G6Iv-0mlQH0y7Tf8" /></figure><p>To make working with CMake easier, I also recommend installing the CMake Highlighter plugin:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*n07dFf2hF5uzsO-a" /></figure><p>Next, add the path to NDK in local.properties. In my case, it looks like this one:<br><strong><em>ndk.dir=/Users/aleksandr_shevelev/Library/Android/sdk/ndk/29.0.13599879</em></strong></p><p>In the next step, you need to add a native build to the <em>build.gradle.kts</em> file of the <em>app</em> module, like here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4338eafcde32794199e35c299539ca5f/href">https://medium.com/media/4338eafcde32794199e35c299539ca5f/href</a></iframe><p>Now we are ready to write code. I chose <a href="https://en.wikipedia.org/wiki/XXTEA">XXTEA</a> as the encryption algorithm because it is:</p><ul><li>easy to implement;</li><li>fast;</li><li>relatively robust.</li></ul><p>Since the code will be in C++, let’s create a cpp directory at the same level as src, and three files in it: <em>CMakeLists.txt, facade.cpp,</em> and <em>xxtea.cpp</em> .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/0*PW7gvwurRyiwsi8r" /></figure><p><em>CMakeLists.txt</em> contains a description of our library, its name, and the files it contains:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aff55153c65c849a3cc78dfade901bab/href">https://medium.com/media/aff55153c65c849a3cc78dfade901bab/href</a></iframe><p>The <em>xxtea.cpp</em> file contains the XXTEA encryption algorithm. It is quite large, so I will not include it in its entirety (it can be found in my demo project, <a href="https://github.com/AlShevelev/api_key_jni_article/blob/main/app/cpp/xxtea.cpp">here</a>), but I will provide a signature of the functions here so that the rest of the material is clear:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4836992a245eced071d2be8bac41633b/href">https://medium.com/media/4836992a245eced071d2be8bac41633b/href</a></iframe><p>And the last file, <em>facade.cpp</em>, is the entry point for the native code. Here’s what it looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7928da801bf1bfbedc3f62b6b7f3073/href">https://medium.com/media/e7928da801bf1bfbedc3f62b6b7f3073/href</a></iframe><p>Let’s take a closer look at what’s going on in this file. Let’s start from the end, from line 43. This line defines a function in the C programming language (<em>extern “C”</em>) that will be called from our Android code, specifically from the <em>ApiKeyExtractor</em> class.</p><p>Note the name of the function: <strong><em>Java_com_shevelev_apikeyjniarticle_ApiKeyExtractor_getApiKey</em></strong><em><br></em>— this is crucial! It indicates that the function is called from code under JVM, contains the name of the calling code package (<em>com_shevelev_apikeyjniarticle</em>), the name of the calling class (<em>ApiKeyExtractor</em>), and the name of the external calling function in the class (<em>getApiKey</em>).</p><p>This function will return a string (<em>jstring</em>) and takes two parameters: <em>operationId</em> (integer operation code: 1 for encryption and 2 for decryption) and <em>source</em> — the string to be encrypted/decrypted. The first two parameters are for the service purpose (a reference to the JNI environment and the calling code) and are not needed for our task.</p><p>Line 50 contains the encryption key (here it is!).</p><p>Next, the input string is converted, the encryption/decryption function is called, its result is converted, and returned.</p><p>Line 10 contains the <em>getData</em> function, which encrypts and decrypts data. As you can see, it takes three arguments: <em>operationId</em> - the operation code (see above), the encryption key (<em>key</em>), and the string being processed (<em>data</em>).</p><p>All this function does is call the XXTEA algorithm for encryption or decryption, prepare the data for it, and convert the result.</p><h3>Final touches</h3><p>So, we have the test application template and the native code. Let’s make them work together.</p><p>First, let’s finish writing the <em>MainActivity</em> code so that it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d0d57a695ec99eb8a48bafddf5b4a0b/href">https://medium.com/media/9d0d57a695ec99eb8a48bafddf5b4a0b/href</a></iframe><p>This code extracts the API key (in line 8), outputs it to the log and to the screen.</p><p>Let’s remember that our unencrypted API key is temporarily stored in <em>local.properties</em>. And in <em>ApiKeyExtractor</em>, the <em>getApiKey</em> function is called with parameter 1 — encryption. This is necessary in order to obtain an encrypted API key on the first run.</p><p>All that remains is to:</p><ul><li>launch the application,</li><li>take the encrypted API Key from the log,</li><li>replace the value of <em>my_api_key</em> in <em>local.properties</em> with the encrypted one,</li><li>in <em>ApiKeyExtractor</em>, replace the value of the encryption parameter from 1 to 2.</li></ul><p>Bob’s your uncle, now, when you launch the application, you will receive a decrypted API key.</p><blockquote>I would like to stress what I said at the beginning: there is no such thing as absolute protection. Our job is simply to make the task of an attacker as difficult as possible, and nothing more.</blockquote><p>The demo application code can be found <a href="https://github.com/AlShevelev/api_key_jni_article/tree/main">here</a>. Please note that I added the <em>local.properties</em> file to the repository. <strong>This was done for demonstration purposes only</strong> — in reality, you should not do this (see what I wrote at the beginning of the article).</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=238135709067" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Game intelligence in turn-based strategies. Part 4 — Decision Making]]></title>
            <link>https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-4-decision-making-53ff31f6219f?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/53ff31f6219f</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Tue, 10 Jun 2025 18:42:26 GMT</pubDate>
            <atom:updated>2025-06-10T18:42:26.673Z</atom:updated>
            <content:encoded><![CDATA[<h3>Game intelligence in turn-based strategies. Part 4 — Decision Making</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*7qPy2DAsVJKAXX2rXMOBWg.png" /></figure><p>In this section, I will describe how decisions are made in step-by-step strategies. First, let’s describe the situation on the playing field that we will be considering. We will look at it from the computer’s point of view — it’s easier that way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qBsUfMg1VO2laeKRIyd3Lw.png" /></figure><p>Here is a diagram of the game field. It is a 5x5 square grid. We (the computer) play for the green side, and our opponent plays for the red side. Our city is located in cell A, and our opponent’s city is located in cell B. Our army is located in cell C. The situation on the game field at the end of the first turn is shown on the left.</p><p>Next, we pass the turn to our opponent, who moves, and his army appears in square D. Now, during our turn, we must make one of the following decisions:</p><ul><li>Do nothing (for example, to heal our troops, who are healed at the end of each turn);</li><li>Defend our city (move to our city square to defend it);</li><li>Capture the enemy city;</li><li>Attack the enemy army.</li></ul><p>Let’s look at the possible ways in which we can make one of these decisions.</p><h3>State Machine</h3><p>The first and most obvious way is to build a state machine. In our simplest case, it might look like this one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XW9FpnmzHMdUwIqrE5zPWw.png" /></figure><p>As you can see, we have four target states and one event. Depending on the conditions, we transition to one state or another, which determines the chosen decision.</p><h4>What are the problems with this approach?</h4><p>The first problem is that state machines only work well in the simplest cases, such as ours. They have a tendency to grow, and maintaining them can be difficult.</p><p>The second problem is that it is difficult to introduce an element of randomness into a state machine. This means that the game intelligence based on it will be boring and predictable.</p><h3>Decision trees</h3><p>In decision trees, the path to a particular decision is laid out as a chain of simple questions located at the nodes of the tree. Let’s look at a specific example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xWrdV_z8etu33MNgvV0dOQ.png" /></figure><p>As you can see, as we descend the tree, we answer simple questions and ultimately arrive at the sheets containing the target solutions.</p><h4>What are the problems with this approach?</h4><p>The use of decision trees eliminates one of the problems of state machines — decision trees are much easier to maintain and modify. But it is also difficult to introduce an element of randomness into them, which can make your game intelligence predictable.</p><h3>Utility system</h3><p>In a utility system, each decision is evaluated independently using a separate evaluation function. Each function outputs a decision evaluation in the form of a positive number. If the spread of evaluations is too large, it can be smoothed, for example, by passing each evaluation through a logarithmic function.</p><p>As a result, we obtain a set of independent evaluations of each decision. All that remains is to select a specific decision at random, but based on the evaluations obtained as weights — the higher the evaluation, the greater the probability of selecting that particular decision. Here is how it looks in the diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Td9SLCWLGvI-KKh7kVVF1g.png" /></figure><h4>Advantages of this approach</h4><p>I believe this approach to decision-making is the most successful because it has a number of significant advantages, namely:</p><ul><li>It is easy to maintain and scale — if you need to add another decision, you just need to write an evaluation function and add it to the collection of existing functions.</li><li>It can be finely tuned — for example, if you want the AI to play more aggressively, you can multiply the output value of the corresponding evaluation functions by a certain correction factor;</li><li>This solution incorporates randomness, which means that the game intelligence will not always make the most advantageous decisions for itself, but overall it will lead its game to victory, which is exactly what is needed to entertain the user.</li></ul><h3>Practical considerations</h3><p>At the very end of this series of articles, I would like to draw attention to a few practical considerations.</p><h4>Algorithms must work quickly</h4><p>During the operation of the game intelligence, your algorithms may be run multiple times (at least dozens or even hundreds of times). This is especially true for path construction and influence map calculation. You need to make sure that your algorithms run as fast as possible — don’t add unnecessary calculations if you don’t need them. Otherwise, you risk getting unacceptably long execution times for your game intelligence, which will inevitably drive users away from your game.</p><h4>Debug logging</h4><p>Game intelligence can make stupid decisions. Moreover, these decisions may depend on decisions made in previous moves. To understand the cause of the problem, you need to use debug logging.</p><p>It may be worth using some kind of fast persistent storage for this, so that you can export logs (which can be voluminous) from it and understand the cause of the problem.</p><h4>The algorithms are simple, but your evaluation functions are not</h4><p>Game intelligence algorithms are fairly simple; the main complexity and value lies in your evaluation functions. And these can be as complex as you like (but don’t forget about performance).</p><h3>Conclusion</h3><p>This is the end of a long journey. I hope you enjoyed this series of articles. I wrote it based on my limited practical experience gained while working on my project. By the way, the project code is publicly available and can be viewed <a href="https://github.com/AlShevelev/TrenchWarfare">here</a>.</p><p>Good luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53ff31f6219f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Game intelligence in turn-based strategies. Part 3— Influence Map]]></title>
            <link>https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-3-influence-map-3039fa13f3eb?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/3039fa13f3eb</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Tue, 10 Jun 2025 18:42:01 GMT</pubDate>
            <atom:updated>2025-06-10T18:46:07.302Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*g-H1z5SPvmdV0H7dblcCWw.png" /></figure><p>An influence map is a model used in the decision-making process on the playing field. It allows you to assess whether a specific cell on the playing field is in a danger zone or not.</p><h3>Building a map</h3><p>Let’s look at a specific example of how an influence map is built.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uEEAlvf0jd0s-28UagmqFA.png" /></figure><p>Here is a diagram of our game field, which features two opposing sides: red and green. Let’s assume that we are playing for the green side.</p><p>We have two units located in the green-shaded cells. Each of them has a strength of 50 and can move 4 cells. Our opponent’s only unit is located in the red cell. It has a strength of 100, but can only move three cells.</p><p>Let’s also assume that the strength of a unit decreases with the distance traveled (soldiers get tired). For simplicity, let’s also assume that this dependence is linear.</p><p>To construct the map, we must go through all the squares that each unit can reach. And for each square, we must calculate the strength of the unit if it arrives at that square. All the values calculated for a specific square are summed up — see the diagram above. This is how the influence map is calculated.</p><h3>What conclusions can be drawn based on the influence map?</h3><p>So, the influence map has been built. What conclusions can be drawn about the yellow and blue cells based on the map?</p><p>The yellow cell is relatively safe — we can pull enough forces there to defend it (a strength of 30 against our opponent’s 25).</p><p>But the situation with the blue cell is the opposite — the enemy has a fivefold advantage. To protect this cell, we must take radical measures (build additional troops on it, etc.).</p><h3>A note about the speed of map construction</h3><p>Let me tell you about one pitfall I stumbled upon in my project.</p><p>On the one hand, building an influence map from scratch can be a rather difficult process (for example, the path-building algorithm from the previous article can be actively used for this). On the other hand, it must contain up-to-date information about the situation on the playing field and must be updated frequently — after each troop movement, after each battle, etc.</p><p>But if you rebuild the map from scratch every time, the processing time of your game intelligence may become unacceptably long.</p><p>Fortunately, it is not necessary to build the map from scratch. It is enough to do it once, for example at the beginning of the turn. And then, considering that it is built independently for each unit on the map, it can simply be updated — this is many times faster.</p><p>For example, when moving a unit, it is enough to subtract its strength in the starting cell and add it to the final one. This is enough to quickly update the influence map and keep it up to date.</p><h3>What’s next?</h3><p><a href="https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-4-decision-making-53ff31f6219f">In the next article</a>, I will talk about how to make decisions on the game field. For example, how to determine when an army should attack, when it should defend, and when it should simply skip its turn, etc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3039fa13f3eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Game intelligence in turn-based strategies. Part 2 — Pathfinding]]></title>
            <link>https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-2-pathfinding-5da2205123d2?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/5da2205123d2</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[pathfinding]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Tue, 10 Jun 2025 18:41:19 GMT</pubDate>
            <atom:updated>2025-06-10T18:45:37.228Z</atom:updated>
            <content:encoded><![CDATA[<h3>Game intelligence in turn-based strategies. Part 2 — Pathfinding</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*He_fDJMB5A0oVx0BjZPYUQ.png" /></figure><p>The pathfinding algorithm allows you to build a route from point A to point B on the game board. This is one of the key algorithms in games. In the case of a human-played game, it allows you to visualize the path on the game map. In the case of a computer-played game, it allows you to understand which cells of the game board are available for action and how difficult they are to reach.</p><p>The most popular pathfinding algorithm is the A* algorithm. You can read about the history of this algorithm, why it is popular, and many other interesting things <a href="https://en.wikipedia.org/wiki/A*_search_algorithm">on Wikipedia</a>.</p><h3>How it works</h3><p>A* step-by-step reviews all paths leading from the initial cell to the final one until it finds the one with the minimum cost (the shortest and easiest to traverse).</p><p>The algorithm finds all neighboring cells for each cell, starting with the initial cell, and selects one candidate cell from among them that may be the next cell on the path.</p><p>The candidate cell is selected based on the sum of two factors:</p><ul><li>how close it is to the final cell of the path (the closer, the better);</li><li>and how easily this cell can be traversed while following the path (the easier it is to traverse, the better).</li></ul><p>The first factor is clear — in most cases, it is simply the geometric distance between cells.</p><p>The second factor requires explanation. Suppose we have a land unit. And there are cells with different types of terrain: meadow, water, and desert. At the same time, according to the rules of our game, we know that: 1) our unit cannot move across water; 2) it has difficulty crossing the desert; 3) it moves easily across the meadow. If we assume that the cost of moving across the meadow is 0, across the desert is 5, and across water is infinity, then we get the numerical value of the second factor.</p><p>After selecting a candidate cell, the algorithm must understand where to move next. To do this, it must find all of its neighboring cells (except those that have already been processed in previous steps), select a new candidate cell, and so on.</p><p>In this way, the algorithm gradually builds a chain of cells that is a path from the starting point to the end point.</p><p>Let’s look at a specific example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tIYxrwJnq3Fj57NDzndZfw.png" /></figure><p>The diagram above shows the game field, where green cells are meadows (easy to move across), yellow cells are deserts (difficult to move across), and blue cells are water (impossible to cross). We can move both straight and diagonally, and these moves are equivalent.</p><p>Let’s see what the path from cell A to cell B will look like:</p><ul><li>First, the algorithm will select the cells with the shortest distance to cell B, so it will arrive at cell C;</li><li>Following the same principle, unable to enter the blue cells, it will arrive at cell D;</li><li>From cell D, it will no longer be possible to further reduce the distance, so the algorithm will return to cell C and from there it will go to cell E;</li><li>In cell E, there are two options: 1) if the yellow cells (desert) are easily traversed, the algorithm will cross cell F and eventually arrive at B; 2) otherwise, the algorithm will bypass the yellow zone and still arrive at cell B.</li></ul><p>Ultimately, the constructed path will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rKBQ97t0jRaE0m3CL2FP7Q.png" /></figure><h3>Algorithm code</h3><p>Now let’s examine the source code of the algorithm. My game project is written in Flutter, so the code is in Dart.</p><p>First, let’s define a class to describe a single cell of the game field. For example, like this one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02e1942f12afa4938a24c4c2736e95e9/href">https://medium.com/media/02e1942f12afa4938a24c4c2736e95e9/href</a></iframe><p>For our purposes, it is necessary that each cell has row and column numbers, since they determine its coordinates on the game field.</p><p>To make our algorithm versatile, let’s define an interface like this, the specific implementation of which we will use in our calculations. This is necessary, for example, so that we can find a path for land and sea units without changing the algorithm itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/267024933790fbd410439ef3ba250fff/href">https://medium.com/media/267024933790fbd410439ef3ba250fff/href</a></iframe><p>In the interface, the <em>calculateGFactorHeuristic(…)</em> method returns the cost of moving from one cell to another, or null if the cell is not available.</p><p><em>isCellReachable(…)</em> method checks whether the cell is available.</p><p>Метод <em>findCellsAround(…) </em>method finds all the cells around a certain cell. Its implementation depends on your game field — hexagonal cells or square cells, whether it is possible to move diagonally on a square cell field, etc.</p><p>Here’s what the entire algorithm looks like, and then we’ll break it down in steps.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69367bb52f6ce5af1e6e63e3e36d83f9/href">https://medium.com/media/69367bb52f6ce5af1e6e63e3e36d83f9/href</a></iframe><p>At the beginning, four collections are defined:</p><ul><li><em>_open</em> — a list of cells that need to be processed;</li><li><em>_cameFrom</em> — a chain of cells of an already constructed path;</li><li><em>_fScore </em>— a collection for storing the total score of how suitable a certain cell is as a candidate for one of the cells of the path;</li><li><em>_gScore</em> — a collection for storing the total cost of moving from the starting cell to a certain cell.</li></ul><p>Next, in line 19, a check is performed to see if it makes sense to search for a path at all.</p><p>Next, the initial cell of the path is saved in the <em>_open</em> collection for further analysis. The distance to the last cell (line 26) and the cost of movement, which is currently equal to 0 (line 25), are calculated for it.</p><p>Note that the distance to the last cell is calculated as the geometric distance between the cells. The rows and columns of the cells are used as their coordinates. This is the simplest way to estimate the distance between two cells. It also works well for hexagonal cells.</p><p>The search continues until there are no more cells to process (line 29).</p><p>In line 30, a candidate cell for the next cell in the path is selected. The cell with the lowest total score is selected as the candidate cell.</p><p>Line 33 checks whether we have reached the last cell of the path or not. If we have, the cells of the path are transformed into a list. This is done by the <em>_reconstructPath</em> function.</p><p>In line 37, the processed candidate cell is removed from the list of cells to be processed.</p><p>Next, the algorithm needs to understand which cells to process next. To do this, all neighboring cells around the current one are found. This is done in line 39.</p><p>In line 42, the cost of moving to each neighbouring cell from the current cell is found. If moving to a cell is not possible, it is skipped (line 45).</p><p>In line 49, the total cost of moving from the start of the path to the neighbouring cell being processed is calculated.</p><p>In line 51, it is checked whether this cell should be included in the list of possible path cells.</p><p>If so, the cell is stored in the path cell chain (line 52), and the total cost of moving and the total score are calculated for it, taking into account the distance from the cell to the end of the path (lines 54–55). After that, the cell is included in the list of cells that are taken into account during processing (lines 57–59).</p><p>Finally, in line 65, an empty path is returned if the algorithm was unable to reach the final cell of the path.</p><h3>What’s next?</h3><p><a href="https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-3-influence-map-3039fa13f3eb">In the third article</a>, I will talk about a very useful model used for decision-making by game intelligence — an influence map.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5da2205123d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Game intelligence in turn-based strategies. Part 1 - Getting started and misconceptions]]></title>
            <link>https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-1-getting-started-and-misconceptions-e991cd28d4d3?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e991cd28d4d3</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[games]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Tue, 10 Jun 2025 18:40:27 GMT</pubDate>
            <atom:updated>2025-06-10T18:44:37.157Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*Va7VvleBp4QuPbd5PNLpfA.png" /></figure><p>Like many of you, I have my own pet project. My current project is a game. It is a turn-based strategy game written in Flutter, but the specific technology is not important for the material presented here, as we will be discussing algorithms. By the way, the project’s source code is open and can be viewed <a href="https://github.com/AlShevelev/TrenchWarfare">here</a>.</p><p>A turn-based strategy game in which the opponent is a computer is a rather interesting type of project. While working on it, I had to answer the question: how do you make a computer play like a human?</p><p>In the end, reading articles, searching forums, trial and error, debugging, rewriting code, and optimization paid off — in my game, the computer really plays against a human. It makes silly mistakes, but I’m not paying attention to that yet because I have a general understanding of how to fix it.</p><p>So that those who do similar projects don’t have to gather information bit by bit, like I did, I decided to write a series of articles covering various game intelligence algorithms.</p><p>The series will consist of the following articles:</p><ol><li>Getting started and misconceptions (this article)</li><li><a href="https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-2-pathfinding-5da2205123d2">Pathfinding</a></li><li><a href="https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-3-influence-map-3039fa13f3eb">Influence map</a></li><li><a href="https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-4-decision-making-53ff31f6219f">Decision Making</a></li></ol><p>But before we move on to describing the algorithms, let’s briefly talk about the misconceptions that often plague those who embark on such gaming projects.</p><h3>Misconceptions</h3><h4>The computer should play as well as possible and without errors</h4><p>Of course, your first desire is to write such game intelligence that it plays as strongly as possible. In principle, this is possible, but… it is not necessary. You are not writing a chess program competing for the world championship title.</p><p>Your task is to entertain the user. This means you need to provide them with a comfortable level of difficulty and nothing more. The computer should play in an interesting way, with errors and randomness in its play, but overall it should lead the game in the right direction — towards its victory.</p><p>The reason why the computer should not play too difficult can be understood using the example of the Heroes of Might and Magic series. Imagine a situation where you have a game campaign — several consecutive maps connected by a single storyline. If playing against the computer is so difficult that the user cannot advance through the storyline, they will simply abandon the game.</p><h4>Why not just go through all the possible moves?</h4><p>In modern games, it is almost impossible to go through all the possible moves and calculate the game several moves ahead. In chess, this is possible, but in modern games, it is almost impossible. To understand why this is so, let’s just compare the main characteristics of the battlefield in chess and in Heroes of Might and Magic 2 (the game was released in 1996, 29 years ago; in newer games in the series, the situation is even more dramatic).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QdfwgFQtIvbrEAiWKrnw_w.png" /></figure><p>As you can see, even in such an ancient game as Heroes of Might and Magic 2, the player has so many options in battle that going through them all, especially several moves ahead, can be very slow, which prevents the player from playing comfortably. Other solutions are needed here, and we will discuss them.</p><h4>Game intelligence algorithms are very complex</h4><p>There is a widespread belief that game intelligence algorithms are very complex and akin to magic. In reality, this is not the case, and magic does not exist, no matter how much we would like it to. These are ordinary algorithms with loops and branches, nothing more. I will demonstrate this in the following articles.</p><h3>What’s next?</h3><p>In the <a href="https://al-e-shevelev.medium.com/game-intelligence-in-turn-based-strategies-part-2-pathfinding-5da2205123d2">second article</a>, I will discuss one of the key algorithms in games: the pathfinding algorithm.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e991cd28d4d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run Flutter code on user inactivity in Android app]]></title>
            <link>https://al-e-shevelev.medium.com/run-flutter-code-on-user-inactivity-in-android-app-91b8f25a20fa?source=rss-c87d941490a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/91b8f25a20fa</guid>
            <category><![CDATA[idle]]></category>
            <category><![CDATA[isolation]]></category>
            <category><![CDATA[alarmmanager]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[androi̇d]]></category>
            <dc:creator><![CDATA[Alexander Shevelev]]></dc:creator>
            <pubDate>Fri, 13 Sep 2024 17:43:53 GMT</pubDate>
            <atom:updated>2024-09-13T17:43:53.335Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8xkDYXmviz4uH9dI" /></figure><p>I am an Android developer who has been writing in Flutter lately. Recently I faced an interesting task — I needed to make an auto-login in our application if the user does nothing for some time. At the same time, we have two possible scenarios: when the application is active (in this case, the user’s session should be interrupted and the application should open the login screen); and when the application is not active (in this case, when it starts, the user should see the login screen).</p><p>This task turned out to be not so simple — I had to collect information bit by bit, try and make mistakes. But in the end, I solved the problem and gained some experience, which I decided to present in the form of this article. I hope it will be useful to you. So, let’s begin.</p><h3>What we want to get as a result</h3><p>As I mentioned earlier, initially I solved the problem of automatic logout. But since each application does logout differently, let’s generalize the task. We want to call some code in the Flutter application if the user does nothing for some time. At the same time, the code can be called both in the background (the application is not running) and in the running application. In the second case, we should get the result of the work on the screen. Let’s get started.</p><h3>Android</h3><p>The general idea for our solution will be the next one. At the start of the application, we should start a timer via <em>AlarmManager</em>. This timer will be restarted as long as the user does something. When the timer is triggered, it calls a handler (inherited from <em>BroadcastReceiver</em>), which in turn calls the Dart code. Sounds simple, let’s start coding.</p><h4>The handler</h4><p>First, let’s write a handler where all the magic of calling the Dart code will happen. I’ll give you its entire code (it’s not so large, actually), and then we’ll analyze how it works line by line. Here is our handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53f640adc5d1f491fd2403c0331b8c5d/href">https://medium.com/media/53f640adc5d1f491fd2403c0331b8c5d/href</a></iframe><p>As you can see, it’s inherited from BroadcastReceiver. So, can be launched both from AlarmManager as well as from the command line via the adb utility (for debugging purpose).</p><p>In the line 4 input arguments are validated. This is to ensure that the handler cannot be launched accidentally or intentionally by anyone. Note that the handler has a special debugging action for launching — DEBUG_ACTION (line 33). It allows you to launch the handler from the command line for debugging purposes like this:</p><p><em>adb shell am broadcast -a com.example.shevelev.user_idle.USER_IDLE_DEBUG_ACTION -p com.example.shevelev.user_idle</em></p><p>In line 10, the handler gets a reference to the application, which will be used to get the Dart entry point.</p><p>The real magic happens in line 12. Here we create the <em>FlutterEngine</em>. As the name suggests, this is the engine that runs the Flutter app code.</p><p>At first, I tried not to instantiate a separate <em>FlutterEngine</em> instance, but to use the global one for the entire application, creating it in advance and caching it (details can be found in <a href="https://docs.flutter.dev/add-to-app/android/add-flutter-screen#step-3-optional-use-a-cached-flutterengine">the documentation</a>).</p><p>Unfortunately, with this approach, I encountered a problem that I could not get around — if the handler is executed when the Flutter application is already running, then I received an error saying that DartExecutor is busy and cannot execute my code. So, I decided to create a separate FlutterEngine for my needs.</p><p>Line 15 specifies the name of the Dart entry point — this is just the name of a function, as we’ll see later.</p><p>Ideally, we should call the <a href="https://api.flutter.dev/javadoc/io/flutter/embedding/engine/FlutterEngine.html#destroy()"><em>destroy</em></a> method of <em>FlutterEngine </em>after executing the Dart entry point to clean up the resources. But when I tried to do this, my entry point stopped executing. It is likely that I called <em>destroy</em> while executing the Dart code, but I was unable to find out when exactly this method should be called, unfortunately.</p><p>Let’s find out now how to configure the launch of the handler.</p><h4>Launching the handler</h4><p>As with the handler, I’ll give you the full launch code, and then we’ll go through it line by line. Here’s the launch code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a509a23c2e622ac260fd1be85ffae5e1/href">https://medium.com/media/a509a23c2e622ac260fd1be85ffae5e1/href</a></iframe><p>Let’s start with the <em>setupAlarm</em> method. As its name suggests, it configures the timer to run the handler. I chose <em>AlarmManager</em> as the timer and the code here is quite straightforward.</p><p>However, pay attention to three things.</p><p>First, in line 24 we explicitly specify the action parameter of the Intent, which will launch the handler. This is done for security reasons — in the handler, in line 25 we check the incoming action parameter, and do not allow the handler to launch if the value of the action is unknown to us.</p><p>Secondly, since the timer start time will be shifting, we have to cancel the old timer first (line 28), and then set the new one (line 30).</p><p>Thirdly, as you can see from line 30, the timer will fire once, so we calculate the firing time by adding the time interval in milliseconds to the current time.</p><p>Let’s now pay attention to the <em>restartAlarm</em> method. Its goal is to restart the timer based on external events. Since events can come frequently, we use Kotlin coroutines to reduce the restart frequency.</p><h4>Settings in the manifest</h4><p>First, let’s set up a handler in our application manifest. To do this, add the following line to the manifest (without it, AlarmManager may not work correctly on applications with targetSDK 31 — Android 12):</p><p><em>&lt;uses-permission android:name=”android.permission.SCHEDULE_EXACT_ALARM”/&gt;</em></p><p>Also, we would like to have an opportunity to run the handler from the command line for debugging purpose (via adb). To do it we have to add the handler to the manifest:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42cde093621c46998a8d9f8abb4b5ea0/href">https://medium.com/media/42cde093621c46998a8d9f8abb4b5ea0/href</a></iframe><h4>Launching the handler</h4><p>The timer is started in MainActivity, the code of which looks like here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9fc1f8c47af57a5fdb91da33bdc7e41/href">https://medium.com/media/b9fc1f8c47af57a5fdb91da33bdc7e41/href</a></iframe><p>The code is trivial here — we start the timer when the activity starts and restart it upon a user action.</p><p>The code for Android is finished, let’s move on to Flutter.</p><h3>Flutter</h3><h4>Entry point in the Flutter app</h4><p>From the Android code we call a function on Flutter. It looks like here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0728555bd831f04bddcb984e44a8f3a7/href">https://medium.com/media/0728555bd831f04bddcb984e44a8f3a7/href</a></iframe><p>It is a piece of cake, isn’t it?</p><p>But let’s pay attention to a few things.</p><p>First, the function name must be the same as what we use in the handler — it’s obvious.</p><p>Secondly, the function needs to be marked with the following annotation:</p><p><em>@pragma(‘vm:entry-point’)</em></p><p>Third, the function must be placed in the <em>main.dart</em> file.</p><p>As for the function itself. In the function, you should call the code you need (in my case, it was a logout procedure — clearing the authentication token, etc.) — I will not provide this code here because it is too specific to a particular application.</p><p>At the very end, you need to forward the result of the work to the running application (if it is running). It’s done in line 5. How exactly — more on that later.</p><h4>Calling the entry point if the application is not running</h4><p>When I was working on the logout task I encountered one not very obvious behavior when receiving events in the background. So I would like to describe it here explicitly.</p><p>If you launch your application from the IDE (in my case it was Android Studio) and terminate it (it does not matter how exactly — by the red square button in the IDE, or by swiping it from the tasks list), you’ll not get events from <em>AlarmManager</em> in the Flutter code. To receive the events the application must be launched in the launcher (via the icon in simple terms).</p><p>How to check out that the events are coming in this case? By the log. In Android code you can use <em>Log</em> class for logging. Personally, I prefer to use the <em>print</em> function in Flutter. I use the <em>print</em> function because the result of its work is visible in <em>logcat</em> and it is convenient to watch.</p><p>So, if you launch the application not from the IDE, you will have to look at the logs in the console. I usually do it like this:<br><em>adb logcat | grep “some_tag_for_filtering”</em></p><h4>Interacting with the running application</h4><p>Our two entry points (the main and userIdleActionEntryPoint functions) are running in different isolates. The interaction between the two different isolates is performed via messages that are sent and received using the <em>SendPort</em> and the <em>ReceivePort</em> classes, respectively (you can read more about it <a href="https://dart.dev/language/isolates#sending-multiple-messages-between-isolates-with-ports">here</a>).</p><p>But in our case, everything will be a little trickier because our entry points are launched from different FlutterEngine. Let’s see how we can pass data between them. We will use simple string messages — we don’t need more in our demo case.</p><p>First, let’s define constants with our messages:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aea79037b4912e299941bfe9de517e4e/href">https://medium.com/media/aea79037b4912e299941bfe9de517e4e/href</a></iframe><p>And this is what the messaging code looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d342f6d7e74a7f11998b5eb1ae8fd912/href">https://medium.com/media/d342f6d7e74a7f11998b5eb1ae8fd912/href</a></iframe><p>As you can see, the code is quite simple. In the <em>receive</em> function, we create a ReceivePort instance and subscribe to its messages. As soon as we receive a message that is useful to us, we call the <em>actionToExecute</em> function, which is passed as an argument to the method.</p><p>In the <em>send</em> method we send messages.</p><p>However, please note one key point.</p><p>In line 17, we remember our send port by calling <em>IsolateNameServer.registerPortWithName</em> . And in line 21, we look up the remembered port by calling <em>IsolateNameServer.lookupPortByName</em> . And that’s exactly what allows us to exchange messages between entry points running in different FlutterEngines.</p><p>If the port was not found in line 21 (it means that the main function was not run and the message was processed in the background) then nothing is sent.</p><h4>Subscribe to incoming messages and show the result in the running application</h4><p>There’s one last thing left. To get a clear confirmation that the message has been received in the main application, we’ll display text on the screen that says so. The easiest way to do it is in the MyHomePage widget like here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08adbde3579bf5337407af6ba282fcb9/href">https://medium.com/media/08adbde3579bf5337407af6ba282fcb9/href</a></iframe><p>The code is quite straightforward here. In line 16, subscription to messages occurs, when a message is received, the widget state is changed. And the new state is displayed in line 38.</p><h3>The last point</h3><p>So, in the article I showed you how to call Flutter code from Android code if a user does nothing for some time. The code can be called even when the Flutter app is not running. And if it is running, I told to you how to pass the call result to the app.</p><p>You can find the full source code for the article <a href="https://github.com/AlShevelev/article_user_idle_action">here</a>.</p><p>I hope this article will be useful to you. Bye!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91b8f25a20fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>