This analysis is based on gallery2 of android4.2.
1. Analyze the menu creation process.
The gallery parent class is the abstractgalleryactivity class, And the abstractgalleryactivity parent class is the activity class. Therefore, the menu creation function is called for the menu creation function of abstractgalleryactivity.
public class AbstractGalleryActivity extends Activity implements GalleryContext { private static final String TAG = "AbstractGalleryActivity"; private StateManager mStateManager; @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); return getStateManager().createOptionsMenu(menu);}}
Each subpage (such as photopage) is a subclass of activitystate. Statemanager is a subpage (activitystate) management class, responsible for subpage switching, command execution, interface refreshing, and other functions. Gallery submits all operation commands to statemanager for execution, and statemanager then delivers the commands to the current subpage for execution.
public class StateManager { private static final String TAG = "StateManager"; private AbstractGalleryActivity mActivity; private Stack<StateEntry> mStack = new Stack<StateEntry>(); public boolean createOptionsMenu(Menu menu) { if (mStack.isEmpty()) { return false; } else { return getTopState().onCreateActionBar(menu); }} public ActivityState getTopState() { Utils.assertTrue(!mStack.isEmpty()); return mStack.peek().activityState;}}
Statemanager will create menu operations and distribute them to the current sub-page (taking photopage as an example) for execution. Configure and update menuexecutor on the sub-Page Based on the page attributes. In this way, the menu creation is completed.
public class PhotoPage extends ActivityState{ private static final String TAG = "PhotoPage"; @Override protected boolean onCreateActionBar(Menu menu) { mActionBar.createActionBarMenu(R.menu.photo, menu); mHaveImageEditor = GalleryUtils.isEditorAvailable(mActivity, "image/*"); updateMenuOperations(); mActionBar.setTitle(mMediaSet != null ? mMediaSet.getName() : ""); return true;}}
2. Run the menu command.
The gallery parent class is the abstractgalleryactivity class, And the abstractgalleryactivity parent class is the activity class. Therefore, the menu creation is to call the menu command of the abstractgalleryactivity to execute the function.
public class AbstractGalleryActivity extends Activity implements GalleryContext { private static final String TAG = "AbstractGalleryActivity"; private StateManager mStateManager; @Override public boolean onOptionsItemSelected(MenuItem item) { GLRoot root = getGLRoot(); root.lockRenderThread(); try { return getStateManager().itemSelected(item); } finally { root.unlockRenderThread(); }}}
Statemanager then delivers the menu command to the currently displayed sub-page (take photopage as an example)
public class StateManager { private static final String TAG = "StateManager"; private AbstractGalleryActivity mActivity; private Stack<StateEntry> mStack = new Stack<StateEntry>(); public boolean itemSelected(MenuItem item) { if (!mStack.isEmpty()) { if (getTopState().onItemSelected(item)) return true; if (item.getItemId() == android.R.id.home) { if (mStack.size() > 1) { getTopState().onBackPressed(); } return true; } } return false;}}
The currently displayed subpage (taking photopage as an example) is used to execute commands. File Operations are involved and distributed to menuexecutor for processing. For example, in the delete operation, the menuexecutor generates a delete task and submits it to the thread pool to execute the task. If you are interested, you can check this part of the code and omit it here. The advantage of using the task-thread pool is: time-consuming operations, the interface will not be stuck, the task can be canceled at any time, fast multiple operations do not affect the system smoothness and stability.
public class PhotoPage extends ActivityState{ private static final String TAG = "PhotoPage"; @Overrideprotected boolean onItemSelected(MenuItem item) { MediaItem current = mModel.getMediaItem(0); int currentIndex = mModel.getCurrentIndex(); Path path = current.getPath(); DataManager manager = mActivity.getDataManager(); int action = item.getItemId(); String confirmMsg = null; switch (action) { case R.id.action_edit: { launchPhotoEditor();return true; } case R.id.action_details: { showDetails();return true; } case R.id.action_delete: confirmMsg = mActivity.getResources().getQuantityString( R.plurals.delete_selection, 1); mMenuExecutor.onMenuClicked(item, confirmMsg, new ImportCompleteListener(mActivity)); }}}
3. Subsequent code analysis, outline
Android4.0 gallery gallery2 code analysis (3) Status Management and window Switching
Android4.2 gallery gallery3d (gallery2) analysis (4) menu Command Execution Process Analysis
Android4.2 gallery gallery2 code analysis (5) Management of various data sources and data sources
Android4.2 gallery gallery2 code analysis (6) rendered data elements and their diversity
Android4.2 gallery gallery2 code analysis (7) Implementation of Remote Control
Android4.2 gallery gallery2 code analysis (8) Data Interaction with data scanning threads
Android4.2 gallery gallery2 code analysis (9) several points that can be optimized