By he minggui (http://blog.csdn.net/hmg25) reprint please indicate the source
Recently, I plan to study the widget-related issues of android and record some of my experiences here. Haha, after the research is completed, I will post the source code of the changes if necessary, to serve readers. Let's take a look at the process of adding widgets in launcher2.
To add a widget, you must first press the blank director in laucher. Therefore, you must first locate the widget in laucher's public boolean onLongClick (View v) and see:
If (mWorkspace. allowLongPress () {<br/> if (cellInfo. cell = null) {<br/> if (cellInfo. valid) {<br/> // User long pressed on empty space <br/> mWorkspace. setAllowLongPress (false); <br/> mWorkspace. performHapticFeedback (HapticFeedbackConstants. LONG_PRESS, <br/> HapticFeedbackConstants. FLAG_IGNORE_VIEW_SETTING); <br/> showadddifo (cellInfo); <br/>}< br/>} else {<br/> if (! (CellInfo. cell instanceof Folder) {<br/> // User long pressed on an item <br/> mWorkspace. performHapticFeedback (HapticFeedbackConstants. LONG_PRESS, <br/> HapticFeedbackConstants. FLAG_IGNORE_VIEW_SETTING); <br/> mWorkspace. startDrag (cellInfo); <br/>}< br/>}
We can see that we jumped to showAddDialog (cellInfo) and found:
Private void showAddDialog (CellLayout. CellInfo cellInfo) {<br/> mAddItemCellInfo = cellInfo; <br/> mWaitingForResult = true; <br/> showDialog (DIALOG_CREATE_SHORTCUT); <br/>}
We can see that he has created a Dialog with the DIALOG_CREATE_SHORTCUT parameter and jumped into Launcher with the parameter. java parent Activity. the java showDialog () method will eventually reach Launcher. java onCreateDialog (int id) method, the Code is as follows:
@ Override <br/> protected Dialog onCreateDialog (int id) {<br/> switch (id) {<br/> case DIALOG_CREATE_SHORTCUT: <br/> return new CreateShortcut (). createDialog (); <br/> case DIALOG_RENAME_FOLDER: <br/> return new RenameFolder (). createDialog (); <br/>}</p> <p> return super. onCreateDialog (id); <br/>}< br/>
The createDialog () method jumps to CreateShortcut:
Dialog createDialog () {<br/> mWaitingForResult = true; <br/> mAdapter = new AddAdapter (Launcher. this); <br/> final AlertDialog. builder builder = new AlertDialog. builder (Launcher. this); builder. setTitle (getString (R. string. menu_item_add_item); <br/> builder. setAdapter (mAdapter, this); <br/> builder. setInverseBackgroundForced (true); <br/> AlertDialog dialog = builder. create (); <br/> dialog. setOnCancelListener (this); <br/> dialog. setOnDismissListener (this); <br/> dialog. setOnShowListener (this); </p> <p> return Diener; <br/>}
Here we can see an AddAdapter class, jump to see, this is the content of the dialog box that appears after the definition of long press:
Public static final int ITEM_SHORTCUT = 0; <br/> public static final int ITEM_APPWIDGET = 1; <br/> public static final int ITEM_LIVE_FOLDER = 2; <br/> public static final int ITEM_WALLPAPER = 3;
If we need to add new content in the original dialog box, the first thing we need to modify is here. Let's go back to the previous place and go down. The dialog responds to the click event, public void onClick (DialogInterface dialog, int which ):
Case addadapter. item_appwidget: {<br/> int appwidgetid = launcher. this. mappwidgethost. allocateappwidgetid (); <br/> // an appwidgetid is generated for appwidgetprovider to be bound later. <br/> intent pickintent = new intent (appwidgetmanager. action_appwidget_pick); <br/> // create an intent, which is an activity that opens a real widgets list. The activity corresponds to appwidgetpickactivity. <br/> pickintent. putextra (appwidgetmanager. extra_appwidget_id, appwidgetid); <br/> // start the pick activity <br/> // set extra_appwidget_id startactivityforresult (pickintent, request_pick_appwidget); <br/> break; <br/>}< br/>
After clicking the widget entry, you can jump to a new pickIntent, which is actually running packages/apps/Settings/src/com/android/settings/AppWidgetPickActivity. java:
Create an InstalledAppWidgets list in the onCreate method, which is all the widgets we can see on the interface.
Click a widgets to access the AppWidgetPickActivity. onClick event listener. Read the method code and it will enterElse:
If (intent. getExtras ()! = Null) {<br/> // If there are any extras, it's because this entry is custom. <br/> // Don't try to bind it, just pass it back to the app. <br/> setResultData (RESULT_ OK, intent); <br/>}else {<br/> try {<br/> mAppWidgetManager. bindAppWidgetId (mAppWidgetId, intent. getComponent (); <br/> // bind the selected Desktop component to the mAppWidgetId. <br/> result = RESULT_ OK; // set the returned result to OK.
After the activity is executed, the launcher. onActivityResult will be displayed. There are two key cases for this function method:
Case REQUEST_PICK_APPWIDGET: <br/> addAppWidget (data); <br/> break; <br/> case REQUEST_CREATE_APPWIDGET: <br/> completeAddAppWidget (data, mAddItemCellInfo );
Go to the addAppWidget (Intent data) in the launcher's addAppWidget. The data is the passed appWidgetId:
Void addAppWidget (Intent data) {<br/> // TODO: catch bad widget exception when sent <br/> int appWidgetId = data. getIntExtra (AppWidgetManager. EXTRA_APPWIDGET_ID,-1); <br/> AppWidgetProviderInfo appWidget = mAppWidgetManager. getAppWidgetInfo (appWidgetId); <br/> // determine whether a configuration page exists. <br/> if (appWidget. configure! = Null) {<br/> // Launch over to configure widget, if needed <br/> Intent intent = new Intent (AppWidgetManager. ACTION_APPWIDGET_CONFIGURE); <br/> intent. setComponent (appWidget. configure); <br/> intent. putExtra (AppWidgetManager. EXTRA_APPWIDGET_ID, appWidgetId); <br/> startActivityForResultSafely (intent, REQUEST_CREATE_APPWIDGET ); <br/>} else {<br/> // Otherwise just add it <br/> onActivityResult (REQUEST_CREATE_APPWIDGET, Activity. RESULT_ OK, data); <br/>}< br/>
Jump back to launcher. onActivityResult through onActivityResult (REQUEST_CREATE_APPWIDGET, Activity. RESULT_ OK, data );
Case REQUEST_CREATE_APPWIDGET:
CompleteAddAppWidget (data, mAddItemCellInfo );
@ Param data The intent describing the appWidgetId. <br/> * @ param cellInfo The position on screen where to create the widget. <br/> */<br/> private void completeAddAppWidget (Intent data, CellLayout. cellInfo cellInfo)
Add widgets in completeAddAppWidget (data, mAddItemCellInfo.
Private void completeaddappwidget (intent data, celllayout. cellinfo) {<br/> bundle extras = data. getextras (); <br/> int appwidgetid = extras. getint (appwidgetmanager. extra_appwidget_id,-1); <br/> If (logd) log. D (TAG, "dumping extras content =" + Extras. tostring (); <br/> // obtain the widget information by appwidgetid, for example, size. <br/> appwidgetproviderinfo appwidgetinfo = mappwidgetmanager. getappwidgetinfo (appwidgeti D); <br/> // calculate the grid spans needed to fit this widget <br/> celllayout layout = (celllayout) mworkspace. getchildat (cellinfo. screen); <br/> int [] spans = layout. recttocell (appwidgetinfo. minwidth, appwidgetinfo. minheight); <br/> // try finding open space on launcher screen <br/> final int [] xy = mcellcoordinates; <br/> If (! Findslot (cellinfo, XY, spans [0], spans [1]) {<br/> If (appwidgetid! =-1) mappwidgethost. deleteappwidgetid (appwidgetid); <br/> return; <br/>}< br/> // build launcher-specific widget info and save to database <br/> launcherappwidgetinfo launcherinfo = new launcherappwidgetinfo (appwidgetid); <br/> launcherinfo. spanx = spans [0]; <br/> launcherinfo. spany = spans [1]; <br/> launchermodel. additemtodatabase (this, launcherinfo, <br/> launchersettings. favorites. container_de Sktop, <br/> mworkspace. getcurrentscreen (), XY [0], XY [1], false); <br/> If (! Mrestoring) {<br/> mshorttopitems. add (launcherinfo); <br/> // perform actual inflation because we're live <br/> launcherinfo. hostview = mappwidgethost. createview (this, appwidgetid, appwidgetinfo); <br/> launcherinfo. hostview. setappwidget (appwidgetid, appwidgetinfo); <br/> launcherinfo. hostview. settag (launcherinfo); <br/> mworkspace. addincurrentscreen (launcherinfo. hostview, XY [0], XY [1], <br/> launcherinfo. spanx, launcherinfo. spany, isworkspacelocked (); <br/>}< br/>