An introduction to Android’s App Shortcuts

Android Nougat 7.1, the newest version of Android at the time of writing this article, comes with several new features.  The one that we will be exploring in this blog post is the App Shortcuts feature.

App Shortcuts allows you to create shortcuts for specific actions within your app that can bring users directly from the launcher to the associated screens. The feature is available on any launcher that supports them, such as Google Now and Pixel launchers. To reveal the shortcuts of an app, simply long-press the launcher icon of that app.

chrome-app-shortcuts

If you are enrolled in the Beta program and got the Android 7.1 Developer Preview, you can find that some of the apps already support App Shortcuts, for example: Camera, Dialer, Messenger, Google Drive, and Youtube. Unfortunately there’s no visual indication on which apps support this feature (at least not at this time), so you will have to long-press on every app’s launcher icon in order to discover the ones that support it.

Adding App Shortcuts to your app

There are 2 ways that can be used to add app shortcuts to your app:  statically (by declaring all the shortcuts in a resource file, also known as manifest shortcuts) and dynamically (by adding the shortcuts at runtime).

First we will take a look at the static approach.

1. Create a resource file: res/xml/shortcuts.xml

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    
    <shortcut
        android:shortcutId="new_task"
        android:enabled="true"
        android:icon="@drawable/ic_shortcut_new_task"
        android:shortcutLongLabel="@string/shortcut_label_create_new_task"
        android:shortcutShortLabel="@string/shortcut_label_new_task"
        android:shortcutDisabledMessage="@string/shortcut_label_disabled" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.vgrec.testshortcuts.NewTaskActivity"
            android:targetPackage="com.example.vgrec.testshortcuts" />
    </shortcut>

    <shortcut
        android:shortcutId="opened_tasks"
        android:enabled="true"
        android:icon="@drawable/ic_shortcut_opened_tasks"
        android:shortcutLongLabel="@string/shortcut_label_view_opened_tasks"
        android:shortcutShortLabel="@string/shortcut_label_opened_tasks"
        android:shortcutDisabledMessage="@string/shortcut_label_disabled" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.vgrec.testshortcuts.OpenedTasksActivity"
            android:targetPackage="com.example.vgrec.testshortcuts" />
    </shortcut>

</shortcuts>

Each shortcut defines a series of attributes, like the icon and labels, and also references an intent which is set to launch a specific activity when triggered.
The recommended maximum number of shortcuts is 4, although it is possible to publish up to 5.

Please note that the android:shortcutId attribute is a mandatory attribute. Not declaring it will cause the respective shortcut not to appear in the shortcuts list.

2. Next step is to reference the shortcuts.xml file in the AndroidManifest.xml as metadata to the app’s launcher activity:

<activity android:name=".MainActivity">
     <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>

     <meta-data
           android:name="android.app.shortcuts"
           android:resource="@xml/shortcuts" />
</activity>

Any activity that has the intent-filter action set to android.intent.action.MAIN and the category to android.intent.category.LAUNCHER can display app shortcuts.

Having all these pieces in place, the result might look something like this:

android app shortcuts

For testing purposes I used the Android Studio wizard to generate the shortcut icons, but in a production app you should follow the Android Design Guidelines 🙂.

If you tap and drag a shortcut then it will be pinned to the device’s launcher:

android-pinned-app-shortcuts

Have you noticed that the text of the shortcuts in the first image differs from the text of the shortcuts in the second image? Well, that’s what the android:shortcutLongLabel and android:shortcutShortLabel attributes are for.

What about the android:shortcutDisabledMessage? In case a shortcut is disabled it will no longer appear in the shortcuts list. However, if the shortcut is already pinned, it will be simply grayed out and tapping on it will display the message specified by the android:shortcutDisabledMessage attribute.

action_disabled

Adding App Shortcuts dynamically

ShortcutManager is the entry point for manipulating (adding, updating, removing) shortcuts at runtime:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

Intent intent = new Intent(this, NewTaskActivity.class);
intent.setAction(Intent.ACTION_VIEW);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, getString(R.string.shortcut_id_new_task))
    .setShortLabel(getString(R.string.shortcut_label_new_task))  
    .setLongLabel(getString(R.string.shortcut_label_create_new_task))
    .setIcon(Icon.createWithResource(this, R.drawable.ic_action_add) 
    .setIntent(intent)
    .build();

 shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

Other methods from ShortcutManager that might be of interest:

updateShortcuts(List ids) – update all existing shortcuts by ID.

removeDynamicShortcuts(List ids) – delete dynamic shortcuts by ID.

disableShortcuts(List ids) – disable dynamic shortcuts by ID.

reportShortcutUsed(String shortcutId) – You want to call this method whenever the user selects the shortcut containing the given ID or when the user completes an action in the application that is equivalent to selecting the shortcut. Typically, launcher applications use this information to build a prediction model so that they can promote the shortcuts that are likely to be used at the moment. (This method works both for static and dynamic shortcuts.)

Please note that attempting to manipulate a static shortcut via the ShortcutManager will throw IllegalArgumentException: Manifest shortcut ID=xyz may not be manipulated via APIs.

Example usage of AppCompatActivity in Android

The latest release of android support library, 22.1, deprecates the ActionBarActivity in favor of AppCompatActivity, which promises to bring a single consistent ActionBar for all devices starting with API Level 7 and above.
Also, the new update adds the ability to tint widgets automatically when using AppCompat, and adds support for consistent material design dialogs. Use support.v7.app.AlertDialog (instead of android.app.AlertDialog), to get nice looking material dialogs across multiple versions of devices.

android-support-22.1

Example:
1. In order to benefit from all these things, the first thing you should do is to update the support library to 22.1.0.

dependencies {
   // … 
   compile 'com.android.support:appcompat-v7:22.1.0'
}
2. Then let your activity extend AppCompatActivity.
public class MainActivity extends AppCompatActivity {
  // ...
}
3. And finally, change the application theme to AppCompat or any descendants of it.
<application android:theme="@style/Theme.AppCompat">
Configuring the material color palette

Following the steps above will setup the application with the default theme, but the actionbar can be styled further by specifying the material color palette:

<style name="AppTheme" parent="Theme.AppCompat">
   <item name="colorPrimary">@color/primary</item>
   <item name="colorPrimaryDark">@color/primaryDark</item>
   <item name="colorAccent">@color/accent</item>
</style>

and then update your application theme to use AppTheme.

<application android:theme="@style/AppTheme">

Introduction to Android Espresso

Espresso is a testing framework that exposes a simple API to perform UI testing of android apps. With the latest 2.0 release, Espresso is now part of the Android Support Repository which makes it more easier to add automated testing support for your project.

But before jumping into Espresso API, lets consider what puts it apart from the other testing frameworks.

  • One of the first things you’ll notice about Espresso, is that its code looks a lot like English, which makes it predictable and easy to learn.
  • The API is relatively small, and yet open for customization.
  • Espresso tests run optimally fast (no waits, sleeps)
  • Gradle + Android Studio support

Adding Espresso to your project

1. First of all make sure you have Android Support Repository installed

android sdk manager

2. Add the following dependencies to your application build.gradle file

dependencies {
   androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
   androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
}

3. Finally, specify the test instrumentation runner in default config

android {

    defaultConfig {
        // ....
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

And that is basically what it takes “to invite your project to a cup of Espresso”!

The main components of Espresso

Espresso is built up from 3 major components.

These components are:

  • ViewMatchers – allows you to locate a view in the current view hierarchy
  • ViewActions – allows you to interact with views
  • ViewAssertions – allows you to assert the state of a view.

For simplicity, you may use these shortcuts to refer to them:

  • ViewMatchers – “find something
  • ViewActions – “do something
  • ViewAssertions – “check something

And, for example when you will need to check something (like, is some text displayed on the screen?), you’ll know you’ll need a ViewAssertion for that.

Below is an example of a test in Espresso, and where the main components find their place.

Android Espresso main components

A simple test using onView()

Suppose we have an app where the user is asked to enter his name.
After he enters the name, he taps on the “Next” button and is redirected to another activity where a greeting message is displayed.

espresso simple test

If we would write a test for this scenario, then it might look something like this:

// locate the view with id "user_name" and type the text "John"
onView(withId(R.id.user_name)).perform(typeText("John"));

// locate the view with id "next" and click on it
onView(withId(R.id.next)).perform(click());

// locate the view with id "greeting_message" and check its text is equal with "Hello John!"
onView(withId(R.id.greeting_message)).check(matches(withText("Hello John!")));

Notice that we don’t specify explicitly what kind of view we are interacting with (eg.: EditText, Button), we simply say that we are looking for a view with a specific id.
Also, when clicking on the “Next” button and later checking the text, we don’t have to write some special code to tell Espresso that we have navigated to another activity.

Now, if we want to actually run this test, then it should be put in a class. In Gradle, the location where tests are stored is: yourApp/src/androidTest/java.

This is an example of a test class, and its main characteristics:

Espresso example test class

A simple test using onData()

Whenever you have a ListView, GridView, Spinner, and other Adapter based views, you’ll have to use onData() in order to interact with an item from that list.
onData() is targeting directly the data provided by your adapter. What does this mean, we will see in a moment.

In a hypothetical application we are asked to select a country from a Spinner, once selected, the country is displayed next to the Spinner.

espresso on data

A test to check that the displayed country is equal with what was selected, might look like this:

// locate the view with id "country_spinner" and click on it
onView(withId(R.id.country_spinner)).perform(click());

// match an item that is a String and is equal with whatever value the COUNTRY constant is initialized, then click on it.
onData(allOf(is(instanceOf(String.class)), is(COUNTRY))).perform(click());

// locate the view with id "selected_country" and check its text is equal with COUNTRY
onView(withId(R.id.selected_country)).check(matches(withText("selected: " + COUNTRY)));

The Spinner you saw in the example above is backed by a simple array of strings, and because of this, we specify that we are looking for an item of type String. If, instead of a String, it were some custom object, then we would specify that instead.
To illustrate, consider the following example where a list of books is displayed:

books-adapter

Since the items in the adapter are of type Book, so will look the query:

onData(allOf(is(instanceOf(Book.class)), withBookTitle(BOOK_TITLE))).perform(click());

DataInteractions

Espresso has a few useful methods that can be used to interact with data.

atPosition() – Might be useful when the element to interact with is not relevant, or when the items always appear in a specific order, so you know every item on what position sits.

onData(...).atPosition(2).perform(click());

inRoot() – Use inRoot() to target non-default windows. One scenario where this can be used is when testing autocomplete. The list that appears in the autocomplete view belongs to a window that is drawn on top of application window.
In this case you have to specify that the data you are looking for, is not in the main window.

onView(withText("AutoCompleteText"))
        .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
        .check(matches(isDisplayed()));

onChildView() – This DataInteraction allows to further refine the query by letting you interact with a specific view from a list row.
Let say that you have a list of items and every item has a delete button. You want to click on Delete button of a specific item:

onData(withBookTitle("My Book"))
      .onChildView(withId(R.id.book_delete)).perform(click());

inAdapterView() – This allows to select a particular adapter view to operate on, by default Espresso operates on any adapter view.
You may find this useful when dealing with ViewPagers and Fragments, and you want to interact with the AdapterView that is currently displayed, or when you have more than one adapter view in your activity.

onData(withBookTitle("My Book"))
      .inAdapterView(allOf(isAssignableFrom(AdapterView.class), isDisplayed()))
      .perform(click());

Espresso and RecyclerView

RecyclerView is an UI component designed to render a collection of data just like ListView and GridView, actually, it is intended to be a replacement of these two. What interests us from a testing point of view, is that a RecyclerView is no longer an AdapterView. This means that you can not use onData() to interact with list items.

Fortunately, there is a class called RecyclerViewActions that exposes a small API to operate on a RecyclerView. RecyclerViewActions is part of a separate lib called espresso-contrib, that also should be added to build.gradle:

dependencies {
    // ...

    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0');
}

Since your project already includes a dependency to recyclerview, and might as well include support libs, some dependencies conflicts might appear. In this case exclude
them from espresso-contrib like this:

dependencies {
    // ...

    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
}

Here is how to click on an item from the list by position:

onView(withId(R.id.recyclerView))
      .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

Or how to perform a click on a View from an item:

onView(withId(R.id.recyclerView))
      .perform(RecyclerViewActions.actionOnItem(
                hasDescendant(withText(BOOK_TITLE)), click()));

More Espresso Examples on Github:
https://github.com/vgrec/EspressoExamples

Introducing SectionedActionBarList

This Android library allows you to replace the drop down navigation list with a custom list where the items in the list are grouped by sections. It was inspired from the Google I/O 2014 app how sessions are grouped in the ActionBar list.

While the new Material Design movement discourages the use of ActionBar lists, in some situations it can be the best available option.

At the moment the library is not available on a repository, you have to include it as a source library in your project, but that will be fixed soon.

Github project page: https://github.com/vgrec/SectionedActionBarList

Example
List
<Section> sections = new ArrayList
<Section>();
 
Section themes = new Section("Themes");
themes.add("Design");
themes.add("Develop");
themes.add("Distribute");
sections.add(themes);
 
Section topics = new Section("Topics");
topics.add("Android");
topics.add("Chrome / Web");
topics.add("Cloud Services");
topics.add("Media");
topics.add("Location");
topics.add("Performance");
sections.add(topics);
 
Section types = new Section("Types");
types.add("Sessions");
types.add("App Reviews");
types.add("Box Talks");
sections.add(topics);
 
SectionedActionBarList actionBarList = new SectionedActionBarList(this).from(sections);
actionBarList.setItemSelectedListener(new ItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id, String sectionName, String itemName) {
      Toast.makeText(MainActivity.this, "Section: " + sectionName + ", Item: " + itemName, Toast.LENGTH_LONG).show();
  }
});

The SectionedActionBarList accepts a list of sections List<Section> sections, and every Section has a name and some associated items.

sectioned actionbar list

Configuration

Small customizations can be done to fit with your application design:

ListConfiguration configuration = new ListConfiguration(this);
configuration.setActionBarItemColorResource(R.color.brown);
configuration.setIndicatorDrawableResource(R.drawable.spinner_indicator_dark);
configuration.setSectionTitleColorResource(R.color.teal);
configuration.setDropdownItemColorResources(R.color.light_blue, R.color.dark_grey);

SectionedActionBarSpinner actionBarSpinner = new SectionedActionBarSpinner(this, configuration).from(sections);
// ....

actionbar list

Thanks to Webucator, a provider of Android classes, for producing the below video illustrating the integration of the SectionedActionbarList.


Speed up creation of parcelable classes with AutoParcel

When the time comes to send a custom object to an activity or to persist it across configuration changes, we all know that we can use either Serializable or Parcelable interfaces. And probably you have heard that Parcelable is the recommended way to do it on Android, because it is much much faster than Serializable (10x times and more, check this article for a detailed comparition between Serializable and Parcelable http://www.developerphil.com/parcelable-vs-serializable/)
However, using Serializable still is more attractive than using Parcelable, because it is simpler. The only thing you have to do is to let your class implement Serializable and you are done, while implementing Parcelable requires you to write a lot of boilerplate code which slows you down.

Fortunately, AutoParcel, a Parcelable generation library, is here to help you and speed up creation of Parcelable classes.

With gradle the integration is easy:
parent build.gradle

buildscript {
   repositories {
       jcenter()
   }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.13.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:+'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
// ...

module build.gradle

apply plugin: 'android-apt'

// ...

dependencies {
   apt 'com.github.frankiesardo:auto-parcel-processor:+'
   compile 'com.github.frankiesardo:auto-parcel:+'
}
Example:
@AutoParcel
public abstract class Book implements Parcelable {
   public abstract String getTitle();

   public abstract String getDescription();

   public static Book create(String title, String description) {
      return new AutoParcel_Book(title, description);
   }
}

Key points:
– your class should be annotated with @AutoParcel
– should be abstract and implement Parcelable
– no fields, just abstract getter methods
– have a method that takes all classes parameters and returns an instance of AutoParcel_<YourModelClass>

Don’t worry if the IDE complains that it cannot find AutoParcel_Book, just hit run and the compilation error should auto resolve.

And the usage is:

Book book = Book.create(bookTitle, bookDescription);

Another feature of AutoParcel that you may find useful, is that unlike regular Parcelable classes, you can have a custom object as a member of an AutoParcel class, without having that object to implement Parcelable.

@AutoParcel
abstract public class Book implements Parcelable {
   public abstract String getTitle();

   public abstract String getDescription();

   public abstract Author getAuthor();

   public static Book create(String title, String description, Author author) {
      return new AutoParcel_Book(title, description, author);
   }
}

The Author class is not required to implement Parcelable:

public class Author {
   private String name;
   
   public String getName() {
       return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

Analysing Android code with SonarQube

sonar analysis

SonarQube, formerly known as Sonar, is a platform to analyze code quality. Analysis covers such aspects as code duplications, potential bugs, coding rules, complexity, unit tests, comments, and architecture & design.
It supports supports more than 20 programming languages and has a reach set of useful plugins that gives you the opportunity to inspect different aspects of the code.

What is caracteristic about SonarQube is that it comes as a platform in the form of a web application. This means that the results of the analysis will be displayed in a web page.

Installing SonarQube

The installation is pretty straightforward, you have just to download an archive and extract it in a folder of your choice.
1. Go to http://www.sonarqube.org/downloads/ and download the latest release.
2. Unzip the archive

Starting SonarQube

1. Go to sonarqube-4.3/bin (or whatever version you downloaded)
2. Open a corresponding folder according to your operating system (linux-x86-64 in my case). There you should see a file called sonar.sh (or StartSonar.bat for Windows)
3. Open up a terminal window and execute: sonar.sh start (or just double click StartSonar.bat on windows). This command will start sonar listening on localhost:9000.
4. Open a browser and enter localhost:9000. The sonar web page should open.
Note that it may take some time until sonar loads, so if you get “page not found” in your browser, try to refresh the page later.

Installing SonarQube Runner

There are several ways to analyse the source code and in this tutorial we will choose to analyse with SonarQube Runner, recommended as the default launcher to analyze a project.

1. Once again go to http://www.sonarqube.org/downloads/ and download SonarQube Runner
2. Extract the downloaded archive into a directory of your choise, which we will refer as: <install_directory>
3. Update global settings by editing: <install_directory>/conf/sonar-runner.properties (if you are running sonar on localhost, like in this tutorial, you don’t have to modify any settings):

#----- Default SonarQube server
#sonar.host.url=http://localhost:9000

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor

#----- Global database settings
#sonar.jdbc.username=sonar
#sonar.jdbc.password=sonar

4. Create a new SONAR_RUNNER_HOME environment variable set to <install_directory>, so that you could invoke sonar runner from any location.
5. Add the <install_directory>/bin directory to your path.

To check that sonar runner was installed properly, open a terminal window and execute sonar-runner -h.

You should get a message like this:

usage: sonar-runner [options]

Options:
-D,--define Define property
-e,--errors Produce execution error messages
-h,--help Display help information
-v,--version Display version information
-X,--debug Produce execution debug output

Analysing source code of a project

Once the sonar runner is properly installed, we can proceed to code analysis.

1. Navigate to the root directory of your project and create a file called sonar-project.properties, which will specify the project settings such as the code source directory, language used, and the project name:

sonar.projectKey=myproject
sonar.projectName=My Project
sonar.projectVersion=1.0

sonar.sources=src
sonar.language=java
sonar.sourceEncoding=UTF-8

Note: for Android Studio, which follows the gradle directory structure, set the sourses as:

sonar.sources=src/main/java

2. Run sonar-runner command to start the analysis.
3. Once the analysis complets, head to localhost:9000 to see the results for your project.

Removing a project from SonarQube

First login as an administrator, admin/admin default username and password.

1. Go to your project dashboard
2. In the top right corner click on Configuration -> Deletion -> Delete Project

delete-shonar-project

Introducing Photo Collage Creator

I was playing with bitmap manipulations and so was born the idea to make this app. It was started some time ago, and, even if at times I was thinking it will never see the daylight, finally it was released!

Description of the app:
Photo Collage Creator is a simple app that lets you create beautiful collages within seconds and share them with your family, friends or colleagues. To create a collage you just need to select the photos, apply the frames, save, and you are done!

Features:
– Create collages composed from up to 5 photos
– Various frames to create a collage
– Shuffle the photos within the collage
– Save your collages
– Share collages on gmail, facebook, picasa, and other social networking sites.

Hope you will like it!

collage maker

photo collage creator android

application on android market