<?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 Evan on Medium]]></title>
        <description><![CDATA[Stories by Evan on Medium]]></description>
        <link>https://medium.com/@evanhalley?source=rss-29ae8a3ad52d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*wVhhjhLYRlyRjqvMXZT-kA.jpeg</url>
            <title>Stories by Evan on Medium</title>
            <link>https://medium.com/@evanhalley?source=rss-29ae8a3ad52d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 22:20:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@evanhalley/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[GroceryJS: JavaScript Your Groceries]]></title>
            <link>https://medium.com/@evanhalley/groceryjs-javascript-your-groceries-3b2548d066e6?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b2548d066e6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[puppeteer]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Wed, 02 Oct 2019 20:22:31 GMT</pubDate>
            <atom:updated>2019-10-02T20:22:31.495Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://pptr.dev/">Puppeteer</a> is a JavaScript / Nodejs library that instruments Google Chrome or Chromium browers using the Chrome DevTools Protocol. Think of it as a way to write JavaScript to control every aspect of a Chrome browser. I wrote an article <a href="https://evanhalley.dev/post/getting-started-with-puppeteer/">Getting Started with Puppeteer</a> that walked through the process of boostrapping a Nodejs / Puppeteer app and turning a webpage into a queryable API using Puppeteer.</p><p>I originally became interested in Puppeteer because I had some inefficiences in my everyday life. One of those inefficiences was how tedious and monotonous grocery shopping is. Puppeteer was instrumental in this endeavor.</p><p>For what it’s worth, grocery shopping isn’t THAT bad. It <em>can</em> be kinda crazy when young kids are tagging along. GroceryJS has the additional advantage of being an interesting technical challenge, so here we are.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/0*Uv6aDuSFhv3dD7xd.gif" /></figure><h3>Breaking Down Grocery Shopping</h3><p>Shopping for groceries is a procedural activity:</p><ul><li>You look through your fridge and pantry for things you need and make a list</li><li>You walk, bike, or drive to the store</li><li>You walk the aisles adding things to your cart as you see them</li><li>Finally you pay and bring your groceries home</li></ul><p>This process tends to happen every week or two, for large grocery orders.</p><p>Translating this to a computer program, the primary actions are:</p><ul><li>Read items from a list</li><li>Search and add items to your cart</li><li>Prepare the cart for checkout &amp; checkout</li></ul><p>With this in mind, I built GroceryJS.</p><h3>Breaking Down the Script</h3><p>GroceryJS is broken up into several pieces of code after plenty of experimentation:</p><h3>Source</h3><p>The source is the data backend, it’s where the grocery list is held. It also is the holding place for the results of a grocery store run. For, GroceryJS, I started with a text (YAML) file, then transitioned to a Google Sheet. I found the Google Sheet to be something that is accessible from everywhere, desktop &amp; mobile, without needing a bunch of UI. Google provides a pretty robust set of Nodejs libraries you can use to interact with the Google Drive and Sheet APIs.</p><p>My grocery list is stored in the first 2 columns and first 50 rows (arbitrary) in the first sheet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YNSMlZk90BzqLTuY.jpg" /></figure><p>The sheets-grocery-source.js has two primary actions after initializing a few objects.</p><p>The first gets the grocery list, getGroceryList().</p><pre>async getGroceryList() {<br>    let spreadsheetId = this._spreadsheetId;<br>    let sheetsService = this._sheetsService;</pre><pre>return await new Promise((resolve, reject) =&gt; {<br>        sheetsService.spreadsheets.values.get({<br>                spreadsheetId: spreadsheetId,<br>                range: &#39;A1:C50&#39;<br>            }, (err, result) =&gt; {<br>            if (err) {<br>                reject(err);<br>            } else if (result &amp;&amp; result.data &amp;&amp; result.data.values) {<br>                let items = [];</pre><pre>for (let i = 1; i &lt; result.data.values.length; i++) {<br>                    let value = result.data.values[i];<br>                    items.push({ name: value[0], quantity: value[1] });<br>                }<br>                resolve(items);<br>            } else {<br>                resolve([]);<br>            }<br>        });<br>    });<br>}</pre><p>The second adds the results of a particular shopping run to the Sheet, addShoppingResults().</p><pre>async addShoppingResults(title, sheetId, results) {<br>        let sheetsService = this._sheetsService;<br>        let spreadsheetId = this._spreadsheetId;</pre><pre>return new Promise((resolve, reject) =&gt; {<br>            let requests = [];<br>            let idx = 1;</pre><pre>// convert results to an array we can write<br>            let data = [];</pre><pre>let headers = [<br>                { userEnteredValue: { stringValue: &#39;Requested&#39; } },<br>                { userEnteredValue: { stringValue: &#39;Item&#39; } },<br>                { userEnteredValue: { stringValue: &#39;Price&#39; } },<br>            ];<br>            data.push({ values: headers });</pre><pre>for (let i = 0; i &lt; results.length; i++) {<br>                let result = results[i];<br>                let row = [];<br>                row.push({ userEnteredValue: { stringValue: result.requested } });</pre><pre>if (result.result) {<br>                    row.push({ userEnteredValue: { stringValue: result.result.title } });<br>                    row.push({ userEnteredValue: { numberValue: result.result.price } });<br>                }<br>                data.push({ values: row });<br>            }</pre><pre>// add the sheet<br>            requests.push({ <br>                addSheet: {<br>                    /* removed for brevity&#39;s sake */<br>                }<br>            });</pre><pre>// updateCells request<br>            requests.push({ <br>                /* removed for brevity&#39;s sake */<br>            });<br>            <br>            // auto size things<br>            requests.push({<br>                /* removed for brevity&#39;s sake */<br>            });</pre><pre>// execute the batch update<br>            sheetsService.spreadsheets.batchUpdate({<br>                spreadsheetId: spreadsheetId,<br>                resource: { requests: requests }<br>            }, (err, result) =&gt; {<br>                    <br>                if (err) {<br>                    reject(err);<br>                } else {<br>                    resolve(`<a href="https://docs.google.com/spreadsheets/d/$%7BspreadsheetId%7D/edit#gid=${sheetId}%60">https://docs.google.com/spreadsheets/d/${spreadsheetId}/edit#gid=${sheetId}`</a>);<br>                }<br>            });<br>        });<br>    }</pre><h3>Shopper</h3><p>The Shopper contains all the code and actions that make up a successful trip to the grocery store. It’s built on top of utility library I wrote called puppet-helper.js.</p><h4>Puppet Helper</h4><p>The Puppet Helper contains all of things needed to interact with a modern web app, like clicking a button, given a CSS selector:</p><pre>async clickButton(selector, clickCount = 1) {<br>    this.assertPageOpen();<br>    let button = await this._page.$(selector);</pre><pre>if (button) {<br>        await button.click({ clickCount: clickCount });<br>    } else {<br>        throw new Error(`Unable to click ${selector}`);<br>    }<br>}</pre><p>Or getting text from an HTML element:</p><pre>async getTextFromElement(element) {<br>    this.assertPageOpen();<br>    return await this._page.evaluate(el =&gt; el.innerText, element);<br>}</pre><p>As you can imagine, you can string enough of these actions together to mimic a user, shopping for groceries online.</p><h4>The Lowes Shopper</h4><p>More and more grocery stores offer online shopping services on the internet, allowing customers the convenience of shopping from their computer, tablet, or mobile phone. We shop at Lowes Foods, a North Carolina-based grocery store chain. Lowes Foods offers an online shopping service, <a href="https://shop.lowesfoods.com/">Lowes Foods To Go</a>. For $49-$99 annually (or $4 to $5 per order), you can order your groceries using their web app. Once you place your order, a Lowes Foods employee will shop your order and call you when they are finished (or if they had any questions). When the order is complete, you can pick it up or have it delivered.</p><p>Dad/mom-hack, shop for groceries online if you can help it. Taking young kids to the grocery store get’s wild! 😅</p><p>I spent a lot of time examining the front-end code for Lowes Foods To Go. I have determined that it is an <a href="https://angularjs.org/">Angular</a> based progressive web app. It lends itself very well to automating with Puppeteer. Using puppet-helper.js, I can string together a few methods to get several things done.</p><p>Searching for groceries</p><pre>async searchImpl(query) {<br>    this._logger.info(`Searching for ${query}`);<br>    let productDivs = null;<br>    await this._puppetHelper.clearText(&#39;#search-nav-input&#39;);<br>    await this._puppetHelper.enterText(&#39;#search-nav-input&#39;, query);<br>    await this._puppetHelper.wait(SHORT);<br>    await this._puppetHelper.clickButton(&#39;#search-nav-search&#39;);<br>    await this._puppetHelper.wait(MID);<br>    // body &gt; div:nth-child(5) &gt; div &gt; div &gt; div.content-wrapper &gt; div &gt; lazy-load &gt; ol<br>    let resultsDiv = await this._puppetHelper.getElement(&#39;ol.cell-container&#39;);<br><br>    if (resultsDiv) {<br>        productDivs = await this._puppetHelper.getElementsFromParent(resultsDiv, &#39;.cell.product-cell&#39;);<br>    }<br>    return productDivs;<br>}</pre><p>Logging in</p><pre>async login(email, password) {<br>    this._logger.info(`Logging into account ${email}...`);<br>    await this._puppetHelper.goToUrl(SHOPPING_URL);<br>    await this._puppetHelper.clickButton(&#39;#loyalty-onboarding-dismiss&#39;);<br>    await this._puppetHelper.wait(SHORT);<br>    await this._puppetHelper.clickButton(&#39;#shopping-selector-parent-process-modal-close-click&#39;);<br>    await this._puppetHelper.wait(SHORT);<br>    await this._puppetHelper.clickButton(&#39;#nav-register&#39;);<br>    await this._puppetHelper.wait(SHORT)<br>    await this._puppetHelper.enterText(&#39;#login-email&#39;, email);<br>    await this._puppetHelper.wait(SHORT)<br>    await this._puppetHelper.enterText(&#39;#login-password&#39;, password);<br>    await this._puppetHelper.wait(SHORT)<br>    await this._puppetHelper.clickButton(&#39;#login-submit&#39;);<br>    await this._puppetHelper.wait(XLONG);<br>}</pre><p>Showing and emptying your cart</p><pre>async showCart() {<br>    this._logger.info(`Opening the shopping cart...`);<br>    await this._puppetHelper.clickButton(&#39;#nav-cart-main-checkout-cart&#39;);<br>    await this._puppetHelper.wait(MID);<br>}<br><br>async emptyCart() {<br>    this._logger.info(`Emptying cart...`);<br>    await this.showCart();<br>    await this._puppetHelper.clickButton(&#39;#checkout-cart-empty&#39;);<br>    await this._puppetHelper.wait(NANO);<br>    await this._puppetHelper.clickButton(&#39;#error-modal-ok-button&#39;);<br>    await this._puppetHelper.wait(MINI);<br>}</pre><h3>Putting it Together</h3><p>With all the pieces mentioned, I can have GroceryJS, prepare a shopping cart full of groceries. When it’s done, it sends me an email with a link to the cart (so that I can quickly checkout) and a link to the Google Sheet for tracking purposes.</p><pre>(async () =&gt; {<br>    let shopper = null;</pre><pre>try {<br>        let sheetSource = new SheetGrocerySource(logger, credential.client_email, credential.private_key, config.source.sheetId);<br>        await sheetSource.init();<br>        let list = await sheetSource.getGroceryList();</pre><pre>// login and create a blank slate to shop<br>        shopper = new LowesShopper(logger);<br>        await shopper.init(config.shopper.headless);<br>        await shopper.login(config.shopper.email, config.shopper.password);<br>        await shopper.emptyCart();</pre><pre>// do the shoppping<br>        let shoppingResults = [];<br>        for (let i = 0; i &lt; list.length; i++) {<br>            let requestedItem = list[i];<br>            let shoppedItem = await shopper.addItemToCart(requestedItem.name, requestedItem.quantity);<br>            shoppingResults.push({ requested: requestedItem.name, result: shoppedItem });<br>        }</pre><pre>// notify <br>        let dateStr = moment().format(&#39;MMMM Do YYYY @ h:mm a&#39;);<br>        let title = `Shopping Trip on ${dateStr}`;</pre><pre>let urlToCart = &#39;<a href="https://shop.lowesfoods.com/checkout/cart&#39;">https://shop.lowesfoods.com/checkout/cart&#39;</a>;<br>        let urlToSheet = await sheetSource.addShoppingResults(title, moment().unix(), shoppingResults);<br>        let emailBody = `<br>            &lt;span&gt;&lt;b&gt;Shopping Cart:&lt;/b&gt; ${urlToCart}&lt;/span&gt;&lt;br /&gt;<br>            &lt;span&gt;&lt;b&gt;Shopping Results:&lt;/b&gt; ${urlToSheet}&lt;/span&gt;`;<br>        let mailOptions = {<br>            service: config.email.sender.service,<br>            user: config.email.sender.email,<br>            password: config.email.sender.appPassword<br>        };<br>        mailUtil.sendEmail(config.email.recipeint.sender,<br>            config.email.recipeint.email,<br>            title, emailBody, mailOptions);<br>    } catch (e) {<br>        logger.error(&#39;Error while shopping&#39;, e);<br>    } finally {</pre><pre>if (shopper) {<br>            await shopper.shutdown();<br>        }<br>    }<br>})();</pre><h3>Conclusion</h3><p>So that’s it. GroceryJS isn’t done yet. The real work is actually in the details, like the algorithm for adding groceries from the search results to your cart. Lowes Foods To Go has it’s own search algorithm for determining the relevance of a result to a search. In many cases their algorithm will not match expectations, but it can be augmented:</p><ul><li>Should GroceryJS prefer groceries that are on sale?</li><li>Should GroceryJS prefer groceries for a specific brand?</li><li>Should GroceryJS prefer groceries I’ve purchased before?</li></ul><p>There are a ton of calculations we make every time we grocery shop that I didn’t realize until I started working on GroceryJS.</p><p>Hit up the GitHub repository for all the source code. Be sure to read the README file before jumping in.</p><p><a href="https://github.com/emuneee/groceryjs">github.com/emuneee/groceryjs</a></p><p>I’m really interested to hear how people are using Puppeteer for personal or company projects!</p><p><em>Thanks for reading! I’m launching an email newsletter @ </em><a href="https://puppethero.com/"><em>PuppetHero.com</em></a><em>! If you like articles, tutorials, and news about Puppeteer, I encourage you to sign up. It’s completely free!</em></p><p><em>Originally published at </em><a href="https://evanhalley.dev/post/shopping-with-puppeteer/"><em>https://evanhalley.dev</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b2548d066e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tensor & Flow: Part 1, TensorFlow & Machine Learning on Android]]></title>
            <link>https://medium.com/hackernoon/tensor-flow-part-1-tensorflow-machine-learning-on-android-73fd536a1a7c?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/73fd536a1a7c</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Thu, 08 Mar 2018 17:35:04 GMT</pubDate>
            <atom:updated>2019-04-27T09:57:13.992Z</atom:updated>
            <content:encoded><![CDATA[<p>Many popular apps on both Android and <a href="https://hackernoon.com/tagged/ios">iOS</a> make extensive use of on-device <a href="https://hackernoon.com/tagged/machine-learning">machine learning</a>. Apps like Inbox by Gmail or Siri make use of on device machine learning because it’s faster and does a better job of protecting a user’s privacy. <a href="https://developer.apple.com/machine-learning/">iOS</a> and <a href="https://developer.android.com/ndk/guides/neuralnetworks/index.html">Android</a> both have proper API support for using on device, neural networks for prediction purposes. On both platforms you can either wire up your own neural network or use a higher level framework like TensorFlow to do the heavy lifting for you.</p><p>Tensor &amp; Flow is a two part series where we will explore the specifics of what is needed to do to deploy a machine learning model to an Android app. I will be using TensorFlow Mobile in Part 1, and TensorFlow Lite in Part 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/0*SGgNKrV9P7GydpQK.gif" /><figcaption>Tensor &amp; Flow demo app on Android</figcaption></figure><h3>Training a Neural Network</h3><p>The very first step on this journey is training a neural network that I can deploy. There are plenty of tutorials that walk aspiring machine learning engineers through building models that can classify flowers, identify objects in pictures, detect spam, and even apply filters to pictures. I chose a rather accessible tutorial, building a model to recognize handwritten numbers.</p><p><a href="https://www.tensorflow.org/tutorials/layers">A Guide to TF Layers: Building a Convolutional Neural Network</a> walks us through the entire process configuring and training a neural network to recognize handwritten characters. This guide walks us neural network configuration, downloading the dataset used for training, and the training process.</p><p>The first step is configuring our neural network.</p><h3>The Neural Network</h3><p>The MNIST tutorial trains a Convolutional Neural Network (CNN) to recognize handwritten numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/0*ay0RD9Y5pombwVGe.gif" /><figcaption>Feature extraction using convolution — <a href="http://deeplearning.stanford.edu/wiki/index.php/Feature_extraction_using_convolution">Source</a></figcaption></figure><p>A CNN is comprised of several different layers:</p><ul><li>Convolutional layers, use convolution operations to extract features from images.</li><li>Pooling layers, down sample the images, which reduces processing time and increases training and inference performance.</li><li>Dense layers, predict a class using the features extracted in the convolutional layers.</li></ul><p>Before continuing, I encourage you to make your way over to the <a href="https://ujjwalkarn.me/">Data Science Blog</a> where Ujjwal Karn has written up a very intuitive blog post aptly named <a href="https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/">“An Intuitive Explanation of Convolutional Neural Networks”</a>. Once you have finished the blog post, visit <a href="http://scs.ryerson.ca/~aharley/vis/conv/flat.html">2D Visualization of a Convolutional Neural Network</a> for a cool demonstration of a CNN in action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*fMvSpYTBfw00UaAA.png" /><figcaption>2D Visualization of a CNN</figcaption></figure><p>Some more specifics about the neural network in this example, the input layer is a one to one mapping of the size of the input data. The MNIST dataset contains tens of thousands of handwritten number samples and labels. Each sample is a monochrome image of a handwritten digit, 28 pixels x 28 pixels. An image is a 2-dimension array, containing of pixel data, meaning our input layer has 784 input nodes (28 x 28 = 784).</p><p>The output layer, a <a href="https://en.wikipedia.org/wiki/Logit">Logits</a> layer, that emits our predictions as raw values. The network uses several additional functions to convert this raw data into a prediction and probability (for training).</p><h3>Training &amp; Prepping for Integration</h3><p>The process for training and integrating a neural network model into an Android app resembles:</p><ol><li>Train the neural network.</li><li>Freeze &amp; optimize the TensorFlow graph for inference.</li><li>View the neural network model in TensorBoard. (optional)</li><li>Import the optimized graph into our Android project.</li></ol><p>Getting everything setup to do the training can be more difficult than the actual training depending on your computing platform. My setup:</p><ul><li>MacBook Pro (2015) running MacOS 10.13</li><li>IDE: PyCharm, made this easier by auto-importing Python dependencies and providing code debugging capabilities</li><li>Python 2.7.13</li><li>TensorFlow 1.5</li><li>Android Studio 3.0.1</li></ul><p>Our guide “A Guide to TF Layers” walks us through setting up our neural network and training. After a few passes through the guide, I made one tweak that made integration into an Android app a bit easier, I gave explicit names to my input and output layers, “input” &amp; “output”, respectively. I did this after spending a few hours attempting to figure out on my own. If you do not name the layers in your neural network, they are given default names. You’ll need to open your trained graph in TensorBoard to determine the names of your layers.</p><p>We will end up with this Python script:</p><pre>from __future__ import absolute_import<br>from __future__ import division<br>from __future__ import print_function</pre><pre># Imports<br>import numpy as np<br>import tensorflow as tf</pre><pre>tf.logging.set_verbosity(tf.logging.INFO)</pre><pre>def cnn_model_fn(features, labels, mode):<br>    &quot;&quot;&quot;Model function for CNN.&quot;&quot;&quot;<br>    # Input Layer<br>    input_layer = tf.reshape(features[&quot;x&quot;], [-1, 28, 28, 1], name=&quot;input&quot;)</pre><pre># Convolutional Layer #1<br>    conv1 = tf.layers.conv2d(<br>      inputs=input_layer,<br>      filters=32,<br>      kernel_size=[5, 5],<br>      padding=&quot;same&quot;,<br>      activation=tf.nn.relu)</pre><pre># Pooling Layer #1<br>    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)</pre><pre># Convolutional Layer #2 and Pooling Layer #2<br>    conv2 = tf.layers.conv2d(<br>      inputs=pool1,<br>      filters=64,<br>      kernel_size=[5, 5],<br>      padding=&quot;same&quot;,<br>      activation=tf.nn.relu)<br>    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)</pre><pre># Dense Layer<br>    pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])<br>    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)<br>    dropout = tf.layers.dropout(<br>      inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)</pre><pre># Logits Layer<br>    logits = tf.layers.dense(inputs=dropout, units=10)</pre><pre>predictions = {<br>      # Generate predictions (for PREDICT and EVAL mode)<br>      &quot;classes&quot;: tf.argmax(input=logits, axis=1, name=&quot;output&quot;),<br>      # Add `softmax_tensor` to the graph. It is used for PREDICT and by the<br>      # `logging_hook`.<br>      &quot;probabilities&quot;: tf.nn.softmax(logits, name=&quot;softmax_tensor&quot;)<br>    }</pre><pre>if mode == tf.estimator.ModeKeys.PREDICT:<br>        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)</pre><pre># Calculate Loss (for both TRAIN and EVAL modes)<br>    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)</pre><pre># Configure the Training Op (for TRAIN mode)<br>    if mode == tf.estimator.ModeKeys.TRAIN:<br>        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)<br>        train_op = optimizer.minimize(<br>            loss=loss,<br>            global_step=tf.train.get_global_step())<br>        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)</pre><pre># Add evaluation metrics (for EVAL mode)<br>    eval_metric_ops = {<br>      &quot;accuracy&quot;: tf.metrics.accuracy(<br>          labels=labels, predictions=predictions[&quot;classes&quot;])}<br>    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)</pre><pre>def main(unused_argv):<br>    # Load training and eval data<br>    mnist = tf.contrib.learn.datasets.load_dataset(&quot;mnist&quot;)<br>    train_data = mnist.train.images  # Returns np.array<br>    train_labels = np.asarray(mnist.train.labels, dtype=np.int32)<br>    eval_data = mnist.test.images  # Returns np.array<br>    eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)</pre><pre># Create the Estimator<br>    mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir=&quot;/tmp/mnist_convnet_model&quot;)</pre><pre># Set up logging for predictions<br>    tensors_to_log = {&quot;probabilities&quot;: &quot;softmax_tensor&quot;}<br>    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)</pre><pre># Train the model<br>    train_input_fn = tf.estimator.inputs.numpy_input_fn(<br>        x={&quot;x&quot;: train_data},<br>        y=train_labels,<br>        batch_size=100,<br>        num_epochs=None,<br>        shuffle=True)<br>    mnist_classifier.train(<br>        input_fn=train_input_fn,<br>        steps=20000,<br>        hooks=[logging_hook])</pre><pre># Evaluate the model and print results<br>    eval_input_fn = tf.estimator.inputs.numpy_input_fn(<br>        x={&quot;x&quot;: eval_data},<br>        y=eval_labels,<br>        num_epochs=1,<br>        shuffle=False)<br>    eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)<br>    print(eval_results)</pre><pre>if __name__ == &quot;__main__&quot;:<br>    tf.app.run()</pre><p>It configures our neural network in cnn_model_fn. Training happens in main. During our training step, we download the MNIST dataset, which is already broken up into a training and evaluation chunks. When training a neural network, you want to be sure you make a subset of your training data available for evaluation purposes. This allows you to test the accuracy of your neural network as training progresses. This can also prevent you from <a href="https://en.wikipedia.org/wiki/Overfitting">overfitting</a> your neural network to the training data.</p><p>Starting training is as easy using the command python train_cnn.py. Depending on the hardware configuration of your computer, training will take anywhere from minutes to hours. This script is configured to train the network for 20,000 iterations. While your training script is running, you’ll periodically see output that shows the progress of the training process.</p><pre>INFO:tensorflow:global_step/sec: 2.75874<br>    INFO:tensorflow:probabilities = [[ 0.10167542  0.10189584  0.10309957  0.11525927  0.09659223  0.08847987<br>       0.09406721  0.10499229  0.093654    0.10028425]<br>     [ 0.10425898  0.11098097  0.10286383  0.09657481  0.10871311  0.08486023 0.09235432  0.09499202  0.10640075  0.09800103]<br>     [ 0.1033088   0.11629853  0.11034065  0.0981971   0.08924178  0.09668511 0.10001212  0.09568888  0.08589367  0.10433336]<br>     [ 0.10667751  0.10386481  0.09242702  0.11075728  0.08897669  0.09205832 0.10070907  0.10779921  0.08927511  0.10745502]<br>    ...</pre><p>It shows the rate of training and an array of probabilities of that sample image being a number. For example:</p><pre>[ 0.00001972  0.00000233  0.00022174  0.00427989  0.00001842  0.97293282 0.00000114  0.00013626  0.00584014  0.01654756]</pre><p>There looks to be a 97.3% probability that this sample image is the number represented by this index (5 or 6 depending on the starting index). These values become more certain as training continues. The neural network is improving its ability to identify the handwritten digits.</p><p>Compare these probabilities at the beginning of training:</p><pre>[ 0.1033088 0.11629853 0.11034065 0.0981971 0.08924178 0.09668511 0.10001212 0.09568888 0.08589367 0.10433336]</pre><p>With these, near the end:</p><pre>[ 0.00000006 0.0000001 0.00000017 0.00000019 0.99616736 0.00000038, 0.00000154 0.00000558 0.00001187 0.00381267]</pre><p>You’ll notice that the network is becoming more accurate with it’s predictions.</p><p>Once training has finished, it will test the neural network against a second dataset of labeled images. This is used to calculated an accuracy of the trained network.</p><pre>INFO:tensorflow:Saving dict for global step 20000: accuracy = 0.9708, global_step = 20000, loss = 0.0991706</pre><p>As of step 20000, our neural network has a prediction accuracy of 97.8%! A bit about the loss value. The goal when training a neural network is to minimize the loss, or the difference between the predicted value and the actual value. Generally, a neural network with a lower amount of loss will give more accurate predictions.</p><p>Now that training is complete, we end up with a bunch of files in /tmp/mnist_convnet_model:</p><pre>checkpoint<br>eval<br>events.out<br>graph.pbtxt<br>model.ckpt-15199.data-00000-of-00001<br>model.ckpt-15199.index<br>model.ckpt-15199.meta<br>model.ckpt-20000.data-00000-of-00001<br>model.ckpt-20000.index<br>model.ckpt-20000.meta</pre><p>The files we are concerned with for the next steps are:</p><pre>graph.pbtxt // graph definition file, human readable protobuf format<br>model.ckpt-20000.data-00000-of-00001  //  variables from our graph model.ckpt-20000.index // identifies the checkpoint / training step<br>model.ckpt-20000.meta  // stores the structure of the graph</pre><p>We need to do a bit of massaging before integrating them into an Android app. We will:</p><ol><li>Freeze our graph and values. This stores the neural network and tensor values in one binary graph definition file (.pb), instead of separate files.</li><li>Optimize our graph for inference. After the training step finishes, our graph contains nodes that were used during training. These nodes are not needed when doing inferencing. This optimization will remove these extra nodes and tensors. This reduces the size of our graph and also reduces the chance that we’ll encounter an operation in our graph that isn’t supported by TensorFlow Mobile.</li></ol><h4>Freezing</h4><p>Freezing our graph is as easy as executing a Python script thats available in the <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py">TensorFlow repository</a>.</p><p>Execution looks like:</p><pre>python freeze_graph.py \<br>   --input_graph=/tmp/mnist_convnet_model/graph.pbtxt \<br>   --input_binary=false \<br>   --input_checkpoint=/tmp/mnist_convnet_model/model.ckpt-20000 \<br>   --output_graph=/tmp/mnist_convnet_model/frozen_graph.pb \<br>   --output_node_names=output \</pre><p>When this script has completed, you’ll see a new file frozen_graph.db in /tmp/mnist_convnet_model. It contains our graph definition and network values.</p><h4>Optimizing for Inference</h4><p>Now, we optimize our neural network for inferencing. There’s also a script that is available in the <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/optimize_for_inference.py">TensorFlow repository</a>. Optimizing for inference removes parts of the graph that are only needed for training.</p><pre>python optimize_for_inference.py \<br>   --input=/tmp/mnist_convnet_model/frozen_graph.pb \<br>   --output=/tmp/mnist_convnet_model/optimized_graph.pb \<br>   --input_names=input \<br>   --output_names=output</pre><p>When this script is finished, you’ll see a new file optimized_graph.db in /tmp/mnist_convnet_model. This is the final state of our neural network model.</p><p>This is a TensorBoard view our frozen and optimized graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/0*BD-U8_P3xSfdd6DJ.png" /></figure><p>If you want to visualize your optimized graph with TensorBoard, check out <a href="https://medium.com/@daj/how-to-inspect-a-pre-trained-tensorflow-model-5fd2ee79ced0">How to inspect pretrained TF model</a>.</p><p>We are now ready to begin using this in an Android project.</p><h3>Integrating into an Android App</h3><p>Deploying a trained TensorFlow neural network model is a relatively task.</p><h4>Adding the TensorFlow Mobile dependency</h4><p>Add the TensorFlow Mobile dependency to the build.gradle in the app/ folder, then sync the project’s Gradle dependencies.</p><pre>implementation &quot;org.tensorflow:tensorflow-android:1.5.0&quot;</pre><p>The class we are going to use to interact with our model, provided by TensorFlow Mobile, is TensorFlowInferenceInterface. It provides several methods for loading our model, feeding new data to the network, running inference, and extracting a prediction.</p><h4>Adding the model</h4><p>Copy your optimized graph to your Android project. It should be copied to src/main/assets. TensorFlowInferenceInterface will load the model from this folder in it’s constructor.</p><h4>Some architecture</h4><p>Our app will allow the user to draw a number with their finger. We will convert their drawing into a bitmap and pass that to our neural network for prediction. Recognizing this, the very first thing I will do is create a Classifier interface.</p><pre>interface Classifier { <br>   fun predict(input: IntArray): <br>   Int fun close() <br>}</pre><p>I am choosing to create an interface so that I can easily create more than one implementation of a Classifier. One using TensorFlow Mobile and one using TensorFlow Lite (in Part 2).</p><h4>Using TensorFlowInferenceInterface</h4><p>As stated before TensorFlowInferenceInterface is how we will be interacting with our trained network.</p><p>Let’s implement our Classifier interface by writing TFMobileClassifier.</p><pre>class TFMobileClassifier(context: Context,<br>   modelFilename: String,<br>   private val inputName: String,<br>   private val inputDimensions: Pair&lt;Long , Long&gt;,<br>   private val outputName: String,<br>   private val outputSize: Int) : Classifier {</pre><pre>   override predict(input: IntArray): Int {<br>      TODO()<br>   }</pre><pre>   override close() {<br>      TODO()<br>   }<br>}</pre><p>Our TFMobileClassifier has a constructor with 6 arguments. The Context is used to access files via AssetManager. The remaining arguments specify our model file and input and output node specifications.</p><p>Instantiating a TFMobileClassifier.</p><pre>val classifier: Classifier = TFMobileClassifier(this,<br>   modelFilename = &quot;file:///android_asset/optimized_graph.pb&quot;,<br>   inputName = &quot;input&quot;,<br>   inputDimensions = Pair(28, 28),<br>   outputName = &quot;output&quot;,<br>   outputSize = 100)</pre><p>Let’s create our TensorFlowInferenceInterface.</p><pre>private val assetManager = context.assetManager<br>private val inferenceInterface = <br>   TensorFlowInferenceInterface(assetManager, modelFilename)</pre><p>Now that we have have a TensorFlowInferenceInterface, let’s start using it by implementing predict().</p><pre>override fun predict(input: FloatArray) {<br>   // 1) create an array to store our predictions<br>   val predictions = LongArray(100)</pre><pre>   // 2) feed our data into input layer of our neural network<br>   inferenceInterface.feed(inputName, floatInput, 1,      <br>       inputDimensions.first, inputDimensions.second, 1)</pre><pre>   // 3) run inference between the input and specified output nodes<br>   inferenceInterface.run(arrayOf(outputName))</pre><pre>   // 4) fetch the predictions from the specified output node<br>   inferenceInterface.fetch(outputName, predictions)</pre><pre>   // 5) tabulate our predictions and return the most probable<br>   return processPredictions(predictions)<br>}</pre><p>A few things to talk about here:</p><ol><li>Our output node emits 100 values, so we need to store them in an array that contains at least 100 elements</li><li>Our input data array size must equal the value when you compute the total elements in a X * Y * Z array. For example, our neural network uses 28 x 28 monochrome images. Our dimensions are going to be: 28 x 28 x 1. This means our input data array should contain 784 values.</li><li>When running inference, we need to specify the name of the output node where inference will end.</li><li>After inference has completed, we will store our results in the 100 element predictions array. This particular neural network returns an array containing 100 predictions given the input data. Going back to our Python training script, we trained our network in with batch_size = 100. This means, even though we feed the neural network a single image, it will give us 100 predictions on what it thinks the user has drawn.</li><li>Because we have 100 predictions, we need to count the occurrence of each prediction, then return the digit that was the predicted the most. We will use this value as our prediction.</li></ol><p>Our implemented TFMobileClassifier.</p><pre>package com.emuneee.tensorandflow.classifier</pre><pre>import android.content.Context<br>import android.content.res.AssetManager<br>import org.tensorflow.contrib.android.TensorFlowInferenceInterface<br>import timber.log.Timber<br>import java.util.*<br>import kotlin.Comparator</pre><pre>/**<br> * Created by evan on 2/28/18.<br> */<br>class TFMobileClassifier(context: Context,<br>   modelFilename: String,<br>   private val inputName: String,<br>   private val inputDimensions: &lt;Long , Long&gt;,<br>   private val outputName: String,<br>   private val outputSize: Int) : Classifier {</pre><pre>   private val assetManager: AssetManager = context.assets<br>   private val inferenceInterface = <br>      TensorFlowInferenceInterface(assetManager, modelFilename)</pre><pre>   override fun predict(input: IntArray): Int {<br>        val floatInput = input.map { it.toFloat() }<br>                .toFloatArray()<br>        // 1) create an array to store our predictions<br>        val predictions = LongArray(outputSize)</pre><pre>        // 2) feed our data into input layer of our neural network<br>        inferenceInterface.feed(inputName, floatInput, 1, <br>           inputDimensions.first, inputDimensions.second, 1)</pre><pre>        // 3) run inference between the input and output nodes<br>        inferenceInterface.run(arrayOf(outputName))</pre><pre>        // 4) fetch the predictions from the specified output node<br>        inferenceInterface.fetch(outputName, predictions)</pre><pre>        // 5) tabulate our predictions and return the most probable<br>        return processPredictions(predictions)<br>   }</pre><pre>   private fun processPredictions(predictions: LongArray): Int {<br>      val counts = predictions.toTypedArray()<br>            .groupingBy { it }<br>            .eachCount()<br>      val predictionSet = TreeSet&lt;Pair&lt;Long, Int&gt;&gt;    <br>         (Comparator&lt;Pair&lt;Long, Int&gt;&gt; { o1, o2 -&gt;     <br>         o2!!.second.compareTo(o1!!.second) })<br>      counts.toList()<br>            .forEach { pair -&gt; predictionSet.add(pair) }<br>      val pair = predictionSet.first()<br>      Timber.d(&quot;Selecting ${pair.first} @ ${(pair.second / 100.0) *<br>          100}% confidence&quot;)<br>      return pair.first.toInt()<br>   }</pre><pre>   override fun close() {<br>      inferenceInterface.close()<br>   }<br>}</pre><h4>Using the Classifier</h4><p>Now that we have implemented a Classifier, it’s time to build some UI that allows the user to submit data with their fingertips. For brevity’s sake, I’m going to pass over a lot of the pure Android concepts, like layouts, and click listeners, etc. Our user interface has 3 components:</p><ol><li>We have a custom CanvasView that allows the user to user their fingertips to draw on a Canvas. When the user has finished drawing on the CanvasView it will emit a bitmap that represents the user’s drawing via a CanvasView.DrawListener</li><li>We’ll have an ImageView that resembles actual data submitted to the neural network.</li><li>Finally, we’ll have a TextView that displays the prediction.</li></ol><p>Before we continue, we will need to address an issue. We’ll need to convert the user input to data format that resembles an image from the MNIST dataset. This is critical because the closer the data resembles the original training data, the more accurate our predictions. The MNIST training data set is filled with 28×28 monochrome images where for a given pixel, the values range from 0 (white) to 255 (black).</p><p>Here is how we convert the bitmap from our CanvasView to a monochrome, 28×28 bitmap:</p><pre>private fun toMonochrome(bitmap: Bitmap): Bitmap {<br>        // scale bitmap to 28 by 28<br>        val scaled = Bitmap.createScaledBitmap(bitmap, 28, 28, false)</pre><pre>// convert bitmap to monochrome<br>        val monochrome = Bitmap.createBitmap(28, 28, Bitmap.Config.ARGB_8888)<br>        val canvas = Canvas(monochrome)<br>        val ma = ColorMatrix()<br>        ma.setSaturation(0f)<br>        val paint = Paint()<br>        paint.colorFilter = ColorMatrixColorFilter(ma)<br>        canvas.drawBitmap(scaled, 0f, 0f, paint)</pre><pre>val width = monochrome.width<br>        val height = monochrome.height</pre><pre>val pixels = IntArray(width * height)<br>        monochrome.getPixels(pixels, 0, width, 0, 0, width, height)</pre><pre>// Iterate over height<br>        for (y in 0 until height) {</pre><pre>for (x in 0 until width) {<br>                val pixel = monochrome.getPixel(x, y)<br>                val lowestBit = pixel and 0xff</pre><pre>if (lowestBit &amp;lt; 128) {<br>                    monochrome.setPixel(x, y, Color.BLACK)<br>                }<br>                else {<br>                    monochrome.setPixel(x, y, Color.WHITE)<br>                }<br>            }<br>        }<br>        return monochrome<br>}</pre><p>The output from toMonochrome() is used to give the user an idea of what the input to the neural network looks like. It’s also converted to a format suitable for inference:</p><pre>private fun formatInput(bitmap: Bitmap): IntArray {<br>   val pixels = IntArray(bitmap.width * bitmap.height)<br>   var i = 0</pre><pre>   for (y in 0 until bitmap.height) {</pre><pre>      for (x in 0 until bitmap.width) {<br>         pixels[i++] = if (bitmap.getPixel(x, y) == Color.BLACK) 255 <br>                       else 0<br>      }<br>    }<br>    return pixels<br>}</pre><p>We do two things here. First we flatten our 28×28 bitmap into a 784 element integer array. Finally, we convert each pixel value to either 0 or 255 if the pixel value is white or black, respectively.</p><p>Our MainActivity.kt looks like:</p><pre>package com.emuneee.tensorandflow<br><br>import android.graphics.*<br>import android.support.v7.app.AppCompatActivity<br>import android.os.Bundle<br>import kotlinx.android.synthetic.main.activity_main.*<br>import android.graphics.Bitmap<br>import com.emuneee.tensorandflow.classifier.Classifier<br>import com.emuneee.tensorandflow.classifier.TFMobileClassifier<br>import com.emuneee.tensorandflow.view.CanvasView<br>import timber.log.Timber<br><br>class MainActivity : AppCompatActivity() {<br><br>    private val classifier: Classifier by <em>lazy </em><strong>{<br>        </strong>TFMobileClassifier(this,<br>                modelFilename = &quot;file:///android_asset/optimized_graph.pb&quot;,<br>                inputName = &quot;input&quot;,<br>                inputDimensions = Pair(28, 28),<br>                outputName = &quot;output&quot;,<br>                outputSize = 100)<br>    <strong>}<br><br>    </strong>override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>activity_main</em>)<br>        Timber.plant(Timber.DebugTree())<br><br>        canvas.drawListener = object: CanvasView.DrawListener {<br>            override fun onNewBitmap(bitmap: Bitmap) {<br><br>                Thread(<em>Runnable </em><strong>{<br><br>                    </strong>// convert the drawing to a 28x28 monochrome image<br>                    val monochrome = toMonochrome(bitmap)<br><br>                    // set the nn input image<br>                    runOnUiThread <strong>{ </strong>scaledCanvas.setImageBitmap(monochrome) <strong>}<br><br>                    </strong>// convert the data to something that resembles the MNIST training data set<br>                    val inputData = toIntArray(monochrome)<br><br>                    // predict<br>                    val pred = classifier.predict(inputData)<br>                    runOnUiThread <strong>{ </strong>prediction.<em>text </em>= pred.toString() <strong>}<br><br>                }</strong>).start()<br>            }<br>        }<br>    }<br><br>    override fun onDestroy() {<br>        super.onDestroy()<br>        classifier.close()<br>    }<br><br>    <em>/**<br>     * Converts a Bitmap to a 28 x 28 monochrome bitmap<br>     */<br>    </em>private fun toMonochrome(bitmap: Bitmap): Bitmap {<br>        // scale bitmap to 28 by 28<br>        val scaled = Bitmap.createScaledBitmap(bitmap, 28, 28, false)<br><br>        // convert bitmap to monochrome<br>        val monochrome = Bitmap.createBitmap(28, 28, Bitmap.Config.ARGB_8888)<br>        val canvas = Canvas(monochrome)<br>        val ma = ColorMatrix()<br>        ma.setSaturation(0f)<br>        val paint = Paint()<br>        paint.<em>colorFilter </em>= ColorMatrixColorFilter(ma)<br>        canvas.drawBitmap(scaled, 0f, 0f, paint)<br><br>        val width = monochrome.<em>width<br>        </em>val height = monochrome.<em>height<br><br>        </em>val pixels = IntArray(width * height)<br>        monochrome.getPixels(pixels, 0, width, 0, 0, width, height)<br><br>        for (y in 0 <em>until </em>height) {<br><br>            for (x in 0 <em>until </em>width) {<br>                val pixel = monochrome.getPixel(x, y)<br>                val lowestBit = pixel and 0xff<br><br>                if (lowestBit &lt; 128) {<br>                    monochrome.setPixel(x, y, Color.<em>BLACK</em>)<br>                }<br>                else {<br>                    monochrome.setPixel(x, y, Color.<em>WHITE</em>)<br>                }<br>            }<br>        }<br>        return monochrome<br>    }<br><br>    <em>/**<br>     * Converts a bitmap to a flattened integer array<br>     */<br>    </em>private fun toIntArray(bitmap: Bitmap): IntArray {<br>        val pixels = IntArray(bitmap.<em>width </em>* bitmap.<em>height</em>)<br>        var i = 0<br><br>        for (y in 0 <em>until </em>bitmap.<em>height</em>) {<br><br>            for (x in 0 <em>until </em>bitmap.<em>width</em>) {<br>                pixels[i++] = if (bitmap.getPixel(x, y) == Color.<em>BLACK</em>) 255 else 0<br>            }<br>        }<br>        return pixels<br>    }<br>}</pre><p>That’s it! We have trained a neural network to recognize handwritten digits using TensorFlow, then successfully deployed it via an Android app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*R_ggEZAW7byM7C-D.gif" /></figure><p>In Part 2, I am going to re-implement our Classifier interface using TensorFlow Lite, instead of TensorFlow Mobile. TensorFlow Lite is a more lightweight framework for doing inference on a mobile device. It can also make use of specialized Neural Network acceleration hardware on Android 8.1+ devices.</p><p>In the meantime, all code, scripts, and model can be accessed on <a href="https://github.com/emuneee/tensorandflow">GitHub</a>.</p><p><em>Originally published at </em><a href="https://emuneee.com/blog/2018/03/08/tensor_and_flow_pt_1/"><em>emuneee.com</em></a><em> on March 8, 2018.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73fd536a1a7c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/tensor-flow-part-1-tensorflow-machine-learning-on-android-73fd536a1a7c">Tensor &amp; Flow: Part 1, TensorFlow &amp; Machine Learning on Android</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Journey Towards Android App Log Subscriptions]]></title>
            <link>https://medium.com/@evanhalley/a-journey-towards-android-app-log-subscriptions-84c3da1fb84f?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/84c3da1fb84f</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[rxjava]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Wed, 14 Feb 2018 17:58:28 GMT</pubDate>
            <atom:updated>2018-02-14T18:39:59.695Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve got an Android project going that uses <a href="https://www.ffmpeg.org/">FFmpeg</a> to build short video files (more on this at a later date). Normally, you run FFmpeg in a terminal window or command prompt. It updates you on the status of a task in that terminal window. I’ve <a href="https://medium.com/@ilja.kosynkin/building-ffmpeg-for-android-607222677a9e">instrumented FFmpeg on Android</a> with some C code and a JNI interface. When I execute FFmpeg from an Android app those status updates are written to logcat.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hfyMG6KAsrhLpdxyPaP4BQ.png" /></figure><p>There are some important details that I was to surface in my app, primarily the progress of an encoding process. In order to do this, I needed a way to read my Android apps logging statements. This is possible on Android and without needing to request the scary READ_LOGS Android permission.</p><p><em>Note: All of my examples are written in Kotlin because I ❤ Kotlin.</em></p><p>You can execute a process with the <a href="https://developer.android.com/reference/java/lang/Runtime.html">Runtime</a> API:</p><pre>val process = Runtime.getRuntime().exec(&quot;some_command_here&quot;)</pre><p>You can then get an InputStream to the Process’s output:</p><pre>val process = Runtime.getRuntime().exec(&quot;some_command_here&quot;) val inputStream = process.inputStream</pre><p>Putting it all together, this is how one would get your apps logging statements from logcat, within the scope of your app:</p><pre>val process = Runtime.getRuntime().exec(&quot;logcat&quot;) <br>reader = BufferedReader(InputStreamReader(process.inputStream)) <br>var line: String?</pre><pre>do {     <br>  line = reader.readLine()      <br>  if (line != null) {         <br>    // do something with this logging statement     <br>  } <br>} while (line != null)  </pre><pre>reader.close()</pre><p>This code essentially runs forever because, as far as I’ve seen, reader doesn’t return a null line.</p><p>I’m close, but I still needed to add some sophistication. I needed to read and parse logs while FFmpeg was running so I can update the user on the status of their job. I needed to start parsing logs when I wanted to update the user while FFmpeg was running and end parsing when FFmpeg was complete. This problem is perfectly solved using RxJava:</p><pre>fun readLogs(): Observable = <br>  return Observable.create ({ emitter -&gt; <br>    var reader: BufferedReader? = null </pre><pre>    try { <br>      val process = Runtime.getRuntime().exec(&quot;logcat&quot;) <br>      r = BufferedReader(InputStreamReader(process.inputStream)) <br>      var line: String? </pre><pre>      do { <br>        line = r.readLine() <br>        <br>        if (line.isNotBlank()) { <br>          emitter.onNext(line) <br>        } <br>      } while (line != null) </pre><pre>      emitter.onComplete() <br>    } catch (e: Exception) { <br>      emitter.onError(e) <br>    } finally { <br>      r?.close() <br>    } <br>})</pre><p>With RxJava, I can emit a logging statement as it’s received by logcat.</p><p>Usage:</p><pre>val disposable = readLogs() <br>  .subscribeOn(Schedulers.io())<br>  .observeOn(AndroidSchedulers.mainThread()) <br>  .subscribe({ logLine -&gt; logTextView.setText(logLine) }, <br>    { error -&gt; throw error }) </pre><pre>// later, when you no longer need to subscribe to your logs disposable.dispose()</pre><p>Because it’s all based on RxJava, you can do more advanced things like filtering:</p><pre>val disposable = readLogs()<br>  .filter({ line -&gt; line.startsWith(&quot;foo&quot;) })<br>  .subscribeOn(Schedulers.io())<br>  .observeOn(AndroidSchedulers.mainThread()) <br>  .subscribe({ logLine -&gt; logTextView.setText(logLine) }, <br>    { error -&gt; throw error })</pre><pre>// later, when you no longer need to subscribe to your logs disposable.dispose()</pre><p>This allows you to “subscribe” to your apps logs!</p><p>/* fini */</p><p><em>Originally published at </em><a href="https://emuneee.com/blog/2018/02/14/reading-your-android-apps-logs"><em>emuneee.com</em></a><em> on February 14, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84c3da1fb84f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Side Project]]></title>
            <link>https://medium.com/@evanhalley/the-side-project-a50ec8bbaa77?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a50ec8bbaa77</guid>
            <category><![CDATA[side-project]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 15:25:50 GMT</pubDate>
            <atom:updated>2017-12-11T15:28:15.094Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*saXQ1y2-TsAcusQfwWp4gw.jpeg" /></figure><p>This is an essay from my weekly post called <em>The Comment</em>. <em>The Comment </em>is a weekly digest of the stuff that grabbed my attention or occupied some part my mind during the past week. Normally, it’ll be one thing that’s really been on my mind, followed by a handful of things that I found interesting. <em>The Comment</em> will be published on <a href="http://emuneee.com/">emuneee.com</a> each Monday at 10:30AM EST.</p><p>~~</p><p>I’ve been in the software industry, as a professional since my first internship at Bayer CropScience in 2003 (thanks <a href="http://inroads.org/">Inroads</a>!). I wrote test cases and did some automated testing for the next 3 summers. My side project was a Java Swing (remember that) GPA calculator and college. My next side project, somewhere between college graduation and Sony Ericsson was a Windows Mobile app, Speeed Reader. Speeed Reader was a Google Reader app for Windows Mobile, and also my first mobile app. A few mobile app side projects and few layoffs later, I started as an Android developer for a healthcare and conference app agency in Durham. After a 3 year stint in the digital marketing automation space, some part-time mobile contract work…and another side project, PremoFM: a podcast app for Android, I found myself back in mobile in a bigger way. Fast forward to today and I work on <a href="http://radiopublic.com/">RadioPublic</a>’s Android app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*xUcjy4BKmpp3-ozho5a53A.jpeg" /></figure><p>Most of my significant learning and career progressions, started out as little ideas and curiosities that became side projects. Side projects allow you to exercise your creativity, while doing some learning that you would not normally get in a work setting. Finally, side projects can grow into significant things like job opportunities or even provide you the ability to work for yourself.</p><p>Side projects are great ways to get some practical hands on experience. When I graduated, it definitely wasn’t my intention to become a mobile app developer. I started out just learning some C# because as an NC State student, access to Visual Studio and the .NET technologies were free! I had a Windows Mobile device and the ability to tinker around, so I thought to myself, “why not?” I started out with the goal of gaining more general programming experience and soon found myself building real things like Speeed Reader, a Google Reader client for Windows Mobile. Over the next few years, I built things for mobile, web, and on the server. I learned new things like, building an API with Node, building MVC webapps with PHP, and SQL. Experience with all of these technologies not only made me a more well rounded engineer with a ton of different perspective’s but also critical to building the skills that enable quick and productive learning.</p><p>The wrap things up, if you see a thing you want to eventually do, don’t feel the need to wait for permission. Just do it. This applies to plenty of things, besides building software. The barrier to entry to a lot of opportunities is the friction it takes to do a Google search.</p><p>~~</p><p>Read the rest of <em>The Comment</em> at <a href="http://emuneee.com/">emuneee.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a50ec8bbaa77" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[// The Comment #1: Pixelization]]></title>
            <link>https://medium.com/@evanhalley/the-comment-1-pixelization-63d8edf68c89?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/63d8edf68c89</guid>
            <category><![CDATA[pixel-xl-2]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[pixel-2]]></category>
            <category><![CDATA[alphago]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Mon, 30 Oct 2017 14:30:01 GMT</pubDate>
            <atom:updated>2017-11-01T13:26:07.011Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G8AElICyHzOB3zPvib9SsQ.jpeg" /><figcaption>#TeamPixel</figcaption></figure><p><em>The Comment is a weekly digest of the stuff that grabbed my attention or occupied some part my mind during the past week. Normally, it’ll be one thing that’s really been on my mind, followed by a handful of things that I found interesting. The Comment will be published each Monday at 10:30AM EST at </em><a href="http://emuneee.com/"><em>emuneee.com</em></a><em>.</em></p><p><em>Thanks for reading.</em></p><h3>## The Google Pixel 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RaPKwgEIp4MIq8i2TrTa3w.jpeg" /><figcaption>Right: Pixel 2 XL, Left: Pixel XL</figcaption></figure><p>I’m not a tech reviewer, but I got my hands on the Google Pixel 2 in XL form. I’ve been using it for about 5 days. I’m coming from a Pixel XL “OG”. The Pixel 2 XL is pretty great for reasons that are important to me. Here’s why:</p><h4>Camera</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AsHXi7e0rXbLPSveeBOv5Q.jpeg" /><figcaption>Washington Memorial, shot on a Galaxy Nexus, cameras have made big leaps since then</figcaption></figure><p>Phones from Google, first the Nexus line and now the Pixel line, haven’t always been great at taking pictures. Starting with the Nexus 5X &amp; Nexus 6P, that changed and continued with the original Pixel. Things have taken a serious step forward on the Google Pixel 2. The images that come off of the sensor and processed through Google’s algorithms are nothing short of incredible. It’s the primary reason I upgraded. I take a lot of pictures, mostly of my daughter, so having the best possible quality, so conveniently, is a huge plus. I enjoy how effortless it is to get some pretty fantastic pictures. Portrait mode is also great, though not perfect. Sometimes, edges around faces or hair aren’t blurred consistently or light causes some distortion.</p><p>The video quality is fine, but the audio quality is so disappointing. It sounds “canny” or super compressed. It’ll probably be <a href="http://www.androidpolice.com/2017/10/27/pixel-2-xl-units-audio-issues-recording-video/">fixed in a software update</a>, but in the meantime, any precious memories captured on video will have pretty bad audio.</p><p>Finally, Google Photos continues to be a great complement to the Pixel line and one of Google’s greatest products.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*klEddcDTtdOy2q1zaEHQIQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v_jE5AhJaGV1UA29GiGetw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*h2jCwIcLgXZIoIZSUwfyuw.jpeg" /><figcaption>Portrait mode with the front facing camera</figcaption></figure><h4>Software</h4><p>I’ve used Android since 2010, when I failed to buy an iPhone 4 and bought a <a href="https://www.gsmarena.com/samsung_i897_captivate-3408.php">Samsung Captivate</a> (the carrier bloated version of the original Samsung Galaxy S) running Android 2.1 “Eclair”. <strong><em>Side note, how different would my life be if I would have bought an iPhone 4?</em></strong> It was because of that phone that I overwhelmingly prefer “clean” versions of Android. No weird theming, terrible customizations, and useless features. I just want fast software and quick updates. The Pixel 2 checks all of those boxes. Finally, the software experience is consistently quick, as it was on OG Pixel.</p><h4>Front Facing Speakers</h4><p>When I jumped from the Nexus 6P (and from the Nexus 6 before that) to the original Pixel, I gave up the front facing stereo speakers. They are back on the Pixel 2 and sound great. I no longer need to cup my phone to hear music, podcasts, and videos from the OG Pixel’s single down firing speaker. The Pixel 2’s speakers get pretty loud, have decent stereo separation, and even a teeny bit of bass.</p><h4>Machine Learning</h4><p>Sometime between the official Pixel 2 announcement and the device becoming available, <a href="https://www.blog.google/products/pixel/pixel-visual-core-image-processing-and-machine-learning-pixel-2/">Google announced that the Pixel 2 has a special purpose machine learning chip</a> embedded in it capable of 3 trillion operations per second (for comparison’s sake, the iPhone 8/X’s “Neural Engine” peaks at 600 million operations per second). The chip is comprised of 8 compute cores or what Google is calling IPUs (Image Processing Units), on-die memory, and a smaller processing core to keep data fed to the 8 IPUs. This chip’s purpose is to offload the HDR+ from the CPU and to accelerate machine learning. Introduced in the Nexus 5X/6P,<a href="https://www.theverge.com/2016/10/18/13315168/google-pixel-camera-software-marc-levoy"> Google’s HDR+ algorithms quickly take a stack of photos (the last 10 or 15) and uses the combined data to drastically reduce noise and increase the dynamic range</a>. This gives you great looking pictures where the shadows have a pretty good amount of detail and the lightest parts of the photo aren’t blown out. On OG Pixel, some portion of this algorithm was accelerated by the Hexagon DSP in Qualcomm’s Snapdragon 821 with the rest running on the CPU. The same is happening on the Pixel 2, but over the next few months Google will run these algorithms on the Pixel Visual Core which will speed up HDR processing by a factor of 5 while reducing power usage by 90%.</p><p>Google is also opening this chip up to developers of camera apps who want to run their Halide image processing code and those who want to run their trained TensorFlow neural network models on the Pixel. The latter is the part that intrigues me. I’ve been toying around with machine learning and getting close to having something deployable. The ability to do inference (inference is using an already training neural network to make an estimate on new data) is entirely on the device, with decent performance is valuable to making the user experience better for apps that make use of machine learning.</p><h4>Display “Drama”?</h4><p>It’s fine. The colors are noticeably less saturated than the Pixel XL, but I’d rather have a properly calibrated display. Otherwise, it looks like any other competent smartphone display.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G3l3b7Vx1E0Nd90vzLEZgg.jpeg" /><figcaption>Left: Pixel 2 XL, Right: Pixel XL</figcaption></figure><h4>Random thoughts</h4><p>The texture on the back of Pixel 2 is an improvement over the slippery-ness of the Pixel. It’s more grippy.</p><p>The fingerprint sensor is noticeably quicker at recognizing my fingerprint than the OG Pixel.</p><p>The ambient music recognizing is pretty cool, though it wasn’t consistent. It recognized music in loud bars and restaurants, music in movies, and sometimes music on the radio. It even correctly recognized a remixed Rihanna track (Rihanna vocals for “Work” over a different beat).</p><p>The body and display are more rounded than the OG Pixel XL. Using the Pixel 2 XL feels more comfortable in hand than the OG Pixel XL.</p><p>The always-on ambient display is super useful. It’s easy to glance at without having to do the double tap dance needed on the OG Pixel XL. A couple of subtle things about the ambient display. It adjusts its brightness depending on the lighting in your environment and it’s not always on. If the proximity sensor detects that something is right in front of it (ie. the phone is in your pocket or upside down on a table), the screen shuts off.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJl2eIb_TsCIknY_7iVYww.jpeg" /><figcaption>Ambient music recognition of one of the greatest southern beats of all time</figcaption></figure><p>The Pixel 2 XL is a worthy upgrade over the OG Pixel. It’s expensive. I had an almost pristine OG Pixel XL, so I got a pretty good deal on the trade in that helped bring the cost down.</p><h3>// Alpha Go Zero learns the rules</h3><p>In 2015 and 2016, Google’s AI division, DeepMind, trained an artificial intelligence to play and master the ancient game of Go. They training method they used a lot of different machine learning techniques, including, learning from amateur and professional Go players. AlphaGo mastered Go and surprisingly beat the best Go players in the world in 2016.</p><p>The next version of AlphaGo, AlphaGo Zero takes things further. This time around, AlphaGo Zero just learned the rules to game. It then trains itself by playing itself millions of times, leveraging reinforcement learning. As a result Alpha Go Zero, not only master Go more quickly, it did so with fewer compute resources, and attained a higher level of expertise in the game of Go. This is fascinating because it shows how efficient computers can be if they aren’t restricted to human level understanding, as was the earlier versions of AlphaGo. To me, this is a breakthrough.</p><p><a href="https://deepmind.com/blog/alphago-zero-learning-scratch">Alpha Go Zero</a></p><h3>// Three Step Guide to Blockchain</h3><p>I’m still wrapping my head around the technology that enables Bitcoin and Ethereum, Blockchain. Thijs Maas lays out 3 fundamental technologies that makes Blockchain so awesome.</p><p><a href="https://hackernoon.com/3-steps-to-understanding-blockchain-8a285572daa3">The Quick, 3-Step Guide to Blockchain Technology</a></p><h3>// In rotation: “Laila’s Wisdom” from Rapsody</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*f5MNsfDKn-6DzyJBWnf7jg.jpeg" /></figure><p>This album is so so so dope. There are a handful of albums that have been memorable and still in my rotation to this day years after their release date. Off the top of me head, a few of them are J. Cole’s “The Warmup”, Katrynada’s “99.9%”, Little Brother’s “The Minstrel Show”, and Wiz Khalifa’s “Kush &amp; Orange Juice”. I’m 100% sure “Laila Wisdom” is going to end up in this list. It’s such a smooth listen from beginning to end. I’m also a huge fan of the midtrack beat switch up.</p><p><a href="https://open.spotify.com/album/3ZF5L9dSjXunkIiFp1XSfK">“Laila’s Wisdom” on Spotify</a></p><p>/* fini */</p><p>Checkout <a href="http://emuneee.com/blog/2017/10/23/the-comment-0-twitter-a-little-less-2/">// The Comment #0</a>.</p><p><em>Originally published at </em><a href="http://emuneee.com/blog/2017/10/30/the-comment-1-pixelization/"><em>emuneee.com</em></a><em> on October 30, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63d8edf68c89" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Share the Cache]]></title>
            <link>https://medium.com/@evanhalley/share-the-cache-1f077909bf0f?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f077909bf0f</guid>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Thu, 13 Apr 2017 02:23:16 GMT</pubDate>
            <atom:updated>2017-04-13T14:31:02.021Z</atom:updated>
            <content:encoded><![CDATA[<p>A lot of Android apps make viewing images a core part of their user experience. Many of these apps use image caching libraries, like <a href="https://github.com/bumptech/glide">Glide</a>, to make image caching easy, robust, and configurable. Sharing these cached images can be a bit tricky. A few questions arose when I tried:</p><ul><li>How do I access files in the cache?</li><li>Do other apps have access to these files?</li><li>What are the needed permissions if I need to manually copy the file somewhere else?</li></ul><p>Those are just a few of the questions that came up, each with a pretty simple answer. A first pass at sharing images in my cache involved, re-caching the image to a public directory (ie. root of the external storage device), generating a URI, and passing that URI to an Intent to share with other apps. This sounds simple, but it is complicated by the fact that I needed to ask the user for permission to read and write to the external storage on their device, if they were running Android 6.0 Marshmallow or above. This flow worked, but I was looking for something much simpler for the user and myself, the developer. Enter the FileProvider.</p><h3>The FileProvider API</h3><p>The <a href="https://developer.android.com/reference/android/support/v4/content/FileProvider.html">FileProvider API</a>, added to the Android Support libraries in version 22.0.0, is a ContentProvider-like API that allows URI specific sharing of files relevant to your application. It can, temporarily, enable access (read and/or write) to the file at the URI. You also do not need to copy the file to a more accessible location on the user’s device. Setting up the FileProvider is very straightforward.</p><h3>Setting up a File Provider</h3><p>Setting up the FileProvider is a three step process.</p><p>First, like we would do with a ContentProvider, we added a <strong>&lt;provider&gt;</strong> entry to our AndroidManifest.xml file.</p><pre>&lt;provider <br>    android:name=&quot;android.support.v4.content.FileProvider&quot;    <br>    android:authorities=&quot;com.yourdomain.android.fileprovider&quot; <br>    android:grantUriPermissions=&quot;true&quot; <br>    android:exported=&quot;false&quot;&gt; </pre><pre>    &lt;meta-data <br>        android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot; <br>        android:resource=&quot;@xml/filepaths&quot; /&gt;<br>&lt;/provider&gt;</pre><p>This provider entry has a <strong>&lt;meta-data&gt;</strong> element that points to an XML file, that defines what paths, within our application file structure, we want to expose with our FileProvider. This is important. You can only share files in the paths contained in this XML file. I created an XML file in the “res/xml” folder.</p><pre>&lt;paths&gt; <br>    &lt;external-files-path name=&quot;image_files&quot; path=&quot;.&quot; /&gt; <br>&lt;/paths&gt;</pre><p>In my particular instance, I am caching images to the <a href="https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()">External Files directory</a> (which can be located on the SD card or a portion of internal memory, simulating an SD Card). Other tags you can use here:</p><p>The name attribute is a string that will be used as a URI-path component in the URI that’s generated by FileProvider. The path attribute, is the relative path to folder containing the files you would like share. In my case, I’m fine with sharing the root because it only contains cached images, but you should probably be as granular as possible in the case where you have multiple directories or file types in this directory.</p><p>Now that my FileProvider is set up and configured, how do I share files out of my cache.</p><h3>Sharing Cached Files</h3><p>The specifics on sharing cached image files is highly dependant on the Image caching library used and how it’s configured. At a high level, the process is:</p><ol><li>Cache an image to a location (done by an image caching library) that falls in the location specified in XML configuration file given to the FileProvider.</li><li>Get a reference to that cached image using the <a href="https://developer.android.com/reference/java/io/File.html">java.io.File</a> API.</li><li>Pass the File reference to <a href="https://developer.android.com/reference/android/support/v4/content/FileProvider.html#getUriForFile(android.content.Context,%20java.lang.String,%20java.io.File)">FileProvider.getUriForFile()</a> to get a URI you can pass to other apps.</li><li>Pack that URI into an Android Intent to start sharing with other apps.</li></ol><p>Very straightforward. Now this is how I did it with Glide.</p><p>My first step was to specify a location for Glide to store cached images. I needed to subclass <a href="http://bumptech.github.io/glide/javadocs/360/com/bumptech/glide/load/engine/cache/DiskLruCacheFactory.html">DiskLruCacheFactory</a> to make this happen (Note: you don’t need to subclass DiskLruCacheFactory if you want to use the Cache directory).</p><pre>private static class DiskCacheFactory extends DiskLruCacheFactory { </pre><pre>    DiskCacheFactory(final Context context, final String     <br>        diskCacheName, long diskCacheSize) { <br>            super(new CacheDirectoryGetter() { <br>               File cacheDirectory = new   <br>                   File(context.getExternalFilesDir(null), <br>                          diskCacheName); <br>               return cacheDirectory; <br>            }, (int) diskCacheSize); <br>    } <br>}</pre><p>As you can see from the code snippet, I pass in a reference to the External Files directory. By default, Glide uses the Cache directory.</p><p>I implement a GlideModule so that I can customize caching behavior in my application. I specify my subclassed DiskLruCacheFactory class in my GlideModule.</p><pre>public class MyGlideModule implements GlideModule { <br>    private static final int IMAGE_CACHE_SIZE = 200_000_000; <br>    <br>    @Override <br>    public void applyOptions(Context context, GlideBuilder <br>        builder) { <br>        builder.setDiskCache(new DiskCacheFactory(context, “.”, <br>            IMAGE_CACHE_SIZE)); <br>    } </pre><pre>    @Override public void registerComponents(Context context, Glide <br>        glide) { <br>            // ... <br>    } <br>}</pre><p>Now, I instruct Glide where it can find my GlideModule by adding a <strong>&lt;meta-data&gt;</strong> tag to the AndroidManifest.xml file, in <strong>&lt;application&gt;</strong> element.</p><pre>&lt;meta-data <br>    android:name=&quot;com.yourdomain.android.MyGlideModule&quot;<br>        android:value=&quot;GlideModule&quot;/&gt;</pre><p>Next, I invoke a manual file caching with Glide.</p><pre>File file = Glide.with(context) <br>    .load(uri) // uri to the location on the web<br>    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)     <br>    .get();</pre><p>This is specific to Glide and worth mentioning. When Glide caches a file to disk, the file name is comprised of generated key with an integer appended to it. There is no extension. This is important because sharing an extension-less image will make it difficult for the apps you are sharing with to determine how this file should be handled (despite the MIME type being sent along with the image in the Intent).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T8ITnlCFdjNBi4JsI6lEuw.png" /><figcaption>Contents of the Glide image cache folder</figcaption></figure><p>I worked around this issue by simply copying that file, appending an extension to the duplicate in the process. Since I know I am always handling JPEGs, I give these files the “.jpg” extension. This may not work in cases where you may be dealing with different types of images like, GIFs, PNGs, WebP, etc.</p><p>Finally, I can get a URI from the FileProvider for my cached image by calling <strong>FileProvider.getUriForFile()</strong>.</p><pre>// be sure to use the authority given to your FileProvider <br>String authority = “com.yourdomain.android.fileprovider”; <br>Uri uri = FileProvider.getUriForFile(context, authority, file); Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType(&quot;image/jpeg&quot;); context.startActivity(Intent.createChooser(intent, “Share via”);</pre><p>The Uri generated by the FileProvider looks like:</p><p><em>content://com.yourdomain.android.fileprovider/external_files/bb65e6a364264255b4833d34e____some_key.0.jpg</em></p><p>Once the Intent makes its way to the target app, things should look just like you are sharing a picture you’ve just taken.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b55yVx2GJiyYbGd7cidvQw.png" /><figcaption>Sharing an image from Traffcams to Gmail</figcaption></figure><p>#Dassit</p><p><em>Originally published at </em><a href="http://emuneee.com/blog/2017/04/12/share-the-cache/"><em>emuneee.com</em></a><em> on April 13, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f077909bf0f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Diving into Android Things]]></title>
            <link>https://medium.com/hackernoon/diving-into-android-things-d46bf3d6fb92?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d46bf3d6fb92</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[android-things]]></category>
            <category><![CDATA[electronics]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Wed, 15 Feb 2017 16:32:03 GMT</pubDate>
            <atom:updated>2019-04-27T09:58:07.239Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve always tinkered with electronics since my teens. I went to school and graduated with a Computer Engineering degree with a focus on hardware (embedded systems, ASIC design, etc). I somehow stumbl...got into software since graduation and fast-forward 9 years later, am now an <a href="https://hackernoon.com/tagged/android">Android</a> developer at <a href="http://radiopublic.com/">RadioPublic</a>. When <a href="https://hackernoon.com/tagged/google">Google</a> announced their IoT platform, <a href="https://developer.android.com/things/index.html">Android Things</a> in late 2016, I was beyond excited because it gave me a reason to break out my old breadboard, resistors, LEDs, and power regulators. It also gave me a reason to buy a Raspberry Pi. With Android Things I’m finally able to leverage my expertise in Android development in a more embedded (and frankly, more interesting) platform.</p><p>I’m not going to cover a ton of Android Things fundamentals here because a lot of really good developers have already done a great job at that:</p><ul><li><a href="https://riggaroo.co.za/android-things-hardware-basics/">Android Things Hardware Basics</a> by Android GDE, Rebecca Franks</li><li><a href="https://blog.mindorks.com/android-things-tutorials-getting-started-8464c11009ff#.29vvi2x82">Android Things Tutorials: Getting Started</a> by Android Developer, Amit Shekhar</li><li><a href="https://medium.com/exploring-android/android-things-electronics-101-e7e5cd2f327f#.yqp5dj987">Android Things: Electronics 101</a> by Buffer Android Engineer, Joe Birch</li></ul><p>I’m going to share a project I began on Friday, February 10th and finished prototyping on Monday, February 13th. When exploring something new, it’s important for me to find a practical application for it. I’m a homeowner of a house built in the early 90s. It can use some home-built tech from 2017. A superficial problem I and other members of my household have trouble with is parking (correctly and in alignment) in the garage. Either we parked to close to the wall and can’t walk around both sides of the car or we aren’t sure if the car will get caught in the garage door.</p><h3>Hello CantParkRight</h3><p>My first Android Things project is to build an assistive parking devices that uses several sensors to assist drivers in parking correctly in the garage. Think of the signals you see when you enter a carwash. Normally, there are two or three lights. When you first enter, the light is green, which instructs you to keep driving forward. When you have driven far enough, the light turns red to alert you to stop. I want this in my garage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*8o3XfRDdd5bxyaW0.png" /><figcaption>Image from Signal Tech</figcaption></figure><p>The first step of this is prototyping CantParkRight.</p><h3>Prototyping the Hardware</h3><p>A huge advantage that Raspberry Pi-like devices provide is the ability to quickly and cheaply prototype assistive devices like the one I’m building. The fact that I can officially leverage Android APIs (and down the road, Google APIs) is a big plus.</p><p>The supplies I used for my prototype include:</p><ul><li>Raspberry Pi Model 3 running Android Things Preview 1</li><li>HC-SR04 Ultrasonic proximity sensor</li><li>2 resistors, 10KΩ and 20KΩ</li><li>3 LEDs (Red, Yellow, Green)</li><li>A breadboard</li><li>Assortment of jumper wire</li></ul><p>I had most of my supplies already. I bought a Raspberry Pi sometime ago and recently bought a pack of 5 HC-SR04 Ultrasonic sensors from <a href="https://www.amazon.com/HC-SR04-Ultrasonic-Distance-MEGA2560-ElecRight/dp/B01MA4O5G5/ref=sr_1_cc_1?s=aps&amp;ie=UTF8&amp;qid=1487174915&amp;sr=1-1-catcorr&amp;keywords=hc-sr04">Amazon</a>. I settled on the HC-SR04 after quite a bit of research. How the HC-SR04 works is, you send a 10µS (microsecond) signal to the TRIGGER pin. Sometime in the next few milliseconds, the HC-SR04 sends a burst of 8 40KHz sound waves that will eventually bounce back. If an object is in range, the signal will bounce back and be detected by the receiver portion of the sensor. The HC-SR04 then sends a variable length echo to any device attached to the ECHO pin. The length of this pulse is determined by the distance the signal traveled before returning to the sensor. The HC-SR04 has a range of around 400cm (~13 feet). Perfect. Note: check out the datasheet on the HC-SR04 <a href="http://www.micropik.com/PDF/HCSR04.pdf">here</a>.</p><p>After a lot of experimentation, here is how my circuit is arranged on my breadboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*eBN5UensAp3rZujFrPS9AA.png" /><figcaption>CantParkRight hardware schematic</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U_qdncFNNtxLAhPJNHqFzQ.jpeg" /><figcaption>CantParkRight IRL, messy</figcaption></figure><p>A few hardware gotchas:</p><ul><li>The accuracy varies greatly between sensors, especially the “knockoffs”. Out of the pack of 5, some sensors were more sensitive to object movements while others exhibited less variation.</li><li>The signal sent to the ECHO pin is at 5V. The GPIO ports on the Raspberry Pi are rated for 3.3V. You can damage it by sending to high a voltage, so I use the resistors to step the voltage down to 3.3V.</li></ul><h3>Prototyping the Software</h3><p>The best part of this project was writing the software in Android Studio, deploying it via ADB (over WiFi), and seeing the results play out in front of my eyes. I based the implementation on:</p><ul><li><a href="https://hackernoon.com/android-things-basics-measure-distance-with-ultrasonic-sensor-3196fe5d7d7c#.fphbtwuy7">Android Things Basics: Measure Distance with Ultrasonic Sensor</a> by Daniel Dallos</li></ul><p>Over the course of the article, Daniel builds several implementations, some synchronous and some asynchronous using while loops, callbacks, and threads. I decided I wanted to build upon that, but use RxJava to implement asynchronous handling of sensor data. I’ve used RxJava in most of the Android apps that I’ve built. It offers quick and convenient ways to build, reuse, and arrange pieces of logic that leverage the flow of data from one end to the next, basically perfect for CantParkRight.</p><p>Disclaimer: I am NOT an RxJava expert. There are likely ways to do what I did using RxJava in a more efficient manner.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c9faa6c0dc07c409c4d429c2ef5391e/href">https://medium.com/media/6c9faa6c0dc07c409c4d429c2ef5391e/href</a></iframe><p>The critical piece is how I go about initiating the TRIGGER and waiting for an ECHO. My first implementation of this used a RxJava Observable that essentially wraps a few While loops (check out my repository, then go to the first commit).</p><p>The process was:</p><ul><li>Send the 10µS signal to the TRIGGER</li><li>Start a while loop that executed until the ECHO goes high, record the start time</li><li>Start a while loop that iterated until the ECHO goes low, record the end time and calculate the pulse width which is used to calculate the distance</li></ul><p>It worked, sometimes, but often for reasons I’m still researching, the sensor would stop responding (ie. the ECHO never went high after a TRIGGER). The improvement came when I used a GpioCallback. A GpioCallback allows you to listen to edge triggers (signal going high, signal going low, etc.) asynchronously. I combined my implementation of a GpioCallback with a RxJava Observable (more specifically an Emitter). From what I’ve read, the advantages of the Emitter over using a plain Observable (using Observable.create) is that it forces you to specify a BackPressure strategy, which is important when reading values pushed from a sensor. CantParkRight uses the BUFFER BackpressureMode. Using RxJava allows me to start the distance detection process, simply by subscribing to the correct Observable. Using an Emitter also allows me to right code to unregister my GpioCallback when I unsubscribe in onDestroy(…). This prevents future memory leaks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*2V59LKGpbQL0LESZPTFMaA.gif" /><figcaption>CantParkRight prototype in action</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FtD3GAstB-BA%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DtD3GAstB-BA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FtD3GAstB-BA%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/087a523280c7e2e0554737a7f800001f/href">https://medium.com/media/087a523280c7e2e0554737a7f800001f/href</a></iframe><h3>What’s Next</h3><p>For CantParkRight, I’m working up to building an actual device I can easily mount in my garage. With the prototype complete, I turn my efforts to making that happen.</p><p>In the meantime, you can check out the source code for <a href="https://github.com/emuneee/cant-park-right">CantParkRight on GitHub</a>. Be sure to follow me on <a href="http://twitter.com/emuneee">Twitter</a> or (cough) <a href="http://google.com/+EvanHalley">Google+</a> for updates on CantParkRight. I intend on posting the finished project here in the coming months, but watching the repository is great way to keep up.</p><p><em>Originally published at </em><a href="http://emuneee.com/blog/2017/02/15/diving-into-android-things/"><em>emuneee.com</em></a><em> on February 15, 2017.</em></p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d46bf3d6fb92" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/diving-into-android-things-d46bf3d6fb92">Diving into Android Things</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrating from ExoPlayer 1.x to 2.0]]></title>
            <link>https://medium.com/@evanhalley/migrating-from-exoplayer-1-x-to-2-0-58fbda36b46c?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/58fbda36b46c</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Wed, 02 Nov 2016 03:19:54 GMT</pubDate>
            <atom:updated>2016-11-02T03:19:54.126Z</atom:updated>
            <content:encoded><![CDATA[<p>In my <a href="https://medium.com/@emuneee/audio-playback-on-android-570ae77eda9e#.3rm2bmxm0">previous article</a>, I walked through how I architected an audio app on Android. I wagered that all of the work I would need to do would be contained to the LocalMediaPlayer class. I was correct. Here is how I migrated from ExoPlayer 1.x to 2.0.</p><p>I updated my app’s module build.gradle:</p><pre>compile ‘com.google.android.exoplayer:exoplayer:r2.0.4’</pre><p>Sync the Gradle changes so the updated ExoPlayer dependency is pulled down. Congratulations, PremoFM no longer compiles.</p><p>An aside, the ExoPlayer import is the only dependency I’ve seen that prepends the version numbers with an ‘r’. 🤔</p><p>Now in the LocalMediaPlayer class, there should be errors everywhere. Let’s fix them.</p><p>Starting in the constructor:</p><pre>mHandler = new Handler();<br>TrackSelector trackSelector = new DefaultTrackSelector(mHandler);<br>DefaultLoadControl loadControl = new DefaultLoadControl();<br>mMediaPlayer = ExoPlayerFactory.<em>newSimpleInstance</em>(mContext, trackSelector, loadControl);<br>mMediaPlayer.addListener(this);</pre><p>There’s a lot to unpack here, so let me explain. I need to create a new Handler object so that ExoPlayer can pass messages to my listeners (the LocalMediaPlayer class implements the EventListener interface). I create and use default implementations of the load control and track selector classes. Finally, I grab an instance of the ExoPlayer class by calling the newSimpleInstance method on the ExoPlayerFactory class. This method class obfuscates a lot of complexity for me. I could have called newInstance instead, but then I’d be required to pass TrackRenderers, which is unnecessary in my case.</p><p>Now let’s define the audio formats my app should be able to support, extract, and render.</p><pre>private static class AudioExtractorsFactory implements ExtractorsFactory {<br>    <br>    <a href="http://twitter.com/Override">@Override</a><br>    public Extractor[] createExtractors() {<br>        return new Extractor[]{<br>                new OggExtractor(),<br>                new WavExtractor(),<br>                new Mp3Extractor(),<br>                new Mp4Extractor()};<br>    }<br>}</pre><p>My AudioExtractorsFactory class will be used when I build my MediaSource class. I include extractors for the Ogg Vorbis, WAV, MP3, and MP4 audio formats since <a href="http://www.feedforall.com/rss2html.php?XMLFILE=http://www.feedforall.com/podcasting-knowledgebase.php&amp;TEMPLATE=http://www.feedforall.com/podcasting-knowledgebase-template.htm&amp;GUID=b101&amp;MAXITEMS=1">podcasts are generally available in these audio formats</a>.</p><p>Let’s handle building the MediaSource. The MediaSource class is tasked with retrieving, decoding, and sending audio samples to the ExoPlayer instance. I want to support two cases in PremoFM, 1) the audio file is already on the local file system and 2) the audio file is on a server somewhere on the Internet. ExoPlayer, again, makes this really easy, I either use the FileDataSourceFactory or the DefaultHttpDataSourceFactory.</p><pre>private MediaSource buildMediaSource() {<br>    DataSource.Factory dataSourceFactory = null;<br>    Uri uri = null;</pre><pre>    // return the uri to play<br>    switch (mEpisode.getDownloadStatus()) {<br>        case DownloadStatus.DOWNLOADED:<br>            uri = Uri.parse(mEpisode.getLocalMediaUrl());<br>            dataSourceFactory = new FileDataSourceFactory();<br>            break;<br>        case DownloadStatus.DOWNLOADING:<br>        case DownloadStatus.NOT_DOWNLOADED:<br>            uri = Uri.parse(mEpisode.getRemoteMediaUrl());<br>            dataSourceFactory = new DefaultHttpDataSourceFactory(<br>               mContext.getString(R.string.<em>user_agent</em>), null,<br>               DefaultHttpDataSource.<em>DEFAULT_CONNECT_TIMEOUT_MILLIS</em>,<br>               DefaultHttpDataSource.<em>DEFAULT_READ_TIMEOUT_MILLIS</em>,<br>               true);<br>            break;<br>    }</pre><pre>    if (uri != null &amp;&amp; dataSourceFactory != null) {<br>        return new ExtractorMediaSource(uri, dataSourceFactory, new       AudioExtractorsFactory(),<br>                mHandler, null);<br>    }<br>    throw new IllegalStateException(&quot;Unable to build media source&quot;);<br>}</pre><p>The FileDataSourceFactory is easy to configure. The Uri in this case could look like:</p><pre>file://data/data/com.mainmethod.premofm/…./this-podcast.mp3</pre><p>The DefaultHttpDataSourceFactory has a few more parameters, if you need them. By default, the single parameter constructor for DefaultHttpDataSourceFactory does not handle cross domain/scheme redirects, which is a very common case when streaming podcast audio. I found out the hard way and now I use the 4 argument constructor. The additional parameters allow me to configure connection and read timeouts. I opt for the default values and pass a ‘true’ to enable cross domain/scheme redirects. The Uri in this case could look like:</p><pre>http://feedproxy.google.com/~r/TheCombatJackShow/~5/s_9fWPxLDu0/188058705-thecombatjackshow-the-j-cole-episode.mp3</pre><p>Finally, ExoPlayer 2, like the more recent versions of ExoPlayer 1.x, supports variable speed audio playback on Android 6.0+ devices. You can customize audio playback speed and even pitch using the PlaybackParams API.</p><pre>PlaybackParams playbackParams = new PlaybackParams();<br>playbackParams.setSpeed(speed);<br>mExoPlayer.setPlaybackParams(playbackParams);</pre><p>That’s the majority of the ExoPlayer 2.X related changes. There are some smaller, app-specific things I needed to change. Since I was no longer leveraging a custom TrackRenderer, I needed a way to broadcast track position updates to my user interface. I spin up a separate thread that accomplishes that.</p><p>One final note, the ExoPlayer.STATE_PREPARING is no longer a thing. There is a method callback called EventListener.onLoadingChange that can alert you to the state of loading for a particular audio/video track.</p><p>You can check out the full diff of my migration <a href="https://github.com/emuneee/premofm/compare/9315cd808a700f844e0e32e3670c51e559348abc...7770bbcad0405fb54fb41a1d09dd23643d58f4e0">here</a>. PremoFM is an open source podcast player for Android. Feel free to fork it and make your contributions or use portions for your own projects.</p><p>ExoPlayer is also open source and well maintained by Google. A ton of excellent documentation is also available on <a href="https://github.com/google/ExoPlayer">GitHub</a>.</p><p>I love talking and writing about Android development so be sure to <a href="https://medium.com/@emuneee">follow me on Medium</a>, recommend or share this article, follow me on <a href="http://twitter.com/emuneee">Twitter</a>, and <a href="http://emuneee.com/">visit my website for more</a>.</p><p>Finally, if you are a podcast fan or even remotely curious, checkout RadioPublic, currently in beta, at <a href="http://radiopublic.com/">RadioPublic.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58fbda36b46c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Audio Playback on Android]]></title>
            <link>https://medium.com/@evanhalley/audio-playback-on-android-570ae77eda9e?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/570ae77eda9e</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Sat, 22 Oct 2016 02:55:17 GMT</pubDate>
            <atom:updated>2016-10-22T02:55:17.277Z</atom:updated>
            <content:encoded><![CDATA[<p>Playing music, podcasts, or other audio is one of the most common activities for smartphones in 2016. Most of the time, audio plays in the background while we are driving, cleaning, working out, or cooking. Architecting your application to support background audio playback is standard fare whether you are incorporating the standard <a href="https://developer.android.com/reference/android/media/MediaPlayer.html">Android MediaPlayer API</a> or using a library, like <a href="https://google.github.io/ExoPlayer/">ExoPlayer</a>.</p><p>I want to briefly walk through how I architected PremoFM, an open source podcast player, to play audio in the background using ExoPlayer. It’s not perfect, but it’s a good starting point for the transition to ExoPlayer 2. If you want to learn more about ExoPlayer 2, check out my previous post, <a href="https://medium.com/@emuneee/exploring-exoplayer-2-d1d1fb37c902#.8rnqjll3h">Exploring ExoPlayer 2</a>.</p><h3>The Architecture</h3><p>In order to play audio in the background (or do anything in the background) the process that manages playback should be based on the <a href="https://developer.android.com/guide/components/services.html">Service</a> class. Services, on Android, allow background work to be done without needing to have a user interface in the foreground. Naturally, I based the background audio playback of PremoFM on a Service, the <a href="https://github.com/emuneee/premofm/blob/develop/app/src/main/java/com/mainmethod/premofm/service/PodcastPlayerService.java">PodcastPlayerService</a>. It is obviously doing a lot. It manages audio playback, updates the database, listens for events like a headphone disconnection, and manages the persistent notification. Initially, most of my code involving direct management of the ExoPlayer was also embedded directly in this service. This led to a bloated class and a highly coupled design. I re-architected things when I added Google Cast support by creating a generic MediaPlayer abstract class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fbc901404534e47f9db8d58c80c0caf/href">https://medium.com/media/9fbc901404534e47f9db8d58c80c0caf/href</a></iframe><p>The abstract class provided a common set of methods for interacting with ExoPlayer like, playing a media file, fast forward &amp; rewind, getting playback state information, and changing the playback speed. All I needed to do was extend my MediaPlayer abstract class, using ExoPlayer. This resulted in all of my ExoPlayer code existing in one class, LocalMediaPlayer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f17f3429152c79ff9e5c818da72ab48/href">https://medium.com/media/5f17f3429152c79ff9e5c818da72ab48/href</a></iframe><p>This will make my upgrade to ExoPlayer 2 significantly easier than if I had continued the previous architecture. All of the code that needs to change exists in one place. In my next article I will get into the nitty gritty of my migration.</p><p>Feel free to take a swing at it before I do. Check out the source code for <a href="https://github.com/emuneee/premofm">PremoFM from GitHub</a> and hack away.</p><p>Follow me on <a href="http://twitter.com/emuneee">Twitter</a> or <a href="http://emuneee.com/">visit my website</a> for more Android Development related articles like this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=570ae77eda9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring ExoPlayer 2]]></title>
            <link>https://medium.com/@evanhalley/exploring-exoplayer-2-d1d1fb37c902?source=rss-29ae8a3ad52d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1d1fb37c902</guid>
            <dc:creator><![CDATA[Evan]]></dc:creator>
            <pubDate>Fri, 14 Oct 2016 13:56:01 GMT</pubDate>
            <atom:updated>2016-10-14T13:56:01.395Z</atom:updated>
            <content:encoded><![CDATA[<p>ExoPlayer is an extensible, application level media player for Android apps. It’s an alternative to the high level <a href="https://developer.android.com/reference/android/media/MediaPlayer.html">Android MediaPlayer API</a>. MediaPlayer is built on several low level media playing APIs like <a href="https://developer.android.com/reference/android/media/AudioTrack.html">AudioTrack</a> and <a href="https://developer.android.com/reference/android/media/MediaDrm.html">MediaDRM</a>. These low level APIs can also be used by developers to build your own media player with it’s own custom behavior. ExoPlayer is built on these low level APIs and it has the additional benefit of being open source. You don’t need to build your own media player, from scratch, to get the behavior you need. You can extend ExoPlayer instead.</p><p>ExoPlayer was created and is maintained by Google. Out of the box, it can play a wide range of audio and video formats such as:</p><ul><li>MP3</li><li>MP4</li><li>WAV</li><li>MKV</li><li>MPEG-TS</li><li>Ogg</li></ul><p>Remember, ExoPlayer is open source, so it can, with some extension, decode and play any format, as long as you build the capability.</p><h3>Just a Few ExoPlayer Basics &amp; Components</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/0*_Tv75TgCA2geMmqy." /><figcaption>Source: ExoPlayer Documentation on GitHub</figcaption></figure><h4>ExoPlayer</h4><p>The <a href="http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/ExoPlayer.html">ExoPlayer</a> class is the actual media player. It depends on a few other components for media loading, buffering, decoding, and track selection. When all of the required components are configured, your app will interact with the ExoPlayer class to control the playback of your media. You can register listeners with ExoPlayer to be notified of certain events like buffering or the conclusion of a track.</p><h4>MediaSource</h4><p>The <a href="http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/source/MediaSource.html">MediaSource</a> class is charged with controlling what media will be played and how it will be loaded. The MediaSource class is used directly by the ExoPlayer class. MediaSource enables a ton of different behaviors. For example, you can merge multiple MediaSource classes together to show video, along with captions or you can use multiple MediaSource classes to create playlists where the transitions between those sources are seamless (gapless).</p><p>There are several prebuilt MediaSource classes available out of the box in ExoPlayer to support many common use cases like playing normal media files or streaming DASH content from a remote server. Of course, you can implement your own to support your application’s use case.</p><h4>DataSource</h4><p>The <a href="http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/upstream/DataSource.html">DataSource</a> class provides samples of data to a MediaSource. These samples of data can originate from a file on the SD card, a resource in the assets directory, and even a remote server. You can use one of the prebuilt DataSource classes or build your own to read data in a way necessary to support your use case. For example, maybe your application will stream media on a company intranet. You can use a custom DataSource to define the rules and protocols that allow this to happen securely.</p><h4>TrackSelector</h4><p>The <a href="http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/trackselection/TrackSelector.html">TrackSelector</a> class dictates which track is selected for rendering and playback.</p><h4>Renderer</h4><p>The <a href="http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/Renderer.html">Renderer</a> class decodes media and renders it. An example implementation is the MediaCodecAudioRenderer, which decodes audio data and renders it using several lower level ExoPlayer APIs.</p><h4>LoadControl</h4><p>The <a href="http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/LoadControl.html">LoadControl</a> class defines the buffering behavior of a particular MediaSource.</p><h3>Finally</h3><p>At this point, I know as much about ExoPlayer 2 as you do. I have some pretty extensive knowledge of ExoPlayer 1.X because I’ve used it on several Android projects that I’ve worked on. This series on ExoPlayer 2 will document my journey of learning about ExoPlayer 2 and upgrading an app to ExoPlayer 2 that is currently using ExoPlayer 1.5.9. I will probably make mistakes, but I hope this series will help a few other developers in their effort to implement ExoPlayer 2 in a real world app.</p><p>The app I will be using for demonstrating this upgrade is PremoFM. PremoFM is an open-source podcast player that I started building almost two years ago. The source code for the app is on GitHub (<a href="https://github.com/emuneee/premofm">https://github.com/emuneee/premofm</a>). I will be using a branch (<a href="https://github.com/emuneee/premofm/tree/exoplayer_2">https://github.com/emuneee/premofm/tree/exoplayer_2</a>) for all of my ExoPlayer 2 upgrade work. I invite you to follow along. I’ll be back next week to discuss the structure of a typical audio playing app and how ExoPlayer fits in.</p><p>Please follow me on Twitter (<a href="http://twitter.com/emuneee">@emuneee</a>) or visit my website, <a href="http://emuneee.com/">emuneee.com</a>.</p><p>Finally, I’m working with a great team at <a href="http://radiopublic.com/">RadioPublic</a> to build an awesome podcast experience for Android and iOS. <a href="http://radiopublic.com/">Hop on the beta today</a>.</p><p><strong>Some resources to review in the meantime:</strong></p><p>ExoPlayer on GitHub</p><p><a href="https://github.com/google/ExoPlayer">https://github.com/google/ExoPlayer</a></p><p>ExoPlayer — Developer Guide</p><p><a href="https://google.github.io/ExoPlayer/guide.html">https://google.github.io/ExoPlayer/guide.html</a></p><p>ExoPlayer on Medium</p><p><a href="https://medium.com/google-exoplayer">https://medium.com/google-exoplayer</a></p><p>Android Developer Backstage 48: ExoPlayer</p><p><a href="http://androidbackstage.blogspot.com/2016/05/episode-48-exoplayer.html">http://androidbackstage.blogspot.com/2016/05/episode-48-exoplayer.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1d1fb37c902" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>