Category Archives: Programming

Reflection on the development of Animol – an Android app for Chemistry

The summer, I took a job offer from a lecturer that taught me in the last semester. The task was to develop Animol – a “simple” Android app for a lecturer from Chemistry department. The main functions proposed were displaying videos and short info for different experiments, synchronising experiment data with the server, on which new experiments can be added. I was in charge of developing the Android app, there are two more students working on iOS and server respectively.

The app was release under NUS account, but is no longer visible in Play Store for some reason. There is a brief description about it on NUS website.

The initial UI proposed looked very simple, so I did not expecting to spend too much time on it. After all, accepting the offer would mean that I will be working on 2 programming projects this summer, the other one being the orbital. I thought that my experience in Android game development would make this an easy job. As it turned out, making a “normal” Android app is somehow harder than making a simple Android game.

I started working on the project in late May. To set up the basic structure of the app, I need to learn about ListView and ListAdapter, to be able to populate data into a list. This did not take very long. After reading a few tutorials, I managed to find a good template that includes a ListView and a ListAdapter for displaying a list of songs. After removing some extra items, the basic list layout was done. Along with the ListView I also learnt simple SQLite functions to insert and retrieve data from Android’s SQLiteDatabase.

From there I got the basic framework and started working on specific requirements of the project. The next big thing is the embedded YouTube video. This was relatively easy for me(as compared to my counterpart working on iOS version of the app) as Google provides a YouTube Android Player API. (It was only later that I found out the downsides of the API.)

As I expected, the part that took the most time is the layout/UI. I had one YouTubePlayerView, several ImageViews and  scrollable TextViews and the whole layout also needs to be scrollable. So I did some research and followed the recommended way: Wrap all the ImageViews and scrollable TextViews in a LinearLayout which is then wrapped in a ScrollView. I also need to add some codes in the Activity to prevent scrolling conflicts between the inner views and the ScrollView wrapper.

After several modifications according to the requirements, my UI for the app was accepted in the weekly meeting. Then we all started working on server communication, I had a few choices here. I can either use the DefaultHttpClient (inherited from HttpClient) from Apache for Android, or the AndroidHttpClient (deprecated). Since there were several templates for DefaultHttpClient, I just followed the templates and wrote a ServiceHandler Class(just a utility Class, not inherited from Service or Handler Class) that handles the http calls. Then, I can use AsyncTasks in my activities to call methods in ServiceHandler for server communications.

It was on 1 July that things that things got a bit messy. I got my new MacBook Air and decided to shift all programming related work there. It just so happened that Android Studio had update moments ago. So it was a cross-platform and cross-version migration of an Android Studio project. This caused me several problems:

First, the Gradle version needed to be updated for the new Android Studio version, however my project was already set up for the old Gradle version and there was no way to do a straight forward upgrade of my project. So I decided to create a fresh project and copy paste my old codes into it, saving time to research on solving the compatibility issues.

The other problem was that the new build versions (version 19 and 20) does not seem to have good support for the package com.android.support:appcompat-v7. So I decided to strip the support package and made my app only compatible with devices running SDK version 15 (Android 4.0.1) or above.

The last and the most annoying one is the rendering of the ActionBarView in Android Studio. For some strange reasons, after I migrated the project, all the views stopped showing previews in Android Studio, saying that there are some problems with the ActionBarView. It worked fine on phones but lack of preview made it extremely difficult for me to design the layout as required. So I raised the issue on stack overflow. It seemed that removing the appcompat package helped to some extent but the new views created still has errors in rendering. Hence, this problem is still not solved.

Having solved most issues with the migration, I then started on more sophisticated operations involving retrieving and processing data from server and updating local database accordingly, as well as sending app usage statistics(click counts for each experiments) back to the server. There was an interesting problem regarding the timing for server communication. Initially, my implementation was to fetch new experiments and send the counts when the user opens the app (via onCreate in the splash screen activity). However, the concern was that the Android user may not have the habit to close the apps and server communication won’t happen unless the user explicitly kills the app. Then I planned to change the implementation to sending the count  and fetching new data when the app is sent to the background, but the disappointing news is that this is very hard for Android to detect sending app to background unless I use some hacks(which may not work in a long term).

So to ensure that the count is sent to the server, I created a BaseActivity that extends Activity, in which onPause will trigger AsyncTask to send count and onResume will trigger AsyncTask to fetch new data. This implementation suited the purpose, but it cost too much network traffic. This is because when user navigates from activity A to activity B, onPause in A and onResume in B are both called, effectively meaning 2 http calls will be executed for each tap on the screen.

Hence, the final decision was to add an interval of 30 seconds between any two server communications. This means after the first server communication, a timestamp is saved to the SharedPreferencesSubsequent server communications will need to go trough a check on timestamp to see if 30 seconds has passed from the previous server communication. This way, we can be sure that when the user launches the app (or relaunch from background), new data will be fetched. And for each subsequent navigation, onPause will be called before onResume, ensuring that the count will be sent in the next server communication.

The next big thing is push notification. This function was not in the original plan, but we all agreed that this should be an important part of the app in the event that we want to notify the users of new experiments or announcements. As it turned out, Google was kind enough to provide a Google Cloud Messaging(GCM) service for Android. The implementation was quite straight forward, except for the fact that it requires Google Play Services, which could be a potential problem for phones purchased in other countries that do not have Google services pre-installed.

The last problem was to dynamically refresh the view when the database gets updated by background server communications. I did not researched into how I can get a result from AsyncTask in my activity and the Android app is structured such that the views are drawn in onCreate of the activity, via setContentView(R.layout.activity_main); .  So in order to refresh the view with updated data, I need to either redraw the whole view or detect the changes to database and update the corresponding view elements. And I can either do it in onResume or set up a listener for database activities. In the end, I took the easy way out and forced a re-draw of the views in onResume (with 2 seconds delay) for each successful fetching of new data in the background.

Overall, I would say that this an awesome experience for me. I learnt a lot of things, not only on Android development but also on server communication techniques involving http request/response and database operations with SQLite. I am looking forward to working on my next Android project, or even pick up an iOS one if I have the necessary resources.

[Solved]Get back Grub in Windows 8.1

I was using grub to dual boot my Lenovo Y410P with Windows 8 and Ubuntu 14.04.

Recently I finally decided to upgrade my Windows 8 to Windows 8.1 considering it should be pretty stable by now. However, to my horror(or rather not surprisingly), my default bootloader has been changed to Windows Bootloader which means I could not access my Ubuntu 14.04.

Usually this takes a while to fix, involving repairing grub/BCD and tend to be tedious. However, I managed to found a much simpler and direct solution to this.

With the new UEFI specification, we can actually choose the default boot manager instead of replacing one with another. All it takes is to go to UEFI Firmware settings and change the order of boot managers. Which means grub and Windows boot manager can co-exist somewhere in the hard disk and we can choose which one to use in the BIOS UEFI settings.

Accessing UEFI settings in Windows 8.1 is a bit different from what happens in Windows 8, you can follow these links:

http://www.thewindowsclub.com/access-uefi-firmware-settings-windows-8-1

http://acer-au.custhelp.com/app/answers/detail/a_id/32048/~/accessing-uefi-in-windows-8.1

The menu is something like this:

UEFI Settings

UEFI Settings

One you are in the UEFI boot priority settings, you can simply change the position of Ubuntu to the top so that grub will load when the PC is turned on:

UEFI Boot order priority setting

UEFI Boot order priority setting

(All photos taken by me)

 

 

Programming concepts in real life – Algorithm

This is the second post on the series of interesting thoughts of “programming concepts in real-life”. Today it occurred to me that one fundamental concept of programming – algorithm, is also heavily used in real life outside the IT world.

What is algorithm

Algorithm is a step-by-step procedure for calculations.

Algorithm used in programming

There are a lot of examples of algorithms in programming, the most basic ones include  breadth-first search (BFS) and  depth-first search (DFS).

Algorithm in real life

Algorithms are applied in our everyday life.

DFS/BFS (Depth-first-search/Breadth-first-search)

For example, for a freshman who just entered NUS, he may choose to study the area of his interest, such as marketing or psychology. He can do this in two ways, one is to really devote all the time and effort into this area and do nothing else for a very long time. So for marketing, he/she will be learning introductory modules on marketing, intermediate modules on marketing, advanced modules on marketing or even do a research on marketing. Only after he/she feels there is nothing else to do on marketing, then the person will consider other options. And surprisingly(or rather not surprisingly), this is exactly the concept of a particular algorithm called DFS. In DFS, you prioritize depth more than breadth, so when you have a chance to go wider or deeper, you will go deeper first.

And of course you can see how the opposite algorithm BFS works for real life. That is when a person takes all the introductory modules for different areas, however after all that still indecisive enough to make a decision for specialization. So he/she would take all the intermediate level modules for different areas and so on. This may work very badly for a freshman but the concept is similar to a liberal arts curriculum, where students are exposed to different disciplines for 1 or 2 years before they decide their major.

Amortized complexity optimization – CS3230 lecture on 13 Feb 2015

We use amortized complexity optimization in real life, for cleaning our rooms. Normally we do not clean our rooms every single day. We wait for the room to fall below a certain cleanliness standard or when there is an important visitor, then we start cleaning the room. Cleaning the room is an expensive operation which takes a lot of time. Hence, by doing it infrequently, only when absolutely necessary, we reduce the total amount of work needed to keep our room clean.

This idea is similar to amortized complexity optimization in algorithm analysis, where we optimize the average runtime to achieve better run time.

 

Other algorithms such as greedy algorithm and dynamic programming are also practised in real life(often without being realized).

 


More of this

The best Friday Hacks session so far

Today I attended the Friday Hacks(#68) by NUS Hackers as usual. I knew that the topic today is on NUSMods, a web app developed by a SoC senior – Beng, that is extremely popular and successful in NUS, and there will be a guest Jeff Moss who is a famous security expert. It turned out both talks were interesting and meaningful.

For the first part, Beng mainly talked about the history and design philosophy of the NUSMods. It was an inspiring story considering that he did this in year 2 and the web app went through a lot of changes before it finally become successful and popular among NUS students. In particular, the part on designing the app is interesting, he talked about “hackers who can design the app well enough is as rare as unicorn.” And I found this to be relevant. Sometimes the app may have the best functionality but the poor design may not attract the users or simply render the app unusable. Luckily, there are some books on design philosophies that are useful in designing apps. I figure that I should take a look at them when I have free time and before doing some serious web project. He also mentioned about the future of NUSMods which includes some APIs for new developers to use the data from NUSMods as well as using NUSMods’ existing popularity to transform it into a platform for displaying other apps relevant to NUS(taking the interpretation of NUSMods as NUS Modifications). These ideas may be relevant for me when I do my future projects.

The second part of the session was a short Q&A session for Jeff Moss and some of his colleagues. A lot of insightful questions ranging from personal security, NSA’s surveillance, issue of trust and prospect of computer security were asked and the reply given by the pros were very informative and insightful. We learnt about how strange phenomenons may be related to NSA’s surveillance programmes, the prospect of post Edward Snowden era for computer security. Some even drilled into the fundamental issue of the extent of trust we should give to others. A good point is that in IT industry, we have to ultimately give our trust to some strangers. It is not possible for us to build a functional computer, or in similar sense a full range of software. Hence, whatever we do, we need to trust the other people who developed the building blocks for the technology that we are using, be it the operating system or the processor.

Programming concepts in real life – Interface

I did not know about the concept of interface until I took the module CS2020 where we use Java to learn data structures and algorithms. In the first few lectures, I was introduced to the idea of interface. I found it extremely intuitive and useful.

What is interface

In simple terms, an interface in programming is set of functions, but only names and descriptions of the functions, not the actual codes. You can write a program to implement(support) these instructions in some ways. Later, you can tell other programs that your program implements this interface and supports these instructions. Then, other programs will be able to directly execute the instructions without knowing how they are implemented. Interfaces can be extended so that the extended interfaces specifies additional functions.

Interface used in programming

One term that people often hear in tech news is API, which just is Application programming interface. With APIs, we can use functions that other applications implements in our own applications. The concept of interface is also important for the communication between hardware and the software. There is a communication layer between hardware and software called instruction set architecture (ISA), which specifies the operations that the processor should support so that the compliers(software) can use them. Then the actual implementation of the functions is done by the producer of the processor.

Interface in everyday life

However, I realised that the concept of interface is not limited to the field of programming. It is actually ubiquitous in our everyday real life as well. Think about the banks, they are essentially different implementations of the same interface – bank, which specifies 2 basic operations for customers: deposit and withdraw. Different banks would then go on and implement these two functions in different ways, then add some more functions that customers can perform using banks. This may lead to the problem of different banks having different functions and they are not compatible with each other. For example, cheques implemented in bank A may not be the same format as bank B. Hence, we still the concept of interface and by extending the interface to support more functions, different banks can communicate with each other.

More examples of interface in everyday life

Other examples of interface? You can just list almost everything.

A car? Yes, it is an interface that specifies functions like move, board, exit and etc.

A TV? Yes, it is an interface that specifies functions like display channel, switch channels, switch on, switch off, adjust volume and etc.

A house? Yes, it is an interface that specifies functions like live in, demolish, get address and etc.

People may think that, “Oh, actually this concept of interface has always been there before the invention of computers. What is the big deal?” That is precisely the big deal that I want to point out. Programming helps people to analyse and formulate things systematically. Although the concept has been always around, it was not formally identified and given a proper name so that it can be studied. Different sectors and fields do have their own regulating bodies but the more general concepts similar to interface are not explicitly there(or not conclusive to encompass all sectors). With programming terminologies, we are able to identify patterns or practices that are common across different fields and sectors and give it a generic name. I believe there are other programming concepts(perhaps object) with this ability, I just need to learn more and find out more.


More of this