Ubuntu: make ADB see your mobile device

If you are making an Android application it's good to test it on the real device while still in the middle of developing process. Probably the best way to do that it to just connect your mobile phone by USB cable / charger to the PC.

Now in order to make it work first thing to do is to prepare your phone:
1. Turn on the "Developer Options" in your mobile phone
2. Set 'Stay awake' // this is not required but makes life easier
3. Set 'USB debugging'

You may notice there is no such thing as "Dev Options"... if so you will need to "google" it up how to enable it for your particular phone.
For instance on Motorola Moto G you need to do the following:
  • Go to Settings
  • Go to About phone
  • Find the 'build number' field and tap on it 7 times (LOL:)

Once done you can verify if your ADB (usually available in android-sdks/platform-tools) can actually see the device by running the following command:
> adb devices
// if everything is good you should see the ID of your device

If something is not quite right you will not see your device or you will see something weird like for example:
?????????? device

Following steps should help.
1. Query your USB:
> lsusb
// locate your device - you should see something like this:
Bus 001 Device 006: ID 11a8:2d77 Motorola PCS
where:
11a8 is vendor id
2d77 is product id

2. Create a new file for rules:
> sudo gedit /lib/udev/rules.d/10-adb.rules

// with the following content (one line)
SUBSYSTEM=="usb", ATTR{idVendor}=="11a8", ATTR{idProduct}=="2d77", MODE="0600", OWNER="your_username"

3. Restart the ADB server:
> sudo adb kill-server
> sudo adb start-server

4. Check if you can see your device:
> adb devices
ZX1E34GW9Z    device

Done! Now you should be able to run your apps directly from your dev IDE/env.

Hoper that helps.


Linux: GRUB rescue how to

There are few scenarios when your system may stop launching due to GRUB misconfiguration.

For example what I did recently was to extend the Ubuntu root partition using the live cd. It went well and my partition was bigger. The only problem was the GRUB boot loading was no longer working.
Cause of this was that the partition was also moved and drive letter changed from /dev/sda6 to /dev/sda5.

There are at least few ways to fix GRUB issues:
So in this article will cover how to fix it manually which doesn't require any third-party tools. You are running this at your own risk so if you are brave enough follow the steps below:

1. Start - you should end up seeing GRUB rescue console. Do the ls to see what drives are available:

grub rescue> ls
(hd0), (hd0,msdos6), (hd0,msdos1) ...

2. Find which drive has the boot/grub directory by running ls command to locate correct one:

grub rescue> ls (hd0,1)/
Not found...
grub rescue> ls (hd0,5)/
Found list of directories... e.g. etc/ boot/

Note: (hd0,5) in this case relates to /dev/sda5 drive

3. Find the latest version of vmlinuz core and corresponding initrd image within the boot/ directory:

grub rescue> ls (hd0,5)/boot/
vmlinuz-3.13.0-27-generic
initrd.img-3.13.0-27-generic

4. Set the prefix by running the following command

grub rescue> set prefix=(hd0,5)/boot/grub

5. Install mods:

grub rescue> insmod (hd0,5)/boot/grub/linux.mod
// not found? try locating it in other place
grub rescue> insmod (hd0,5)/boot/grub/i386-pc/linux.mod

// install additional mods
grub rescue> insmod part_msdos
grub rescue> insmod ext2
grub rescue> insmod gzio


6. Final step - run the following commands:

// note: sda5 relates to the drive where grub is available so you may have it on a different one
grub rescue> set root=(hd0,5)
grub rescue> linux /boot/vmlinuz-3.0.13-27-generic root=/dev/sda5
grub rescue> initrd /boot/initrd.img-3.0.13-27-generic
grub rescue> boot


You system should be starting. All you need to do now is to fix the grub inside the running OS - run the following:

xuser@xsystem> sudo update-grub
xuser@xsystem> sudo grub-install /dev/sda



Done! Hope that helps someone.


GIT setup: basic configuration

Hola!

After installing Git client it's ready to use right away. However there are few things you may want to consider setting up. Those are things like for example user name and e-mail address.

You can display current config values with the following command:
$ git config --list

Important Note
When setting configuration parameters you can set it either as global or local value. By default you are setting the local value. By using --global param it will be set-up globally which means it will be used for all of your projects unless overridden by a local setting.


Setting user name
$ git config --global user.name "xuser"
$ git config user.name "xuser"

Setting e-mail

$ git config --global user.email xuser@example.com
$ git config user.email xuser@example.com



That's it.


Android: simulate incoming call

Hi,

There are at least 2 ways of making incoming calls to your test device (either real device or emulator).

Solution A: Make the call from within Eclipse DDMS UI



Solution B: Make the call using command line

1. List the available devices:

$ adb devices

List of devices attached
localhost:5554    device


2. Telnet to the device:
$ telnet localhost 5554

3. Make the incoming call:
> gsm call <specify_number>



Cheers!


Android Emulator: How to run APK file?

If you want to install and run APK on the emulator device follow the steps below.


You will be using adb script which should be located in android-sdks/platform-tools/

1. List the available devices:
$ ./adb devices

2. Install the APK file:
$ ./abd install example_app.apk



Success!



Git Error: Permission denied (publickey)

Hello,

The other day I was trying to fetch the changes from the remote repo and the following error has been displayed:

$ git fetch origin

Agent admitted failure to sign using the key.
# Permission denied (publickey).



In this case the reason was that I've changed / switched between two different ssh keys. However if you got similar issues the problem has been fixed by telling ssh to use the key by running the following command:

$ ssh-add

That's it - problem solved :)


Flash player debugger on Linux x64 revisited

Hi there,

If you remember older post about it: Flash Player debugger for Linux x64
...it may not always work - recently I come across the error message:
"no appropriate viewer found" (when running nspluginwrapper on the plugin)

So there is other solution which worked for me.

1. Install flashplugin-installer package:
sudo apt-get install flashplugin-installer

2. Wrap it up:
sudo apt-get install nspluginwrapper
sudo cp ~/Downloads/libflashplayer.so /usr/lib/flashplugin-installer/
sudo nspluginwrapper -i /usr/lib/flashplugin-installer/libflashplayer.so

That's it! You should now have a debugger version of flash player plugin!



Android dev environment to work with Android OS on VirtualBox

Hi there,

Following the post about installing Android OS on VirtualBox: http://devmain.blogspot.co.uk/2014/04/how-to-install-android-on-virtualbox.html
it would be useful to make use of such virtual device.

This is an alternative to using built-in Android device emulators, e.g. available from Android Device Manager on the IDE. Now from my experience it's extremely slow to run and debug apps from Eclipse (with Android Plugin) on the emulated mobile devices - at least on Linux based OS. So running it directly on Android OS installed on VBox sounds good to me ;-)

And it's quick and easy to make it work.
First step is to find out the IP address of your Android system:
1. On running OS on VBOX press ALT+F1 to open console window.
2. Use ifconfig command to get the IP address, i.e.:
ifconfig eth0 (or wlan0 depending on the network settings)
3. Press ALT+F7 to back to default view.

Second step is to connect ADB tool to your device:
1. From usual Linux console run the following command:
adb connect <ANDROID_DEVICE_IP_ADDRESS>

ADB is a tool which should be available within Android SDK tools directory.

And finally when you run mobile app code from your IDE it should see your VirtualBox machine as an available device to run on. In other words it should act like a normal mobile phone connected via USB or whatever.

And that's it I guess :)


Linux Ubuntu - touchpad not working fix

Hi,

Finally I've managed to resolve the issue with broken touchpad on my Dell laptop under Ubuntu 12.04 OS :)

Run the following commands as sudo:
sudo modprobe -r psmouse
sudo modprobe psmouse proto=imps
That should do the trick :D



How to install Android on VirtualBox

Hi there,

Today will be giving instructions about installing Android OS on the VirtualBox.

Pre-steps / requirement:
1. Download the latest Android ISO image: http://code.google.com/p/android-x86/downloads/list

2. Download and install VirtualBox: https://www.virtualbox.org/wiki/Downloads

How to step by step:
  1. From VirtualBox create new virtual machine from the "New" button.
  2. Give a name and choose system Linux 2.6.
  3. Assign memory - usually over 512 MB should be more than enough.
  4. Create new virtual disk with hard drive size of 3 GB or more.
  5. Select the newly created machine and go to "Settings"
  6. From Storage -> IDE Controller add/select CD/DVD drive start disk to be Android image downloaded earlier.
  7. Double click the machine to start it.
  8. Select install Android on hard disk.
  9. Create new primary partition with "bootable" attribute enabled.
  10. Format new partition to the "ext3" type.
  11. Answer YES to question about installing GRUB boot loader.
  12. Complete the installation.
Network problems / enable
1. Go to Network settings on you virtual machine.
2. Select "Enable Network Adapter" checkbox.
3. Attach it to "Bridge Adapter" with item name "eth0" or "wlan0" (depending on your network connection)


Useful tips & tricks:

  • To control mouse pointer select "Disable Mouse Integration" from the Machine menu

  • To add extra video mode / resolution / screen size you can use "vboxmanage" program by running following command (if on Linux OS):
vboxmanage setextradata "VM_NAME_HERE" "CustomVideoMode1" "320x480x16"

Then when starting your Android OS press "a" and type: "vga=ask".
Next hit <ENTER> to view available video modes.
You should see your newly added video mode available to choose.


  • To start separate console on Android OS press ALT+F1, to back to normal press ALT+F7

Hope it will be helpful :-)

Cheers!





Things to do before uploading an app to the Android Market

Hi there,

Imagine the scenario when you have just finished developing your cool mobile application and want to share it with the world.
Hold on a moment before you do it and consider below list of recommended things to do.

Here's the list of things TO DO - most of them require some code changes:
  • Make sure your app looks good enough on as many as devices as possible, i.e. different screen sizes, tablets, etc.
  • Verify if your app needs all the permissions specified in the Android Manifest file. Usually the less permission requests the more likely user will install the app.
  • Enabled fullscreen mode in your activities:
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  • Add some statistics to your app in order to track what's going on. I would recommend going with Google Analytics - it's quite simple and quick integration.
  • If your app has free version you should consider adding some ads. Again would recommend a Google product - AdMob. You can show banner ad and fullscreen ad on application start or close. Take it easy to not annoy users with "aggresive" ads :)
  • Add 'Rate this app' and 'Send feedback' buttons. It is important that your app has good rating and also you are aware of user feedback.
  • If you have some other apps consider adding section / tab 'More apps' to show them. It's an easy and quick way to advertise your work.
  • Reduce number of third-party libraries used by your project. In general if there are things that are relatively easy to do without library then it's better to do it manually.
  • Add the version information / details aka changelog. Users would like to know what has changed since the last version.
  • Add settings / preferences and help "features" to your app.
  • Set the preferred installation place of your app to external storage (e.g. SD card) - in the manifest file set:
 android:installLocation="preferExternal"
  • Remove all error loggers and logging and try and catch errors in risky places to not show the exceptions to the end users.
  • Verify values for min, max and target SDK version specified in the manifest file.
  • Run Android LINT tool to check for warnings and error. Sometimes it may not be possible to clean them all but try to fix as much as you can.
  • Enable ProGuard in project.properties:
proguard.config=proguard.cfg

ProGuard will reduce size and optimize your code. If it's enabled it runs automatically when exporting signed APK. You may want to run full set of tests after it just in case if everything is working the same as in the original version.
  • Use zipalign tool to "zipaligned" the APK
zipalign -f -v 4 src.apk out.apk
  • Test the app on some real devices - emulators are there but you know it's never the same :)
  • Prepare good description and promotion text together with some screen shots.
  • Finally rate your own app - yeah why not starting with some good rating :)
    You can also ask friends and family to download and rate the app.
OK that's quite a long list but still worth doing :)
 
By the way let me know in the comments what else you are doing or recommend to do before  publishing and mobile app. Any ideas and comments welcome :)

Thanks!

Google Plus One button in the Android app

The quickest way to let users to like your Google page is to provide 'plus one' button.

There is a simple way you can do it withing Android APP.

1. In layout XML of your target activity add the followin:

<com.google.android.gms.plus.PlusOneButton
        xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
        android:id="@+id/plus_one_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        plus:size="standard"
        plus:annotation="inline" />


 
2. In the activity class:
private PlusOneButton plusOneButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    plusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
}


protected void onResume() {
    super.onResume();
    plusOneButton.initialize(GOOGLE_PLUS_URL, PLUS_ONE_REQUEST_CODE);
}



Where:
- GOOGLE_PLUS_URL is the address of the Google page to be +1'd
- PLUS_ONE_REQUEST_CODE is the request code to use when opening the +1 activity. This value must be an unsigned 16 bit integer.

That's all you need.


Spring locale configuration for web application

Hi,

Today we will set up spring configuration for locale based settings for web application in order to support many languages

Basic XML config for you servlet usually named servlet_name-servlet.xml:
1. Definite message source bean.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basename" value="classpath:/locales/messages" />
<!-- do not use system locale by default as it can differ based on OS-->
  <property name="fallbackToSystemLocale" value="false" />
</bean>


Property basename definies path and name of your "messages" files, i.e.:
- messages // default text to use
- messages_es  // text in Spanish
- messages_pl // text in Polish
etc.

2. Define locale resolver and support automatic locale changes:
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
  <!-- <property name="defaultLocale" value="en" /> you can specify default here -->
</bean>


3. Interceptor responsible for resolving locale:
<mvc:interceptors>
  <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="ln" />
  </bean>
</mvc:interceptors>


Remember to specify MVC namespace:
xmlns:mvc="http://www.springframework.org/schema/mvc"

So for example let say you have an URL /yeyeyey.html which returns English content.
If you go to /yeyeyey.html?ln=es it should return content in Spanish.

Of course you will need to  have messages defined in messages_es file.

Now if you want to access specific message in the code you can do it through the MessageSource bean:
@Autowired
private transient MessageSource messageSource;


messageSource.getMessage("text.key.value", null, locale);

where current locale you can get from Spring context:
LocaleContextHolder.getLocale();

If you want get the correct text value on the UI (i.e. JSP) you can use <spring:message> tag:
<spring:message code="text.key.value" text="PUT_DEFAULT_VALUE_HERE" />

That's it.


Linux: how to connect Sony mobile phone via USB

Hi there,

Recently I was trying to connect my Sony Xperia S mobile phone (with Android OS) to my PC running on Linux (Ubuntu)....
... and guess what no success :(


So here are the steps TO DO in order to successfully connect your mobile phone:
1. Connect to the  USB port and run lsusb command to see the devices.
2. Note the vendor ID for you device.

3. Install additional packages:
sudo apt-get install libusb-dev
sudo apt-get install gmtp

4. Download libmtp-1.1.6 extract and build:
cd libmtp-1.1.6
./configure --prefix=/usr
make
sudo make install
 

5. Copy libmtp rules to rules.d:
sudo cp 69-libmtp.rules /etc/udev/rules.d/

6. Edit the rules:
Add something similar to below with correct product id specified earlier in lsusb
# SONY XPERIA SL
ATTR{idVendor}=="0fce", ATTR{idProduct}=="4169", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"
ATTR{idVendor}=="0fce", ATTR{idProduct}=="0169", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"


Save and close.

7. Reload the rules: sudo udevadm control --reload-rules

8. Connect device again to USB port and run GMTP to connect.

You should now be able to transfer files to and from your device.

Also if you are an Android developer you can now debug your application directly on your mobile phone :)

Hope that will be helpful for someone :)




Hit counter for JSP servlet web application

Hola!

There are many free nice and simple hit counters available out there. However they are not totally free as you will also have some kind of hidden url to the external website/resource within it.

If you want you can implement such counter yourself within JSP file in a no time :)

One of the simplest way is to just do it within the JSP file with no core java work:
1. Use application attribute within your JSP file:
<%
    Integer counter = (Integer) application.getAttribute("counter");
    if(counter == null || counter == 0) {
       counter = 1; // initial value
    } else {
       counter += 1;
    }
    application.setAttribute("counter", counter);
%>



2. Then just show it: <%= counter%>

Instead of storing it in application attribute you could declare it as a static field. As you know static fields in programming are generally shared across all instances of an object.

Note that we are not storing the actual value of the counter in the datastore. It means everytime the application server is restarted the counter returns to the initial value.

It shouldn't be a problem because usually you restart an  app when you want to upload a new version anyway. In that case you can set the counter to the last known value instead of "1" in the new code.




Linux: release cache memory

Hi!

Sometimes it may be useful to release some of the cache memory under Linux OS environment.

Here is a simple script which does that:
---------
#!/bin/sh
sync;
sudo sync;
echo 1 > /proc/sys/vm/drop_caches;
echo 2 > /proc/sys/vm/drop_caches;
echo 3 > /proc/sys/vm/drop_caches;
sync;
sudo sync;

echo "WOW! I feel good :D"

---------

Save it under preferred name, e.g. cleaner.sh
Run it as a sudo: sudo sh cleaner.sh
That's it!


How to upload web application to App Engine

Hello all,

It's been a while but here I come with another hopefully useful post :)

Ever wondered what you need to do in order to upload your web app to the Google AppSpot (app engine)?

Let's rock!

Assuming you already have an account (if not create one https://appengine.google.com/)
Requirements:
1. Login to your account and create new application - follow on screen instructions and use common sense :)

2. Download official App Engine SDK: https://developers.google.com/appengine/downloads

3. Create appengine-web.xml (next to standard web.xml file):

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>your_app_name</application>
  <version>1</version>
  <threadsafe>true</threadsafe>
  <sessions-enabled>true</sessions-enabled>
</appengine-web-app>


4. Build the app locally like usual to create WAR file and related app folder (if you WAR does not extract automatically you can extract it manually), e.g.:
/home/user/dev/app-name/target/app-name.war
/home/user/dev/app-name/target/app-name

5. Go to the bin directory of the app-engine SDK home:
cd /home/user/APP_ENGINE_SDK_HOME/bin/

6. Run the appcfg script:
./appcfg.sh update /home/user/dev/app-name/target/app-name/

OR equivalent for other OS

It will prompt you for your registered e-mail and password and that's it. All done :)

In few minutes your application should be available online!

Have fun!