Finishing by: Insomnia
The original author is ominous, if you are the original author, please contact me, timely correction.
This article does not do detailed verification, if there are errors, please point out, thank you very much
main files and classesLauncher.java
The main activity in launcher.
Draglayer.java:
Launcher layout of the Rootview. Draglayer is actually an abstract interface for handling drag and initial processing of events and then distributing them according to the situation, and the role is a controller. It is first used onInterceptTouchEvent(MotionEvent) to intercept all touch events, if the long press the item is dragged without passing the event, directly to the onTouchEvent() processing, so that the item can be moved, if not drag the item, the event to the target view, The event handler function with target view is processed accordingly. If there is a special need for the event can be modified onInterceptTouchEvent(MotionEvent) to achieve the required functionality.
Dragcontroller.java:
An interface defined for the drag. Contains an interface, two methods, and two static constants. The interface is Draglistener (contains onDragStart() , two functions), which is called when the drag is onDragEnd() onDragStart() started, and onDragEnd() is called when the drag is complete. A typical application in launcher is Deletezone, which is called when a long press drags the item onDragStart() and is hidden when the drag is finished onDragEnd() . The two functions include and are used to pass the information about the item startDrag() setDragItemInfo().startDrag() to be dragged, as well as the way it is dragged, to setDragItemInfo() pass the item's parameter information, including its position and size. Two constants are drag_action_move,drag_action_copy to identify the way to drag,Drag_action_move to move, Indicates that you need to delete the original item when dragging,drag_action_copy is a copy-type drag, which means to keep the dragged item.
Launchermodel.java:
Auxiliary files. There are many packages of operations on the database. Contains several threads, the most important of which are applicationsloader and Desktopitemsloader. Applicationsloader is used when loading all applications, Desktopitemsloader is used when loading workspace. Other functions are the encapsulation of the database, such as the task of updating the database and the UI when deleting, replacing, adding a program.
Workspace.java:
Abstract desktop. Consists of n Celllaout, which deals with events from a higher level of celllayout.
Launcherprovider.java:
The launcher database, which stores information about the desktop item. When creating a database loadFavorites(db) method, Loadfavorites () will parse the XML directory under the Default_workspace.xml file, the contents of which read out to write to the database, so that the desktop is prefabricated.
Celllayout.java:
The view that makes up the workspace, inherits from ViewGroup, is both a dragsource, is also a droptarget, can drag out the item inside it, can also hold the item that drags over. Some of its view parameters are set in the Workspace_screen.
Iteminfo.java:
For item abstraction, the parent class for all types of item, item contains a property that has an ID (identifying the item's ID), CELLX (position on the horizontal position, starting at 0), celly (position in portrait position, starting at 0), SpanX (the unit occupied in the horizontal position), Spany (the unit in the vertical position), screen (the first few screens in workspace, starting from 0), ItemType (type of item, widget,search,application, etc.), Container (item is located).
Userfolder.java:
User-created folders. You can drag the item into the folder, open the folder when you click it, and long press the title above the folder to rename the folder.
Livefolder.java:
The system comes with a folder. A folder, such as a contact, created from the system.
Deletezone:
Delete box. In the usual hidden state, when you drag the item long, it is displayed, and if you drag the item to the Delete box position, the item is deleted. The Deletezone implements the DropTarget and Draglistener two interfaces.
Launchersettings.java:
The definition of the string. A string definition of a database item, where the type of container is defined, and the definition of itemtype, in addition to the type definition of some special widgets, such as the definition of search,clock.
Additions to the Launcher Engineering class:
Main modules1. Interface Model:
Launcher Interface Rootview is Draglayer, it is a framelayout, on it workspace (should be said Celllayout) accounted for the vast majority of space, The celllayout parameter file is workspace_screen.xml. Workspace is both a droptarget and a dragsource, you can drag the application from the Allappgridview to put it on it, or you can drag the item inside it to delete or drag it into the BOTTOMABR.
(for students who want to modify launcher, you can customize the Draglay.java, such as change to Absolutelayout, and then modify the Launcher.xml layout file, you can achieve a variety of styles launcher several sides. )
2.drop& Drag Model:⒉1 DragSource
You can drag the object from the source of the container, in the launcher there are mainly allappgridview,workspace and so on.
void ondropcompleted (View target,boolean success,int x,int y);
⒉2 DropTarget
The container where the dragged object can be placed. There are folder,workspace,bottombar in launcher, and a view can be either DragSource or DropTarget. The main features are the following interfaces:
| Method Name |
usage |
| boolean acceptdrop (dragsource source,int x,int y,int xoffset,int yoffset,object dragInfo The |
acceptdrop function is used to determine whether the DropTarget can accept the item placed inside itself. |
| void ondragenter (dragsource source,int x,int y,int xoffset,int yoffset,object DragI NFO) |
ondragenter is the callback when item is dragged into a droptarget. |
| void ondragover (dragsource source,int x,int y,int xoffset,int yoffset,object dragIn FO) |
ondragover is the callback of item at the same time as the last location and the droptarget at this time. |
| void ondragexit (dragsource source,int x,int y,int xoffset,int yoffset,object dragIn FO) |
ondragexit is the callback when item is dragged out of droptarget. |
| boolean onDrop (dragsource source,int x,int y,int xoffset,int yoffset,object draginf o) |
ondrop is the callback when item is placed into the droptarget. |
The calling mode of the function is:
DropTarget DropTarget = Finddroptarget ((int) x, (int) y,coordinates);if(DropTarget! =NULL) {/** * When the target is at the same time as the previous one, move item according to the coordinates */ if(Mlastdroptarget = = droptarget) {Droptarget.ondragover (mdragsource,coordinates[0],coordinates[1], (int) Mtouchoffsetx, (int) mtouchoffsety,mdraginfo); }Else{/** * When the last position is different from this one and the last position is not empty, the item move * is moved out, the last View is rearranged according to the last coordinates, and the current coordinates are rearranged according to the current coordinate * / if(Mlastdroptarget! =NULL) {Mlastdroptarget.ondragexit (mdragsource,coordinates[0],coordinates[1], (int) Mtouchoffsetx, (int) mtouchoffsety,mdraginfo); } droptarget.ondragenter (mdragsource,coordinates[0],coordinates[1], (int) Mtouchoffsetx, (int) mtouchoffsety,mdraginfo); } }Else{//If this time is null, the last time is not NULL, then remove the cell from the previous coordinate position if(Mlastdroptarget! =NULL) {Mlastdroptarget.ondragexit (mdragsource,coordinates[0],coordinates[1], (int) Mtouchoffsetx, (int) mtouchoffsety,mdraginfo); } }//Record the last DropTargetMlastdroptarget = DropTarget;
3.Touch Event Summary:
Because of the launcher events more complex, so in the event of the general use of Rootview onintercepttouchevent (motionevent) to intercept all the touch events, after judgement and distributed to Childview.
The rules for judging are as follows:
The A.down event is first passed to the Onintercepttouchevent () method
B. If the ViewGroup onintercepttouchevent () return false after receiving the down event processing, subsequent events such as Move,up will continue to be passed to the ViewGroup first. It is then passed to the ontouchevent () processing of the final target view like the down event.
C. If the ViewGroup onintercepttouchevent () return true after receiving the down event processing, subsequent events such as Move,up will no longer be passed to Onintercepttouchevent (), Instead, the ontouchevent () process passed to the viewgroup like the down event, noting that the target view will not receive any events.
D. If the Ontouchevent () of the view that eventually needs to handle the event returns false, the event is passed to the ontouchevent () processing of the view at its previous level.
E. If the ontouchevent () of the view that eventually needs to handle the event returns TRUE, then subsequent events will continue to be passed to the view's ontouchevent () processing.
how to solve several problems1. Arrange all the apps on the desktop
All applications are arranged in the desktop by first creating a three-dimensional Boolean global array to record the arrangement of the item, the first dimension is the number of screens, the second dimension is the vertical arrangement, the third dimension is the horizontal arrangement, if the position is occupied by item is marked 1, Otherwise, it is marked as 0. Initializes the global array to 0 at startup, and then positions the corresponding position 1 at the time of the addition. All changes involving the item on the workspace, such as moving, adding, deleting operations need to maintain the array, keep the array correct, Because when you install a new program, you determine where to add the item, based on the state of the array.
2. Dynamically increase the screen
Dynamically increasing the screen is worksapce .addchild(view) achieved through the way. The basic idea is to first specify the maximum number of screens allowed, then create a celllayout instance when you need to increase the screen and the current number of screens does not exceed the maximum number of screens (CellLayout)mInflater.inflate(R.layout.workspace_screen,null) , and then add it through Addchild. The item on the screen is deleted by judging if there is an item on the screen from the last screen, if there is one, then delete the last screen, and so on.
3. Prefabricated DesktopsA. Add a normal application shortcut:
In.. Add the default Default_workspace.xml file under/res/xml to the normal application you want to place. The added format is:
<favorite launcher:packageName="... " //应用的packageName launcher:className="... " //应用启动时的第一个activity launcher:screen="..." //放置在第几屏(放在workspace的时候需要,从0开始,0为第一屏,1为第二屏,以此类推...) launcher:x="..." //放置x方向的位置(在列中的位置) launcher:y="..." /> //放置y方向的位置(在行中的位置)
PackageName and classname can find comp={...} by clicking on the program and then in the printed log, such as the following information:
Comp={com.estrongs.android.taskmanager/com.estrongs.android.taskmanager.taskmanager}. Where Com.estrongs.android.taskmanager is packagename,com.estrongs.android.taskmanager.taskmanager for classname.
The layout of the workspace is as follows:
(0,0) (1,0) (2,0) (3,0) (4,0)
(0,0) (1,0) (2,0) (3,0) (4,0)
(0,1) (2,1) (3,1) (4,1)
(0,2) (2,2) (3,2) (4,2)
B. Add Widget:
In.. Add the default Default_workspace.xml file under/package/apps/vlauncher/res/xml to the normal application you want to place. The added format is:
<widget Launcher:packagename= //widget packagename launcher:classname= "..." //the name of the receiver class that implements the widget. Launcher:container = "..." //placement (only for desktop) launcher:screen= Span class= "hljs-comment" >//placed on the first screen launcher:x= //placed x position launcher:y= //placed y position launcher:spanx= //in x direction launcher:spany=/> //the number of cells in the Y direction
For example, to place the second column in the first row of the 3rd screen, put a clock in the x direction that occupies two units, and two units in the Y direction, you can add the following code:
<appwidget launcher:packageName="com.android.alarmclock" launcher:className="com.android.alarmclock.AnalogAppWidgetProvider" launcher:container="desktop" launcher:screen="2" launcher:x="1" launcher:y="0" launcher:spanx="2" launcher:spany="2"/>
4. Change the way the main interface is arranged
To modify the arrangement of the desktop, as follows, modify the parameters of the Workspace_screen.xml shortaxiscells and Longaxiscells according to the screen settings, and then modify the Number_cells_ in Launcher.java The values of x and Number_cells_y, which will be judged when the item is added to the database in version 2.3, will cause some of the item to be displayed without modifying Number_cells_x and number_cells_y. Causes the failure of the pre-fabricated apk.
5. Increase the number of screens on the Worksapce
To increase the number of screens, first in accordance with the screen in the Launcher.xml
XML File1.workspace_screen.xml
| Properties |
meaning |
| Launcher:cellwidth= "95dip" |
The width of the cell (that is, item) |
| launcher:cellheight= "93dip" |
The width of the cell (that is, item) |
| Launcher:longaxisstartpadding= "25dip" |
Long (the width of the screen and high school, depending on the direction of the vertical and horizontal screen) in the direction of the distance from the beginning of the number of pixels |
| Launcher:longaxisendpadding= "55dip" |
Long (the width of the screen and high school, depending on the direction of the vertical screen) the number of pixels from the end of the direction |
| Launcher:shortaxisstartpadding= "20dip" |
Shorter (the width of the screen and the direction of high school, depending on the direction of the vertical screen) in the direction of the distance from the beginning of the number of pixels |
| Launcher:shortaxisendpadding= "120dip" |
Shorter (the width of the screen and the direction of high school, depending on the direction of the vertical screen) in the direction of the distance from the beginning of the number of pixels |
| Launcher:shortaxiscells= "3" |
Number of cells that can be accommodated in a shorter direction |
| Launcher:longaxiscells= "5" |
Number of cells that can be accommodated in a longer direction |
PS: shortaxiscells and Longaxiscells decide that the number of items that can be accommodated on a workspace (that is, celllayout) is shortaxiscells*longaxiscells.
2. Application_boxed.xml
The definition of item in all applications and system folders.
3.application.xml
The layout definition of the workspace item.
Launcher development of Android system