Open source

ReactiveNetwork – new releases & roadmap

2016-06-10 Android, Java, Open source, RxJava No comments

Recent updates

I’ve recently released version 0.3.0 of ReactiveNetwork library for Android. As you can see in the release notes, it contains a lot of updates.
Highlights:

  • Deprecated methods related to monitoring WiFi Access Points and WiFi Singal level in favor of ReactiveWiFi project, which has this functionality extracted from ReactiveNetwork
  • Deprecated methods and enums related to monitoring connectivity with The Internet
  • Added Observables, which allows monitoring Internet connectivity basing on socket connection with a remote host (we can also monitor specific host with given parameters)

Roadmap for the future versions

Updates planned for 0.4.0:

  • Removing deprecated methods
  • Removing unused permissions from AndroidManifest.xml

Track progress of releasing 0.4.0 on GitHub

Updates planned for 0.5.0:

  • Updating library code with respect to the updates in Android 5 and 6 (especially ConnectivityManager) related to monitoring network connectivity mentioned in issue #62 on GitHub
  • Creating strategy interface to keep backward compatibility with Pre-Lollipop devices, so we’ll be able to monitor network in a different way depending on the given Android version

Track progress of releasing 0.5.0 on GitHub

Another future updates (not related to any version):
These updates are also important, but they’re not related to library API

  • Adding example of library usage with Retrofit (without breaking reactive stream chain)

Track progress of resolving this issue on GitHub

Final thoughts

Currently, it’s my most popular open-source project and people rely on it in production apps, so I’m trying to keep it as simple and as good as I can with respect to the recent updates of Android SDK. I’m getting really good feedback from people on GitHub and seriously considering it during the development process. If something bothers you in that project, don’t hesitate to create an Issue or new Pull Request on GitHub.

Handle swipe events in reactive style

2016-03-11 Android, Java, Open source, RxJava No comments

Introduction

Initial Swipe Detector project was created on 24th of December 2014 on GitHub, so more than one year ago. I also wrote an article about that on my blog. I needed that to detect moment when user is swiping horizontally, to block vertical scroll on the ListView while deleting item with “swipe to delete” functionality. This gives better UX and apps from Google works in the same way. Mentioned project was just a simple proof of concept showing how can we detect whether user is swiping on the screen of mobile device and in which direction. Recently, I thought that it would be cool if we had a separate generic library to do the same thing. It would be even cooler if this library will provide RxJava Observable to handle such events in a reactive way. That’s why I’ve decided to take this project, extract generic code to library and write an Observable wrapping listener, which handles swipe events. Please, take a look at the outcome of that work.

swipe

Preparation

New library is called swipe. If we want to use it, we need to add proper dependencies to our build.gradle file:

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

Next, we need to create Swipe attribute in our Activity:

private Swipe swipe;

We also need to remember to call dispatchTouchEvent(MotionEvent) method:

We need to perform things described above both for listener and RxJava Observable.

Imperative way

If we are not familiar with RxJava, we can handle swipe events in an imperative way with listener:

Reactive way

If we want to use the power of RxJava and code our app in a reactive style, we need to add Subscription attribute.

private Subscription subscription;

Now, we can subscribe an Observable:

Please note, there’s much less code than in the case of listener.

We need to remember to unsubscribe our Observable, when it’s no longer needed:

That’s it! I hope it will help you to detect and handle swipe events in your apps.

Source code of the library is available on GitHub: https://github.com/pwittchen/swipe.

Infinite scroll for RecyclerView in Android

2016-02-28 Android, Java, Open source, UI 5 comments

Introduction

Three years ago I wrote short article about Endless ListView in Android. This solution was dedicated to ListView, wasn’t perfect and generic. Moreover, lot of people asked questions about this solution and they weren’t sure how to use it properly. I also had some problems with implementing endless scroll AKA infinite scroll in Android apps. In addition, in the newest Android SDK it’s recommended to use RecyclerView instead of ListView. For a long time I couldn’t find the right implementation of the infinite scroll AKA endless scroll for Android. A few solutions I’ve found weren’t production ready, weren’t working correctly or had too many features. I wanted to have small, easy and flexible solution to implement infinite scroll for Android, which works with RecyclerView from the newest Android API. That’s why I created new project called InfiniteScroll, which helps to implement infinite scroll on Android. Library is tested and its behavior is documented by appropriate unit tests. If you would like to see it in action, check out gif animation presenting how sample app works. Library is very tiny, but does its job.

How to use it?

First of all, we need to add library dependency to our build.gradle file:

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

We also need to create necessary fields in our Activity:

public RecyclerView recyclerView;
private LinearLayoutManager layoutManager;

Next, we need to create new InfiniteScrollListener:

Then, we need to initialize RecyclerView and LinearLayoutManager in our Activity and add previously created listener to RecyclerView:

If we want to display loading progress, we should add additional view for it, show it while loading starts and hide it when loading is finished.

That’s it!

Please note: We can also implement “Load more” button with that library. Instead of loading items immediately, we can display such button when user scrolled to the end of the list.

Exemplary apps

You can see how, library can be implemented in the following places:

  • Sample app from main repository, which loads Strings into RecyclerView
  • Search Twitter app, which allows to search tweets and scroll them infinitely

Summary

I think, this time infinite scroll is implemented in the right and re-usable way. I hope, it will make your life easier while developing your apps.

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

ReactiveNetwork – release of v. 0.2.0

2016-02-11 Android, Java, Open source, RxJava No comments

I’ve recently released new version of ReactiveNetwork library for Android.

Here are the fresh release notes for version 0.2.0:

  • added possibility to observe WiFi signal level with observeWifiSignalLevel(context, numLevels) and observeWifiSignalLevel(context) method
  • created WifiSignalLevel enum
  • added internet check to parameters of getConnectivityStatus(context, checkInternet) method
  • made getConnectivityStatus(context, checkInternet) method public
  • changed String variable status in ConnectivityStatus enum to description and made it public
  • changed output of the toString() method in ConnectivityStatus to keep consistency with another enum
  • made ReactiveNetwork class non-final
  • bumped Kotlin version in sample app to 1.0.0-rc-1036
  • increased immutability of code of the library
  • updated sample apps and documentation

Thanks to @llp and his Pull Request, we are able to observe WiFi signal level AKA RSSI now! It’s one of the most interesting features in the newest release.

We can do it as follows:

or we can observe an enum value instead of integer:

WifiSignalLevel enum can have the following values:

public enum WifiSignalLevel {
  NO_SIGNAL(0, "no signal"),
  POOR(1, "poor"),
  FAIR(2, "fair"),
  GOOD(3, "good"),
  EXCELLENT(4, "excellent");
  ...
}

Any feedback will be appreciated!

Happy coding!

3 questions about your Git repository

2015-12-28 Git, Linux, Open source, Python 3 comments

Introduction

Can you answer the following questions about your Git repository?

  1. Does development branch has all changes from master branch?
  2. Is your gitlog a crap?
  3. How old are your branches?

If not, but you want to know answers, you’re lucky, because I prepared 3 simple scripts for you, which can help to find it out.

Does development branch has all changes from master branch?

git-branch-comparator is a python script, which checks if development branch has all changes from master branch in Git repository.

Another, easier way to accomplish the same task suggested in comments by Mike (thanks!) is to call simply:

$ git pull
$ git branch --contains master --no-merged development

When we are working in a Git Flow and critical bug occurs on production, sometimes there is a necessity to create so called hot-fix. We can create separate branch from master branch for this hot-fix and then merge it into master branch or we can commit a change on master branch. Second option is not recommended. After that, we have to remember to merge master branch into a development branch to have our hot-fix in a development version as well and avoid merge conflicts in the future.

This python script checks, if all changes made on master branch were also merged into development branch to keep those two branches consistent. We can add it as a job into Jenkins CI server and monitor branches consistency. In addition, release jobs can depend on that job and we can avoid merge conflicts or project unstability before release.

source code & documentation: https://github.com/pwittchen/git-branch-comparator

Is your gitlog a crap?

craplog is a python script, which checks if git git log of the given project is crappy or not. Right now, script is very simple. It just checks if more than half of the commit messages are good. Commit message is considered as good, when it contains more than two words. Of course, this is not the only condition determining the quality of the commit message, but this is early beta version of the script and can be improved later.

I’ve read a discussion in one of the pull requests to Linux kernel. It made me think about quality of Git commit messages. Of course, Linux kernel is a specific project and has its own standards. Maybe not all of these standards will be valid for a simpler or less complicated projects. Nevertheless, a lot of people don’t pay attention to git commit messages. They put crappy stuff inside them like random letters and numbers or stupid expressions, which has no specific meaning, aren’t related to the project or aren’t informative enough. In my opinion, good git log is one of the factors determining good quality of the project. Sometimes, we need to browse log to find some changes or analyze project history in order to fix a bug or find important information. It’s easier to do it, when git log is good. I’ve made some of the mentioned mistakes in the past, but I try to avoid them now.

source code & documentation: https://github.com/pwittchen/craplog

How old are your branches?

git-aged-branches is a shell script showing git branches of defined repository with age of their last commit. It works on Mac OS X, Linux and can be helpful while investigating old Git branches to delete. This script does not delete anything! It’s just for informational purposes.

source code & documentation: https://github.com/pwittchen/git-aged-branches

Summary

I hope, some of you will find these tools useful and maybe they’ll solve your current problems or help to improve quality of your projects.
If you would like to know more details about mentioned projects, check instructions how to use them and their source code, visit linked repositories on GitHub.

Note: Any feedback, new issues or pull requests are appreciated!

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!

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.