2013년 6월 26일 수요일

Context

안드로이드 레퍼런스의 설명만으로 정리가 되어 버린다.
Interface to global information about an application environment.
This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
왜 진작 찾아 보지 않았는지.. Activity든 Service든 Context를 간접적으로 상속받고 있다.

ContentObserver

안드로이드 레퍼런스에서는 다음과 같이 설명한다.
Receives call backs for changes to content. Must be implemented by objects which are added to a ContentObservable.
Latin IME의 소스에서 사용되는 부분을 발췌하면 다음과 같다.
com.android.inputmethod.latin.UserBinaryDictionary의 생성자에서 아래 코드를 발견할 수 있다.
        ContentResolver cres = context.getContentResolver();

        mObserver = new ContentObserver(null) {
            @Override
            public void onChange(final boolean self) {
                // This hook is deprecated as of API level 16, but should still be supported for
                // cases where the IME is running on an older version of the platform.
                onChange(self, null);
            }
            // The following hook is only available as of API level 16, and as such it will only
            // work on JellyBean+ devices. On older versions of the platform, the hook
            // above will be called instead.
            @Override
            public void onChange(final boolean self, final Uri uri) {
                setRequiresReload(true);
                // We want to report back to Latin IME in case the user just entered the word.
                // If the user changed the word in the dialog box, then we want to replace
                // what was entered in the text field.
                if (null == uri || !(context instanceof LatinIME)) return;
                final long changedRowId = ContentUris.parseId(uri);
                if (-1 == changedRowId) return; // Unknown content... Not sure why we're here
                final String changedWord = getChangedWordForUri(uri);
                ((LatinIME)context).onWordAddedToUserDictionary(changedWord);
            }
        };
        cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
이코드만 보면, getContentResolver에서 ContentResolver를 가져와서 Observer를 등록하면 된다.
종료 때는 위 소스에는 빠져있지만 unregisterContentObserver를 사용해서 해제하면 된다.
전혀 복잡할 내용도 없고 간결한데 뭔가 답답한 이유는,
context, Uri 에 대한 개념이 부족해서이리라.

contextUri를 좀 더 확인 후 여기에도 정리해 두겠다.


== 추가 내용 ==
Uri는 특별한게 없고, 예상하던 Uniform Resource Identifiers가 맞다.
어렵게 생각할 필요 없었다.
Words.CONTENT_URI는 android.jar에 포함되어 있었다. 이 값이 궁금해 진행이 늦었다.
latin IME소스에 있는 줄 알고 계속 Find usages 호출해서 헤메고 있었는데. Navigation bar를 잘 봤어야지..
어쟀든 값은
public static final Uri CONTENT_URI = Uri.parse("content://user_dictionary/words");