@Override
public void onPause() {
super.onPause();
if (mRenderView != null)
mRenderView.onPause();
if (mWakeLock != null) {
if (mWakeLock.isHeld()) {
mWakeLock.release();
}
mWakeLock = null;
}
mPause = true;
}
我们上篇分析了gallery3d的入口,主要集中在gallery.java这个文件里面。
上次我们主要分析了onCreate()函数,我们知道Gallery这个类继承Activity,那么Activity的其他函数呢?
首先分析onResume这个函数。
public void onResume() {
super.onResume();
if (mDockSlideshow) {
if (mWakeLock != null) {
if (mWakeLock.isHeld()) {
mWakeLock.release();
}
}
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "GridView.Slideshow.All");
mWakeLock.acquire();
return;
}
Log.i(TAG, "Gallery:onResume");
if (ImageManager.hasStorage()) {
CacheService.computeDirtySets(this);
CacheService.startCache(this, false);
}
if (mRenderView != null) {
mRenderView.onResume();
}
if (mPause) {
// We check to see if the authenticated accounts have changed, and
// if so, reload the datasource.
HashMap<String, Boolean> accountsEnabled = PicasaDataSource.getAccountStatus(this);
String[] keys = new String[accountsEnabled.size()];
keys = accountsEnabled.keySet().toArray(keys);
int numKeys = keys.length;
for (int i = 0; i < numKeys; ++i) {
String key = keys[i];
boolean newValue = accountsEnabled.get(key).booleanValue();
boolean oldValue = false;
Boolean oldValObj = mAccountsEnabled.get(key);
if (oldValObj != null) {
oldValue = oldValObj.booleanValue();
}
if (oldValue != newValue) {
// Reload the datasource.
if (mGridLayer != null)
mGridLayer.setDataSource(mGridLayer.getDataSource());
break;
}
}
mAccountsEnabled = accountsEnabled;
mPause = false;
}
}
mDockSlideshow这个变量是说如果用户以slideshow方式浏览图片时,设置为true。这个时候要求保持屏幕全亮和CPU运行:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "GridView.Slideshow.All");
mWakeLock.acquire();
mDockSlideshowmDockSlideshow接来下检查是否有新的相册:
if (ImageManager.hasStorage()) {
CacheService.computeDirtySets(this);
CacheService.startCache(this, false);
}
不过这个有个小问题,因为第一次介入gallery3d的时候,onCreate开启了一个线程,线程里面同样检查是否有新的相册。我们知道Activity的生命周期是onCreate->onStart->onResume->Activity is running。所以这里最好做一个改动:
if(GalleryisCreated)
{
if (ImageManager.hasStorage()) {
CacheService.computeDirtySets(this);
CacheService.startCache(this, false);
}
}
这里检查是否有新的相册,是基于这样的场景考虑的:用户使用gallery3d浏览图片或者视频,这时候用户突然想拍一个照片,按了Home键,galler3d的生命周期就是这样onPause->onStop。拍完照片后,用户再次进入gallery3d,这时候执行onRestart->onStart->onResume等动作,这时候就需要加入新的相册了。
接下来调用RenderView的onResume了。
if (mRenderView != null) {
mRenderView.onResume();
}
最后一段的处理是针对picasa数据源的。如果用户切换了用户名,需要重新加载数据。
再来看看onPause函数:
@Override
public void onPause() {
super.onPause();
if (mRenderView != null)
mRenderView.onPause();
if (mWakeLock != null) {
if (mWakeLock.isHeld()) {
mWakeLock.release();
}
mWakeLock = null;
}
mPause = true;
}
调用了RenderView的onPause函数,另外释放屏幕和CPU控制。
onStop函数又做了什么事情呢?
@Override
public void onStop() {
super.onStop();
if (mGridLayer != null)
mGridLayer.stop();
if (mReverseGeocoder != null) {
mReverseGeocoder.flushCache();
}
LocalDataSource.sThumbnailCache.flush();
LocalDataSource.sThumbnailCacheVideo.flush();
PicasaDataSource.sThumbnailCache.flush();
CacheService.startCache(this, true);
}
调用GridLayer的stop函数,接着将数据写入如下索引文件:地址cache索引文件,本地文件的相册和视频cache索引文件,picasa源的cache索引文件。
同时在后台启动service,构造缩略图的索引。
@Override
public void onDestroy() {
// Force GLThread to exit.
setContentView(R.layout.main);
if (mGridLayer != null) {
DataSource dataSource = mGridLayer.getDataSource();
if (dataSource != null) {
dataSource.shutdown();
}
mGridLayer.shutdown();
}
if (mReverseGeocoder != null)
mReverseGeocoder.shutdown();
if (mRenderView != null) {
mRenderView.shutdown();
mRenderView = null;
}
mGridLayer = null;
super.onDestroy();
Log.i(TAG, "onDestroy");
}
onDestroy函数设置当前view为main。main是一个空布局的view。接着切断数据源,停掉数据监听和导入线程,停掉地址计算线程,停止页面更新。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mRenderView != null) {
return mRenderView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
} else {
return super.onKeyDown(keyCode, event);
}
}
Gallery的键盘事件处理都移交给了RenderView的键盘事件处理。