Speed up Android home screen display

I followed the suggestions in “Speeding up the Samsung Galaxy Note” which worked, but now a new problem: after pressing the Home button, the ‘desktop’ takes very long to show the icons. This is on my Samsung Note (original) i717 with Android 4.1.2, on AT&T network.

Searching I found that many were blaming the lack of memory.

What I did
Using the SystemPanel Lite app, I saw that there were a bunch of apps in the Inactive (Cached) Applications list. I closed these. Now the home screen pops up a little quicker.

Issue?
I don’t think it is just a memory issue. Something is wrong with this OS version on my phone. More likely the caching system needs a major revisit. Are the latest versions of Android any better?

Links
SystemPanel Lite version 1.3.1

So What” by Miles Davis from “Kind of Blue

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Roadways as linear induction power generators

Take every roadway in a country, embed a series of electromagnetic coils, now put a magnet on the bottom of every vehicle, result is electrical generation. This idea came up during the morning shower. Can’t embellish, late for work!

Back home ….

Turns out reuse of roadways to harvest energy is an ongoing research and in some places experiments. So far a web search shows that piezoelectrical and thermoelectrical are widely sited as technologies to use. I have not seen the use of linear inductors mentioned. Perhaps there are serious engineering reasons, maybe they could even cause even more energy waste.

Some commentators on various news sites state that these systems are more like bogus perpetual motion machines, in that the vehicles will have to expend energy on the road energy generators, so there is no gain. I think that is nonsense since the vehicle will expend waste energy in many different ways, they are already deflecting the road surface, causing wind energy, and so forth. But, I don’t know the field.

Resources

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

File ASCII Hex Dump using Groovy

Viewing the bytes of a file is not a common operation. Many most environments and file editors do have tools to do this. Except when they don’t. Like Windows 7, it does not include it, having removed the ‘Dump’ command. Jump to source.

Intro
Dumping the bytes of a file is a very simple Programming 101 exercise. Groovy makes this even simpler since it adds the “eachByte()” closure to the java.io.File class. The closure is not just syntax, it hides the closing of the resource. As with any toy problem, it could become more complex as you add more features: showing visible ASCII chars, different byte-endian, code-pages, GUI, and so forth.

A typical hex dump looks like this:

OFFSET     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f  | ASCII
00000000: 4c 69 6e 65 20 6f 6e 65 0d 0a 4c 69 6e 65 20 74  | L i n e   o n e . . L i n e   t 
00000010: 77 6f 0d 0a 4c 69 6e 65 20 74 68 72 65 65 0d 0a  | w o . . L i n e   t h r e e . . 
00000020: 21 2f 30 31 32 33 34 0d 0a 35 0d 0a 36 37 38 39  | ! / 0 1 2 3 4 . . 5 . . 6 7 8 9 
00000030: 41 7a     

Approach
Via the ‘eachByte()’ closure on File, we read 16 bytes at a time and store into a list that we then dump. Each output line has a byte offset value, the 16 bytes, and the ASCII representation of those bytes for printable characters.

Source
The source is also available at Groovy Hex Dump Gist

HexDump.groovy


HexDumpTest.groovy


TDD
After I wrote the first version of this I went back to write a JUnit test for it. Could not, it was too complex. Thus, I rewrote it to be testable. Hardly worth it for a throw away script but very instructive. If I had practiced Test Driven Development, this testing issue would not have occurred.

The trick was to output via a closure that would capture the output: { line -> lines << line}. The list is then analyzed for expected output.

Lesson: If it is too complex to test, it is too complex.

File Dump Utilities
Rather than writing one, it is best to download or use those available in developer tools.

Linux has the OD command and many others. Some are also available on Cygwin.
Eclipse has several plug-ins for binary editing, such as the Eclipse Hex Editor Plugin

Environment

  • Groovy 2.2
  • Windows 7
  • Eclipse 4.3

References

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Relax Melodies app review

The “Relax M.” app is available for Android and iOS. This app is recommended for helping one to relax for sleeping, for help with Tinnitus, and general relaxation.

This post is related to the 2.3.3 version on Android. Apparently the iOS version of the app is much improved.

I read some reviews that missed the whole point of the app. Relax M. consists of a wide range of “soothing” sounds that you play, sure, but the big thing about it is that you combine these sounds to create your own personalized “mix”. For example, you can add windy day sounds to the sound of rain. The free version offers 46 sounds, the paid app 96 sounds.

Though this app is marketed as a sleep aid it could be used for other things:

  • Noise suppression
  • Background sounds
  • Hearing problems
  • Soothing sounds for baby (careful with volume on anything next to a child!)
The 46 free sounds in the free version
 * Afternoon                * Heavy Rain     * Orchestral          * Thunder 
 * Birds                    * Humming        * Oscillating Fan     * Thunder Storm 
 * Butterfly                * Icy Snow       * Piano               * Toskana 
 * Campfire                 * Immersed       * Rain                * Underwater 
 * Cat Purring              * Lounge         * Rain on Roof        * Urban Rain 
 * Cavern                   * Medieval       * Rainstorm           * Vacuum 
 * City Ambiance            * Melody         * Rainy Day           * Waterfall 
 * Duduk                    * MonkChant      * River               * White Noise 
 * Flute                    * Music Box      * Seaside             * Wind Chimes 
 * Frogs                    * Night          * Slow Waves          * Winds 
 * Grandfather Clock        * Ocean          * Storm               * Zen 

How this app could have been better?
1. Allow the user to add new sounds and music. Maybe someone will find that a freight train or steam locomotive is soothing. Me, I would find the sounds of the “L”, BMT Canarsie Line in New York City very soothing.
2. Easier way to change the volume of each sound in the current mix.
3. Easier way to find sounds: alphabetic, type, last used, and so forth.
4. Easy pause button.

Resources

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Private P2P HSM, a network for optimal virtual storage

In “Hierarchical Storage Management, from drive to cloud” I wrote about the idea of applying HSM by using a central storage or Cloud model. Here I discuss the next stage, using Peer-to-peer technology to create private clouds. The central remote cloud services are then useful as the expensive slow end point of data migration.

Background
Hierarchical storage management (HSM) is a system of using layers or tiers of storage resources to migrate files according to various criteria. Data would autmatically be migrated from RAM to Solid-State Drives, to disks, and finally to Tapes. This is an old technology, around since the 1970s, and primarily used at enterprise business. However, various forms of HSM have been used in consumer products, services, or at the OS level. One example is a new service such as Bitcasa.

P2P Synchronization
The use of P2P to synchronize storage to one’s own devices got a big boost with BitTorrent Sync (BTSync) created by BitTorrent. BTSync, though not a replacement for something like DropBox, offers an additional model to offset some limitations of cloud storage.

P2P HSM
BTSync showed that Peer-to-peer (P2P) sync works and is useful. Could this approach be used as a base to provide not only synchronization but migration of files to different tiers? A peer-to-peer-hierarchical-storage-management (p2pHSM).

With this approach, the private storage hierarchy is a true cloud, a private network of storage resources. This private cloud could also be connected to traditional cloud storage vendors, like BitCasa or DropBox, creating a hierarchy of storage Clouds. A further optimization is for the private P2P HSM to arbitrate or bid with multiple external Cloud providers for best rates and other criteria.

Scenario 1
Your extended family and real friends provides many computing devices to the private cloud: smartphones, NAS, USB drives, STBs, vehicles, laptops, PCs, tablets, and so forth. Each of these devices has limited storage capacities and bandwidth limits.

The storage resources on each device allocate a percentage of storage and bandwidth to the private cloud. The storage is secure and private, and its content available via access control permissions (ACL). For example, children cannot access their parents content.

  1. Your walking outside wearing your head mounted device, like Google Glass, and a Hummingbird flies into view, you turn on Record and create a video.
  2. The video gets transmitted to your mobile device.
  3. Your device notes that you are running out of space and a P2P storage request is made to the network of devices available.
  4. A high-end mobile phone is found that has extra space on a memory card.
  5. The original mobile device sends the video to that device.
  6. A week later, no one is viewing that video anymore so the system migrates that video to other storage.
  7. The next level is the laptop since it has plenty of room and is faster.
  8. A month later the video is again migrated to the family PC which has plenty of free space.
  9. Two months later it is again migrated to the family’s external cloud storage service if the monetary rules configured allow it.
p2pHSM

p2pHSM

At every file migration, the system seeks the optimization of price, performance, access, security, and capacity. Only when the private cloud is approaching certain limits or for backup purposes is the top, slowest, and most expensive tier, the conventional Cloud, used.

Issues
Of course, the history of file sharing systems is full of controversy. Some would say that a private P2P adds to the problems, notwithstanding that there are already plenty of public P2P networks and massive file sharing already occurring. Some issues are:

  • Security
  • Privacy
  • Copyrights and fair use
  • DRM
  • Is data accessed at the “Migration Level”, or migrated back?

Resources

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Don’t let Java varargs limit an API

Was looking at Simple Logging Facade for Java (SLF4J), a Java logging library. One nice feature is that one can use message formats that don’t get evaluated until the log statement is executed, which in turn depends on the log level.

It seems, maybe I missed it, that using this feature disallows you from logging exceptions. For example, you can log a complicated string and the exception like so:

logger.error("Oh no, thing '" + 
     thing + "' took down the system!", ex);

Alternatively, you can use SLF4J’s message formatting to avoid the costly String manipulations, and just pass in the ‘thing’ object and anything else needed in the message as varargs:

logger.error("Oh no, thing '{}' 
     took down the system!", thing);

But, where to you chain in the exception that caused this?

Solution?

Add to the API:

void error(String format, Throwable t, Object... arguments)
  Log an exception and message at the ERROR level according to the 
  specified format and arguments.

  This form avoids superfluous string concatenation when the 
  logger is disabled for the ERROR level. However, this variant 
  incurs the hidden (and relatively small) cost of creating an 
  Object[] before invoking the method, even if this logger is 
  disabled for ERROR. The variants taking one and two arguments 
  exist solely in order to avoid this hidden cost.

   Parameters:
       format    - the message accompanying the exception
       t         - the exception (throwable) to log
       arguments - a list of 3 or more arguments

Then the log could have been done as:

logger.error("Oh no, thing '{}' 
    took down the system!", ex, thing);

But…
Of course, I’m sure the developers of SLF4j, and other libraries that change their API to take advantage of varargs, have very good reasons for the resulting changes and “limitations”.

Varargs should be used sparingly?
One advice given in Java docs is:
“… you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called”.

Resources

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Use Groovy to recurse folders and create files via template

Via scripting one can create multiple files in a folder hierarchy and customize the file contents based on the folder location. This can be easily done using the Groovy language.

Note: In the past “package.html” files were used, but the latest approach is to create “package-info.java” files. These Java files contain the package Javadoc info, and have other uses. Kind of the return of Java classes as configuration as used in the bad ole complex early EJB days.

In this particular use case, I have a legacy Java project that does not contain package level Javadoc files. There are about a hundred package folders. It would be daunting to create these manually. It is not a mere copy since the package info must contain the specific package declaration: package x.y.z;.

 

In listing 1 below, the processFolders method takes in a closure “createInfo” and a variable length argument of Strings. The strings are to allow creating the files starting at multiple base folders. With the paths.each{} we take each string and create a File object. Now we recurse all folders at this file location. At each folder in the recursion we invoke the closure.

import groovy.io.FileType

def processFolders(createInfo, String... paths) {
    paths.each{
        def base = it
        new File(it).eachFileRecurse(FileType.DIRECTORIES){ file -> 
             def path = file.getPath()
             println "$path"
             createInfo(base, path)                                
        }
   }
}

def templateText = ''' 
/**
 * TODO Summary.
 * <p>
 * TODO Description.
 */
package ${pack};
'''

def template = new groovy.text.SimpleTemplateEngine()
       .createTemplate(templateText)

processFolders({base, p ->
    def pack = (p-base).replace(File.separator, '.')
          .replaceFirst(/^\.+/, '');
    new File(p,"package-info.java").text = template.make(['pack':pack]) 
}, 'src\\main\\java','src\\test\\java')

Listing 1
 

Because we are using variable length arguments, which must be the last parameter in a method, we can’t use the usual Groovy closure idiom, and define it inline (but maybe there is a way?):

processFolders(){
   ... body of closure ...
}

Alternatively, we could have used a named closure instead of inline, as follows:

def proc = {base, p ->
    def pack = (p-base).replace(File.separator, '.').replaceFirst(/^\.+/, '');
    new File(p,"package-info.java").text = template.make(['pack':pack]) 
}

At each folder during recurse, we keep the base folder, then later we subtract the folder paths (yup, Groovy allows String subtraction), and convert the folder path into a package string. Then we resolve the template with the binding containing the package name. The String produced by the template is written to a new file via the file’s ‘text’ property.

The result when run is the creation of package-info.java files at each subfolder, and the package line will be correct. Each file will have two TODO entries for the summary and description. Of course, the same code with minor changes can be used to create package.html files instead. Or just add a switch for ‘old’ or ‘new’ package level info file.

Updates
March 4, 2014: Changed to use eachDirRecurse. Not tested yet.
March 5, 2014: Hmmmm. eachDirRecurse didn’t work.

Environment

  • Groovy Version: 2.2.2 JVM: 1.7.0_25 Vendor: Oracle Corporation OS: Windows 7
  • Eclipse 4.3
  • Groovy-Eclipse Feature: 2.9.0.xx-20140228-1800-e43-SNAPSHOT

Some links

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Design smell: Must run the app to configure the app

The prevailing method of configuring an application is to run the application and then select various configuration screens or preferences. This is fine, until it is not.

Sometimes you cannot run the application, so can’t configure it. An example would be the Eclipse IDE. It is possible to break Eclipse by loading a misbehaving plug-in or other means. If Eclipse cannot start, one can’t remove the offending plug-in or make other required changes.

This brings up the main problem with relying on in-app configuration. Without its use, one must be skilled in the underlying configuration storage used by the app. Further, this storage may be minimally documented, complex, or in multiple places. In worse cases, this storage is non-textual. For instance, referring to Eclipse example above, I have yet to find via a web search how to easily use the OSGi Equinox run-time (that Eclipse builds upon) to remove a recently installed feature or plug-in. I’m sure it is possible, and probably minor if you know Equinox. But, how many users of Eclipse IDE have even heard of Equinox or OSGi?

Issues

  • The app may be compromised or broken.
  • Configuring via configuration files or other apps requires a different skill set.
  • Attempts to repair via non-app UI can make things worse.

Solutions

  • Allow a subset of an application to be used for configuration use.
  • Allow the running of the app in “safe” modes. Example, Browser without plugins.
  • Create a separate configuration application.
  • Allow easier means to roll back to previous configuration.

The best solution is to create a secure application that can use the target application’s configuration storage systems. If the in-app configuration support can be modularized, this is optimal.


Drawing Hands” by M. C. Escher, accessed at WikiPaintings
© This artwork may be protected by copyright. It is posted on the site in accordance with fair use principles

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Groovy template fails if language keywords used

While using the SimpleTemplateEngine to process a template with a “$package” expression I got an error. Rename this expression to “$pack” and it works.

Puzzling. Then I looked at the source code for the template engine. Oops, forgot that Groovy uses script parsing of text to implement many powerful features, like templates. Kind of like extreme EYODF*. Thus, a snippet of the source code for SimpleTemplateEngine shown in listing 1 below, the word “package” is interpreted as the Java language keyword “package“.

try {
  template.script = groovyShell.parse(script, "SimpleTemplateScript" + counter++ + ".groovy");
} catch (Exception e) {
  throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}
Listing 1

Below is an example of this.

def templateText = '''package $package;'''

def engine = new groovy.text.SimpleTemplateEngine(true)
def template = engine.createTemplate(templateText)
def binding = ['package':'a/b/c']

def s = template.make(binding)
println s
Listing 2

Results in:

Caught: groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed:
SimpleTemplateScript1.groovy: 1: unexpected token: package @ line 1, column 23.
   out.print("""package $package;""");
                         ^

1 error

Now change the expression to $pack:

def templateText = '''package $pack;'''

def engine = new groovy.text.SimpleTemplateEngine() // pass in true to see debug output
def template = engine.createTemplate(templateText)
def binding = ['pack':'a/b/c']

def s = template.make(binding)
println s
Listing 3

And, the result is:

package a/b/c;

Environment

  • Groovy Version: 2.2.2 JVM: 1.7.0_25 Vendor: Oracle Corporation OS: Windows 7
  • Eclipse 4.3
  • Groovy-Eclipse Feature: 2.9.0.xx-20140228-1800-e43-SNAPSHOT

Some links

EYODF*: “Eat Your Own Dog Food”

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Share source code using Gist

Gist is a modern pastebin service operated by GitHub. Unlike a simple pastebin site, a gist behind the scenes is using the Distributed Version Control System (DVCS) Git. “All gists are Git repositories, so they are automatically versioned, forkable and usable from Git.”

When including source code on a site there are scripts that allow one to include the code from the gist site. Anyone interested in the source has a cleaner interface to view, copy, download, or fork.

An example is on one of my blog posts. To show the source on the page took only one line:

[gist id=”6680049″ file=”Gather.groovy”]

This depends on the gist support you have on your blog system. WordPress, for example, supports gist embedding by use of ‘shortcodes‘.

Yes, this is old news, but I had this post in draft mode, so I may as well post it.
 

References

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.