Android News for June 2015

Android Design Support Library

The design support library makes it easy to use material design when building Android apps. The best part, it’s compatible all the way back to Android 2.1. This support library includes a better navigation drawer, collapsing navigation bars, snack bars and a number of other key components of material design. Combine this with a view pager, recycler view and the swipe to refresh library for a complete base application.

Google developer blog:
http://android-developers.blogspot.com/2015/05/android-design-support-library.html

Sample code:
https://github.com/blackcj/DesignSupportExample

Developing for Android: Technical Guide

Google released a collection of best practices and common pitfalls written by the Android framework and runtime teams. This is a must read for all Android developers. It’s only eight chapters long so you have no excuse not to read it.

https://medium.com/google-developers/developing-for-android-introduction-5345b451567c

Free Material Design Icons

Need icons for your Android, iOS or Web app? Google has provided a great collection of icons that are free to use:

https://google.github.io/material-design-icons/
https://www.google.com/design/icons/

Stay tuned for more Android news and examples.

Cell Number Formatting with Google Apps Script

Problem:

The setNumberFormat function within Google Apps Script is not well documented in the API reference documentation. This function accepts a wide range of parameters in a single string that can be used to specify the format for a range of cells. Knowing the accepted formats will give you control over the data in your cells when using Google Apps Script for Google Sheets.

Solution:

The following examples demonstrate how to use various string patterns to define the format of a particular range within your active spreadsheet. You can set the formatting of a single cell, a whole column starting from a specific cell or an entire spreadsheet. Additional resources can be found on the Google Sheet support page for number formats. It would be nice if there was a link to that support page from within the API reference.

Plain text for a single cell:

// Use these first two lines for all four examples
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Single cell
var cell = sheet.getRange("B2");

// Plain text
cell.setNumberFormat("@");

Date for single column

// Single column, select every row from 2..n in column B
// To select a limited range put the ending row number (“B2:B10”)
var column = sheet.getRange("B2:B");

// Simple date format
column.setNumberFormat("M/d/yy");

Additional details about accepted date formats can be found here: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Currency for entire sheet:

// Select all rows and columns from a spreadsheet
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());

// Money format
range.setNumberFormat("$#,##0.00;$(#,##0.00)");

Additional currency and number formats can be found here: https://support.google.com/docs/answer/56470. You can specify up to four parts for custom number formats; positive, negative, zero and text. You can even add color by including one of the compatible colors in the string format.

Custom number format with color

// Number format with color
var row = sheet.getRange("A1:D1");
row.setNumberFormat('$#,##0.00[green];$(#,##0.00)[red];"zero";@[blue]');

// Sample values
var values = [
   [ "100.0", "-100.0", "0", "ABC" ],
];
row.setValues(values);

Number format example with color.

This gives you a quick way to set conditional formatting without having to write custom logic. You can use this to quickly identify problematic data.

Product Review: Moshi ClearGuard for the MacBook Pro

I use protective covers on all of my electronics. I recently purchased the Moshi ClearGuard keyboard cover for my MacBook Pro. In addition to protecting against spills, it also helps keep the keys free of grease. I have oily fingers which translates to shiny keys within months without a cover. This was a must have product for me.

Moshi ClearGuard Keyboard Cover

Moshi Keyboard Cover (lifted slightly off the keys)

Pros

Easy to Install

Compared to other keyboard covers, this one was easy to install and fit perfectly out of the box. Installing the cover almost felt too easy. You just take it out of the packaging and set it on the keyboard. That’s it. I searched the box looking for instructions and ended up watching a YouTube video to be certain that I had not missed a step. No adhesive tape or instructions needed.

Washable

The keyboard cover is durable and easy to clean. Just lift the cover off the keyboard and wipe it down with warm water. Make sure to let it dry completely before putting it back on the keyboard. I’ve washed mine a couple of times in the past month and it still fits great.

Protective

Having the cover on my keyboard gives me piece of mind. In addition to protecting against spills, it also collects dirt, crumbs and grease. I frequently eat lunch at my desk and no longer have to worry about pasta sauce or cookie crumbs getting into my keys.

Cons

Cost

At $25, it’s not cheap. For comparison, a similar cover for my old Fujitsu laptop was $14. Either way, if you protect even a single spill, it would pay for itself. The cover has the added benefit of increasing the re-sale value of the device by keeping the keyboard looking new. It would be great to see this product closer to the $15 marker. Unfortunately, I wasn’t able to find any quality competitors for the MacBook Pro.

Easy to Remove

This could be a pro or a con. The cover rests on top of the keyboard. The cover for my Fujitsu laptop sealed around the edge with double sided tape. This helped keep it in place and further protected from seeping liquid (in the event of a spill). On the other hand, it does make the cover easier to clean.

Makes the Screen Dirty

As with all protective covers I’ve owned, the dirt tends to rub off on the screen. This is not permanent and can be easily wiped off but an inconvenience nonetheless. Something I’m gladly willing to do for the piece of mind it provides. I keep a micro fiber cloth in my bag to use for wiping off the screen once a week.

Bottom line, protect your investment with a Moshi keyboard cover.

Full product name: Moshi ClearGuard MB for Pro 13″,15″,17″ White MacBook (2009) MB air 13″ US Layout (99MO021901).

Use Forever.js to Log Data from Your Spark Core

Intro

As an alternative to using PHP & MySQL, you can use Node.js with Deployd and MongoDB to store data retrieved from a connected home device. JavaScript normally runs on the client machine. With Node.js, you can run JavaScript code directly on your server allowing you to write directly to a database (DB) without PHP.

In this example, we’ll query a Spark Core every 10 minutes and store the temperature data in MongoDB. We can then use Deployd to create a simple API end point to use for requesting historical data. This can be used in combination with a mobile app or web interface to visualize historic temperature data.

Prerequisites

You need to have a basic understanding of Node.js and have a server (or localhost) set up with Node.js and npm installed.

Warning: Do not install Deployd to your public_html directory or put any server side JavaScript files in your public_html directory on a live server. Always put server-side JavaScript files at least one directory above your public_html folder to prevent others from being able to open and view the contents of your files. Unlike PHP, someone can navigate to your JavaScript file and view it’s contents. This is fine for client-side JavaScript but bad for server-side JavaScript that may contain API keys or DB credentials.

You will need to install the following libraries. Remember to use “-g” (global install) when installing via npm so you have access to the libraries in all directories.

Deployd

After installing and configuring Deployd, create a table called temperature with fields temperature, humidity and timestamp. Make sure that you are able to successfully read and write information to MongoDB using the Deployd API you just created.

Sample Code

Update the DEPLOYD_PORT, DB_PORT, DEVICE_ID and API_KEY to match your configuration. Put the code at least one folder above your public_html folder in a file called production.js.

// production.js
var deployd = require('deployd');       // Interface to the database
var request = require('request');       // Make requests to external server
var CronJob = require('cron').CronJob;  // Execute requests on interval

// Set up deployd with mongodb information
var server = deployd({
  port: DEPLOYD_PORT,
  env: 'production',
  db: {
    host: 'localhost',
    port: DB_PORT,
    name: 'deployd'
  }
});

// Start listening for requests
server.listen();

server.on('listening', function() {
    // Store a reference to the Deployd temperature table
    var temperatureStore = server.createStore('temperature');
    
    // Run every 10 minutes. Write the data to the Deployd table.
    new CronJob('00 */10 * * * *', function(){
        // Make request to the Spark Core
        request("https://api.spark.io/v1/devices/DEVICE_ID/result?access_token=API_KEY", function(error, response, body) {
            // Access results as JSON data
            var result = JSON.parse(JSON.parse(body).result);
            // Convert the timestamp to seconds
            var timestamp = new Date().getTime() / 1000;
            // Write the data as a new row into the Deployd temperature table
            temperatureStore.insert({temperature: result.data2, humidity: result.data1, timestamp: timestamp}, function(err, result){
                if(result) {
                    // Success! 
                }
            });
        });
        
    }, null, true, "America/Los_Angeles");
});

server.on('error', function(err) {
  console.error(err);
  process.nextTick(function() { // Give the server a chance to return an error
    process.exit();
  });
});

Start your script with forever.js

Navigate to the folder you put the production.js file and run the following command.

forever start production.js

Conclusion

This code will run until your server is restarted. You can add a command to your server start up to ensure Forever.js starts up after a reboot.

Why Node.js? Because it’s fun! Rather than learning PHP, you can leverage your JavaScript knowledge to write server side code. I haven’t run any benchmarks myself, but from what I’ve read, your server hardware plays a much bigger role in performance than the software solution you use (MongoDB vs. MySQL). Use what you feel most comfortable with. Understanding all available solutions will help you select the best option given your specific project needs.