前言
该分析从AndroidManifest.xml定义的Launcher Activity: com.android.camera.Camera入手。尽量将UI的变换和代码的流程结合起来,了解其控制流程转移机制。
UI layout的基本分析在camera 4.2 UI layout 结构(2013/4)
概览
Camera继承自ActivityBase,并实现了其它类的listener。继承关系如下图所示:com.android.gallery3d.app.GalleryContext Interface
接口GalleryContext在Gallery的app中定义,提供了一下几个方法:
DataManager getDataManager () Context getAndroidContext () Looper getMainLooper () Resources getResources () ThreadPool getThreadPool ()
根据Doxygen生成的文档,我们可以方便找出被Camera用到的方法只有getMainLooper(),getResources()。
getMainLooper()
Referenced by:主要在Camera处理key event时使用。
com.android.gallery3d.ui.ActionModeHandler.ActionModeHandler(), gallery只有一个地方再使用。 com.android.camera.Camera.initializeFocusManager(), com.android.camera.actor.PhotoActor.initializeAfterPreview(), com.android.camera.actor.PhotoActor.isCameraPrepareDone(), com.android.camera.actor.PhotoActor.onBackPressed(), com.android.camera.actor.PhotoActor.onKeyDown(), com.android.camera.actor.PhotoActor.onKeyUp(), com.android.camera.actor.PhotoActor.onShutterButtonClick(), com.android.camera.actor.PhotoActor.onShutterButtonFocus(), com.android.camera.actor.PhotoActor.resetPhotoActor().
getResources()
Referenced by :被Gallery和camera多处使用。UI相关。
com.android.gallery3d.app.AbstractGalleryActivity.toggleStatusBarByOrientation(), com.android.camera.manager.ViewManager.checkConfiguration(), com.android.camera.manager.IndicatorManager.onDetectedSceneMode(), com.android.camera.manager.ZoomManager.shouldIgnoreScrollGesture(), com.android.camera.manager.ViewManager.show(), com.android.camera.actor.VideoActor.stopVideoRecordingAsync(),
com.android.gallery3d.app.AbstractGalleryActivity
初次学习代码未发现跟Camera有啥重要关系。从继承关系上感觉跟Gallery关系更大com.android.camera.ActivityBase
抽象类,负责跟Gallery的衔接。比如进入Gallery时让Camera UI控件消失掉。(所以APP UI的root view定义在了这里 View CameraAppView = findViewById(R.id.camera_app_root);) 其中的import值得留意
import com.android.gallery3d.app.AbstractGalleryActivity; import com.android.gallery3d.app.AppBridge; import com.android.gallery3d.app.GalleryActionBar; import com.android.gallery3d.app.PhotoPage; import com.android.gallery3d.ui.GestureRecognizer.Listener; import com.android.gallery3d.ui.PhotoView; import com.android.gallery3d.ui.ScreenNail;
定义内部类MyAppBridge(其注释中写道:The is the communication interface between the Camera Application and the Gallery photoPage.) MyAppBridge实例化了CameraScreenNail类(Gallery中camera preview display的位置)。
在进一步分析时可以考虑在Gallery中做手机旋转,缩放等行为对camera preview display的影响。其中的onLayoutChange,full screen等行为应该与此有关。
之后需要结合Gallery那么的接口做更深入的分析。
重要的方法:
protected void createCameraScreenNail(boolean getPictures) 参数getPictures为true表示正常启动;false为第三方应用启动。该方法完成了对MyAppBridge的实例化。并通过Bundle与Gallery通信。
[edit]
com.android.camera.Camera
com.android.camera.Camera是初次研究的核心。 这一节对其onCreate()中发生的行为做分析。
在onCreate()中,完成以下内容:
- 加载UI
- 打开相机
- 根据屏幕尺寸显示preview(需补充分析内容)
- 创建PhotoActor
- 创建ComboPreferences
- 创建CameraScreenNail
下面对提及的内容进行简介。
Camera UI layout
加载UI是放在onCreate()中打开hardware camera的代码mCameraStartUpThread.start();之后。这是为了提高加载速度。
在UI layout的基本分析camera 4.2 UI layout 结构(2013/4)中曾提到,UI APP Views的加载应该是通过代码完成的。这里我们看看是怎么实现的。
Camera 主线程加载 view
于是在onCreate()中通过initializeCommonManagers()创建各ViewManager。
ViewManager在构造函数中将自身保存到Camera的属性List<ViewManager> mViewManagers
相关方法:
//initial managers before new actor, so we can count views created by Camera.java private void initializeCommonManagers()
//only initialize some thing for open private void initializeForOpeningProcess() 该函数负责find layout View id, 实例化LocationManager,MyOrientationEventListener
//may be we can lazy this function. private void initializeAfterPreview() 只对ShutterManager,SettingManager,mModePicker,mThumbnailManager进行了加载。
CameraStartUpThread init view
CameraStartUpThread open camera成功以后(暂不考虑失败情况),
- mCameraActor.onCameraOpenDone();向CameraActor传递信息。
- initializeFocusManager()
- initializeCameraPreferences() -> 发送MSG_CAMERA_PREFERENCE_READY ->notifyPreferenceReady()
- 配置Parameters -> 发送MSG_CAMERA_PARAMETERS_READY ->notifyParametersReady()
- 通过mMainHandler.sendEmptyMessage(MSG_CAMERA_OPEN_DONE);向主线程发送消息。
这里只学习加载View相关内容。
notifyPreferenceReady()
Open hardware camera
openCamera之前会检查effectType,effectParameter。
openCamera成功以后初始化FocusManager,preference,重置callbacks,发送open done message.
CameraManager持有AndroidCamera对象。CameraHolder只持有该对象的引用。
CameraManager 简介
定义了单例内部类CameraProxy(代理)作为与外界通信的接口,对外提供ICamera定义的方法的具体实现。实现的方法为发送CameraManager内部定义的消息类型。
CameraHolder 简介
根据其定义的方法判断其作用是降低对系统的调用,提高运行效率。
内置消息机制对keep状态下release做延迟。
对已经open的CameraProxy采用reconnect.
定义了更多的行为如tryOpen()
CameraActor 简介
CameraActor的注释写到://just control capture flow, don't set parameters
CameraActor,PhotoActor都没有版权信息,可以猜测是MTK的实现。
CameraActor定义了两个属性Camera,FocusManager。
CameraActor定义的方法:
- 各种拍照模式Callback的get方法。
- OnZoomChangeListener, FaceDetectionListener的get方法。
- user action行为的监听,以onXXX()形式定义的方法。
- shutter button callback,以getXXXListener()形式定义的方法。
- camera life cycle 接受外部通知的方法,以onCameraXXX()形式定义。
- 其他一些方法。
其中只有public abstract int getMode()强制要求子类实现。所以,子类继承以后只需要实现需要重载的方法即可。
PhotoActor 简介
对于各种特色拍照功能,只需要继承PhotoActor,重载相关的方法,添加新方法(也可在CameraActor中添加)。
对PhotoActor之后会有更详尽的分析。
创建 ComboPreferences
Collaboration diagram其中的preferences分为global和不同cameraId对应的local preferances.
提供了OnSharedPreferenceChange时候的listener接口,可注册观察者。