2012-05-18

A Smater Way to Convert MotionEvent#getRawY() into Relative

I faced a kind of problem. The problem is that MotionEvent#getY() doesn't returns expected values, while MotionEvent#getRawY() keeps returning expected, sequential positions. So I tried to use MotionEvent#getRawY() instead of MotionEvent#getY(). However, values getRawY() sent are based on the absolute position of displays. Some sites show hacks to exclude unnecessary height of, such as the status bar. But the heights of the bars are not same on all platforms and sometimes the bars are even got hidden.

From Android API Level 1(This is not my mistake, indeed from level ONE!), the View class provide getLocationOnScreen(int[]) method. Using this method, you can convert the result of MotionEvent#getRawY() into relative to your views.

class MyView implements OnTouchListener
{
 @Override
 public boolean onTouch(View pmView, MotionEvent pmEvt)
 {
  int[] cViewPosXY = new int[2];
  getLocationOnScreen(cViewPosXY);
  int cTouchX = cEvt.getRawX() - cViewPosXY[0];
  int cTouchY = cEvt.getRawY() - cViewPosXY[1];
 }
}

2012-05-06

BitmapFactory.decodeStream(InputStream)

I desired to generate a new bitmap image with a subclass of java.nio.Buffer for some reason.
I found a simple way to convert Buffer into InputStrem in a post on StackOverFlow titled "java - Wrapping a ByteBuffer with an InputStream"(http://stackoverflow.com/questions/4332264/wrapping-a-bytebuffer-with-an-inputstream). However, I encountered a NPE with the below message on my logcat after integrating my own wrapping ByteBuffer class.
SkImageDecoder::Factory returned null

I investigated this problem and found an essential reason of the message.

2012-04-11

Does The Monkey Tool Launch Not Your Android App But Other Apps?

Someday I faced a problem with my monkey test. The problem was that the monkey command didn't launch my app specified with -p option. At that time, the monkey command launch other apps like alarm which I didn't specified at all.

However, the monkey command launched my app correctly when I specified -p option in the first argument of the monkey command.

$ adb shell
$ monkey -p your.app.package -v 1 ...

I don't know why, but you should specify your package name with -p options in the first argument.

2012-04-06

Get layout values in onCreate method. And Speed up Activity loading times.

Activity#onCreate() is the one of most fundamental methods in Android app creation. However there are some difficulties for this method. For example, you can't obtain layout values of views because layout values are not fixed until the codes in onCreate() methods is done.

But few hacks are left to get layout values in the methods.

The hack is to use IdleHandler (see the IdleHandler description in Android SDK's page). With using IdleHandler, you can run your code after the Android systems finish running current tasks which the UI thread holds.

This hack is also useful to speed up activity loading times, because tasks defined in IdleHandler would be processed after the screen is displayed.

@Override
  public void onCreate(Bundble savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    ...


    Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler()
      {
        @Override
        public boolean queueIdle()
        {
          HEIGHT_OF_MENU_RELATIVE_LAYOUT = findViewById(R.id.main_rlMenu).getHeight();
          return false;
        }
      }
    );
  }
      

2012-03-17

Doesn't your Android app show the default volume control bar?

The above screenshot is taken with a developing version.

Are you troubled with the volume control in your Android app? You're my friends ;-)

I also faced this problem and took a lot of time to fix it. I doubt openal or GLSurfaceView causes this problem. So I try to investigate on the Android source code to know what prevents displaying the default volume control.

But I gave up researching, because the audio management logic of the Android source code is message-based and I found it would take a lot of times to identify the essential factor. In addition to that, I often face this kind of problem even in apps without NDK nor OpenGL.

Then, I found a simple way to display the default volume control. This is just to write explicitly how to handle the volume button events and adjust the volume level manually. It is important to use the flag FLAG_SHOW_UI when adjust the level with AudioManager#setStreamVolume method.

Below is the code, which worked fine for me.

2012-02-27

Avoid removals of last columns in redirection of top command

Once a day I tried to redirect a result of top command in batch mode (-b option) with crontab. At that time I toggled many top columns to observe my server machine. But some columns, which are shown the right side of top screen, are not outputted to the specified redirection files.
I investigated this problem and found an essential logic of the top command.
The top command refer to the shell variable of COLUMNS and it regulate the column chars of output. So I adjusted COLUMNS value and I got whole columns successfully in a redirected file.

If you excuse redirection of the top command in batch mode, you should do the following before excuse the top command.
*/10 * * * * export COLUMNS=n;top -b -n 1 >......

2012-01-09

How to deal with the authorization results of Facebook Android SDK

In the last release of my app "Jyoya Bell", I implemented a Facebook connection with using Facebook Android SDK.
The framework contains two way of login sequences, one is Single Sign-On(SSO) and the other is dialogs with WebView.
Both are so useful and are enabled by default (SSO have first priority. If end users have already installed Facebook app, the SSO sequence will start).
However, from implementing point of view, there are some difficulties. The difficulties is that authorization result is not constant.
The following introduce how I dealt with the results from both SSO and WebView.