2012年9月26日 星期三

[AndroidDev] New resources selector (>= API 3.2)

Screen density dpi (dots per inch)
ex: 相同 physical area "low" density 會比 a "low" density screen "normal" or "high" density screen 較少 pixel。

dp (density-independent pixel)
  • The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen
  • The baseline density assumed by the system for a "medium" density screen (160 dpi).
  • px = dp * (dpi / 160)
    • ex: on a 240 dpi screen, 1 dp equals 1.5 physical pixels.
  • scaling ratio
    • 3(ldpi) : 4(mdpi) : 6(hdpi) : 8(xhpi)
    • ex: 480 x 800 hdpi/1.5 -> 320 x 533 mdpi in dp's

Screen size
  • Supported from 1.6.
  • 是系統依照 pixel and density 去算出長寬的 dp 然後對應所得。
  • Resolustion convert to dp presentaion of mdpi and decide its size (small, ..)。
  • 分類:
    • xlarge screens are at least 960dp x 720dp. (>= 2.3)
      • approximately-10” tablets, ex: Motorola Xoom
    • large screens are at least 640dp x 480dp.
      • ex: Dell Streak and original 7” Samsung Galaxy Tab
    • normal screens are at least 470dp x 320dp.
    • small screens are at least 426dp x 320dp.
      • Android does not currently support screens smaller than this.
  • 將裝置 dpi 轉至 mdpi,對應上表,長寬最接近且不小於標準之分類。ex:
    • A QVGA screen is 320x240 ldpi.
      • ldpi * 4/3 = mdpi
      • Result: 426dp x 320dp => small
    • The Xoom is a typical 10” tablet with a 1280 x 800 mdpi screen.
      • Result: 1280 x 800 => xlarge
    • The Dell Streak is a 800 x 480 mdpi screen.
      • Result: 800 x 480 => large
    • A typical 7” tablet has a 1024 x 600 mdpi screen.
      • Result: 1024 x 600 => large
      • But!! Exception "Samsung Galaxy Tab"
      • Samsung Galaxy Tab 1024 x 600 7” 應該是為 large,但因為 device configure 視為 hdpi 所以 hdpi * 2/3 = mdpi => 682dp x 400dp => normal!! 所以它應該是 large 但系統會將其視為 normal。
      • This was a mistake in the framework’s computation of the size for that device that we made. Today no devices should ship like this....

新增於 3.2 的屬性:
  • width dp
    • 現在螢幕方向的寬(dp),也就是當方向改變,寬也會隨之改變。
    • the current width available for application layout in “dp” units; changes when the screen switches orientation between landscape and portrait.
  • height dp
    • 現在螢幕方向的高(dp),也就是當方向改變,寬也會隨之改變。
    • the current height available for application layout in “dp” units; also changes when the screen switches orientation.
  • smallest width dp
    • 螢幕高與寬的最小值,此值固定,不會因為螢幕方向改變而改變。
    • the smallest width available for application layout in “dp” units; this is the smallest width dp that you will ever encounter in any rotation of the display.

Typical numbers for screen width dp are:
  • 320
    • a phone screen 
    • 240 x 320 ldpi, 320 x 480 mdpi, 480 x 800 hdpi, etc
  • 480
    • a tweener tablet like the Streak 480 x 800 mdpi
  • 600
    • a 7” tablet 
    • 600 x 1024
  • 720
    • a 10” tablet
    • 720 x 1280, 800 x 1280, etc


Resources Selector Examples

限定使用資源的最小寬度(smallest-width),如下:
res/layout/main_activity.xml           # For phones
res/layout-sw600dp/main_activity.xml   # For 7” tablets
res/layout-sw720dp/main_activity.xml   # For 10” tablets
Android 會選擇最接近但不大於的自身寬度的資源,如 700dp x 1200dp tablet 會使用 layout-sw600dp 裡的資源。


限定使用資源的寬度(width),假設你想直向時只顯示某區塊,而橫向時因為寬度較大,想顯示多點內容,可以使用這方式,如下:
res/layout/main_activity.xml          # Single-pane
res/layout-w600dp/main_activity.xml   # Multi-pane when enough width

smallest width < 600 時使用 layout; 大於 600 則會依據方向所在的寬使用 layout-sw600dp 或 layout-sw600dp-w720dp:
res/layout/main_activity.xml                 # For phones

res/layout-sw600dp/main_activity.xml         # Tablets

res/layout-sw600dp-w720dp/main_activity.xml  # Large width



// 或是寫成 layout-sw600dp-port,表示直向時使用此

res/layout/main_activity.xml                 # For phones

res/layout-sw600dp/main_activity.xml         # Tablets

res/layout-sw600dp-port/main_activity.xml    # Tablets when portrait


和舊的 qualifier 一同使用,如下:
// 使用 layout-swNNNdp 比 layout-port 好
res/layout/main_activity.xml                 # For phones
res/layout-land/main_activity.xml            # For phones when landscape
res/layout-sw600dp/main_activity.xml         # Tablets


相容性寫法

Type 1 - Using new qualifiers with old qualifiers
new qualifiers(ex: layout-sw600dp) 會被舊版本忽略,所以以下寫法能 work:
res/layout/main_activity.xml           # For phones
res/layout-xlarge/main_activity.xml    # For pre-3.2 tablets
res/layout-sw600dp/main_activity.xml   # For 3.2 and up tablets

Type 2 - Using new qualifiers with old qualifiers for different layouts
利用 values selector 使用相同名稱指向不同 layout:

res/layout/main_activity.xml           # For phones
res/layout/main_activity_tablet.xml    # For tablets


res/values-xlarge/layout.xml
res/values-sw600dp/layout.xml

res/values-sw600dp/layout.xml:

    
            @layout/main_activity_tablet
    


Type 3 - Select in code
在程式中判斷螢幕大小決定 layout:
public class MyActivity extends Activity {
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate();

        Configuration config = getResources().getConfiguration();
        // config.smallestScreenWidthDp: >= API 3.2
        if (config.smallestScreenWidthDp >= 600) {
            setContentView(R.layout.main_activity_tablet);
        } else {
            setContentView(R.layout.main_activity);
        }
    }
}


* Reference
- Android Developers Blog: New Tools For Managing Screen Sizes
- Supporting Multiple Screens

2012年9月15日 星期六

[Screen] Sensor-on-lens

Sensor-on-lens
  • 傳統螢幕
    • 由三層組成,最底層是顯示層 (Display),中間是感應層 (Sensor),而最上是鏡片層 (Len)。
    • 這三層就組成了觸控屏幕,既可顯示影像,亦可感應手指觸控。
  • 而 Sensor-on-lens 就是將感應器整合在鏡片內,螢幕就由兩層所組成。
  • 優點
    • 可讓螢幕變得更薄。
    • 提升了畫面的清晰度。
    • 在光線下影像會較明亮。
    • user 使用起來會更覺 “貼手”。


* Reference
- Sony Mobile

[JavaScript] Regular Expression syntax

RegExp(pattern [, flags])
/pattern/flags

  • pattern
    • The text of the regular expression.
  • flags
    • If specified, flags can have any combination of the following values:
  • g
    • global match
    • 找全部。因為 REGEX 預設碰到第一個符合的內容就結束比對。
  • i
    • ignore case 勿略大小寫。
  • m
    • Treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)


* Reference
- RegExp | MDN
石頭閒語:Regular Expression (RegExp) in JavaScript - 樂多日誌

[Android] Memory Cache

Reference
  • reference 的可以分為多種強度,我們最熟悉和最常使用的是 strong reference。
  • Four different degrees of reference strength: strong, soft, weak, and phantom.
  • 強度的差異在於 reference 和 garbage collector 間的互相影響關係。

Strong Reference
// buffer 就是 strong reference
StringBuffer buffer = new StringBuffer();
  • 當 reference 指向一個以上的物件,便是 strong reference。
  • 可以防止此 reference 被 GC。
  • 缺點:
    • Memory: 因為使得物件不能被回收,所以有造成 memory leak 的危險。
    • Caching: 假設在 application 中應用到許多大圖片,為了一直 reload 而 cache 圖片,所以 always 會有 reference 指向圖片,使得它在 memory 且不會被回收,也就是說我們得決定它是否該被移除且使其可以回收。

WeakReference
weak reference 則是隨時有可能已被回收。
// API 使用方式如下,建立 Widget 的 WeakReference。
WeakReference weakWidget = new WeakReference<widget>(widget);
// 取出 Widget object。
// 注意: 因為物件可以被回收,所以可能會得到 null。
weakWidget.get()
WeakHashMap
  • 作用類似 HashMap,只是 key 是使用 weak reference,若 key 變成 garbage 則它的 entry 便自動會被移除。
  • 但並不是你啥也也沒做他就能自動釋放,而是你使用到 value 才會被釋放。
  • super(key, queue); 只有 key 才是 weak reference,value 仍是 strong reference,所以 System.gc() 後,key 會被清除,value 則是才被視為 weak reference 然後到被調用到了才會被清除。

Reference queues
  • 當 WeakReference 回傳 null 時表示指向的物件已被回收,WeakReference object 也無用了,所以如 WeakHashMap 會去移除這類的 WeakReference object,避免 WeakHashMap 一直成長,其中卻有很多無用的 WeakReference 。
  • ReferenceQueue 可以幫助你管理這些 dead reference。
    • 如果你將 ReferenceQueue 傳入 weak reference's constructor,當有 reference 指向的物件被回收時,該 reference 會自動被 insert 到 ReferenceQueue 中,所以你可以定期清裡 ReferenceQueue 也就會清理了 dead reference。

Soft references
  • 類似於 weak reference,差異是比 weak reference 不易被回收。
  • 因為 soft reference 會盡量被保留在 memory 所以適合做 image cache。

Phantom References
  • Its get() method always returns null.
  • 用處:
    • 可以讓你決定何時將物件從 memory 移除。ex: manipulating large images,你可以先確定現在這一張已被移除,再載入下一張以避免 OOM。
    • 避免 finalize() method 可以 "resurrect" 建立了 strong reference 的 object。
      • override finalize() 的物件至少要被視為 GC 對象兩次以上才能被回收到,而不能及時的被回收,也因此會有多個待回收的 garbage 存在著等著被回收。

Android itself employs some caching as part of the resources infrastructure, so you are already getting the benefit of memory caching.

使用 bitmap 時,記得使用 recycle(),可以幫助釋放空間,所以若是再呼叫 getPixels() or setPixels(),可能會得到 exception,所以建議當不會再使用到 bitmap 時再使用。

LruCache
  • API level 12 (3.1)
  • Support Library back to API level 4 (1.6).
  • 管理與 cache 物件,呼叫 get(key) 取出物件,若物件不存在則會呼叫 create() 產生物件,並將新產生的物件加入 head of cache 再將其回傳。
  • 加入新物件時,cache 會檢查是否超過指定的大小,若超過則會刪除最後一筆 (tail of cache list)。

// override 這個 method 可以讓我們決定 cache 中每個物件 size 怎麼計算,default returns 1。
@Override
protected int sizeOf( String key, Bitmap value )
{
    return value.getByteCount();
}

Note:
Bitmap#getByteCount()
  • Supported from API Level 12。
// 建構子中則傳入想預設此 cache 的大小,這裡設定最大為 5 M。
// 所以一旦超過 5 M,物件會從最後一直被移除直到總大小小於 5 M。
public LruMemoryCache(Context context)
{
    super( 5 * 1024 * 1024 );
    this.context = context;
}

為避免發生 OutOfMemoryError,在系統呼叫 onLowMemory() 時建議呼叫 LruCache#evictAll() 移除所有元素再重建。

Note:
  • onLowMemory() 會被呼叫是因為整個系統的空間不足而不是因為你的 App 所用的空間已不足!
    • 所以等到此 method 被呼叫時已來不及,更好的方式是在 App 執行一開始便設定適當的大小限制給它。
    • ActivityManager#getMemoryClass() (API Level 5) 可以取得該 application 在 該 device 中的分配空間:
      • 單位為 megabytes。
      • 預設為 16 M,空間大點裝置可到 24 或更高。

也可以利用 LruCache 來管理在 SD card 中的檔案大小:
  • 儲存 File object 在 LruCache(override create())。
  • 以 file.length() 計算檔案大小 (override sizeOf())。
  • 移除物件時刪除 SD card 中的檔案 (override entryRemoved())。


* Reference
- Understanding Weak References | Java.net
- Styling Android » Blog Archive » Memory Cache – Part 1
- Styling Android » Blog Archive » Memory Cache – Part 2
- Styling Android » Blog Archive » Memory Cache – Part 3
- Styling Android » Blog Archive » Memory Cache – Part 4
- WeakHashMap的神话 - - ITeye技术网站
- WeakHashMap是如何清除不用的key的

2012年8月27日 星期一

[Java] Serialize

  • 以字串型態傳遞物件。
  • 不能跨平台。
  • 像物件的 version control id。
  • 當物件屬性改變此 id 就會改變。
  • 當 B side 接收到 Serialize id,會根據此 id 去 new 相對應的物件將值設定進去。

[Spring] enum type attribute

// 如果物件中有 enum type 的屬性。
public class User {
    private MyEnumType name;

    ...... 
}

<%-- 
    JSP 中 form 欄位的 value 必須放入 MyEnumType 才能對回物件。
    使用 List options
--%>
.....

2012年8月26日 星期日

[Spring] Type not matched when String is "" and is tried to convert to date

ERROR MESSAGE
Type not matched when String is "" and is tried to convert to date


Solution

因為 form 中的 date 為 "" (空字串) 因為要轉成 Date 時會出錯。

@InitBinder
protected void initializeBinder (WebDataBinder binder)  {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd" );
    dateFormat.setLenient(false);
    CustomDateEditor dateEditor = new CustomDateEditor(dateFormat, true);

    binder.registerCustomEditor(Date. class, dateEditor);
    binder.registerCustomEditor(ModelEntity.class, customPropertyEditor);
}

2012年8月25日 星期六

[JPA] @ModelAttribute("xxx")

// entity: request 中的值對設定到物件中。
@RequestMapping(...)
public void methodName(@ModelAttribute("model") EntityType entity) {}



---- Update on 2013/08/25 ----

// @ModelAttribute("xxx") 會建立 EntityType 的物件,並且將此物件放入 request.
@ModelAttribute("model")
public Object prepare(...) {
    Object model = new EntityType();
    return model;
}


[Android] MSISDN, ICCID, IMEI, IMSI

下列值在以下情況會被改變或是取不到正確值:
  • ICCID
    • 換了 SIM card
  • IMEI
    • 山寨機
  • ANDROID_ID
    • 恢復原廠設定


Related
MSISDN, ICCID, IMEI, IMSI

[ORM] Date and Timestamp

DB 中時間的 type 有:
  • Date
  • Timestamp

對應到 java 中的 type 皆是宣告為 Date

DB 都是收到相同的值 然後再依欄位 type 決定存入的值

例如若是 date 會被 truncate 掉 millisecond 後再存入

[Animation] Property Animation (>= 11)

Supported from API Level 11.

Property Animation
  • A property animation changes a property's (a field in an object) value over a specified length of time.
  • 以某項 attribute 進行動畫。ex: x-axis 使物件橫向移動。
  • 分為兩部份:
    • 計算產生動畫的值。
    • 設定動畫值於物件的 property 中。

  • The view animation system
    • View animation 是藉由重繪 view 來顯示動畫,而此動作是由每個 view 自己的 container 來做(because the View itself had no properties to manipulate)。
    • 雖然結果可以看到 view 出現在不同位置,但實際上沒有改變到 view object 的任何 property,所以關於 view object 的設定卻還停留在原位 (ex: 點擊原始位置才會觸發 view 的事件)。
  • The property animation system
    • Property animation 則是改善了這個問題,因為它是改變 view object 的 property 來顯示動畫。
    • 而當 view object 的 property 改變,會自動呼叫 invalidate() 。

TypeEvaluator
  • 決定如何去計算進行動畫的 attribute 的值。
  • An interface that allows you to create your own evaluator.
  • If you are animating an object property that is not an int, float, or color, you must implement the TypeEvaluator interface to specify how to compute the object property's animated values.
  • You can also specify a custom TypeEvaluator for int, float, and color values as well, if you want to process those types differently than the default behavior.

ValueAnimator
  • Keeps track of your animation's timing.
  • How long the animation has been running.
  • The current value of the property that it is animating.
  • 管理 object 動畫中的 attribute 在動畫過程中的值,包含資訊:
    • 是否重複動畫。
    • 接收更新動作通知的 listeners。
    • 使用客制化的 evaluator。
  • 但 ValueAnimator 並不會將計算後的值設給 property,因此要使用 listeners 接收計算後的值再自行去更新,例如需實作 ValueAnimator.AnimatorUpdateListener

ObjectAnimator
  • A subclass of ValueAnimator.
  • 可以設定發生動畫的物件以及決定動畫內容的 property。
  • 此 class 會更新 property 的值,所以大部份使用此 class 比較方便,不需自行處理更新這部份動作。

AnimatorSet
  • 設定動畫效果於一群 view,他們能一起變化、有序的或是延遲的。
  • Provides a mechanism to group animations together so that they run in relation to one another. You can set animations to play together, sequentially, or after a specified delay.


* Reference
- Property Animation | Android Developers

2012年7月1日 星期日

[Spring] Mapping among jsp, controller and DB

// === @ModelAttribute ===
@ReuestMapping(value=...)
public String m(
    @ModelAttribute(ATTRIBUTE_MODEL) MyObject myObject) {
    
    // 若 MyObject 前有宣告 @ModelAttribute(ATTRIBUTE_MODEL)
    // 會受 prepare() 中對 MyObject o 操作的影響
    // 若沒有宣告 則仍單純是 form bean

}

@ModelAttribute(ATTRIBUTE_MODEL)
public Object prepare() {
    // 進入 m() 前會先經過這裡
    MyObject o = new MyObject();
    return o;
}


// === URL Mapping ===
@RequestMapping(value = URL_MAPPING, method = RequestMethod.POST)
public String m(
    @RequestParam(value = ATTRIBUTE_ID, required = true) BigDecimal id) {
    // 除了 RequestMapping 中的 value 要 mapping 到以外
    // 若 RequestParam 中 required = true,request 中也必須帶有此 attribute
}


// === Mapping enum object to DB ===
// DB 內儲存的是 NAME1
// @Enumerated 便會儲存 MyObject 所對應的 name
@Enumerated (EnumType.STRING)
public MyObject getMyObject() {}

enum MyObject {
    NAME1("value1");
}


// DB 內儲存的是 DB_VALUE
// 從 DB 取出後 可藉 valueOfCode 取得相對應 MyObject
enum MyObject {
    NAME1("DB_VALUE");

    private static volatile Map<String, MyObject> registry = new HashMap<String, MyObject>();

    // Use DB_VALUE be the key of Map
    // put value to Map and use key to get MyObject
    public static MyObject valueOfCode (String code) {
        if(0 == registry.size())  {
             synchronized ( registry) {
                 if(0 == registry.size())  {
                     for(MyObject o: MyObject.values())  {
                         registry.put(o. code, o);
                     }
                 }
              }
        }
            return registry.get(code);
        }
    }

[Apache] Directory setting

httpd.conf
Alias /virtualUrl /RealUrl


    AllowOverride None
    Options None
    Order allow,deny
    Allow from all


2012年6月30日 星期六

[Spring] Spring Expression Language (SpEL)

Assume get this value from DB #{#root["valueB"]}/path
// Using Expression parser, valueB should be found from DB, too.
Expression expression = parser.parseExpression(property.getValue(), 
    new TemplateParserContext());
property.setValue(expression.getValue(context, String.class));


* Reference
- Spring Expression Language (SpEL)

2012年6月22日 星期五

[Web] Brief notes


  • slf4j
    • 在 jar 檔時就 binding,比較好切換。
  • framework
    • 提供 pattern 的模組
  • Spring framework
    • 本身是個大 factory,將工作交給底下的 factory...
  • Struts2
    • #attribute 作用約是 ${attribute}
  • iBatis
    • $var$
      • paraClass 中的 attribute
    • #fieldName:fieldType#
      • Table field
  • HTML
    • 由上而下 compiler,結果會是 DOM tree 結構。
    • DTD
      • 描述 HTML syntax
    • <meta>
      • 提供和 browser or search engine 相關的訊息。
      • ex: 描述文檔的內容。