April 20, 2017

Updating hosts file on Android Emulator

To update the hosts file on an Android Emulator, follow these steps:

  1. Start a command prompt window and switch to Android SDK's tools folder:
    C:\>cd %LOCALAPPDATA%\Android\sdk\tools
    
  2. Use the emulator tool to find the name of the AVD you want to start. (Note that in AVD Manager GUI tool, the names listed here are the AVD IDs, basically spaces replaced with underlines.)
    C:\Users\user\AppData\Local\Android\sdk\tools>emulator -list-avds
    Pixel_API_25
    
  3. Start the emulator with writable system image option turned on:
    C:\Users\user\AppData\Local\Android\sdk\tools>emulator -avd Pixel_API_25 -writable-system
    

    Wait for the emulator to boot up. The documentation says -writable-system option creates a temporary copy of the system image that will be destroyed when the emulator exists, but the hosts file change seems to survive shutdowns.

  4. Start another command prompt window, and switch to Android SDK's platform-tools folder:
    C:\>cd %LOCALAPPDATA%\Android\sdk\platform-tools
    
  5. Use the adb (Android Debug Bridge) tool to remount:
    C:\Users\user\AppData\Local\Android\sdk\platform-tools>adb root
    
    C:\Users\user\AppData\Local\Android\sdk\platform-tools>adb remount
    remount succeeded
    

    This will remount system/ as read/write.

  6. Now use the adb to start a remote interactive shell and run the following commands to update the hosts file:
    C:\Users\user\AppData\Local\Android\sdk\platform-tools>adb shell
    generic_x86:/ # cd /system/etc
    generic_x86:/system/etc # echo "192.168.1.1 dev.site.com" >> hosts
    

    Make a backup first if needed, and use cat hosts to verify that hosts file has been updated.

Now start Chrome in the emulator and browse to http://dev.site.com and it should work.

Googling on this topic also mentions using adb push/pull commands to copy the hosts file, but it didn't seem to work at first, at least with Android Studio 2.3.1/API Level 25. The hosts file copies successfully, but Chrome gives DNS error. However, subsequent tries worked after creating a new hosts file and copying it over, instead of pulling the existing hosts file and editing it then pushing it. Not sure what the cause was, my initial guess was the ordering of IP4 and IP6 addresses in the hosts file, but confirmed that's not the case...the file was LF endings, too.

Reference: http://www.bradcurtis.com/hosts-files-and-the-google-android-emulator/

March 23, 2017

Automatically restarting after reboot

I've noticed that some programs, such as Chrome, automatically restarts after a forced reboot from Windows Update. So out of curiosity, did some googling, and found that there's something called Restart Manager. Works the way I thought it would work — listen for some messages during the shutdown process to save state, and register with Windows to have it start the program after reboot.

"shutdown -g" will also restart registered programs after reboot.

October 1, 2016

Setting time on a Linux machine

Had to work with a CentOS VM in VirtualBox format for a bit, and needed to sync the time since it didn't have Guest Additions, and couldn't connect to internet so ntp daemon wouldn't work.

Steps:

sudo rm /etc/localtime
sudo ln -s /usr/share/zoneinfo/EST5EDT etc/localtime
sudo date MMDDhhmmYYYY
sudo hwclock --systohc

It's interesting that Unix keeps separate time from the system time, and how VirtualBox does not sync the virtual BIOS clock with the host computer when it's been turned on or restored. Makes sense I suppose, the VM is not running on host time.

On a side note, it's been quite some time since I've worked on Linux... Brought back some memories when I used to run Slackware on an old machine with two NICs, configuring iptables to work as my home firewall...

September 30, 2016

Github Pages from master branch

Now you can publish Github Pages directly from the master branch. You don't have to have gh-pages branch. Go to your project's settings and change the source of your Github Pages. Three choices are gh-pages, master, or master/docs.

Reference: Github Configuring Publishing Source

I've removed the gh-pages branch from multi-week-calendar. No more extra step of merging into gh-pages!

March 13, 2016

Git Flow Diagram

We've been using git flow with some slight changes here and there. I wanted to document the process for future reference. View the post for the full size image.

January 15, 2016

Git Case Sensitivity on Branch Name

I was trying to push a local branch to remote, but kept getting this error (simplified example):

git push -u origin Bugfix/Foobar
fatal: Bugfix/Foobar cannot be resolved to branch

After googling a bit, came across this StackOverflow question, and that got me thinking about case sensitivity, so I changed the "B" to "b":

git push -u origin bugfix/Foobar

And that worked. Yes, I was pushing to a *nix based server from my Windows machine, though perhaps the message given by git could've been more clear. Other people had already pushed branches with the lower case "bugfix" before, so my guess is that git on the remote server couldn't create the folder using "Bugfix"...

November 19, 2015

Date Object Creation Format Differences in JavaScript

Another issue that came up in my multi-week calendar was due to how date parsing in JavaScript yields different date objects depending on the format:

  • new Date('11/18/2015') creates a date object of 11/18/2015 local time zone.
  • new Date('2015-11-18') creates a date object of 11/18/2015 UTC time zone. For example, if it's Eastern Standard Time (-5:00), then it will be 11/17/2015 19:00.

Try it out on your web browser's console. One good thing is Chrome, IE, and Firefox all seems to have the same behavior. Granted, I ought to be specifying date in a more specific format, but it's interesting to see how the same "date" yields different date objects based on the format. For the multi-week calendar fix, I just removed the native date creation (which I probably should've done from the beginning) and used moment.js's multiple date format creation method. Moment.js documentation has a reference to a nice table of JavaScript date parsing behavior. Hmm, maybe it's time to add some tests...