Category Archives: Hacks

Setting up Java in Debian the right way

I’ve been using the OpenJDK, but I had some problems with it: Android Studio and PyCharm are tested with Oracle’s JDK, and as such there are known bugs and crashes with OpenJDK. To fix my instability issues I had to take the hint and switch to Oracle’s. Here’s the official doc for setting this up in Debian:

https://wiki.debian.org/JavaPackage

Following the explanations, I was able to convert Oracle’s tarball to a deb package using make-jpkg, and with the update-alternatives examples I could set the default version to use correctly. (Before this I was setting JAVA_HOME and PATH by hand, which is dirty, don’t do it.)

Overriding TMPDIR when you don’t have enough space on /tmp

Sometimes you might not have enough disk space on the /tmp directory but need it badly. For example when creating a deb package from a Java tarball in Debian using make-jpkg, there’s no command line parameter or any other apparent means to specify an alternative working directory for temporary files.

Check-mate? Luckily, no! I could force a different directory using the TMPDIR environment variable like this:

TMPDIR=/some/where make-jpkg  jdk-7u45-linux-x64.tar.gz

Disabling access to Facebook

Here’s a little tip for the not-so-tech-savvy, because it’s important.

Facebook tracks the websites you visit even if you don’t click on Like buttons. When a Like button is loaded, it executes code on facebook.com, sending your IP address, the website you are on, among other things. There is a way to switch this off effectively, by telling your computer the wrong address of facebook.com, so that your browser and any other processes will never find Facebook, and thus will never send any information to Facebook. You can do this by overriding the address of facebook.com in the so-called hosts file on your computer.

Press the windows key + r to run this command:

notepad %systemroot%\system32\drivers\etc\hosts

This opens the hosts file on your system in notepad, a plain text editor. Append this line to end of the file:

127.0.0.1 facebook.com www.facebook.com connect.facebook.net

Once this is done, Facebook, and its Like buttons will stop working, because your browser will not find Facebook. If you want to re-enable Facebook, edit the file again and either remove this line, or prefix it with a # sign to make it ignored.

Adding udev rules for USB debugging Android devices

USB debugging of my Google Nexus 4 phone used to work just fine from my Debian desktop. Until someday it just stopped working. The symptoms looked like this:

$ adb logcat
- waiting for device -
^C
$ adb devices
List of devices attached 
???????????? no permissions

That is, adb logcat was just waiting forever even though my phone was connected, and adb devices was giving permission errors. There was no easy answer on stackoverflow, but lots of clumsy ones, suggesting to make the adb binary setuid, or adding udev rules with 0666 permissions. I really had to get to the bottom of this myself.

These are the steps to properly debug, understand and resolve the issue.

1. Find the bus and device id assigned by the kernel:

$ lsusb 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 002: ID 045e:0053 Microsoft Corp. Optical Mouse
Bus 004 Device 003: ID 0566:3002 Monterey International Corp. 
Bus 001 Device 008: ID 18d1:d002 Google Inc.

I bought my device from Google, so it’s probably the last one.

2. Check the permissions of the device file

$ ls -l /dev/bus/usb/001/008
crw-rw-r-T 1 root root 189, 8 Nov 10 18:34 /dev/bus/usb/001/008

The path comes from the output of lsusb: 001 is the Bus, and 008 is the Device.

The problem is clear: the file is owned by user root and group root, which I am neither. The elegant solution is to add a udev rule so that the device will belong to a reasonable group, like plugdev, of which I’m a member.

3. Create a udev rules file, let’s say: /etc/udev/rules.d/51-android.rules

SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="d002", MODE="0660", 
GROUP="plugdev", SYMLINK+="android%n"

Here, idVendor and idProduct come from the output of lsusb: 18d1:d002. Do similarly for your own device. The rule specifies that a matching USB device should be created with permissions 0660, with group plugdev, and a symlink conveniently pointing to it.

Now that everything is ready, simply plug the device out and back in to confirm the result:

$ lsusb | grep oogle
Bus 001 Device 010: ID 18d1:d002 Google Inc. 
$ ls -l /dev/bus/usb/001/010
crw-rw-r-T 1 root plugdev 189, 9 Nov 10 18:37 /dev/bus/usb/001/010
$ ls -l /dev/android7
lrwxrwxrwx 1 root root 15 Nov 10 18:37 /dev/android7 -> bus/usb/001/010
$ adb devices
List of devices attached 
01d1e2510d9c6d28 device

PS: no need to restart udev, the changed rule should be picked up automatically.

Adding all unknown files in a Subversion checkout

Adding all unknown files in a working tree is usually very simple in other version control systems, for example:

git add .
bzr add

Not so simple in Subversion:

$ svn add .
svn: warning: '.' is already under version control

But if you add the --force flag, that will do!

Keep in mind that this is not the same as:

svn add * --force

That would add not only unknown files, but ignored files too, which is usually not you want. Make sure to specify directories explicitly, avoid using * with this command.

How to enable developer options on Android Nexus 4

I spent a couple of long minutes looking for the developer menu on my shiny new Nexus 4. I was looking for the options normally called USB debugging and Stay awake, normally under Applications / Development. I could not find them anywhere on this otherwise beautiful piece. Strangely, the menu is disabled by default. You can enable by following these steps:

  • Open your phone settings—swipe down on the phone, click on the button in the top-right corner, click Settings
  • Click About phone, it’s at the bottom
  • Click on Build number, 7 times (!!!), it’s at the bottom

What the hell… Who would have known? After this the Developer options menu becomes available, right next About phone.

I found the tip here:

http://www.redmondpie.com/enable-developer-options-in-android-4.2-on-nexus-4-and-galaxy-nexus/

 

 

Released Programming Quiz on Android

I just released the first version of Programming Quiz LITE.

It’s a simple multiple-choice quiz to test your programming skills. It aims to be interesting, fun and educational. You could even use it to prepare for programming job interviews, regardless of whether you are an interviewee or an interviewer.

Questions are spread out across multiple categories and difficulty levels:

  • Hobbyist level: 51 questions
  • Professional level: 116 questions
  • Expert level: 61 questions

Question categories are designed along the lines of the “programmer competency matrix” http://sijinjoseph.com/programmer-competency-matrix/ There are 228 questions in total for now, I plan to more soon, making it at least 100 on all levels. The questions are 100% original I create them together with my programmer friends.

You can “play” in two modes: normal mode and sudden death mode. In normal mode you can postpone answering a question and you can navigate back and forth in the questions. In sudden death mode you have to answer N questions in a row. You can customize the number of questions in both modes.

To report problems or feature requests, you can either email me directly or use this page: https://github.com/janosgyerik/manyquiz/issues

Project homepage: https://github.com/janosgyerik/manyquiz

This is the LITE version: it is FREE with NO ADS. There will be a paid version soon, which will be identical to this one, but it will have a price of 1 euro just to give you a way to say “thank you” and support my efforts if you want. If you like the app, please remember to rate it, +1 it, tweet it, review it, and spread the word. Thank you!

HabitRPG is helping me live healthier and balance my activities better

I started using HabitRPG last weekend. At first it was just fun, but after one week I’m actually seeing some positive effects already:

  • I take the stairs more
  • I do more exercises at home
  • I go to the gym more
  • I floss more
  • I go to bed earlier
  • I eat less snacks after dinner
  • I spend less time in front of the computer

I find that the following elements of gamification work the best on me:

  • “Dailies” are recurring daily tasks. You lose hit points of you fail to do them. You gain increasingly more bonus if you keep doing on consecutive days. These two features give an added incentive to keep doing your “dailies”. Examples:
    • Floss
    • Do 20 pushups
  • “Habits” are things you should be doing, or shouldn’t be doing. So habits can have a positive or negative meaning, respectively. You lose hit points if you do a negative habit, and you gain bonus if you do something positive. Colors indicate how much you’ve been doing or not doing something. In case of positive tasks green means you’ve done enough, in case of negative tasks red means you’ve done too much. These features give incentive to keep doing positive habits and avoid negative habits. Examples:
    • Positive: Take the stairs instead of elevator
    • Positive: Go to the gym
    • Negative: Awake at midnight
    • Negative: Snack after dinner

Sounds interesting? Check out the tutorial to see how it works. This is a screenshot from my page just to give you an idea:

habitrpg1

A surprising effect of the dailies was that now I spend much less time on answering questions on Stack Overflow compared to before. Recently it’s been kind of an addiction/obsession for me to collect reputation points on stackoverflow.com. I was aware that I was spending more time than I should, but it was hard to resist. There, a simple daily task on HabitRPG seems to have cured that addiction. The task says: “Answer 1 question on Stack Overflow”. As a result, after I already answered one question on a given day, I have no more incentive to do more. In fact that would risk missing my other dailies.

In other words, if you think you are doing X too much, add a daily that says: “Do X for 1 hour”. After you’ve done that a day, you will get no extra benefit from it. The incentive to do more will be taken away. At least in terms of HabitRPG.

Sure, this might not work for everyone. In any case here are some extra tips for beginners:

  • The site is extremely buggy at the moment. Be careful. Maybe don’t gamify your entire life just yet. Start slow, and see as it goes, discovering the bugs and learning how to deal with them.
  • Don’t bother creating your own Rewards. From level 2 you can see the many built-in rewards, which are too good and hard enough to earn.
  • Don’t bother about the tags and advanced options. If you want to avoid hitting bugs, try to use the site the simplest possible way.
  • Don’t bother entering Todos. Install the Chrome Extension and link it to your other todo applications, such as Google Tasks, Asana or Workflowy.
  • Don’t add more Dailies than you can actually maintain. Start slow, add more later.
  • Prefer to add the small tasks that you always want to do but somehow you just never do… In my case these were things like flossing, pushups, sit-ups, taking stairs, etc. Things that don’t really take time, but somehow normally I just really don’t do them.
  • Whatever you do, do NOT become a HabitRPG addict! That would defeat the purpose!

Oh and by the way it’s open-source, here’s the main repository:

https://github.com/lefnire/habitrpg

Released jQuery Upvote plugin

Published my first ever jQuery plugin just now: jQuery Upvote

http://plugins.jquery.com/upvote/

The plugin simply generates a voting widget like the one used on Stack Exchange sites:

  • You can either upvote or downvote, not both at the same time
  • You can cancel an upvote or downvote by clicking again
  • The count is update accordingly
  • You can star (= favorite) and unstar
  • A callback method is fired on any update (upvote, downvote, star, and their reverse)

See it in action: http://janosgyerik.github.io/jquery-upvote/

Documentation: https://github.com/janosgyerik/jquery-upvote/blob/master/README.md

I’ve been needing this BADLY to add voting features on http://www.bashoneliners.com/. Hopefully that’s coming soon too 😉

Learning history expansion tricks in bash

Have you ever read the full man bash? Me: nah-uh… It’s way too much! Deep deep deep stuff.

In any tool, I tend to focus on a small set of tricks with very high practical value. For example these bash features:

  • Backward search command history with C-r
  • Delete words backwards with C-w or forward with ESC-d and paste them later with C-y
  • Jump around on the command line with C-a, C-e, ESC-b, ESC-f, or actually use C-w as a form of jumping back fast, or C-c to cancel and re-type

And so on. I use these in literally every single minute I spend in the shell, so they were really well worth memorizing! (See my slides on these and similar time-saving tricks on SpeakerDeck.)

I rarely memorize new tricks. There’s a sweet spot on the effort-benefit curve, beyond which the benefits are not that great. But from time to time I discover something new that might be worth learning and adding to my arsenal. Right now some history expansion tricks look pretty damn handy.

The event designator !!

!! is a type of so-called event designator: it refers to the previous command. I use this to save complex commands for later use, for example this operation on an Apache log file (get the number of requests per user agent):

cat access.log | cut -d\" -f6 | sort | uniq -c | sort -n
echo "!!" >> later.sh

This is not a perfect solution in this case, because the backslash there will disappear in the output, and in general it won’t work if there are unescaped double quotes in the command. However, there are many use cases when it can simplify my typing.

The word designators !$ and !^

As the man page says: Word designators are used to select desired words from the event. These two are expanded to the last argument and the first argument, respectively. They are relatively easy to remember if you consider the meaning of $ and ^ in regular expressions.

A typical example situation:

less /path/to/some/file
rm /path/to/some/file

Doing this and similar actions involves a bit too many key strokes: up + C-a + ESC-d + [type “rm”]. Easier to do like this:

less /path/to/some/file
rm !$

A similar feature is ESC-. which pastes the last argument while editing, so you can make changes before running the next command.

You can read more about these and similar tricks in man bash, under “HISTORY EXPANSION”.