2012年11月1日 星期四

Java - Static Inner Classes


Static Inner Classes

Consider the following Java code fragment:
public class A
{
    int y;
    public static class B
    {
 int x;
 void f () {}
    }
}
This fragment defines the class A which contains an static inner class B.
A static inner class behaves like any ``outer'' class. It may contain methods and fields, and it may be instantiated like this:
A.B object = new A.B ();
This statement creates an new instance of the inner class B. Given such an instance, we can invoke the f method in the usual way:
object.f();
Note, it is not necessarily the case that an instance of the outer class A exists even when we have created an instance of the inner class. Similarly, instantiating the outer class A does not create any instances of the inner class B.
The methods of a static inner class may access all the members (fields or methods) of the inner class but they can access only static members (fields or methods) of the outer class. Thus, f can access the field x, but it cannot access the field y.
----------------------
The inner class and outer classes could be considered as two separated classes and the inner class could access the static members of the outer class.


2012年10月28日 星期日

typename in C++

typename as we know in C++ is used in template.
The difference between typename and class is in the nested type.
Take the class sp in RefBase.h of Android Framework as example:


template <typename T>
class sp
{
public:
    typedef typename RefBase::weakref_type weakref_type;
    .........
}



class RefBase
{
public:
           .........
         class weakref_type
         {
          .........
          }
}
--------------------------
Notice that " typedef typename RefBase::weakref_type weakref_type; "
Compiler will  not know that if weakref_type is a variable of RefBase.  Compiler will know that weakref_type is a type or class of RefBase by giving typename .





2012年10月16日 星期二

Android -Google Maps (Webview)

Use WebView to show the map:

1.webView.getSettings().setJavaScriptEnabled(true);
 Open the JavaScript of the web.

2.webView.getSettings().setGeolocationEnabled(true);
 Open the geolocation for the web to get location through Javascript

PS:Use LocationManager to get the location from network or GPS.


Ref:

More info about Webview

2012年10月14日 星期日

Android bindService() notice

There are roughly two ways of using the service in Android,Context.startService() and Context.bindService().

Context.startService(): can be used only by one application.
Context.bindService(): could be exposed to other applications.

Here we focuse on Context.bindService().

bindService() -> onCreate() -> onBind() ->onServiceConnected()-> running.

There is one thing we have to notice.

The onServiceConnected() may not be called immediately and this might cause some problem!


Ref:
http://stackoverflow.com/questions/2486692/onserviceconnected-never-called-after-bindservice-method



Android - Animation


(1) tweened animation
1.alpha
2.scale
(2)frame by frame
1.translate
2.rotate
----------------
Using by two ways:
1.XML

Animation myAnimation= AnimationUtils.loadAnimation(this,R.anim.myanimationxml);

2.Non-XML

Animation myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);

Reference from:
http://jjnnykimo.pixnet.net/blog/post/30359165-android-amimation


2012年5月21日 星期一

Linux C Select() - multi input trace


fd_set set;
    FD_ZERO(&set);      /*clear set */
    FD_SET(fd, &set);   /*add fd into set */
    FD_CLR(fd, &set);   /*remove fd from set*/
    FD_ISSET(fd, &set); /*return true if fd is in set*/
------------------------------------------------------------------------
int maxfd = MAX (fd1, fd2)+1;

struct timeval tv;
tv.tv_usec = 0;
tv.tv_sec = 2;


while (1) {
     FD_SET(fd1, &readfs);  /* input 1*/
     FD_SET(fd2, &readfs);  /* input 2 */
     /* block until input becomes available */
     int rul = select(maxfd, &readfs, NULL, NULL,  &tv );


     if (FD_ISSET(fd1))         /* if input 1 has signal*/
       handle_input_from_source1();
     if (FD_ISSET(fd2))         /* if input 2 has signal */
       handle_input_from_source2();
/*
     if(rul == -1)
         return errorout;
     if(rul == 0)
        return timeout;
     
*/
   }

2012年5月20日 星期日

C address pointer scope notice

Define:
(A file) : char * tmp
(B  file): function(char *ptr)
             {
                  ptr = open("/tmpfile.txt","w");
             }
--------------------------------------------
(A file)  code:   call (B file)function( tmp )
                        fclose( tmp )            ==> address is null;
             
=============================
the pointer return from open("/tmpfile.txt","w") exist only in (B  file): function(char *address).

C fopen()

#include <stdio.h>
FILE *fopen(const char *restrict filename, const char *restrict mode);


mode is listed as following:

r or rb
Open file for reading.
w or wb
Truncate to zero length or create file for writing.
a or ab
Append; open or create file for writing at end-of-file.
r+ or rb+ or r+b
Open file for update (reading and writing).
w+ or wb+ or w+b
Truncate to zero length or create file for update.
a+ or ab+ or a+b
Append; open or create file for update, writing at end-of-file


reference from http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html

2012年1月18日 星期三

Character alignment

To put a string at a certain position, I wrote a simple java method.


Ex: firstSegLength = 20 , a = stringA, b = stringB
      Output =stringA             stringB 


There are 13 space character between stringA and stringB.


public String string_alignment(int SegLength,String a,String b)
  {
String output = null;
int len = a.length();

//Error check
if((SegLength < 1) || (a == null) || (b == null))
return null;
if(a.length() > (len - 1))
{
//should not be happened, case handle
}
int totallen = SegLength + b.length();
int sepLen = SegLength - len;
StringBuilder workstr = new StringBuilder();
//append first string
workstr.append(a);


//create space string
char seprator[] = new char[sepLen];
for(int i = 0;i < sepLen; i++)
{
seprator[i] = ' ';
}
        //append the second string
workstr.append(seprator);
workstr.append(b);
output = workstr.toString(); 
return output;
  }

2012年1月17日 星期二

Use Intent To pick system contact data

Call android to pop out an list for user to pick up a system contact data.
startActivityForResult(new Intent(Intent.ACTION_PICK, 
 android.provider.ContactsContract.Contacts.CONTENT_URI),user-defined);


System may do the following process.
Button pickButton=(Button)findViewById(R.id.pick_button);
pickButton.setOnclickListener(new View.OnClickListener(){
  public void onClick(View view){
                //Store the URI of the selected item
    Uri data=Uri.parse(“content://horses/”+ selected_id);
    Intent result=new Intent(null,data);
    result.putExtra(IS_INPUT_CORRECT,inputCorrect);
    result.putExtra(SELECTED_PISTOL,selectedPistol);
    setResult(RESULT_OK,result);
    finish();
  }
});


Return the result to activity
protected void onActivityResult (int requestCode, int resultCode, Intent data) 
{
              //Abstract data from intent
             Uri uriRet = data.getData()
             boolean inputCorrect=data.getBooleanExtra( IS_INPUT_CORRECT,false);
    String selectedPistol=data.getStringExtra( SELECTED_PISTOL);
}

2012年1月12日 星期四

2012年1月11日 星期三

Creativity

Creativity seems to be an important thing in this fast changing world.
How to improve my creativity? Well, I do not have a good answer for it.
But I think if one knows lot of things and knowledge, he should be more creative.
It is because that he has lot of based points in his brain and it is easier for him to connect any two points in his brain.  A connection of two or more points may be a creativity.
Creative could also be produced from discontent or inconvenience.
Anyway, creativity is really important.


Suffering

Again,it is suffering.
I know that i am not rich enough. But,i know i am not that bad. I have great potential and possibility. This homeless feeling is so bad. The place which you live is not yours. Just like a leaf moves along with the wind. I don`t know where to rest.

2012年1月10日 星期二

Working Holiday

Here is the table of the related information for working holiday.
I want to live abroad and feel the different culture.  
But there are much things laid on my shoulder.


國家期間名額資格簽證費受理單位備註
年齡存款規定
(至少)
紐西蘭1年60018-30歲4,200紐幣120
紐幣
★紐西蘭商
工辦事處
同一雇主不可
受雇超過3個月
課程:一次最
長不超過3個月
澳洲1年不限18-30歲5000澳幣270
澳幣
★澳大利亞
商工辦事處
可申請2次
工作:同一雇
主不可受雇超
過6個月
課程:一次最
長不超過4個月
日本1年200018-30歲持有足以
維持停留
日本最初
期間之生
活所需費
用。
1100
台幣
★日本
交流協會
分兩階段申請
6月12月
課程:一次不
超過3個月
韓國1年40018-30歲3000美元免費★駐韓國
台北代表部
專門職不得
申請(醫師.
律師.教授)
加拿大1年100018-35歲2,500加幣150
加幣
★加拿大駐
台北貿易辦
事處
打工度假950人
建教25人、
專業青年25人
德國1年20018-30歲2000歐元60
歐元
★德國在
台協會
同一雇主不可
受雇超過3個月
英國2年100018-30歲1600英鎊9300
台幣
★英國貿易
文化辦事處
可從事全職工
作,可上任何
課程 需至
青輔會申請
有效贊助者
證明(certificate 
of sponsorship)

2012年1月9日 星期一

Divide & Combine File -- SequenceInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.SequenceInputStream

Example of divide a file:
      filepath: the file to be separated.
      size:the size of each separated file.
      count: the number of separated file.

FileInputStream fileInputStream = new FileInputStream(new File(filepath)); 
BufferedInputStream bufInputStream = new BufferedInputStream(fileInputStream); 
byte[] data = new byte[1]; 
int count = 0;  
//Count the number of separated files.
if(fileInputStream.available() % size == 0) 
      count = fileInputStream.available() / size; 
else 
      count = fileInputStream.available() / size + 1; 
for(int i = 0; i < count; i++) { 
      int num = 0; 
      File file = new File(filepath + "_" + (i + 1));
      BufferedOutputStream bufOutputStream = new BufferedOutputStream( 
                       new FileOutputStream(file)); 
      //Write the read-out data into a separated file
      while(bufInputStream.read(data) != -1) { 
             bufOutputStream.write(data);
             //count the bytes of the separated file.
             num++; 
             if(num == size) { 
                  bufOutputStream.flush(); 
                  bufOutputStream.close(); 
                  break; 
             } 
     } 
 
     if(num < size) { 
            bufOutputStream.flush(); 
            bufOutputStream.close(); 
     } 
} 
Example of combine separated files:
      filename: the path of  the separated files locate   
      number  : the number of the separated files.
List<InputStream> list = new ArrayList<InputStream>();
//collect the separated files
for(int i = 0; i < number; i++) {
       File file = new File(filename + "_" + (i+1));
       list.add(i, new FileInputStream(file));
}
final Iterator<InputStream> iterator = list.iterator();
Enumeration<InputStream> enumation = new Enumeration<InputStream>() {
                public boolean hasMoreElements() {
                    return iterator.hasNext();
                }

                public InputStream nextElement() {
                    return iterator.next();
                }
            };
BufferedInputStream bufInputStream = new BufferedInputStream( 
                    new SequenceInputStream(enumation), 
                    8192); 
BufferedOutputStream bufOutputStream = new BufferedOutputStream( 
                       new FileOutputStream(filename), 8192); 
byte[] data = new byte[1]; 

while(bufInputStream.read(data) != -1) 
        bufOutputStream.write(data); 
bufInputStream.close(); 
bufOutputStream.flush(); 
bufOutputStream.close(); 

Ideas

I have lots of ideas.  But i do not implement them.  I should try to realize them i think.

Paint my love - Michael Learns to Rock

From ICRT, i heard the song "Paint my love" by Michael Learns to Rock today.
Once i heard this song,I knew that it must be Michael Learns to Rock.
Their songs have special feeling for me,such as "That`s why you go away" and "Sleeping Child".

Here is the lyric of "Paint my love".
==============
From my youngest years
till this moment here
I've never seen
such a lovely queen

From the skies above
to the deepest love
I've never felt
crazy like this before

Chorus:
Paint my love
you should paint my love
it's the picture of a thousand sunsets
it's the freedom of a thousand doves
Baby you should paint my love


Been around the world
then I met you girl
It's like comming home
to a place I've known

Chorus:
Paint my love
you should paint my love
it's the picture of a thousand sunsets
it's the freedom of a thousand doves
Baby you should paint my love

Since you came into my life
the days before all fade to black and white
Since you came into my life
Everything has changed

Mood of Love

It has been 2 more months since we separated.  I have changed a lot. 
And the changes are not for you,but for me.  Now, it seems that you 
don`t believe the new me.  Maybe the changes are too fast and large 
for you to believe.  You may need time to see whether the changes is 
real inside me  or just it is temporarily.   Without you, everyday is 
suffering.