package com.david.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.content.SharedPreferences;
public class AndroidTestActivity extends Activity {
private static String TAG = "AndroidTestActivity";
MainThreadHandler mth;
MyThreadHandler myth;
private static String s = "i";
MyThread mt = null;
WindowManager m = null;
Display d = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "AndroidTestActivity.onCreate *BEGIN* pid: " + android.os.Process.myPid() +
", tid=" + android.os.Process.myTid());
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m = getWindowManager();
d = m.getDefaultDisplay();
SharedPreferences sp = this.getPreferences(MODE_PRIVATE);
int defValue = 0;
int temp = sp.getInt(s, defValue);
if (temp == 0){
sp.edit().putInt(s, 1).commit();
} else {
String tmp = null;
// tmp.charAt(1); // for test activity restart
}
// Log.i(TAG, "temp: " + temp);
Button btn = (Button)findViewById(R.id.button2);
btn.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent intent = new Intent(AndroidTestActivity.this, Activity2.class);
startActivity(intent);
return false;
}
});
// btn.setVisibility(View.GONE);
Animation ani = AnimationUtils.loadAnimation(this, R.anim.trans_anim);
btn.startAnimation(ani);
mth = new MainThreadHandler();
mt = new MyThread();
mt.start();
SystemClock.sleep(1);
Message msg2 = mth.obtainMessage(1);
mth.sendMessage(msg2);
Log.i(TAG, "AndroidTestActivity.onCreate *FINISH* pid: " + android.os.Process.myPid() +
", tid=" + android.os.Process.myTid());
}
class MyThread extends Thread {
Looper loop = null;
@Override
public void run() {
// TODO Auto-generated method stub
// super.run();
Thread.currentThread().setName("MyThread");
Looper.prepare();
loop = Looper.myLooper();
myth = new MyThreadHandler();
Log.i(TAG, "MyThread.run() before loop, MyThread name: " + Thread.currentThread().getName() +
", pid=" + android.os.Process.myPid() +
", tid=" + android.os.Process.myTid());
Looper.loop();
Log.i(TAG, "MyThread.run() after loop, MyThread name: " + Thread.currentThread().getName() +
", pid=" + android.os.Process.myPid() +
", tid=" + android.os.Process.myTid());
}
public Looper getMyLooper(){
return loop;
}
}
class MyThreadHandler extends Handler {
/**
*
*/
public MyThreadHandler() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param looper
*/
public MyThreadHandler(Looper looper) {
super(looper);
// TODO Auto-generated constructor stub
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
Log.i(TAG, "MyThreadHandler.handleMessage case 1, thread name:" + Thread.currentThread().getName() +
", pid=" + android.os.Process.myPid() +
", tid=" + android.os.Process.myTid());
TestClass t = new TestClass();
Object[] args = new Object[]{20, 30};
PerformSelector ps = new PerformSelector(t, "funcWithArgs", args, 5, this.getLooper(), true);
ps.perform();
}
super.handleMessage(msg);
}
}
class MainThreadHandler extends Handler {
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
Log.i(TAG, "MainThreadHandler.handleMessage case 1, thread name:" + Thread.currentThread().getName() +
", pid=" + android.os.Process.myPid() +
", tid=" + android.os.Process.myTid());
// Message msg1 = myth.obtainMessage();
// msg1.what = 1;
// myth.sendMessage(msg1);
//
// TestClass t = new TestClass();
// PerformSelector ps = new PerformSelector(t, "funcWithNoArgs", null);
// ps.setAsync(true);
// ps.setAfterDelay(0);
// ps.perform();
//
// Object[] args = new Object[] { 20, 30 };
// ps = new PerformSelector(t, "funcWithArgs", args);
// ps.setAsync(true);
// ps.setAfterDelay(5);
// ps.perform();
// ChatDialog dialog = new ChatDialog(AndroidTestActivity.this, R.style.dialog);
int x = (int)(d.getWidth()/2);
int y = (int)(d.getHeight()/2);
Log.i(TAG, "dispaly width=" + d.getWidth() + ", height=" + d.getHeight());
Log.i(TAG, "Position x=" + x + ", y=" + y);
// dialog.setPosition(10, 20);
// dialog.setRegion(150, 300);
// dialog.setCanceledOnTouchOutside(true);
// dialog.show();
TestDialog t = new TestDialog(AndroidTestActivity.this, R.style.dialog);
t.setLayoutResID(R.layout.chat_dialog);
t.setPosition(x/2, y);
t.setRegion(150, 200);
t.setCanceledOnTouchOutside(true);
t.show();
}
super.handleMessage(msg);
}
}
}