Detailed analysis of the source code of Android_launcher

Source: Internet
Author: User
Tags addchild

Reprint Please specify source: http://blog.csdn.net/fzh0803/archive/2011/03/26/6279995.aspx

Last year did a launcher related work, looked for a long time. Many people are modifying the launcher, but there is no detailed documentation, the accumulation of their own things to share, we accumulate together. This source code is based on the launcher2 of 2.1, although the later version has changed, but the approximate principle has been retained.

I. Main documents and Classes

1.launcher.java:The main activity in Launcher.

2.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 first uses onintercepttouchevent (motionevent) to intercept all the touch events, if the long press item to drag, do not pass the event, directly to Ontouchevent () processing, so that the item can be moved, If the item is not dragged, the event is passed to the target view, and the event handler with the target view is processed accordingly. You can modify Onintercepttouchevent (motionevent) to achieve the required functionality if you have special needs for the event.

3. Dragcontroller.java: an interface defined for the drag. Contains an interface, two methods, and two static constants. The interface is Draglistener (contains ondragstart (), Ondragend () two functions), ondragstart () is called at the beginning of the drag, and Ondragend () is called when the drag is complete. A typical application in launcher is Deletezone, which is called when a long press drag item ondragstart () is displayed, and Ondragend () is hidden at the end of the drag. Two functions include StartDrag () and Setdragiteminfo (). StartDrag () is used to drag the information that is passed to the item to be dragged and how to drag, Setdragiteminfo () The parameter information that is used to pass the item, including its location and size. Two constants are drag_action_move,drag_action_copy to identify the way you drag, drag_action_move to move, indicating that you need to delete the original item,drag_action_copy as a copied drag when dragging , which indicates that the item being dragged is retained.

4.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.

5.workspace.java: abstract Desktop. Consists of n Celllaout, which deals with events from a higher level of celllayout.

6.launcherprovider.java:Launcher's database, which stores information about the desktop item. When the database is created, the Loadfavorites (db) method is used, and loadfavorites () parses the Default_workspace.xml file in the XML directory, reads the contents of it and writes it to the database, thus making the desktop prefabricated.

7.celllayout.java: Composed of Workspace view, inherited from ViewGroup, is both a dragsource, but also a droptarget, you can drag the item inside it, You can also hold the item dragged over it. Some of its view parameters are set in the Workspace_screen.

8.iteminfo.java: Abstract to item, the parent of all types of item, item contains a property that has an ID (identifying the item's ID), CELLX (position in landscape position, starting at 0), celly (position in portrait position, Starting from 0), SpanX (the unit in the horizontal position), Spany (the unit in the vertical position), screen (in workspace, starting from 0), ItemType (item type, with Widget,search, application, etc.), container (item is located).

9.userfolder.java: user-created folder. 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.

10.livefolder.java: The system comes with a folder. A folder, such as a contact, created from the system.

11.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.

12.launchersettings.java: The definition of a 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.

Second, the main module

1. 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.

2.drop& Drag Model:

DragSource: A container that can be dragged from the source of an object, mainly allappgridview,workspace in launcher.

void ondropcompleted (View target, Boolean success,int x,int y);

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:

Boolean Acceptdrop (dragsource source, int x, int y, int xoffset, int yoffset, Object draginfo);

The Acceptdrop function is used to determine whether DropTarget can accept the item in its own place.

void OnDragEnter (DragSource source, int x, int y, int xoffset, int yoffset, Object draginfo);

OnDragEnter is the callback when item is dragged into a droptarget.

void OnDragOver (DragSource source, int x, int y, int xoffset, int yoffset, Object draginfo);

OnDragOver is the callback of item at the same time as the last position and the droptarget at the same time.

void Ondragexit (DragSource source, int x, int y, int xoffset, int yoffset, Object draginfo);

Ondragexit is the callback when item is dragged out of droptarget.

Boolean OnDrop (dragsource source, int x, int y, int xoffset, int yoffset, Object draginfo);

OnDrop is the callback when item is placed into DropTarget.

The calling mode of the function is:

DropTarget DropTarget = finddroptarget ((int) x, (int) y, coordinates);

if (DropTarget! = null) {

/**

* When the target is the same as the previous one, move item according to 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, the last View is rearranged according to the last coordinate, 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 DropTarget

Mlastdroptarget = 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 moves, up, and so on 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 moves, up, and so on 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.

Three, the solution of several problems

1. 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 achieved by WORKSAPCE. Addchild (view). The basic idea is to first specify the maximum number of screens allowed, and then pass (celllayout) minflater.inflate (R.layout.workspace_screen) when you need to increase the screen and the current number of screens does not exceed the maximum number of screens. NULL) Create a Celllayout instance 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 Desktops

A. to add a normal application shortcut:

in the.. 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= "... "//Application of the PackageName

Launcher:classname= "... "//The first activity when the app starts

Launcher:screen= "..."//placed in the first screen (when placed in workspace need, starting from 0, 0 for the first screen, 1 for the second screen, etc...) )

Launcher:x= "..."//position in x direction (position in column)

Launcher:y= "..."/>//position in y direction (position in row)

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 is classname.

The layout of the workspace is as follows:

(0,0)

(1,0)

(2,0)

(3,0)

(4,0)

(0,1)

(all)

(2,1)

(3,1)

(4,1)

( 0,2)

(2,2)

(3,2)

(4,2)

B. Add Widget:

in the.. 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 position (desktop only)

launcher:screen="..." Placed on the first screen

launcher:x="..." placed X position

launcher:y="..." //Place y position

launcher:spanx="..." The number of cells in the x direction

launcher:spany="..."/>//number of blocks 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, first modify the Workspace_ according to the screen settings Screen.xml shortaxiscells and Longaxiscells parameters, and then modify the number_cells_x and number_cells_y values in the Launcher.java, When you start adding item to the database in version 2.3, you will be judged that if you do not modify number_cells_x and number_cells_y, it will cause some of the item's display to fail, resulting in the failure of the prefab apk.

5. Increase the number of screens on the worksapce

to increase the number of screens, first remove or add <include android:id= "@+id/cell" in the <com.android.launcher.workspace according to the Launcher.xml in the screen. N "layout=" @layout/workspace_screen "/>, and then modify the Screen_count value in Launcher.java.

Four, XML file

1.workspace_screen.xml

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

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.

Detailed analysis of the source code of Android_launcher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.