Evil Coding Monkey Ubuntu, Android, web development, open source, etc.

23May/110

Detect Mobile Devices with PHP

Detecting when your visitors are using mobile devices may be very useful, and sometimes you don't need or want a complete detection plugin with advanced functionalities. So for those cases where you only need basic Mobile OS recognition, here's a very simple PHP function you can integrate to your scripts:

/**
 * Based on the HTTP_USER_AGENT, this function tries to detect a
 * mobile user agent and returns its name in a string.
 * @return A string that represents the mobile user agent when one
 *         is found, false otherwise.
 * @author Matt (www.evilcodingmonkey.com)
 */
function getMobileAgent() {
    $mobileUA = false;

    // Make sure the user agent is set
    if(isset($_SERVER['HTTP_USER_AGENT'])) {
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);

        if(strpos($userAgent, 'android')) {
            $mobileUA = 'android';
        }
        elseif(strpos($userAgent, 'iphone') ) {
            $mobileUA = 'iphone';
        }
        elseif(strpos($userAgent, 'ipad')) {
            $mobileUA = 'ipad';
        }
        elseif(strpos($userAgen, 'windows phone os 7.0')) {
            $mobileUA = 'winphone7'
        }
        elseif(strpos($userAgent, 'blackberry')) {
            $mobileUA = 'blackberry';
        }
    }

    return $mobileUA;
}

This very simple function will tell you when a user is behind an iPhone, iPad, Android, Windows Phone 7 or Blackberry. It returns false when none of these user agents are detected. Depending on your specific needs, you way need more specific information or more advanced features. There are several open source offers out there, such as this one: Detect Mobile Browser. Also, feel free to use this code and modify it as you wish.

18May/116

eMusic Download Manager for Ubuntu

eMusic LogoI recently realized I didn't want to buy CDs anymore but being an artist myself, I really feel I should encourage my favorite bands in a way or another. Sure, I could buy their merch, but buying $55 t-shirts from all the bands I like would be rough on my budget. This is why I am now BUYING music online (almost sounds weird) for the first time of my life. After looking at many online stores, I decided to go with eMusic. All songs cost $.59 each and are DRM-free.

Looking for a download manager

As all eMusic members, I need to use a download manager to get the music I buy. One problem though: they no longer support their own Linux DLM. After a quick research, I found eMusic/J, a Java download manager for eMusic. Here's the official description: eMusic/J is a Java-based download manager aimed at Linux users. It takes the .emp file that you get from the eMusic.com Web site and downloads the music and artwork specified in it.

Download and run

First, access the official download page and carefully select the correct version for your system (32 or 64-bits). Then extract the downloaded file where you want it (in your home folder for example). From the newly extracted folder (should be named "emusicj-linux"), run the file named "emusicj". If you are not familiar with how to do that, open a terminal, navigate to the emusicj-linux folder and then execute the following:

./emusicj

The first time you run the program, the preferences window will pop up. You may read the user manual if you need more help at this point. The only thing you need to do now is open .emp file from eMusic/J's File / Open... menu.

Enjoy your music!

10May/110

Best and Easiest Flash Player Setup for Ubuntu

Flash-Aid LogoLike many Ubuntu users out there, you have probably had problems with Flash Player in the past. I've had a lot myself, mostly related to poor performance, and sometimes with strange behavior such as videos playing too fast. People at Ubuntu have done a good job at adding better Flash support in the last year or two but I often feel it's still not enough. This is why I have been using the Flash-Aid plugin for Firefox for many months now.

Helping Firefox, but also Chromium

According to the official page, this Firefox add-on removes conflicting flash plugins from Ubuntu Linux systems, installs the appropriate version according to system architecture and applies some tweaks to improve performance and fixes common issues. All that in a single click! And because Chromium (and I suppose other browsers you may like) uses the same Flash packages, Flash-Aid will also "help" them.

How to install and use in Firefox 4

  1. Open Firefox
  2. Go to Tools / Add-ons menu
  3. In the Add-ons Manage, type "flash-aid" (without the quotes) in the search box located  in the top right corner
  4. In the result list, locate Flash-Aid and click its install button located on the same row
  5. Restart Firefox, you should now have the Flash-Aid icon in the top right corner of your browser
  6. Click the icon and follow instructions
  7. You now have one of the best Flash installations possible

Performance

If you are also a Windows user, you will notice performance downgrade compared to Windows. At this point, you should mostly blame Adobe who won't put more effort in their Linux support.

If you have any other tip or trick to Flash tweaking, don't hesitate to post them.

9May/110

Setting Up Eclipse for Android Under Ubuntu

Android using Eclipse under UbuntuI know, tutorials such as this one can be found all around the Web. But I feel this might be helpful for many people reading this blog. I will try to cover everything from JDK installation to deploying applications directly to your phone using the "run" button in Eclipse.

Used for this tutorial

  • Ubuntu 11.04 (Natty Narwhal)
  • Acer TimelineX 1830T netbook
  • Samsung Galaxy S Fascinate
  • USB cable

Before you continue

Before you continue, please note that I rarely use the Ubuntu Software Center to install packages and applications so I will mostly use command line in this article. If this causes you any problem, feel free to post your questions in the comments section.

Installing Eclipse

If you don't already know, Eclipse is a free and open source IDE (integrated development environment) that can be used to develop in Java, C++, PHP, Ruby, and many more. To install Eclipse, open a Bash shell and execute the following:

sudo apt-get install eclipse

By installing the Eclipse package, many other packages such as Java JRE and JDK will be installed. This will save you a lot of configuration time.

Installing the Android SDK

Call it a personal preference. but I like to install SDK's in the /usr/lib folder. If you prefer to install it somewhere else, in your home folder for example, feel free to do so. Note: replace your_user by your Ubuntu user name.

cd /usr/lib
sudo mkdir android
sudo chown -R your_user:your_user android
cd android

Now navigate to the Android SDK Download Page and copy the link to the Linux (i386) SDK. At the time of writing this article, the URL is: http://dl.google.com/android/android-sdk_r10-linux_x86.tgz. Now back to your shell:

wget http://dl.google.com/android/android-sdk_r10-linux_x86.tgz
tar -xvzf android-sdk_r10-linux_x86.tgz

Installing the ADT Plugin for Eclipse

The Android Development Tools (ADT) Plugin is required for any Android development. Follow these instructions to install it:

  1. Open Eclipse (confirm workspace location if prompted, default location is good)
  2. In Eclipse's top menu, select Help / Install New Software...
  3. Click on the "Add..." button
  4. Enter "ADT Plugin" for the name, the following for the URL and click the OK button: https://dl-ssl.google.com/android/eclipse/
  5. Check "Developer Tools" (to download everything offered) and click the Next button twice
  6. Accept terms and conditions and click "Finish"
  7. If security warning pops, click "OK" to continue
  8. When asked, click "Yes" to restart Eclipse

Configuring Eclipse with the Android SDK

Eclipse needs to know where the Android SDK was installed:

  1. In Eclipse's top menu, select Window / Preferences
  2. In the window's left menu, click on "Android"
  3. Click "Browse..." and locate the place where the Android SDK was previously installed (in my case: /usr/lib/android/android-sdk-linux_x86)
  4. Click "OK" but not "Apply"

Other essential SDK components

Before you are ready to start developing, some essential components must be downloaded and installed using the Android SDK and AVD Manager. To open the manager, you can use the download-like arrow icon located in left part of Eclipse's icon bar, or you can select Window / Android SDK and AVD Manager from the top menu.

Click on "Available Packages" on the left, and under "Android Repositories" select the following packages:

  • Android SDK Platform-tools
  • Android SDK Tools (if in the list, but it shouldn't at this point)
  • Documentation for Android SDK
  • One or several SDK Platforms (I chose Android 2.1 and 2.2 because that's the API's I'm developing for)
  • Click "Install Selected"
  • Select "Accept All" and click "Install"
  • When asked, click "Yes" to restart the ADB

Note 1: it is recommended you choose an Android API as low as possible to reach as many users as possible. Click here for the latest usage share.

Note 2: if you need more help selecting components, please read this.

Adding the Platform Tools to your environment

To make the Android SDK Platform-tools available from everywhere, you need to add the platform-tools directory to the PATH environment variable. To do so, use your favorite text editor (I use nano) and do the following:

nano ~/.bashrc

Scroll to the end of the file and add this line:

PATH=$PATH:/usr/lib/android/android-sdk-linux_x86/platform-tools/

Logout from your Ubuntu session and log back in. Now the "adb" command should be available from anywhere in your Bash shell.

Setting your phone

To be able to deploy your applications directly from Eclipse to your phone, you need to do two things:

  1. Set USB Debugging on (on my phone, with Android 2.2, it's located under Settings / Applications / Development)
  2. Allow install of non-market applications (again, on my phone, under Settings / Applications)

Connect your phone to ADB

Using your USB cable, connect your phone to your computer. Your phone should "say" it is now under USB debugging. On your computer, open a shell and type:

adb devices

You should get an output like this one:

List of devices attached
1000ed430c4c    device

If you can't see your device, try:

adb kill-server
adb start-server

Finally, deploy to your phone

Now start Eclipse. Open your Android project or create a new one (not covered in this tutorial).

You will need to create a run configuration at this point if not done already. What is important here is to set your configuration's target to "Manual". When finished, press "run" (the green play button). A window entitled "Android Device Chooser" should now open and your phone should be there. Select it and press the OK button. It should then deploy to your phone and the application will start by itself.

If you get a timeout error

When deploying to your phone, you may someday get:

Android error: Failed to install *.apk on device *: timeout.

It can be solved easily by going into Eclipse's top menu in Window / Preferences / Android / DDMS, and changing the ADB connection timeout.

Why not use the emulator?

The emulator may be a good tool in some situations (you can't own/test all different devices). But I prefer to develop directly on my phone. This is why this tutorial won't cover the use of the emulator. At this point, I'm almost sure you will figure it out by yourself as all the required tools have now been installed.

Need more help?

I really hope this will help you getting started with your Android development projects. If this tutorial doesn't answer all your questions, here are some sites/pages you may want to visit:

As always, feel free to post your questions or comments!

1May/1122

Starting an Android AR Project with QCAR

Qualcomm LogoIn my previous post, Augmented Reality on Android, I listed several AR libraries for Android. After reading and trying some of them, I finally decided to go with Qualcomm's QCAR. By great coincidence, QCAR's first production-ready release happened this week with version 1.0.0.

My reasons for choosing QCAR

  1. Official support for a great list of devices, old and recent
  2. Their demos, tested on my Samsung Galaxy S, are way snappier than other libraries I tried
  3. It recognizes partial markers (when partly in the camera's sight)
  4. Great developer guide and API documentation
  5. Great developer community (forums)

Loading a 3D model

As stated in my previous post, I need to load a 3D model over a marker when it comes in the camera's sight. The model I have to load is a .obj file, but QCAR only "works" with .h files (the usual way of doing things in OpenGL). I found two ways of solving this problem:

  1. Converting .obj file to .h file using this script: OBJ2OPENGL
  2. Using a second library to load .obj files

Right now, I'm trying both options. I've had success with the min3d library, but I'm experiencing some image distortion. I still have a lot of work to do. Like I said before, I'm no Android expert, and I'm still learning the basics. I'll be back soon with more on this subject. In the meantime, feel free to post any comment or question.

---- EDIT (September 9, 2011) ----

I am not working on this project anymore, but I'd like to let everyone know I didn't use min3d. I converted my 3D model to OpenGL ES .h file. Read this post for more information.

   
WordPress SEO

Switch to our mobile site