Thursday, 31 December 2015

Bash - basic script for requesting input from the user

As mentioned in this post, my know-how of Bash scripting is pretty nil so I've been skimming through this guide the last few days to get to grips with the basic concepts. I've put together this basic script as a reminder to myself for future reference of how to request and parse input from the user:

#!/bin/bash

echo "Enter the word that you'd like to add to the dictionary and then press [Enter]."

read WORD

PATH_DICTIONARY="/Users/adil/Downloads/MyDictionary.txt"
COUNT=`grep -c "^$WORD$" $PATH_DICTIONARY`

if [ $COUNT -gt 0 ]
then echo "Word is already contained in the dictionary."; exit;
fi

echo "\$COUNT = $COUNT"

echo "Adding $WORD to the dictionary..."

echo "$WORD" >> $PATH_DICTIONARY

echo "Done"

Bash - basic script for checking positional params

My know-how of Bash scripting is pretty nil so I've been skimming through this guide the last few days to get to grips with the basic concepts. I've put together this basic script as a reminder to myself for future reference of how to pass in and parse positional params in a script:

#!/bin/bash

if [ $# -lt 2 ];
then echo "Must pass in at least two params."; exit 1;
fi

#set -x
GREETING="Hello"
echo "$GREETING $USER"
#set +x

echo ""
echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"
echo ""

[ -f $0 ] && (echo "File $0 exists.";) || (echo "File $0 doesn't exist.";)

echo ""

if [ $1 -eq $2 ]; then
echo "The value of \$1 is equal to the value of \$2.";
elif [ $1 -lt $2 ]; then
echo "The value of \$1 is less than the value of \$2.";
else echo "The value of \$1 is greater than the value of \$2.";
fi

echo ""

AREA=$[$1 * $2]
echo "\$AREA = \$1 * \$2 = $AREA"

case $AREA in
([-][0-9]*)
    echo "The value of \$AREA is negative.";;
([0-9])
    echo "The value of \$AREA is < 10.";;
([1-9][0-9])
    echo "The value of \$AREA is < 100.";;
*)
    echo "The value of \$AREA is >= 100.";;
esac

echo ""
echo "Goodbye $USER"

If I run this script as follows: bash MyScript.sh, then I'll see this output:

Must pass in at least two params.

If, however, I run this script as follows: bash MyScript.sh 5 4, then I'll see this output:

Hello adil

$0 = MyScript.sh
$1 = 5
$2 = 4

File MyScript.sh exists.

The value of $1 is greater than the value of $2.

$AREA = $1 * $2 = 20
The value of $AREA is < 100.

Goodbye adil

That's it. That's a basic script which demonstrates how to perform some conditional checks on positional params passed into a script.

Saturday, 12 December 2015

Proguard: rules for building a library

So you have a library defined and you want to run proguard on it when building it to remove unused code and to obfuscate code. Of course you do not want to remove or obfuscate classes/interfaces/enums/methods/fields that are public since these will be called on by the projects that depend on your library.

Here are the rules that you need in your proguard config file:

-keepparameternames

-keep public interface com.mycompany.mylibrary.** {
    <methods>;
}

-keep public class com.mycompany.mylibrary.** {
    public <init>(...);
    public <fields>;
    public static <fields>;
    public <methods>;
    public static <methods>;
}

-keep public enum com.mycompany.mylibrary.** {
    public <init>(...);
    public <fields>;
    public static <fields>;
    public <methods>;
    public static <methods>;
}

The above is not an exhaustive list of all the rules which you might need but it's a starting point and should suffice for most libraries. If you need to add some more rules you'll need to get a grasp of the proguard rules syntax here: http://proguard.sourceforge.net/manual/usage.html

Wednesday, 2 December 2015

Android: start Service of another app

So you want to start a Service which belongs to another app? There's two ways to do this depending on how the Service has been defined.

Let's assume first that the Service is defined in the manifest of the first app with the exported attribute as follows:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thinkincode.someapp" >
    <application>
        <service
            android:name=".SomeService"
            android:exported="true" />
    </application>
</manifest>

In this case the Service can be started from the second app as follows:

Intent intent = new Intent();
intent.setComponent(new ComponentName(
    "com.thinkincode.someapp",
    "com.thinkincode.someapp.SomeService"));

startService(intent);

The better way of declaring a Service to be used by other apps is of course to use intent filters. So, going back to the manifest of the first app, this is how we'd define the Service:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thinkincode.someapp" >
    <application>
        <service
            android:name=".SomeService"
            android:exported="true">
            <intent-filter>
                <action android:name="StartSomeService" />
            </intent-filter>
        <service/>
    </application>
</manifest>

And this is how we'd now start the Service from the second app:

Intent intent = new Intent();
intent.setAction("StartSomeService");

startService(intent);

That's it! You should now be able to (1) define Services which can be used by other apps and (2) call on Services defined by other apps.

Monday, 23 November 2015

Book Summary: Head First Design Patterns, by Eric Freeman and Elisabeth Robson

Below is a summary of the main Design Patterns and Design Principles covered in the Head First Design Patterns book. Excellent book by the way! A must read for all software engineers.

Design Patterns

Strategy
  • Encapsulates interchangeable algorithms/behaviours and uses delegation to decide which one to use.
  • Allows the algorithm to vary independently from the clients that use it.
Observer
  • Defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are updated and notified automatically.
Decorator
  • Wraps another object dynamically and provides additional behaviour for it.
  • Provides a flexible alternative to subclassing for extending functionality.
  • Doesn't alter the interface but adds responsibility.
Factory Method
  • Defines an interface for creating an object, but lets subclasses decide which class to instantiate.
  • Lets a class defer instantiation to subclasses.
Abstract Factory
  • Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Singleton
  • Ensures a class has only one instance, and provides a global point of access to it.
Command
  • Encapsulates a request as an object, thereby letting you parameterise other objects with different requests, queue or log requests, and support undoable operations.
  • Decouples an object making a request from the one that knows how to perform it.
Adapter
  • Wraps an object and provides a different interface to it.
  • Converts the interface of a class into another interface the clients expect.
  • Lets classes work together that couldn't otherwise because of incompatible interfaces.
Facade
  • Wraps a bunch of objects to simplify their interface.
  • Provides a unified interface to a set of interfaces in a subsystem.
  • Defines a higher-level interface that makes the subsystem easier to use.
Template Method
  • Encapsulates algorithms.
  • Defines the skeleton (/steps) of an algorithm in a method, deferring some steps to subclasses.
  • Subclasses decide how to implement steps in an algorithm (without changing the algorithm's structure).
Iterator
  • Provides a way to traverse/access the elements of an aggregate object sequentially without exposing its internal structure.
  • Takes the job of iterating over an aggregate and encapsulates it in another object.
Composite
  • Provides a structure to hold both individual objects and composite objects.
  • Lets clients treat individual objects and collections/compositions of objects uniformly.
State
  • Allows an object to alter its behaviour when its internal state changes.
  • Encapsulates state into separate classes and delegates to the object representing the current state.
Proxy
  • Wraps another object to control access to it.
  • Acts as a representative for another object.

Design Principles

Encapsulate what varies – Identify the aspects of your application that vary and separate them from what stays the same. Take the parts that vary and encapsulate them.

Program to an interface, not to an implementation.

Composition over inheritance – Favour composition over inheritance.

Loose coupling – Strive for loosely coupled designs between objects that interact.

Open-Closed – Classes should be open for extension, but closed for modification.

Dependency inversion
  • Design upon abstractions. Do not depend on concrete classes.
  • High-level components should not depend on low-level components; rather, they should both depend on abstractions.
  • No variable should hold reference to a concrete class.
  • No class should derive from a concrete class.
  • No method should override an implemented method of any of its base classes.
Principle of least knowledge
  • Talk only to your immediate friends.
The Hollywood Principle
  • Don't call us, we'll call you.
  • High level components call low level components, and not vice-versa.
Single responsibility
  • A class should have only one responsibility.
  • A class should have only one reason to change.

Sunday, 4 October 2015

Android: How to pass data from DialogFragment to Activity

So you've started a DialogFragment from within an Activity and want to request an action or pass data from the DialogFragment to the Activity. The best, cleanest way I find to do so is by means of local broadcasts. Here's how...

First, define a BroadcastReceiver in your Activity as follows:

private class LocalBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // safety check
        if (intent == null || intent.getAction() == null) {
            return;
        }

        if (intent.getAction().equals("SOME_ACTION")) {
           doSomeAction();
        }
    }
}

Second, declare an instance of LocalBroadcastReceiver in your Activity, as follows:

private BroadcastReceiver localBroadcastReceiver;

Third, instantiate the BroadcastReceiver in the activity's onCreate(Bundle) method:

localBroadcastReceiver = new LocalBroadcastReceiver();

Fourth, register for the activity to listen out for the local broadcasts in the activity's onResume() method:

LocalBroadcastManager.getInstance(this).registerReceiver(
        localBroadcastReceiver,
        new IntentFilter("SOME_ACTION"));

Fifth, unregister the broadcast receiver in the activity's onPause() method:

LocalBroadcastManager.getInstance(this).unregisterReceiver(
        localBroadcastReceiver);

Sixth and final, send the broadcast from wherever makes sense in your DialogFragment, as follows:

LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(
    new Intent("SOME_ACTION"));

Sunday, 27 September 2015

Android: how to check that internet connection is able to transfer data

Checking whether an Android device is connected to the internet is straightforward. Checking whether the device's internet connection is actually working and able to upload/download data unfortunately is not so straightforward! We'll cover both things in this tutorial.

Firstly, here's the method to check whether the device has an internet connection:

boolean isActiveNetworkConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    return networkInfo != null
        && networkInfo.isAvailable()
        && networkInfo.isConnected();
}

Now, how to check that the internet connection is actually able to transfer data? One way to do this is to ping a web location of your choice, as follows:

boolean isGoogleReachableWithPing() {
    try {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("/system/bin/ping -c 1 www.google.com");

        int exitValue = process.waitFor();

        return exitValue == 0;
    } catch (Exception ex) {
        return false;
    }
}

The method above is ideal but unfortunately the ping command works on some Android devices and not on others (see here). An alternative way to check that the internet connection is able to transfer data is as follows:

boolean isGoogleReachableWithInetAddress() {
    try {
        InetAddress inetAddress = InetAddress.getByName("www.google.com");

        return inetAddress != null && !inetAddress.equals("");
    } catch (Exception ex) {
        return false;
    }
}

Putting all these methods together, what you want is this:

boolean isActiveNetworkConnectedAndWorking(Context context) {
    return isActiveNetworkConnected(context)
        && (isGoogleReachableWithPing() || isGoogleReachableWithInetAddress());
}

UPDATE: These methods are now contained in the NetworkInspector class in this repository.

Saturday, 1 August 2015

Book Review: The Nomadic Developer, by Aaron Erickson

Decent book on what it means to be a technology consultant and how it does (or should) differ from the role of a contractor. I'm not a technology consultant so I was reading it from an outside perspective and found the contents of the book useful still. I liked some of the general tips in the book. For example, the importance of personal appearance, being good at interviews, always learning, being easy to work with, having energy for the job, and maintaining a strong network of people inside and outside of your current employment.

Here's some quotes from the book which stood out for me in particular:

"Seniority isn't measured in years at the company, but in the number and quality of relationships developed over time."

"Starting your network when you suddenly need a new position is not the best time."

"With respect to appearance generally, this tip really comes down to the fact that people would rather work with folks who are physically, mentally, and emotionally well than people who are not."

"One of the things that separates a contractor from a consultant is the former is there to do a specific job for a specific amount of time. The latter, the consultant, is there to provide other humans with advice and guidance in addition to doing work."

"Without any discernible process, there is no way to track with consistency the accuracy of estimates, progress against a plan, or any other metric that would allow you to know how a project is doing."

"Being willing to be fired because you stand up for what you believe is right (pushing back timelines, ensuring quality, and so on) is a powerful thing."

Saturday, 7 March 2015

Go To: Software Superheroes, by Steve Lohr

I bought this book back in 2002 and only got around to reading it in 2015! Good thing too as it probably wouldn't have made much sense back then! Good book on the evolution of software and programming from the 1950s through to the 2000s I thought. I liked the last few chapters in particular which chronicled the birth of the internet and web programming and then the introduction of Java and lastly the growth of Apache and the open source community.