Saturday, February 18, 2017

Copying to Clipboard in Mac OS X Terminal

In case you want to copy the entire content of a file to clipboard, here is what you can do conveniently.

$ cat some_file.txt | pbcopy

This command will copy the standard output to the clipboard. Since the cat command pipes the file's text to standard output, pbcopy will copy it into clipboard.

That's it!

Tuesday, February 14, 2017

Fundamental Git Concepts and Useful Git Commands

Summary of fundamental git concepts:

- repository: a collection of all commits and logs
- commit: a snapshot of the files at the time it was created
- branch: label for a commit and its subsequent commits
- master: the main branch
- HEAD: current commit
- origin: remote repository
- staging area: intermediate step before creating a commit


Summary of most frequently used commands in git:

# show current status
$ git status

# show commits in chronological order [with statistics]
$ git log [--stat]

# show branching graph between two branches [represented in single lines]
$ git log --graph [--oneline] branch1 branch2

# show difference between two commits
$ git diff commit1 commit2

# show difference between the specified commit and its direct parent
$ git show commit_hash

# load specified commit
$ git checkout commit_hash

# load specified branch
$ git checkout branch_name

# save current work as a new branch
$ git checkout -b new_branch_name

# show [hidden] branches
$ git branch [-a]

# create a new branch
$ git branch new_branch_name

# add branch2 into branch1; i.e., branch1 will incorporate branch2
$ git merge branch1 branch2

# add files to staging area
$ git add .

# remove files from staging area
$ git reset

How to Push an Existing Repository to Git Server

Say you have a Github account or a local git server where you want to publish your work either publicly or privately as a bare repository. This post will explain how to create a bare repository on Github or git server. Refer here for official Github documentation.

First off, you will need to create a bare repository. If you are using Github, simply create a new repository. Note that you must NOT add README or, .gitignore files.

If you are using a local git server, you will need to run the following as a local user in the server:
$ git init --bare test_repo.git 

Note that by convention bare repositories end with .git to differentiate with working repositories. Bare repositories refer to those that are on Github or a local git server where users only upload / download and not directly work with. In contrast, a working repository is what you would have on your desktop, constantly editing and saving files locally.

Next, open up terminal and change directory to your project directory. If you haven't initialized it as a working git repository, then do so.
$ git init
$ git add .
$ git commit -m "initial commit"

Now, add your remote repository
$ git remote add origin GIT_REMOTE_REPOSITORY

where GIT_REMOTE_REPOSITORY will be your remote git repository in https or ssh.

Finally, push your current repository to the remote repository:
$ git push -u origin master

Now, the two repositories should be in sync!

Sunday, February 12, 2017

Java Threading: Synchronization II

In the previous post, we looked at how synchronized methods can be used to ensure safe access to a single object, where multiple threads try to read/write to it.

Here, we improve upon the example. In the last post, I mentioned that there were still fixes to be made, and today we will discuss one of them.

Below is the copy from the previous lesson. Take a closer look at the run() method of removeThread, line 54-57 below:


You will notice that the thread continuously checks whether there is at least 1 or more elements in the queue, and when there is, it calls dequeue() method. The question is: can we guarantee that element in the queue will remain in between the two calls? That is, what if another thread calls dequeue() in between, intercepting the element first? This is certainly possible...

Solution? I'd say we fix this issue by having dequeue() method check for the number of elements in the queue. Since dequeue() method is synchronized, it is guaranteed that if there is an element, it will be able to retrieve it for sure. What if there is no element in the queue? It will return immediately with an exception stating that there is no element to retrieve. Thus, we need to modify removeThread run() method to loop dequeue() method continuously until it does not throw an exception. Take a look at code below for implementation.


The modified code above certainly is better than the previous version, but we are not done yet. There are still a few more fixes to be made, and we will tackle one by one in the subsequent posts!

Saturday, February 11, 2017

Android Threading: Handler Example

In this post, we will go over a very simple Handler example for Android development. Hopefully this post will explain and demonstrate why and how to use Handler class in Android.

Consider a very simple app, where you have two buttons: task button and increment button.

Upon click, the task button will notify the user that the task is now running, and carry out some heavy task, which may take up to seconds. When complete, it will notify the user that the task is finally done.

Upon click, the increment button will simply increment counter and display its current value.

Let's take a look at a crude attempt to implement this using a simple Thread. Here is the layout file.

Here is the implementation activity file.


Here, upon the task button click, it launches the task, which is simulated by sleep(3000), on a separate thread so that we don't slow down the main UI thread. However, the problem is that we must wait for this task thread to complete because we need to update the button's text upon completion. Thus, we use join() method to wait for the task to complete. With this implementation, we find that the UI thread becomes unresponsive while waiting. This is NOT a good way of implementing the task.

The core problem is that we ask the task thread to carry out some heavy task, and we need to make sure that the main UI thread does not just sit and wait, but rather do its jobs on its own. The task thread then must communicate with the UI thread and let it know the task is complete, at which point, the UI thread can update the UI accordingly.

The Handler class in Android achieves exactly this, and below is modification that makes use of the Hander class.


As you can see, the task thread will do its job, and when the task is complete, it notifies the main UI thread that the job is done through handler. The handler is created in the main activity, so it is bound to the main UI thread. When the handler receives a message from the task thread that the job is complete, it will then update the button's text. In the meantime, the main UI task carries out its own tasks, such as incrementing the counter when the increment button is pressed.

Of course, one can implement this without using the Handler class, shown below. However, this solution is possible because we are communicating with the UI thread in this example. If, however, you have two different task threads that must communicate with each other, then you need to use the Handler.

Tuesday, February 7, 2017

How to Load Java OpenCV Library to Android Studio

In this tutorial, I will go through a step by step method to load Java OpenCV Library to Android Studio.

First, download OpenCV for Android from here. Extract the zip file, and you should see OpenCV-android-sdk folder.

Next, in Android Studio, open up a project where you want to integrate OpenCV Java library. Then, click File - New - Import Module and select OpenCV-android-sdk/sdk/java folder. Android Studio will ask about import option, and just accept the default.

You should see OpenCVLibrary module in your Android project. Select its build.gradle file and set appropriate versions for compileSdkVersion, buildToolsVersion, etc.

Now, you need to add dependency. Open up your app's build.gradle file and add
compile project(':openCVLibrary310')
into dependencies { ... } section.

Next, you will need to make sure that your app loads in the OpenCV library. Go to the activity class file that first uses OpenCV Library, and add static statement, similar to below:

...
import org.opencv.android.OpenCVLoader;

public class MainActivity extends Activity {
    final static String TAG = "Main Activity";

    static {
        if(!OpenCVLoader.initDebug()){
            Log.d(TAG, "OpenCV not loaded");
        } else {
            Log.d(TAG, "OpenCV loaded");
        }
    }
...

Finally, you will need to copy native binary files. Copy OpenCV-android-sdk/sdk/native/libs folder as src/main/jniLibs folder in your app's directory.

That's it! You should now be able to use OpenCV functions in your app!