顯示具有 tmp 標籤的文章。 顯示所有文章
顯示具有 tmp 標籤的文章。 顯示所有文章

2011年10月9日 星期日

[Java] Copying list to linkedList

Error Message

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0

occurred when copying list to linkedList


Solution

[Java] List


Error Message

java.lang.UnsupportedOperationException
      at java.util.AbstractList.remove(AbstractList.java:144)
      at java.util.AbstractList$Itr.remove(AbstractList.java:360)
      at java.util.AbstractCollection.removeAll(AbstractCollection.java:337)

occurred when  using removeAll


Solution

2011年10月2日 星期日

[AndroidManifest] android:configChanges

* Definition
  • When a configuration change occurs at runtime, the activity is shut down and restarted by default.
  • But declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
  • 在預設上,當任一屬性改變時,activity 會被關閉並且重啟;但若有指定此設定及屬性,則當被指定屬性改變時,activity會執行並通知系統 call onConfigurationChanged().


* Attribute values
  • mcc
    • The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.
    • IMSI 中的移動國家號碼,有三位數,每個國家都有自己獨立的MCC,可以識別手機用戶所屬國家。
  • mnc
    • The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.
    • 在一個國家或地區中,用於區分手機用戶的服務商。
  • locale
    • The locale has changed — for example, the user has selected a new language that text should be displayed in.
    • 使用者的所在區域改變。
  • touchscreen (?)
    • The touchscreen has changed. (This should never normally happen.)
  • keyboard
    • The keyboard type has changed — for example, the user has plugged in an external keyboard.
  • keyboardHidden
    • The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.
  • navigation (?)
    • The navigation type has changed. (This should never normally happen.)
  • orientation
    • The screen orientation has changed — that is, the user has rotated the device.
    • 螢幕直向與橫向的轉換。
  • fontScale
    • The font scaling factor has changed — that is, the user has selected a new global font size.
    • 字體改變大小。
  • uiMode (?)
    • The user interface mode has changed — this can be caused when the user places the device into a desk/car dock or when the the night mode changes. See UiModeManager.
    • Introduced in API Level 8.
    • CAR_MODE or DESK_MODE....
  • screenSize (?)
    • The current available screen size has changed. 
    • This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. 
    • However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
    • Added in API level 13.
  • smallestScreenSize
    • The physical screen size has changed.
    • This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display.
    • A change to this configuration corresponds to a change in the smallestWidth configuration.
    • However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
    • Added in API level 13.



* Reference
- Android ConfigChanges用法 - Android实例教程 - Android开发论坛 - 安卓开发论坛 - Android开发
- Android Developer - android:configChanges

2011年9月28日 星期三

[AndroidSign] apk sign

* Restriction
  • Android 系統禁止更新安裝簽名不一致的 APK。
  • You cannot release your application to the public when signed with the debug certificate.


* Generate apk sign
  • 要簽名一個沒有簽名過的APK,可以使用一個叫作 Auto-sign 的工具。
    • Auto-sign工具實際執行的是一個叫做Sign.bat的批處理命令。
    • 用文本編輯器開啟這個批處理文件。
  • Obtain a suitable private key


* signapk.jar
  • 是 Android 源碼包中的一個簽名工具。路徑為/build/tools/signapk/SignApk.java
  • 對比一個沒有簽名的 APK 和一個簽名好的 APK,會發現,簽名好的 APK 包中多了一個叫做 META-INF 的文件夾。裡面有三個文件:
    • MANIFEST.MF
    • CERT.SF
    • CERT.RSA。
  • signapk.jar 就是生成了這幾個文件(其他文件沒有任何改變。因此我們可以很容易去掉原有簽名訊息)。


* signapk.jar process:

1、生成 MANIFEST.MF 文件
  • 程式遍歷 apk 包中的所有文件(entry),對非文件夾非簽名文件的文件,逐個生成SHA1 的數字簽名訊息,再用 Base64 進行編碼。
  • 之後將生成的簽名寫入 MANIFEST.MF 文件。
  • SHA1 數字簽名
    • 是一種安全 hash 算法,類似於 MD5 算法。它把任意長度的輸入,通過散列算法變成固定長度的輸出(這裡我們稱作「摘要訊息」)。
    • 你不能僅通過這 個摘要訊息復原原來的訊息。另外,它保證不同訊息的摘要訊息彼此不同。
    • 因此,如果你改變了 apk 包中的文件,那麼在 apk 安裝校驗時,改變後的文件摘要信息與 MANIFEST.MF 的檢驗訊息不同,於是程式就不能成功安裝。

2、生成 CERT.SF 文件
  • 對前一步生成的 Manifest,使用 SHA1-RSA 算法,用私鑰進行簽名
  • RSA 是一種非對稱加密算法。
    • 用私鑰通過RSA算法對摘要訊息進行加密。在安裝時只能使用公鑰才能解密它。
    • 解密之後,將它與未加密的摘要訊息進行對比,如果相符,則表明內容沒有被異常修改

3、生成 CERT.RSA 文件
  • CERT.RSA文件中保存了公鑰、所採用的加密算法等訊息。


* Overview
1、 Android 簽名機制其實是對 APK 包完整性和發佈機構唯一性的一種校驗機制。
2、 Android 簽名機制不能阻止 APK 包被修改,但修改後的再簽名無法與原先的簽名保持一致。(擁有私鑰的情況除外)。
3、 APK 包加密的公鑰就打包在 APK 包內,且不同的私鑰對應不同的公鑰。換句話言之,不同的私鑰簽名的APK公鑰也必不相同。所以我們可以根據公鑰的對比,來判斷私鑰是否一致。


* APK Parser
  • 源碼中有一個隱藏的類用於APK 包的解析。這個類叫 PackageParser
  • 路徑為 frameworks\base\core\java\android\content\pm\PackageParser.java
  • 當我們需要獲取APK包的相關訊息時,可以直接使用這個 class。

我們就可以通過 packageInfo.signatures 來訪問到APK的簽名訊息。

它們的關係如下面代碼所示:

也就是說 signature = new Signature(certificate.getEncoded());

certificate 證書中包含了公鑰和證書的其他基本訊息。

公鑰不同,證書肯定互不相同。

我們可以通過 certificate 的 getPublicKey 方法獲取公鑰訊息。

所以比對簽名證書本質上就是比對公鑰訊息



Related
[Android] Debug Key


* Reference
- Android APK簽名對比及說明 - Android 軟體設計 - Android 台灣中文網 - Android(安卓,安致)討論區 - APK.TW **
- Signing Your Applications

2011年9月23日 星期五

[Android] Content Provider between Threads?

Error Message

java.lang.UnsupportedOperationException: Only CrossProcessCursor cursors are supported across process for now...



Solution

發生在 content provider 呼叫另一 content provider 的情況下

原因待解

20110923
- matrixCursor
- crossprocesscursor
- call content provider in another thread

2011年9月5日 星期一

[Lifecycle] Activity Lifecycle


應用程式中所使用的 activity 會被堆疊在 stack 中 (task?)。

畫面上的呈現與 activity 狀態對應如下:
  • running <-> visible and get focus
    • 正在運行的 activity 便是 stack 最上面的那個 activity,會顯示在畫面上的。
  • onPause <-> visible but lost focus
    • 在畫面上仍是可見的,但已失去焦點。
    • ex: 最上的 activity 畫面獲得焦點且是可看見背景的,那麼原本的 activity ,即使是 onPause 也仍存活著,保持自己的狀態與訊息,仍然與 window manager 保持連接,但有可能會被系統殺掉。
  • onStop <-> non-visible and lost focus
    • 在畫面上已被其他 activity 完全擋住兒而不可見的,仍保有自己的狀態與資訊,但 window manager 已不再管理其訊息,系統資源不夠時會被殺掉。


當 activity is onPause or onStop,系統則可以清楚它,可能提示 user 是否結束或只是殺掉其 process。




* 當Progress Dialog啟動時Activity模糊化(blur)進入OnPause()狀態, 並以執行緒(Thread)模擬事件進度.



* Reference
- Android activity 堆栈(1) - 嘴嘴的小的日志 - 网易博客
- tsots的Android範例Source: ProgressDialog~ProgressBar~搭配Handler顯示執行進度

2011年9月3日 星期六

[Android] ANDROID_ID

* Definition
- A 64-bit number (as a hex string) that is randomly generated on the device's first boot and should remain constant for the lifetime of the device. (The value may change if a factory reset is performed on the device.)
- 第一次啟動 Android 設備時隨機生成的64位數(以十六進制字符串表示), "應在"保持設備一生不變的. (當設備執行重置出廠值時, 該值可能改變.)

* from level 3

* How to get?
- Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
- but you will get "null" on emulator.

* Is it unique for each Device?


Update on 20111106
  • 透過設定中的選項恢復原廠設定後,ANDROID_ID 仍是相同。
  • 若是更新系統,是否仍會相同?
  • 重新刷ROM的話,是否仍會相同?


* Reference
- Settings.Secure | Android Developers
- 獲取 Android 設備的 ID,Settings.Secure.ANDROID_ID

2011年8月31日 星期三

[Oracle] ORA-01489: 字串過長

ORA-01489

Scenario:

使用sys_connect_by_path()


Message:

Cause: java.sql.SQLException: ORA-01489: 串接而成的字串過長
; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
字串串接太長 , default is VARCHAR2 (4000bytes)




20110912
JDBC 某些 driver 會有預設值限制,產生類似的問題,而用不同版本改善,可 survey 此方向相關資訊。

2011年8月13日 星期六

[AndroidLayout] Custom SeekBar/ProgressBar/RatingBar style

android:indeterminteDrawable
  • 設置進度顏色 (?)
  • default value is "false",不能設定進度值,即使設定 android:indeterminateDrawable 也不會有作用(?),只能靠拖拉設定。
  • value is "true" 才能設定進度值。


android:thumb
  • bar上的游標


android:isIndicator="true" (ratingbar)

  • 不可選僅顯示



* Reference
- Android圆形进度条颜色的设置 : Customize progress bar.
- SeekBar学习 - 码夫的专栏 - CSDN博客 : Customize seek bar.

2011年3月28日 星期一

[Resources] Backward Compatibility for Applications

* Should add a to the application's manifest, to ensure your app won't be installed on older devices.

* Suppose there's a simple new call you want to use, like android.os.Debug.dumpHprofData(String filename). The Debug class has existed since Android 1.0, but the method is new in Anroid 1.5 (API Level 3). If you try to call it directly, your app will fail to run on devices running Android 1.1 or earlier.

* The simplest way to call the method is through reflection.
  • This requires doing a one-time lookup and caching the result in a Method object. 
  • Using the method is a matter of calling Method.invoke and un-boxing the result.
* For each additional method you want to call, you would add an additional private Method field, field initializer, and call wrapper to the class.

* Using a wrapper class
  • The idea is to create a class that wraps all of the new APIs exposed by a new or existing class. 
  • Each method in the wrapper class just calls through to the corresponding real method and returns the same result.
* If the target class and method exist, you get the same behavior you would get by calling the class directly,

* If the target class or method doesn't exist, the initialization of the wrapper class fails, and your application knows that it should avoid using the newer calls.

* When wrapping an existing class that now has new methods, you only need to put the new methods in the wrapper class. Invoke the old methods directly. The static initializer in WrapNewClass would be augmented to do a one-time check with reflection.

(?)


* Reference
- Backward Compatibility for Applications

2011年3月25日 星期五

[Resources] Copy and Paste

* The clip object (ClipData.Item) can take one of three forms:
  1. Text
  2. URI
  3. Intent

* The clipboard holds only one clip object at a time. When an application puts a clip object on the clipboard, the previous clip object disappears.

* To add data to the clipboard, you create a ClipData object that contains
  1. A description of the data: A ClipDescription object.
  2. The data itself: One or more ClipData.Item objects.

* A ClipDescription object
- Contains an array of available MIME types for the clip's data: when you put a clip on the clipboard, this array is available to pasting applications, which can examine it to see if they can handle any of available the MIME types.

* To copy data
  1. An application puts a ClipData object on the ClipboardManager global clipboard.
  2. The ClipData contains one or more ClipData.Item objects and one ClipDescription object.

* To paste data
  1. An application gets the ClipData, gets its MIME type from the ClipDescription
  2. And gets the data either from the ClipData.Item or from the content provider referred to by ClipData.Item.

* You should verify that the clipboard contains a clip and that you can handle the type of data represented by the clip.


* Reference
- Copy and Paste