Saturday, February 25, 2012

[Java] Access modifier

Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Common Lisp and Emacs

Tập tành đú đởn.

Cài clisp
Cài emacs
Cài slime
Config 1 tí, nhớ đừng dùng vim đã auto closed :))

tạo folder:
mkdir ~/.slime

ghi dòng sau vào file ~/.emacs

;;; Lisp (SLIME) interaction
(setq inferior-lisp-program "clisp")
(add-to-list 'load-path "~/.slime")
(require 'slime)
(slime-setup) 
(global-font-lock-mode t)
(show-paren-mode 1)
(add-hook 'lisp-mode-hook '(lambda ()
      (local-set-key (kbd "RET") 'newline-and-indent))) 



Từ terminal gõ: emacs -nw
Meta-X slime

Chiến thôi :))

[Java] Assert and Exception

How to use?
Use assert /ə'sə:t/ as a comment, to TEST a comment
You should now use an assertion whenever you would have written a comment that asserts an invariant.
   if (i % 3 == 0) {
        ...
    } else if (i % 3 == 1) {
        ...
    } else {
        assert i % 3 == 2 : i;
        ...
    }
assert for test/debugging, not error-checking
  • use exceptions when checking parameters passed to public or protected methods and constructors
  • use exceptions when interacting with the user or when you expect the client code to recover from an exceptional situation
  • use exceptions to address problems that might occur
  • use assertions when checking pre-conditions, post-conditions and invariants of private/internal code
  • use assertions to provide feedback to yourself or your developer team
  • use assertions when checking for things that are very unlikely to happen otherwise it means that there is a serious flaw in your application
  • use assertions to state things that you (supposedly) know to be true
In other words, exceptions address the robustness of your application while assertions address its correctness.
Assertions are designed to be cheap to write, you can use them almost everywhere and I'm using this rule of thumb: the more an assertion statement looks stupid, the more valuable it is and the more information it embeds. When debugging a program that does not behave the right way, you will surely check the more obvious failure possibilities based on your experience. Then you will check for problems that just cannot happen: this is exactly when assertions help a lot and save time.

http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html
http://stackoverflow.com/questions/1957645/when-to-use-assertion-vs-exception

Friday, February 24, 2012

[Java] Static method

As defined, power is stateless and has no side effects on any enclosing class so it should be declared static.

Friday, February 10, 2012

[Android] Tổng kết kinh nghiệm làm việc với các loại View

1. Bạn muốn tạo OptionsMenu có khả năng thay đổi?
SOLUTION: sử dụng onPrepareOptionsMenu(Menu menu) thay vì onCreateOptionsMenu


2. Khi nào thì cần extend một View?
SOLUTION: có lẽ bạn chỉ cần 1 loại View mới khi hình ảnh của bạn thường xuyên thay đổi hoặc thật là quái dị :)) khác biệt với các View mà android đã cung cấp. Với những thao tác với 1 View bạn muốn làm như: di chuyển, thay đổi kích cỡ, độ trong suốt thì bạn vẫn chưa cần phải extend 1 View. 

Friday, February 3, 2012

onStart, onCreate.... trình tự chạy của các lệnh

Biết mỗi activity đều implement các method như onCreate, onStart... nhưng có chắc bạn nắm chính xác trình tự chạy của chúng. Hãy làm 1 bài test nhỏ như sau để kiểm nghiệm:

Tạo 1 activity chính, 1 activity 2. Ta implement các method
onCreate
onStart
onResume
onPause
onStop
onDestroy

và đặt log ở tất cả các method này trong cả 2 activity.

Chạy chương trình , bấm nút chuyển từ Activity chính sang Activity 2. Kết quả thu được như sau:

D/AndroidRuntime(  468): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime(  468): CheckJNI is ON
D/AndroidRuntime(  468): --- registering native functions ---
I/ActivityManager(   59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.aps.test/.OrderTestActivity }
D/AndroidRuntime(  468): Shutting down VM
D/dalvikvm(  468): Debugger has detached; object registry had 1 entries
I/AndroidRuntime(  468): NOTE: attach of thread 'Binder Thread #3' failed
D/dalvikvm(  436): GC_EXPLICIT freed 1421 objects / 94480 bytes in 1668ms
CHẠY CHƯƠNG TRÌNH
D/ACT1    (  436): onCreate
D/ACT1    (  436): onStart
D/ACT1    (  436): onResume

I/ActivityManager(   59): Displayed activity com.aps.test/.OrderTestActivity: 523 ms (total 523 ms)
I/ActivityManager(   59): Starting activity: Intent { cmp=com.aps.test/.Act2 }
BẤM NÚT CHUYỂN SANG ACTIVITY 2
D/ACT1    (  436): onPause
D/ACT2    (  436): onStart
D/ACT2    (  436): onResume

I/ActivityManager(   59): Displayed activity com.aps.test/.Act2: 391 ms (total 391 ms)
D/ACT1    (  436): onStop
W/KeyCharacterMap(  436): No keyboard for id 0
W/KeyCharacterMap(  436): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

THOÁT KHỎI ACTIVITY 2
D/ACT2    (  436): onPause
D/ACT1    (  436): onRestart
D/ACT1    (  436): onStart
D/ACT1    (  436): onResume
D/ACT2    (  436): onStop
D/ACT2    (  436): onDestroy 




THOÁT KHỎI CHƯƠNG TRÌNH (ACTIVITY 1)
D/ACT1    (  436): onPause
D/ACT1    (  436): onStop
D/ACT1    (  436): onDestroy


Thật quá rõ ràng ^^
PS: để lấy log ra file text trên linux: adb logcat -d > log

Code:
https://github.com/famihug/OrderCallWhenChangeActivity

Android SharedPreferences


Q: mỗi package có 1 defaultSharedPreferences?

A: Sai!
Cả 1 App chỉ có 1 defaultSharedPreferences.

Theo bài này:
http://stackoverflow.com/questions/5946135/difference-between-getdefaultsharedpreferences-and-getsharedpreferences

Thì mỗi ứng dụng có 1 defaultSharedPreferences với tên là tên của package ứng dụng + _preferences
(Chú ý: getPackageName() trả về tên của package của App - là package được khai báo trong file Manifest chứ không phải các package con)

public static SharedPreferences getDefaultSharedPreferences(Context context) {
    return context.getSharedPreferences( 
         getDefaultSharedPreferencesName(context),
        getDefaultSharedPreferencesMode());
}
private static String getDefaultSharedPreferencesName(Context context) {
    return context.getPackageName() + "_preferences";
}
private static int getDefaultSharedPreferencesMode() {
    return Context.MODE_PRIVATE;
}