AbstractCursor.java文件
/**
*Thisisanabstractcursorclassthathandlesalotofthecommoncode
*thatallcursorsneedtodealwithandisprovidedforconveniencereasons.
*/
publicabstractclassAbstractCursorimplementsCrossProcessCursor{
privatestaticfinalStringTAG="Cursor";
DataSetObservablemDataSetObservable=newDataSetObservable();
ContentObservablemContentObservable=newContentObservable();
-------------------省略------------------
publicvoiddeactivate(){
deactivateInternal();
}
/**
*@hide
*/
publicvoiddeactivateInternal(){
if(mSelfObserver!=null){
mContentResolver.unregisterContentObserver(mSelfObserver);
mSelfObserverRegistered=false;
}
mDataSetObservable.notifyInvalidated();
}
publicbooleanrequery(){
if(mSelfObserver!=null&&mSelfObserverRegistered==false){
mContentResolver.registerContentObserver(mNotifyUri,true,mSelfObserver);
mSelfObserverRegistered=true;
}
mDataSetObservable.notifyChanged();
returntrue;
}
publicbooleanisClosed(){
returnmClosed;
}
publicvoidclose(){
mClosed=true;
mContentObservable.unregisterAll();
deactivateInternal();
}
-------------------省略------------------
publicvoidregisterContentObserver(ContentObserverobserver){
mContentObservable.registerObserver(observer);
}
publicvoidunregisterContentObserver(ContentObserverobserver){
//cursorwillunregisterallobserverswhenitclose
if(!mClosed){
mContentObservable.unregisterObserver(observer);
}
}
/**
*Thisishiddenuntilthedatasetchangemodelhasbeenre-evaluated.
*@hide
*/
protectedvoidnotifyDataSetChange(){
mDataSetObservable.notifyChanged();
}
/**
*Thisishiddenuntilthedatasetchangemodelhasbeenre-evaluated.
*@hide
*/
protectedDataSetObservablegetDataSetObservable(){
returnmDataSetObservable;
}
publicvoidregisterDataSetObserver(DataSetObserverobserver){
mDataSetObservable.registerObserver(observer);
}
publicvoidunregisterDataSetObserver(DataSetObserverobserver){
mDataSetObservable.unregisterObserver(observer);
}
/**
*Subclassesmustcallthismethodwhentheyfinishcommittingupdatestonotifyall
*observers.
*
*@paramselfChange
*/
protectedvoidonChange(booleanselfChange){
synchronized(mSelfObserverLock){
mContentObservable.dispatchChange(selfChange);
if(mNotifyUri!=null&&selfChange){
mContentResolver.notifyChange(mNotifyUri,mSelfObserver);
}
}
}
/**
*SpecifiesacontentURItowatchforchanges.
*
*@paramcrThecontentresolverfromthecaller'scontext.
*@paramnotifyUriTheURItowatchforchanges.Thiscanbea
*specificrowURI,orabaseURIforawholeclassofcontent.
*/
publicvoidsetNotificationUri(ContentResolvercr,UrinotifyUri){
synchronized(mSelfObserverLock){
mNotifyUri=notifyUri;
mContentResolver=cr;
if(mSelfObserver!=null){
mContentResolver.unregisterContentObserver(mSelfObserver);
}
mSelfObserver=newSelfContentObserver(this);
mContentResolver.registerContentObserver(mNotifyUri,true,mSelfObserver);
mSelfObserverRegistered=true;
}
}
-------------------省略------------------
@Override
protectedvoidfinalize(){
if(mSelfObserver!=null&&mSelfObserverRegistered==true){
mContentResolver.unregisterContentObserver(mSelfObserver);
}
}
/**
*CursorsusethisclasstotrackchangesothersmaketotheirURI.
*/
protectedstaticclassSelfContentObserverextendsContentObserver{
WeakReference<AbstractCursor>mCursor;
publicSelfContentObserver(AbstractCursorcursor){
super(null);
mCursor=newWeakReference<AbstractCursor>(cursor);
}
@Override
publicbooleandeliverSelfNotifications(){
returnfalse;
}
@Override
publicvoidonChange(booleanselfChange){
AbstractCursorcursor=mCursor.get();
if(cursor!=null){
cursor.onChange(false);
}
}
}
-------------------省略------------------
protectedContentResolvermContentResolver;
protectedbooleanmClosed=false;
privateUrimNotifyUri;
privateContentObservermSelfObserver;
finalprivateObjectmSelfObserverLock=newObject();
privatebooleanmSelfObserverRegistered;
}
/**
*Thisisanabstractcursorclassthathandlesalotofthecommoncode
*thatallcursorsneedtodealwithandisprovidedforconveniencereasons.
*/
publicabstractclassAbstractCursorimplementsCrossProcessCursor{
privatestaticfinalStringTAG="Cursor";
DataSetObservablemDataSetObservable=newDataSetObservable();
ContentObservablemContentObservable=newContentObservable();
-------------------省略------------------
publicvoiddeactivate(){
deactivateInternal();
}
/**
*@hide
*/
publicvoiddeactivateInternal(){
if(mSelfObserver!=null){
mContentResolver.unregisterContentObserver(mSelfObserver);
mSelfObserverRegistered=false;
}
mDataSetObservable.notifyInvalidated();
}
publicbooleanrequery(){
if(mSelfObserver!=null&&mSelfObserverRegistered==false){
mContentResolver.registerContentObserver(mNotifyUri,true,mSelfObserver);
mSelfObserverRegistered=true;
}
mDataSetObservable.notifyChanged();
returntrue;
}
publicbooleanisClosed(){
returnmClosed;
}
publicvoidclose(){
mClosed=true;
mContentObservable.unregisterAll();
deactivateInternal();
}
-------------------省略------------------
publicvoidregisterContentObserver(ContentObserverobserver){
mContentObservable.registerObserver(observer);
}
publicvoidunregisterContentObserver(ContentObserverobserver){
//cursorwillunregisterallobserverswhenitclose
if(!mClosed){
mContentObservable.unregisterObserver(observer);
}
}
/**
*Thisishiddenuntilthedatasetchangemodelhasbeenre-evaluated.
*@hide
*/
protectedvoidnotifyDataSetChange(){
mDataSetObservable.notifyChanged();
}
/**
*Thisishiddenuntilthedatasetchangemodelhasbeenre-evaluated.
*@hide
*/
protectedDataSetObservablegetDataSetObservable(){
returnmDataSetObservable;
}
publicvoidregisterDataSetObserver(DataSetObserverobserver){
mDataSetObservable.registerObserver(observer);
}
publicvoidunregisterDataSetObserver(DataSetObserverobserver){
mDataSetObservable.unregisterObserver(observer);
}
/**
*Subclassesmustcallthismethodwhentheyfinishcommittingupdatestonotifyall
*observers.
*
*@paramselfChange
*/
protectedvoidonChange(booleanselfChange){
synchronized(mSelfObserverLock){
mContentObservable.dispatchChange(selfChange);
if(mNotifyUri!=null&&selfChange){
mContentResolver.notifyChange(mNotifyUri,mSelfObserver);
}
}
}
/**
*SpecifiesacontentURItowatchforchanges.
*
*@paramcrThecontentresolverfromthecaller'scontext.
*@paramnotifyUriTheURItowatchforchanges.Thiscanbea
*specificrowURI,orabaseURIforawholeclassofcontent.
*/
publicvoidsetNotificationUri(ContentResolvercr,UrinotifyUri){
synchronized(mSelfObserverLock){
mNotifyUri=notifyUri;
mContentResolver=cr;
if(mSelfObserver!=null){
mContentResolver.unregisterContentObserver(mSelfObserver);
}
mSelfObserver=newSelfContentObserver(this);
mContentResolver.registerContentObserver(mNotifyUri,true,mSelfObserver);
mSelfObserverRegistered=true;
}
}
-------------------省略------------------
@Override
protectedvoidfinalize(){
if(mSelfObserver!=null&&mSelfObserverRegistered==true){
mContentResolver.unregisterContentObserver(mSelfObserver);
}
}
/**
*CursorsusethisclasstotrackchangesothersmaketotheirURI.
*/
protectedstaticclassSelfContentObserverextendsContentObserver{
WeakReference<AbstractCursor>mCursor;
publicSelfContentObserver(AbstractCursorcursor){
super(null);
mCursor=newWeakReference<AbstractCursor>(cursor);
}
@Override
publicbooleandeliverSelfNotifications(){
returnfalse;
}
@Override
publicvoidonChange(booleanselfChange){
AbstractCursorcursor=mCursor.get();
if(cursor!=null){
cursor.onChange(false);
}
}
}
-------------------省略------------------
protectedContentResolvermContentResolver;
protectedbooleanmClosed=false;
privateUrimNotifyUri;
privateContentObservermSelfObserver;
finalprivateObjectmSelfObserverLock=newObject();
privatebooleanmSelfObserverRegistered;
}