Java

New release of Kirai – elegant string formatting library for Java

2015-11-22 Android, Java, Open source No comments

I’ve recently released version 1.4.0 of Kirai library. Kirai means phrase in Swahili language. It’s string formatting library written in Java. It originally started as an Android library, but it evolved to pure Java library. It’s first possibilities were basic string formatting and text formatting for Android TextViews. Now, it allows to format strings for Java, Web, Android and even Unix Terminal! Have you ever wanted to have colorful and styled text in your mobile app, website or terminal app? Now you can with an elegant and fluent API! Moreover, I’ve added test coverage supported by codecov.io. It’s really nice service, which integrates with Travis CI and is free for open-source projects. It’s available for various programming languages and build systems.

Check library source code and samples at: https://github.com/pwittchen/kirai

You can add it to your project via Maven:

<dependency>
    <groupId>com.github.pwittchen.kirai</groupId>
    <artifactId>library</artifactId>
    <version>1.4.0</version>
</dependency>

or through Gradle:

dependencies {
  compile 'com.github.pwittchen.kirai:library:1.4.0'
}

Bunch of updates in my OSS for Android

2015-11-08 Android, Java, Kotlin, Open source No comments

Introduction

In the last days I prepared a bunch of updates in my open-source software for Android. Most of them are bug fixes and are related to increasing robustness of the projects as well as their overall quality. I also decided to play a little with Kotlin language from JetBrains, which seems to be reasonable choice for mobile applications running on Android. Nevertheless, writing an app in Kotlin requires some additional configuration and we should remember, it’s still in beta version. If you are interested in Kotlin programming for Android, take a look at Getting started with Android and Kotlin guide from official Kotlin website. You can also take a look at one of my sample apps written in Kotlin and its configuration in Gradle.

Summary of updates

ReactiveNetwork v. 0.1.3

  • fixed bug with incorrect status after going back from background inside the sample app reported in issue #31
  • fixed RxJava usage in sample app
  • fixed RxJava usage in code snippets in README.md
  • added static code analysis
  • updated code formatting
  • added sample sample app in Kotlin

Unfortunately, in Android we cannot use Java 8 yet and code should be written in Java 7. We can do some hacks like using RetroLambda or libraries implementing part of functionalities available in Java 8 like streams, but these solutions are still hacks – not the right way. In Kotlin we can use lambdas like in Java 8. In addition, we have a lot of other cool features, which allow us to write less lines of code and detect possible mistakes while writing apps. E.g. Kotlin helps us to avoid NPEs with its additional operators like !!, which tells us that NPE can occur, so we can think of eliminating this possibility. If we don’t use this operator when we should, IntelliJ IDEA or Android Studio will warn us. Below, we can see exemplary usage of ReactiveNetwork library with Kotlin. In this code snippet, we are using so called synthetic properties from Kotlin Extensions for Android. Value connectivity_status is an id of the view defined in XML layout. We can call it directly in Kotlin code and treat as object. It’s really useful and allows us to avoid calling findViewById(...) method for every view in Activity or injecting views with additional libraries like ButterKnife or KotterKnife. It means that we can get rid of a lot of boilerplate code.

ReactiveSensors

  • fixed RxJava usage in sample app
  • fixed RxJava usage in code snippets in README.md
  • added static code analysis
  • refactored sample app and removed repetitions in code

In this project I made only changes inside the documentation, improved Gradle configuration and added another sample app. There were no changes inside the library code, so there was no need to release new library version to Maven Central Repository. Moreover, I’m going to add sample app in Kotlin for this project in the nearest future.

ReactiveBeacons v. 0.3.0

  • replaced distinct() operator with distinctUntilChanged() operator in Observable observe() method in ReactiveBeacons class
  • added permissions ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION to satisfy requirements of Android 6
  • renamed void requestBluetoothAccessIfDisabled(activity) method to void requestBluetoothAccess(activity)
  • added boolean isBluetoothEnabled() method
  • added boolean isLocationEnabled(context) method
  • added void requestLocationAccess(activity) method
  • modified sample app in order to make it work on Android 6 Marshmallow
  • reduced target API from 23 to 22 in library due to problems with additional permissions and new permission model (it can be subject of improvements in the next releases)
  • added package private AccessRequester class
  • added sample app in Kotlin

WeatherIconView v. 1.1.0

  • added icons from 2.0 version of the original weather-icons project
  • updated compile sdk version
  • updated Gradle Build Tools version

Prefser v. 2.0.2

  • fixed bug reported in issue #70: get(...) method now returns a null value instead of “null” string when setting default value to null of String type
  • fixed RxJava usage in sample app
  • fixed RxJava usage in code snippets in README.md
  • changed code formatting to SquareAndroid
  • added static code analysis
  • improved code according to static code analysis suggestions

Kirai v. 1.1.0

  • removed formatter(...) method from Kirai class
  • added format(...) method accepting implementation of Formatter interface to Kirai class
  • added Syntax interface and HtmlSyntax class implementing this interface
  • added put(String key, Object value, Syntax syntax) method to Piece class
  • set HtmlSyntax as default Syntax implementation in Piece class
  • removed dependencies to Android SDK
  • updated project dependencies
  • applied Square code style
  • updated tests, sample app and code snippets in README.md
  • added gh-pages with JavaDoc

Any suggestions of further improvements are more than welcome as usual!

An Opinionated Guide to Modern Java by Parallel Universe

2015-11-05 Java No comments

Introduction

I recently read series of three articles titled An Opinionated Guide to Modern Java by Parallel Universe. It presents really interesting point of view and can be read even by non-Java developers due to its form. Non-Java developers can learn that Java can be fast, elegant and lightweight as much as other hipster technologies like Ruby, Go or Node.js. The main difference is the fact that Java is quite old and widely used technology what means it’s well tested, stable, solid, has great development and monitoring tools. Besides widely known projects, author of the articles presents some information about tools built by Parallel Universe, which are not commonly used and are less popular, but are also worth considering for modern development. E.g. he writes about so called Capsule project, which is intended to be a Docker alternative for Java and Quasar, which is interesting high level API for multi-threading with concept of actors, which is also available in Akka and Erlang. I recommend any Java and non-Java developer to read this series to learn new things and consolidate the knowledge.

Series of articles

Introducing ReactiveBeacons

2015-09-30 Android, Context awareness, Java, Open source No comments

Recently, I’ve created yet another reactive library for Android. It’s called ReactiveBeacons and it’s responsible for observing BLE (Bluetooth Low Energy) beacons. Beacons are small devices, which became quite popular in the last years. They can be utilized in creating Contextual Awareness, Contextual Computing and Internet of Things. Beacons have lithium battery, are constantly turned on and emit signals via Bluetooth all the time. ReactiveBeacons library allows to transform these signals into Observable stream compatible with RxJava. Whenever new beacon is detected or RSSI (Received signal strength indication) changes, new immutable beacon data is emitted.

Usage of the library inside the Activity is simple:

We also have to remember to unregister subscription correctly in order to stop BLE scan, which can drain the battery.

If you want to use this library in your project, add the following dependency to your build.gradle file.

dependencies {
  compile 'com.github.pwittchen:reactivebeacons:0.0.1'
}

Don’t forget to add dependency to RxAndroid if you want to use Android-specific features of RxJava.

Source code of the library can be found at: https://github.com/pwittchen/ReactiveBeacons.

Any new issues or pull requests are welcome!
Happy coding!

Introducing ReactiveSensors

2015-09-05 Android, Java, Open source, RxJava No comments

Another month, another library. Recently, I’ve released yet another reactive library called ReactiveSensors. It’s an open-source Android library monitoring hardware sensors with RxJava Observables. Library is compatible with RxJava 1.0.+ and RxAndroid 1.0.+ and uses them under the hood.

Library is available at: https://github.com/pwittchen/ReactiveSensors.

In my opinion, hardware sensors are perfect case for applying RxJava, because in fact we’re constantly receiving a stream of events emitted by many sensors. With Reactive Programming approach we have plenty of possibilities and easy API for manipulating received sensor’s data.

Usage of the library is really simple. You just need to subscribe an Observable with RxJava in the same way like in any other reactive library.

Code sample below demonstrates how to observe Gyroscope sensor:

Please note that we are filtering events occuring when sensors reading change with ReactiveSensorEvent.filterSensorChanged() method. There’s also event describing change of sensor’s accuracy, which can be filtered with ReactiveSensorEvent.filterAccuracyChanged() method. When we don’t apply any filter, we will be notified both about sensor readings and accuracy changes.

We can observe any hardware sensor in the same way. You can check list of all sensors in official Android documentation.

I’ve created section about Good Practices regarding working with hardware sensors on Android in README.md file in the GitHub repository.You should also read an article about Best Practices for Accessing and Using Sensors in official Android documentation.

Read more in the README.md file located in the repository of the library at: https://github.com/pwittchen/ReactiveSensors.

You can also find JavaDoc at: http://pwittchen.github.io/ReactiveSensors/.

If you want to use ReactiveSensors in your project, add the following dependency to your build.gradle file:

dependencies {
  compile 'com.github.pwittchen:reactivesensors:0.0.1'
}

Feel free to send me feedback, report an issue or fork the library!

Learning Android and being up to date

2015-08-22 Android, Java, Open source No comments

Recently a few people asked me, what are my methods for gathering knowledge and being up to date with all news connected with Android. I didn’t really think about that. I just grabbed all knowledge from a various places and then tried to use it in practice. I decided to sum everything up and create a collection of my knowledge resources. Of course, we are not able to use all of that every day, because it’s too much, but it’s good to know where we can find something interesting. Android is very dynamic mobile platform, which changes really fast. It’s hard to be up to date with all of new features and programming techniques. That’s why it’s worth to find places where we can gather interesting information. Below, I collected some resources, links and information, which I find useful for learning Android & Java and being up to date with Android development.

Podcasts

  • Fragmented Podcast – talks about building good software and becoming better Android developer – you can hear interviews with famous developers like Jake Wharton here

Videos

Collections of articles, issues & libraries

  • Android Weekly – free newsletter that helps us to stay cutting-edge with our Android Development
  • Android Arsenal – huge collection of categorized open-source Android libraries and tools
  • Slicedham.co – collection of engineering stories (not only about Android)

Blogs

Not all articles on the blogs below are connected with Android.

StackOverflow

We can browse interesting topics tagged with Android tag, read them and even try to solve some problems. It will allow us to learn something new or improve existing knowledge.

GitHub repositories

It’s worth to check open-source repositories of great companies and developers to see how smart people solve the problems.
Not all repositories of the companies and developers below are connected with Android, but most of them do.
Moreover, we can still learn a lot from various kinds of OS projects.

Companies

Organizations

Developers

We can also check trending Java repositories or trending repositories in general.

In addition, we can try to analyze source code of various repositories and try to contribute back to them. I guarantee, we’ll learn a lot. We can start with small steps and we don’t have to implement the most complicated features in the beginning.

Twitter

We can follow famous developers or companies. We can also check tags like: #AndroidDev, #Android or #Java and so on.

Experiments & open-source projects

We can create our own projects. We can publish our app on Google Play or create an open-source library on GitHub and let other people use it and review it. Then, we’ll be able to determine if our libraries are usable, code is clean, API is simple and documentation informative enough. In addition, other developers can report bugs or new issues, which will allow improve our projects and develop our programming and communication skills.

Other

Meetings, conferences, hackathons, etc.

Sometimes it’s good to go to a conference, meeting, etc. to talk with people and learn from them. We can take a look at the events around us, attend them or maybe present something. If there’s no events in our location, we can always organize one and ask others for help!

Books

It’s good to have strong basics. Not every book is universal and valuable over the time, but there’re a few fundamental positions for programmers worth reading like Clean Code, Effective Java, Java Concurrency in Practice & Test Driven Development: By Example. It’s an open list and we can always extend it with another position on our shelf.

What else?

Everyone has his or her own method of learning new things. If you know another interesting resources or methods, share them in comments below this article.

Introducing ReactiveNetwork

2015-08-10 Android, Open source, RxJava No comments

I’ve recently released ReactiveNetwork. It is an open-source Android library listening network connection state and change of the WiFi signal strength with RxJava Observables. It’s a successor of Network Events library rewritten with Reactive Programming approach.

Library is compatible with RxJava 1.0.+ and RxAndroid 1.0.+ and uses them under the hood. Min Android SDK version is 9.
JavaDoc can be found at: http://pwittchen.github.io/ReactiveNetwork.
Repository is available at: https://github.com/pwittchen/ReactiveNetwork.

This library is much simpler and easier to use than NetworkEvents. Even code-base is much smaller, but we have to remember that it utilizes powerful RxJava and RxAndroid. RxJava hase huge API and gives a lot of possibilities. That’s why I was able to obtain desired result with fewer lines of code.

Basic library usage is quite simple. E.g if we want to monitor ConnectivityStatus (WIFI_CONNECTED, MOBILE_CONNECTED or OFFLINE), we can create the following subscription, which is quite familiar for software developers who already know RxJava:

When we want to monitor available WiFi Access Points and we want to get fresh list of them whenever strength of the WiFi Access Points signal changes (e.g. when we are moving with a mobile device around), we can use the following code snippet:

If you want to use ReactiveNetwork in your project, add the following dependency to your build.gradle file:

dependencies {
  compile 'com.github.pwittchen:reactivenetwork:0.0.2'
}

Find more in the GitHub repository of the project at: https://github.com/pwittchen/ReactiveNetwork.

It’s worth mentioning that this library was featured on Android Arsenal, Android Weekly and Android Weekly China websites.

I hope you will find it useful and you will make your apps more reactive!
Feel free to fork the library. Any feedback is welcome as usual.

Searching word in a string with KMP algorithm

2015-07-22 Algorithms, Java No comments

Sometimes it’s good to revise some algorithms and try to implement them in order to get to know, how the built-in functions of high level programming languages actually works. One of the popular problems is string searching. We have many approaches to solve this problem. For example:

  • Naive string search algorithm
  • Rabin-Karp string search algorithm
  • Finite-state automaton based search
  • Knuth-Morris-Pratt algorithm
  • Boyer-Moore string search algorithm
  • Bitap algorithm

This time, I’ve decided to focus on Knuth-Morris-Pratt (KMP) algorithm. It’s quite easy to implement, when you understand it. In addition, its time complexity is quite good and can be defined as O(n).

In this algorithm, we simply go through all letters in a given string and compare them with searched string. When position of searched string reaches length of searched string, we can assume, that our string was found. If two compared letters are different, we set position of searched string to zero and start new search from the next position after which we started searching process before. It’s quite good described on Wikipedia with some pseudo-code. You can also take a look at my code in Java available below, which I’ve written just for practice.

Result of the execution of this program should be as follows:

String found at 15 position.

Please note, that we count position of the letter from zero like in the most cases in Computer Science.

Get past the syntax, the real scare is in the semantics

2015-07-06 Java, Software Development No comments

Dr. Venkat Subramaniam presented an interesting point of view during his talk at Devoxx conference. Programmers, who start learning a new language, often complain about syntax. They focus on using available keywords and constructions instead of trying to understand their meaning and purpose. Programmers also have their own habits. That’s why Java has similar syntax to C and C++. Creators of Java designed new language with syntax, which was familiar to them, with significant improvements. Perception of the world is based on experience and things we get used to. The same rule applies to programming languages. Most software developers are familiar with imperative programming paradigm. They often focus on describing computation in terms of statements and changing program state. This approach leads us to necessity of creating a lot of temporary variables and boilerplate code. Let’s have a look at the following code snippet:

This code prints sum of the square roots of the first ten even numbers starting from zero. It’s not so complicated task, but there is a lot of code, temporary variables, two loops, we have to be careful with operators to avoid ArrayIndexOutOfBoundsException and so on. Moreover, code is quite hard to analyze and we can make a mistake in many places. Let’s see what happens when we use functional programming approach, Java 8 with stream API and lambdas. We can do the same task in the following way:

As we can see, the code is simpler, cleaner and easier to analyze. The only difference is the fact that we changed approach from imperative to the functional one and applied different semantics. In this case, instead of focusing on how to do the task, we focused on the result we want to obtain. Instead of learning only syntax, we should spend more time on learning semantics to understand its purpose. This will allow us to create better and robust solutions in less time.

This article was also published as a part of summary of Devoxx 2015 Conference in Kraków, Poland on technical blog of Future Processing:
http://www.future-processing.pl/blog/devoxx-conference-summary/

Devoxx Poland 2015 – conference summary

2015-06-29 Conferences, Java No comments

This year, for the first time the Devoxx conference was held in Poland. There were plenty of lectures and experts to learn from.

On technical blog of Future Processing, you can find Devoxx Conference summary:
http://www.future-processing.pl/blog/devoxx-conference-summary/

In my opinion, one of the most interesting talks was prepared by Venkat Subrananiam and titled: “Get Past the Syntax, The Real Scare is in the Semantics”. That’s why I presented my thoughts connected with this presentation. Please, check out mentioned article to find more information about this and other topics.