Monday, January 7, 2013

Connecting Android to Apache on localhost

Sometimes the requirements given to client/s are a bit odd especially when you are working as a freelance.  In my previous android projects, apps that requires internet permissions is as easy as what on the android documentation says.  Recently, I've got a project that uses internet to fetch updates.  The resources that I have are a site and a mysql database.  Now, I have to setup this site into local and run some tests on localhost.  In order for you to connect your android device to access localhost, make sure your laptop and android device are on the same wifi network.  You need to get the ip address of your localhost.  To do this, execute this command:

$ ifconfig

This is a utility used by system administrators to display and analyze network interface parameters.  Result should display like the image below.

The ip address of the laptop is at the wlan0 which is 192.168.1.35.  We may have different results depending on how many devices connected on wifi.  You may have something like 192.168.1.xx.  So on your test device, try changing the localhost to your ip address.

WebView webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://192.168.1.35/drupal/");

Thanks to this post at StackOverflow.

Thursday, January 3, 2013

Android Terminal Emulator Crashes on cm-10.1 [Part 1]

After I installed the cm-10.1 on LGE p970 and tested it, it was pretty fast and smooth except for 2 things.

  1. Android Terminal Emulator (ATE) app crashes on launch.
  2. People app couldn't find vcard during import even though it exists on the root directory of the sdcard.
The ATE is a very important tool (at least for me).  I always use this during development and hacking.  I prefer  to work and debug things wirelessly.  So this article is about tweaking android and stuffs that I wanted them to behave.

The Cause.  Before I flashed cm-10.1, I remembered updating the ATE from Google Play.  During testing cm-10.1, the app crashes.  Finding the cause was easy.  First, I checked the stack trace.  Here's what the stack trace said.

Serioulsy?  This wasn't clear to me at all.  Second, I went to Google Play and found out that ATE is an open source project hosted at github.  From there, I browsed the issues before doing stupid things (i.e. asking solutions from developers or posting it in the issue tracker) and found this interesting issue (same as mine).

issue #133 Force close on start (UnsatisfiedLinkError)

It says,
I think if you install the apk into /system/app, the library loader needs the native library in /system/lib. (If you install to /data via the normal route, the package manager unpacks any native libraries in the package to /data/data/package.name/lib, and the library loader will look there also.)Basically, either extract libjackpal-androidterm4.so from the apk and install it to /system/lib, or install the package normally and let the package manager deal with this for you. This isn't a bug in ATE.
By reading this comment, everything is now clear.

So What Happened? By reading the first line of the stack trace,

java.lang.UnsatisfiedLinkError: Couldn't load jackpal-androidterm4 from loader dalvik.system.PathClassLoader[dexPath=/system/app/Term.apk,libraryPath=/data/data/jackpal-androidterm/lib]: findLibrary returned null

the system couldn't load a shared library in

jackpal-androidterm.so

in the library path,

libraryPath=/data/data/jackpal.androidterm/lib

this makes sense because after I update ATE, its path changed to /data/data/jackal.androidterm.  Since cm-10, ATE is preconfigured in the rom so path should be

libraryPath=/system/lib

as mentioned in the comment, extract jackpal-androidterm4.so from the apk and push it into /system/lib.  I haven't try this yet so I'll post it on the next series.

Wednesday, December 19, 2012

Filter logcat Output by Tag Names

When debugging on android development, sometimes it is hard to find what we need when there are too much data displays on logcat. Most of the time when I do debugging on android, I use the terminal. It's pretty fast and I find it more comfortable than the logcat from Eclipse android plugin. Sometimes I missed out important data because there are unnecessary information displayed on logcat. So what I do is to save the logcat into a file and check it into a browser. This is time consuming though you have all the information. One way to sort out this problem is to filter logcat. adb logcat command has the option to filter your tags that you define in your application. In your code, you might have something similar like this:
private static final String TAG = "Fragment01";
Log.d(TAG, "getUserVisibleHint=" + getUserVisibleHint());
To display "Fragment01" in logcat,
$ adb logcat -s "Fragment01"
But this information isn't enough. What if you also need to see the values of fragment 2? or fragment 3? and etc.? Run this command.
$ adb logcat -s "Fragment01" "Fragment02" "Fragment03"
By adding the tag names on the command this will allow adb to filter it and display in logcat. Do you have some ideas similar to this? Glad if you could share. Let me know your thoughts.

Friday, September 21, 2012

USB Debugging on Samsung Galaxy Tab Using Ubuntu

If you are developing android applications using either the Samsung Galaxy Tab 7" or the 10.1", chances are you'll have a hard time using adb to these devices.  If you have followed these instructions in the android developer site and you find it working for you, then congratulations!  In my case, it's different.  Restarting the udev and adb doesn't work for me.  When I try the command adb devices, it shows,

$ adb devices
List of devices attached
????????????    device

and if I check the devices attached to my machine using lsusb it shows,

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 0a5c:21bc Broadcom Corp. BCM2070 Bluetooth 2.1 + EDR
Bus 001 Device 005: ID 04e8:6860 Samsung Electronics Co., Ltd GT-I9100 Phone [Galaxy S II]

Samsung handles these attached devices differently.  I'm still not sure what causes this but it has something to do with the MTP (Mobile Transfer Protocol).  So what I did, I googled for answers and found this answer from stack exchange.  Thanks to this guy a.k.a Flow.  I guess this may work for you.  So go ahead and check his answer and don't forget to upvote it.

Tuesday, September 4, 2012

Is Unity3D Supported On Your Ubuntu?

One way to check if Unity3D is supported in your Ubuntu, open a terminal and type this command

$ /usr/lib/nux/unity_support_test -p

Result should tell if Unity3D is supported like the image below.


Do you have any tips on how to tell if you're on Unity3D?  Glad if you share.

Wednesday, August 29, 2012

ActionBarSherlock and ViewPagerIndicator Pattern for Pre-HoneyComb Devices

Download the libraries:
ActionBarSherlock
ViewPagerIndicator

Extract the zip files.  These libraries are packaged with sample codes.  Import these libraries into your Eclipse projects.

Create your new project.  I assume that you are an Eclipse user and not new to android development.  Add these libraries to your project as a library project.  By adding these libraries to your new project, you are telling Android that you are using ActionBarSherlock and ViewPagerIndicator as part of your android project.

To check if libraries are added, go to Project Properties > Android.  It should look like this:


If you encountered an error like this, it's okay don't panic.
This tells that Eclipse detected android-support-v4.jar file in the dependecy list of your project twice but with different SHA-1 hash.  To fix this, just remove the file under <your_projectname>/libs/android-support-v4.jar

Now let's proceed to the fun part... the code.

layout xml.

    
    
    
    
    
    



MainActivity.java
package com.zipcerio.vpi;

import java.util.ArrayList;
import java.util.List;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.viewpagerindicator.TitlePageIndicator;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

public class MainActivity extends SherlockFragmentActivity {
 private ViewPager mPager;
 private TitlePageIndicator mIndicator;
 private MainPagerAdapter mAdapter;
 private List mFragments;
 
 private static final String FRAGMENT1 = Fragment1.class.getName();
 private static final String FRAGMENT2 = Fragment1.class.getName();
 private static final String FRAGMENT3 = Fragment1.class.getName();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.a_main);
  
  // add fragments
  mFragments = new ArrayList();
  mFragments.add(Fragment.instantiate(this, FRAGMENT1));
  mFragments.add(Fragment.instantiate(this, FRAGMENT2));
  mFragments.add(Fragment.instantiate(this, FRAGMENT3));
  
  // adapter
  mAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);
  
  // pager
  mPager = (ViewPager) findViewById(R.id.view_pager);
  mPager.setAdapter(mAdapter);
  
  // indicator
  mIndicator = (TitlePageIndicator) findViewById(R.id.title_indicator);
  mIndicator.setViewPager(mPager);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  return true;
 }
 
}


MainPagerAdapter.java
package com.zipcerio.vpi;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MainPagerAdapter extends FragmentPagerAdapter {
 private List mFragments;
 private String[] titles = new String[] {"FRAGMENT1", "FRAGMENT2", "FRAGMENT3"};
 private int mCount = titles.length;

 public MainPagerAdapter(FragmentManager fm, List f) {
  super(fm);
  mFragments = f;
 }

 @Override
 public Fragment getItem(int position) {
  return mFragments.get(position);
 }

 @Override
 public int getCount() {
  return mCount;
 }

 @Override
 public CharSequence getPageTitle(int position) {
  return titles[position];
 }
}


Fragment1.java
package com.zipcerio.vpi;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;

public class Fragment1 extends SherlockFragment {
 private ListView mList;

 @Override
 public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
  View v = inf.inflate(R.layout.f_fragment1, grp, false);
  mList = (ListView) v.findViewById(R.id.listView1);
  return v;
 }

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  ArrayAdapter adapter = new ArrayAdapter(
    getActivity(), android.R.layout.simple_list_item_1, Cheese.STRINGS);
  mList.setAdapter(adapter);
 }

}


AndroidManifest file


    

    
        
            
                

                
            
        
    




Finally, this is how it should look like.

See the source code here.

Credit goes to Jake Wharton for these great libraries.
Github: https://github.com/JakeWharton
Twitter: https://twitter.com/JakeWharton
StackOverflow: http://stackoverflow.com/users/132047/jake-wharton

Check CPU Information and Speed

To check CPU information and speed on your Ubuntu or any Linux distro, open the terminal and run this command:

$ cat /proc/cpuinfo

If you have other techniques and ways, please feel free to add it in the comments.