Android2.2 Luncher2扩展(仿Ipad DockBar、HTC预览图、批量添加快捷方式、启用3D效果等)...

1、给Launcher添加DockBar

2、修改Launcher长安AllApps的预览图

(1、2两点请参考何明桂何兄的博客,地址分别为:http://blog.csdn.net/hmg25/article/details/6289438 http://blog.csdn.net/hmg25/article/details/6289725)DockBar的实现,何兄只提供了竖屏,我在其博客的评论中看到不少人说是要实现横屏,下面我将我的实现帖出来。主要是修改layout-land/launcher.xml文件,以下是这个文件的全部代码:

  1. <spanstyle="font-size:18px;"><com.android.launcherex.DragLayer
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
  4. android:id="@+id/drag_layer"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <includelayout="@layout/all_apps"/>
  8. <!--Theworkspacecontains3screensofcells-->
  9. <com.android.launcherex.Workspace
  10. android:id="@+id/workspace"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:scrollbars="horizontal"
  14. android:fadeScrollbars="true"
  15. launcher:defaultScreen="2">
  16. <includeandroid:id="@+id/cell1"layout="@layout/workspace_screen"/>
  17. <includeandroid:id="@+id/cell2"layout="@layout/workspace_screen"/>
  18. <includeandroid:id="@+id/cell3"layout="@layout/workspace_screen"/>
  19. <includeandroid:id="@+id/cell4"layout="@layout/workspace_screen"/>
  20. <includeandroid:id="@+id/cell5"layout="@layout/workspace_screen"/>
  21. </com.android.launcherex.Workspace>
  22. <RelativeLayout
  23. android:id="@+id/dock_layout"
  24. android:layout_gravity="bottom|left"
  25. android:layout_width="match_parent"
  26. android:layout_height="95dip"
  27. >
  28. <com.android.launcherex.ClippedImageView
  29. android:id="@+id/previous_screen"
  30. android:layout_width="93dip"
  31. android:layout_height="@dimen/button_bar_height"
  32. android:layout_alignParentTop="true"
  33. android:layout_alignParentLeft="true"
  34. android:layout_marginLeft="6dip"
  35. android:scaleType="center"
  36. android:src="@drawable/home_arrows_left"
  37. android:onClick="previousScreen"
  38. launcher:ignoreZone="56dip"
  39. android:focusable="true"
  40. android:clickable="true"/>
  41. <com.android.launcherex.ClippedImageView
  42. android:id="@+id/next_screen"
  43. android:layout_width="93dip"
  44. android:layout_height="@dimen/button_bar_height"
  45. android:layout_alignParentTop="true"
  46. android:layout_alignParentRight="true"
  47. android:layout_marginRight="6dip"
  48. android:scaleType="center"
  49. android:src="@drawable/home_arrows_right"
  50. android:onClick="nextScreen"
  51. launcher:ignoreZone="-56dip"
  52. android:focusable="true"
  53. android:clickable="true"/>
  54. <com.android.launcherex.DeleteZone
  55. android:id="@+id/delete_zone"
  56. android:layout_width="@dimen/delete_zone_size"
  57. android:layout_height="@dimen/delete_zone_size"
  58. android:paddingLeft="@dimen/delete_zone_padding"
  59. android:layout_marginBottom="@dimen/dock_bar_height"
  60. android:layout_alignParentBottom="true"
  61. android:layout_centerHorizontal="true"
  62. android:scaleType="center"
  63. android:src="@drawable/delete_zone_selector"
  64. android:visibility="invisible"
  65. launcher:direction="vertical"
  66. />
  67. <RelativeLayout
  68. android:id="@+id/all_apps_button_cluster"
  69. android:layout_height="fill_parent"
  70. android:layout_width="@dimen/button_bar_height_portrait"
  71. android:layout_gravity="bottom|center_vertical"
  72. android:layout_marginBottom="@dimen/half_status_bar_height"
  73. >
  74. <!--<com.android.launcherex.HandleView
  75. style="@style/HotseatButton"
  76. android:id="@+id/all_apps_button"
  77. android:layout_centerVertical="true"
  78. android:layout_alignParentRight="true"
  79. android:src="@drawable/all_apps_button"
  80. launcher:direction="vertical"
  81. />-->
  82. <!--android:layout_below="@id/all_apps_button"-->
  83. <!--android:layout_above="@id/all_apps_button"-->
  84. <ImageView
  85. android:id="@+id/hotseat_left"
  86. style="@style/HotseatButton.Left"
  87. android:src="@drawable/hotseat_phone"
  88. android:visibility="gone"
  89. android:onClick="launchHotSeat"
  90. />
  91. <ImageView
  92. android:id="@+id/hotseat_right"
  93. style="@style/HotseatButton.Right"
  94. android:visibility="gone"
  95. android:src="@drawable/hotseat_browser"
  96. android:onClick="launchHotSeat"
  97. />
  98. </RelativeLayout>
  99. <com.android.launcherex.DockBar
  100. android:id="@+id/dockbar"
  101. android:layout_width="fill_parent"
  102. android:layout_height="@dimen/button_bar_height"
  103. android:background="@drawable/dock_bg"
  104. android:layout_alignParentBottom="true"
  105. launcher:direction="horizontal">
  106. <HorizontalScrollView
  107. android:id="@+id/dock_scroll_view"
  108. android:scrollbars="none"
  109. android:fadingEdge="none"
  110. android:saveEnabled="false"
  111. android:layout_width="fill_parent"
  112. android:layout_height="fill_parent">
  113. <LinearLayout
  114. android:orientation="horizontal"
  115. android:id="@+id/dock_item_holder"
  116. android:saveEnabled="false"
  117. android:layout_width="fill_parent"
  118. android:layout_height="fill_parent">
  119. <com.android.launcherex.HandleView
  120. android:id="@+id/all_apps_button"
  121. android:layout_centerVertical="true"
  122. android:src="@drawable/all_apps_button"
  123. launcher:direction="horizontal"
  124. android:layout_width="fill_parent"
  125. android:layout_height="fill_parent"
  126. android:focusable="true"
  127. android:clickable="true"/>
  128. </LinearLayout>
  129. </HorizontalScrollView>
  130. </com.android.launcherex.DockBar>
  131. </RelativeLayout>
  132. </com.android.launcherex.DragLayer>
  133. </span>
<span style="font-size:18px;"><com.android.launcherex.DragLayer xmlns:android="http://schemas.android.com/apk/res/android" xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher" android:id="@+id/drag_layer" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/all_apps" /> <!-- The workspace contains 3 screens of cells --> <com.android.launcherex.Workspace android:id="@+id/workspace" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="horizontal" android:fadeScrollbars="true" launcher:defaultScreen="2"> <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> <include android:id="@+id/cell2" layout="@layout/workspace_screen" /> <include android:id="@+id/cell3" layout="@layout/workspace_screen" /> <include android:id="@+id/cell4" layout="@layout/workspace_screen" /> <include android:id="@+id/cell5" layout="@layout/workspace_screen" /> </com.android.launcherex.Workspace> <RelativeLayout android:id="@+id/dock_layout" android:layout_gravity="bottom|left" android:layout_width="match_parent" android:layout_height="95dip" > <com.android.launcherex.ClippedImageView android:id="@+id/previous_screen" android:layout_width="93dip" android:layout_height="@dimen/button_bar_height" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginLeft="6dip" android:scaleType="center" android:src="@drawable/home_arrows_left" android:onClick="previousScreen" launcher:ignoreZone="56dip" android:focusable="true" android:clickable="true" /> <com.android.launcherex.ClippedImageView android:id="@+id/next_screen" android:layout_width="93dip" android:layout_height="@dimen/button_bar_height" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_marginRight="6dip" android:scaleType="center" android:src="@drawable/home_arrows_right" android:onClick="nextScreen" launcher:ignoreZone="-56dip" android:focusable="true" android:clickable="true" /> <com.android.launcherex.DeleteZone android:id="@+id/delete_zone" android:layout_width="@dimen/delete_zone_size" android:layout_height="@dimen/delete_zone_size" android:paddingLeft="@dimen/delete_zone_padding" android:layout_marginBottom="@dimen/dock_bar_height" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:scaleType="center" android:src="@drawable/delete_zone_selector" android:visibility="invisible" launcher:direction="vertical" /> <RelativeLayout android:id="@+id/all_apps_button_cluster" android:layout_height="fill_parent" android:layout_width="@dimen/button_bar_height_portrait" android:layout_gravity="bottom|center_vertical" android:layout_marginBottom="@dimen/half_status_bar_height" > <!-- <com.android.launcherex.HandleView style="@style/HotseatButton" android:id="@+id/all_apps_button" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:src="@drawable/all_apps_button" launcher:direction="vertical" />--> <!-- android:layout_below="@id/all_apps_button" --> <!-- android:layout_above="@id/all_apps_button" --> <ImageView android:id="@+id/hotseat_left" style="@style/HotseatButton.Left" android:src="@drawable/hotseat_phone" android:visibility="gone" android:onClick="launchHotSeat" /> <ImageView android:id="@+id/hotseat_right" style="@style/HotseatButton.Right" android:visibility="gone" android:src="@drawable/hotseat_browser" android:onClick="launchHotSeat" /> </RelativeLayout> <com.android.launcherex.DockBar android:id="@+id/dockbar" android:layout_width="fill_parent" android:layout_height="@dimen/button_bar_height" android:background="@drawable/dock_bg" android:layout_alignParentBottom="true" launcher:direction="horizontal"> <HorizontalScrollView android:id="@+id/dock_scroll_view" android:scrollbars="none" android:fadingEdge="none" android:saveEnabled="false" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:id="@+id/dock_item_holder" android:saveEnabled="false" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.android.launcherex.HandleView android:id="@+id/all_apps_button" android:layout_centerVertical="true" android:src="@drawable/all_apps_button" launcher:direction="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="true" android:clickable="true" /> </LinearLayout> </HorizontalScrollView> </com.android.launcherex.DockBar> </RelativeLayout> </com.android.launcherex.DragLayer> </span>

3、启用3D效果

3D效果启动很简单,只需要将all_apps.xml文件下的

<include layout="@layout/all_apps_2d" />改为:

<include layout="@layout/all_apps_3d" />

即可。当然你可能还需要对图标做一些调整。我主要是调整了一下几个参数:

  1. <spanstyle="font-size:18px;">publicstaticfinalintICON_WIDTH_PX=64;
  2. publicstaticfinalintICON_TEXTURE_WIDTH_PX=74;
  3. publicstaticfinalintSELECTION_TEXTURE_WIDTH_PX=74+20;
  4. publicstaticfinalintICON_HEIGHT_PX=64;
  5. publicstaticfinalintICON_TEXTURE_HEIGHT_PX=74;
  6. publicstaticfinalintSELECTION_TEXTURE_HEIGHT_PX=74+20;</span>
<span style="font-size:18px;"> public static final int ICON_WIDTH_PX = 64; public static final int ICON_TEXTURE_WIDTH_PX = 74; public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20; public static final int ICON_HEIGHT_PX = 64; public static final int ICON_TEXTURE_HEIGHT_PX = 74; public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;</span>

4、替换应用程序图标

主要是在IconCache中做了以下修改。修改的只是Launcher上显示的图标,长按Home键显示最近开启的应用时,图标还是原先默认的图标,这些就自己去改吧。
  1. <spanstyle="font-size:18px;">privateCacheEntrycacheLocked(ComponentNamecomponentName,ResolveInfoinfo){
  2. CacheEntryentry=mCache.get(componentName);
  3. if(entry==null){
  4. entry=newCacheEntry();
  5. mCache.put(componentName,entry);
  6. entry.title=info.loadLabel(mPackageManager).toString();
  7. if(entry.title==null){
  8. entry.title=info.activityInfo.name;
  9. }
  10. //entry.icon=Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager),mContext);
  11. entry.icon=suBIcon(info);
  12. }
  13. returnentry;
  14. }
  15. /**
  16. *ReplacethedefaultLaunchericon
  17. *@paraminfo
  18. *@return
  19. */
  20. privateBitmapsuBIcon(ResolveInfoinfo){
  21. Drawabledrawable=null;
  22. Stringpname=info.activityInfo.packageName;
  23. if(pname.equals(Constant.PACKAGE_VIDEOPLAYER)){
  24. drawable=mResources.getDrawable(R.drawable.lu_videoplayer);
  25. }elseif(pname.equals(Constant.PACKAGENAME_BROWSER)){
  26. drawable=mResources.getDrawable(R.drawable.lu_browser);
  27. }elseif(pname.equals(Constant.PACKAGE_CALCULATOR)){
  28. drawable=mResources.getDrawable(R.drawable.lu_calculator2);
  29. }elseif(pname.equals(Constant.PACKAGE_CALENDAR)){
  30. drawable=mResources.getDrawable(R.drawable.lu_calendar);
  31. }elseif(pname.equals(Constant.PACKAGE_CAMERA)){
  32. drawable=mResources.getDrawable(R.drawable.lu_camera);
  33. }elseif(pname.equals(Constant.PACKAGE_CLOCK)){
  34. drawable=mResources.getDrawable(R.drawable.lu_alarmclock);
  35. }elseif(pname.equals(Constant.PACKAGE_EMAIL)){
  36. drawable=mResources.getDrawable(R.drawable.lu_email);
  37. }elseif(pname.equals(Constant.PACKAGE_GALLERY)){
  38. drawable=mResources.getDrawable(R.drawable.lu_gallery);
  39. }elseif(pname.equals(Constant.PACKAGE_MUSIC)){
  40. drawable=mResources.getDrawable(R.drawable.lu_music);
  41. }elseif(pname.equals(Constant.PACKAGE_SETTINGS)){
  42. drawable=mResources.getDrawable(R.drawable.lu_settings);
  43. }
  44. if(drawable!=null){
  45. returnUtilities.createIconBitmap(drawable,mContext);
  46. }else{
  47. returnUtilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager),mContext);
  48. }
  49. }</span>
<span style="font-size:18px;">private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) { CacheEntry entry = mCache.get(componentName); if (entry == null) { entry = new CacheEntry(); mCache.put(componentName, entry); entry.title = info.loadLabel(mPackageManager).toString(); if (entry.title == null) { entry.title = info.activityInfo.name; } // entry.icon = Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager), mContext); entry.icon = suBIcon(info); } return entry; } /** * Replace the default Launcher icon * @param info * @return */ private Bitmap suBIcon(ResolveInfo info){ Drawable drawable = null; String pname = info.activityInfo.packageName; if(pname.equals(Constant.PACKAGE_VIDEOPLAYER)){ drawable = mResources.getDrawable(R.drawable.lu_videoplayer); }else if(pname.equals(Constant.PACKAGENAME_BROWSER)){ drawable = mResources.getDrawable(R.drawable.lu_browser); }else if(pname.equals(Constant.PACKAGE_CALCULATOR)){ drawable = mResources.getDrawable(R.drawable.lu_calculator2); }else if(pname.equals(Constant.PACKAGE_CALENDAR)){ drawable = mResources.getDrawable(R.drawable.lu_calendar); }else if(pname.equals(Constant.PACKAGE_CAMERA)){ drawable = mResources.getDrawable(R.drawable.lu_camera); }else if(pname.equals(Constant.PACKAGE_CLOCK)){ drawable = mResources.getDrawable(R.drawable.lu_alarmclock); }else if(pname.equals(Constant.PACKAGE_EMAIL)){ drawable = mResources.getDrawable(R.drawable.lu_email); }else if(pname.equals(Constant.PACKAGE_GALLERY)){ drawable = mResources.getDrawable(R.drawable.lu_gallery); }else if(pname.equals(Constant.PACKAGE_MUSIC)){ drawable = mResources.getDrawable(R.drawable.lu_music); }else if(pname.equals(Constant.PACKAGE_SETTINGS)){ drawable = mResources.getDrawable(R.drawable.lu_settings); } if(drawable!=null){ return Utilities.createIconBitmap(drawable, mContext); }else{ return Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager), mContext); } }</span>

5、批量添加快捷方式

操作:长按桌面 -- Add to Home Screen点击Applications即显示所有安装应用程序列表。可任意选择多个应用程序的快捷方式添加到Launcher上。
1、Activity类:AddShortcutsActivity 主要用于显示安装应用程序信息和添加操作界面
2、列表适配: AddShortcutsAdapter 继承BaseAdapter。
3、快捷方式实体类:ShortcutsEntity
4、add_shortcuts_adapter.xml
5、add_shortcuts.xml

直接上代码:

  1. <spanstyle="font-size:18px;">packagecom.android.launcherex;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importandroid.app.Activity;
  5. importandroid.content.Intent;
  6. importandroid.content.pm.ActivityInfo;
  7. importandroid.content.pm.PackageManager;
  8. importandroid.content.pm.ResolveInfo;
  9. importandroid.os.Bundle;
  10. importandroid.os.Parcelable;
  11. importandroid.util.Log;
  12. importandroid.view.View;
  13. importandroid.view.View.OnClickListener;
  14. importandroid.widget.AdapterView;
  15. importandroid.widget.AdapterView.OnItemClickListener;
  16. importandroid.widget.Button;
  17. importandroid.widget.ListView;
  18. importcom.android.launcher.R;
  19. publicclassAddShortcutsActivityextendsActivityimplementsOnClickListener,OnItemClickListener{
  20. privateAddShortcutsAdaptermShortcutsAdapter;
  21. privatefinalstaticStringTAG="AddShortcutsActivity";
  22. privateListViewlstShortcuts;
  23. privateButtonbtnAddShortcuts;
  24. privateButtonbtnCancelShortcuts;
  25. privateArrayList<ShortcutsEntity>mShortcutsEntity=newArrayList<ShortcutsEntity>();
  26. @Override
  27. protectedvoidonCreate(BundlesavedInstanceState){
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.add_shortcuts);
  30. setupViews();
  31. }
  32. privatevoidsetupViews(){
  33. lstShortcuts=(ListView)findViewById(R.id.shortcuts_list);
  34. //lstShortcuts.setItemsCanFocus(false);
  35. lstShortcuts.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  36. btnAddShortcuts=(Button)findViewById(R.id.shortcuts_add);
  37. btnCancelShortcuts=(Button)findViewById(R.id.shortcuts_cancel);
  38. btnAddShortcuts.setOnClickListener(this);
  39. btnCancelShortcuts.setOnClickListener(this);
  40. lstShortcuts.setOnItemClickListener(this);
  41. mShortcutsEntity=getAllShortcuts();
  42. mShortcutsAdapter=newAddShortcutsAdapter(mShortcutsEntity,this);
  43. lstShortcuts.setAdapter(mShortcutsAdapter);
  44. }
  45. publicvoidonClick(Viewarg0){
  46. switch(arg0.getId()){
  47. caseR.id.shortcuts_add:
  48. ArrayList<Intent>intents=getSelectedShortcuts();
  49. Intentintent=getIntent();
  50. //Readbaseintentfromextras,otherwiseassumedefault
  51. Parcelableparcel=intent.getParcelableExtra(Intent.EXTRA_INTENT);
  52. if(parcelinstanceofIntent){
  53. intent=(Intent)parcel;
  54. }else{
  55. intent=newIntent(Intent.ACTION_MAIN,null);
  56. intent.addCategory(Intent.CATEGORY_DEFAULT);
  57. }
  58. Bundlebundle=newBundle();
  59. bundle.putSerializable("intents",intents);
  60. intent.putExtras(bundle);
  61. setResult(Activity.RESULT_OK,intent);
  62. finish();
  63. break;
  64. caseR.id.shortcuts_cancel:
  65. finish();
  66. break;
  67. }
  68. }
  69. /**
  70. *Getalltheapplications
  71. *@return
  72. */
  73. privateArrayList<ShortcutsEntity>getAllShortcuts(){
  74. ArrayList<ShortcutsEntity>entitys=newArrayList<ShortcutsEntity>();
  75. Intentintent=newIntent(Intent.ACTION_MAIN);
  76. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  77. PackageManagermPackageManager=this.getPackageManager();
  78. List<ResolveInfo>mAllApps=mPackageManager.queryIntentActivities(intent,0);
  79. intsize=mAllApps.size();
  80. for(inti=0;i<size;i++){
  81. ShortcutsEntityentity=newShortcutsEntity();
  82. ResolveInforesolveInfo=mAllApps.get(i);
  83. entity.Title=resolveInfo.loadLabel(mPackageManager);
  84. entity.Icon=resolveInfo.loadIcon(mPackageManager);
  85. entity.Info=resolveInfo;
  86. entitys.add(entity);
  87. Log.i(TAG,"entity.Title="+resolveInfo.loadLabel(mPackageManager));
  88. }
  89. //TODO:sort
  90. returnentitys;
  91. }
  92. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){
  93. mShortcutsAdapter.setSelectItem(arg2);
  94. mShortcutsAdapter.notifyDataSetChanged();
  95. }
  96. /**
  97. *Beenselectedshortcuts
  98. *@return
  99. */
  100. privateArrayList<Intent>getSelectedShortcuts(){
  101. ArrayList<Intent>selecteds=newArrayList<Intent>();
  102. ArrayList<Boolean>isCheck=AddShortcutsAdapter.isChecked;
  103. intcheckSize=isCheck.size();
  104. for(inti=0;i<checkSize;i++){
  105. if(isCheck.get(i)){
  106. ShortcutsEntityentity=mShortcutsEntity.get(i);
  107. ActivityInfoactivityInfo=entity.Info.activityInfo;
  108. Intentintent=newIntent();
  109. intent.setClassName(activityInfo.applicationInfo.packageName,activityInfo.name);
  110. selecteds.add(intent);
  111. Log.i(TAG,"selecteditem.name="+mShortcutsEntity.get(i).Title);
  112. }
  113. }
  114. returnselecteds;
  115. }
  116. }
  117. </span>
<span style="font-size:18px;">package com.android.launcherex; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.os.Parcelable; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import com.android.launcher.R; public class AddShortcutsActivity extends Activity implements OnClickListener,OnItemClickListener{ private AddShortcutsAdapter mShortcutsAdapter; private final static String TAG = "AddShortcutsActivity"; private ListView lstShortcuts; private Button btnAddShortcuts; private Button btnCancelShortcuts; private ArrayList<ShortcutsEntity> mShortcutsEntity = new ArrayList<ShortcutsEntity>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_shortcuts); setupViews(); } private void setupViews(){ lstShortcuts = (ListView) findViewById(R.id.shortcuts_list); // lstShortcuts.setItemsCanFocus(false); lstShortcuts.setChoiceMode(ListView.CHOICE_MODE_SINGLE); btnAddShortcuts = (Button) findViewById(R.id.shortcuts_add); btnCancelShortcuts=(Button) findViewById(R.id.shortcuts_cancel); btnAddShortcuts.setOnClickListener(this); btnCancelShortcuts.setOnClickListener(this); lstShortcuts.setOnItemClickListener(this); mShortcutsEntity = getAllShortcuts(); mShortcutsAdapter = new AddShortcutsAdapter(mShortcutsEntity,this); lstShortcuts.setAdapter(mShortcutsAdapter); } public void onClick(View arg0) { switch(arg0.getId()){ case R.id.shortcuts_add: ArrayList<Intent> intents = getSelectedShortcuts(); Intent intent = getIntent(); // Read base intent from extras, otherwise assume default Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT); if (parcel instanceof Intent) { intent = (Intent) parcel; } else { intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_DEFAULT); } Bundle bundle = new Bundle(); bundle.putSerializable("intents", intents); intent.putExtras(bundle); setResult(Activity.RESULT_OK,intent); finish(); break; case R.id.shortcuts_cancel: finish(); break; } } /** * Get all the applications * @return */ private ArrayList<ShortcutsEntity> getAllShortcuts(){ ArrayList<ShortcutsEntity> entitys = new ArrayList<ShortcutsEntity>(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); PackageManager mPackageManager = this.getPackageManager(); List<ResolveInfo> mAllApps = mPackageManager.queryIntentActivities(intent, 0); int size = mAllApps.size(); for(int i=0;i<size;i++){ ShortcutsEntity entity = new ShortcutsEntity(); ResolveInfo resolveInfo = mAllApps.get(i); entity.Title = resolveInfo.loadLabel(mPackageManager); entity.Icon = resolveInfo.loadIcon(mPackageManager); entity.Info = resolveInfo; entitys.add(entity); Log.i(TAG, "entity.Title="+resolveInfo.loadLabel(mPackageManager)); } //TODO: sort return entitys; } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { mShortcutsAdapter.setSelectItem(arg2); mShortcutsAdapter.notifyDataSetChanged(); } /** * Been selected shortcuts * @return */ private ArrayList<Intent> getSelectedShortcuts(){ ArrayList<Intent> selecteds = new ArrayList<Intent>(); ArrayList<Boolean> isCheck = AddShortcutsAdapter.isChecked; int checkSize = isCheck.size(); for(int i=0;i<checkSize;i++){ if(isCheck.get(i)){ ShortcutsEntity entity = mShortcutsEntity.get(i); ActivityInfo activityInfo = entity.Info.activityInfo; Intent intent = new Intent(); intent.setClassName(activityInfo.applicationInfo.packageName,activityInfo.name); selecteds.add(intent); Log.i(TAG, "selected item.name="+mShortcutsEntity.get(i).Title); } } return selecteds; } } </span>
  1. <spanstyle="font-size:18px;">packagecom.android.launcherex;
  2. importjava.util.ArrayList;
  3. importcom.android.launcher.R;
  4. importandroid.content.Context;
  5. importandroid.view.LayoutInflater;
  6. importandroid.view.View;
  7. importandroid.view.ViewGroup;
  8. importandroid.widget.BaseAdapter;
  9. importandroid.widget.CheckBox;
  10. importandroid.widget.ImageView;
  11. importandroid.widget.TextView;
  12. publicclassAddShortcutsAdapterextendsBaseAdapter{
  13. privateArrayList<ShortcutsEntity>mShortcuts;
  14. privatefinalLayoutInflatermInflater;
  15. privateContextmContext;
  16. publicstaticArrayList<Boolean>isChecked;
  17. publicAddShortcutsAdapter(ArrayList<ShortcutsEntity>shortcuts,Contextcontext)
  18. {
  19. mShortcuts=shortcuts;
  20. mContext=context;
  21. mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  22. isChecked=newArrayList<Boolean>();
  23. intsize=mShortcuts.size();
  24. for(inti=0;i<size;i++){
  25. isChecked.add(false);
  26. }
  27. }
  28. publicintgetCount(){
  29. if(mShortcuts!=null){
  30. returnmShortcuts.size();
  31. }
  32. return0;
  33. }
  34. publicObjectgetItem(intarg0){
  35. if(mShortcuts!=null){
  36. returnmShortcuts.get(arg0);
  37. }
  38. returnnull;
  39. }
  40. publiclonggetItemId(intarg0){
  41. return0;
  42. }
  43. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  44. ViewHolderholder=null;
  45. ShortcutsEntityentity=mShortcuts.get(position);
  46. if(convertView==null){
  47. holder=newViewHolder();
  48. convertView=mInflater.inflate(R.layout.add_shortcuts_adapter,parent,false);
  49. holder.imageView=(ImageView)convertView.findViewById(R.id.sc_icon);
  50. holder.textView=(TextView)convertView.findViewById(R.id.sc_title);
  51. holder.checkBox=(CheckBox)convertView.findViewById(R.id.sc_checkbox);
  52. convertView.setTag(holder);
  53. }else{
  54. holder=(ViewHolder)convertView.getTag();
  55. }
  56. if(entity!=null){
  57. holder.textView.setText(entity.Title);
  58. //TODO:Modifythepicturesize.
  59. holder.imageView.setBackgroundDrawable(Utilities.getDrawable(entity.Icon));
  60. }
  61. if(isChecked!=null&&!isChecked.isEmpty()){
  62. holder.checkBox.setChecked(isChecked.get(position));
  63. }
  64. returnconvertView;
  65. }
  66. publicvoidsetSelectItem(intitem){
  67. if(isChecked.get(item)){
  68. isChecked.set(item,false);
  69. }else{
  70. isChecked.set(item,true);
  71. }
  72. }
  73. privateclassViewHolder{
  74. ImageViewimageView;
  75. TextViewtextView;
  76. CheckBoxcheckBox;
  77. }
  78. }
  79. </span>
<span style="font-size:18px;">package com.android.launcherex; import java.util.ArrayList; import com.android.launcher.R; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; public class AddShortcutsAdapter extends BaseAdapter { private ArrayList<ShortcutsEntity> mShortcuts; private final LayoutInflater mInflater; private Context mContext; public static ArrayList<Boolean> isChecked; public AddShortcutsAdapter(ArrayList<ShortcutsEntity> shortcuts,Context context) { mShortcuts = shortcuts; mContext = context; mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); isChecked = new ArrayList<Boolean>(); int size = mShortcuts.size(); for(int i=0;i<size;i++){ isChecked.add(false); } } public int getCount() { if(mShortcuts!=null){ return mShortcuts.size(); } return 0; } public Object getItem(int arg0) { if(mShortcuts!=null){ return mShortcuts.get(arg0); } return null; } public long getItemId(int arg0) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; ShortcutsEntity entity = mShortcuts.get(position); if(convertView==null){ holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.add_shortcuts_adapter, parent, false); holder.imageView = (ImageView) convertView.findViewById(R.id.sc_icon); holder.textView = (TextView) convertView.findViewById(R.id.sc_title); holder.checkBox = (CheckBox) convertView.findViewById(R.id.sc_checkbox); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } if(entity!=null){ holder.textView.setText(entity.Title); //TODO: Modify the picture size. holder.imageView.setBackgroundDrawable(Utilities.getDrawable(entity.Icon)); } if(isChecked!=null && !isChecked.isEmpty()){ holder.checkBox.setChecked(isChecked.get(position)); } return convertView; } public void setSelectItem(int item){ if(isChecked.get(item)){ isChecked.set(item, false); }else{ isChecked.set(item,true); } } private class ViewHolder{ ImageView imageView; TextView textView; CheckBox checkBox; } } </span>
  1. <spanstyle="font-size:18px;">packagecom.android.launcherex;
  2. importandroid.content.pm.ResolveInfo;
  3. importandroid.graphics.drawable.Drawable;
  4. publicclassShortcutsEntity{
  5. publicCharSequenceTitle;
  6. publicDrawableIcon;
  7. publicResolveInfoInfo;
  8. }
  9. </span>
<span style="font-size:18px;">package com.android.launcherex; import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; public class ShortcutsEntity { public CharSequence Title; public Drawable Icon; public ResolveInfo Info; } </span>
  1. <spanstyle="font-size:18px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ImageView
  8. android:id="@+id/sc_icon"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentTop="true"
  12. android:layout_alignParentLeft="true"
  13. android:layout_marginLeft="6dip"/>
  14. <TextView
  15. android:id="@+id/sc_title"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_alignParentTop="true"
  19. android:layout_toRightOf="@+id/sc_icon"
  20. android:layout_marginLeft="4dip"/>
  21. <CheckBox
  22. android:id="@+id/sc_checkbox"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_alignParentTop="true"
  26. android:layout_alignParentRight="true"
  27. android:focusable="false"
  28. android:layout_marginRight="6dip"/>
  29. </RelativeLayout>
  30. </span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/sc_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginLeft="6dip"/> <TextView android:id="@+id/sc_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/sc_icon" android:layout_marginLeft="4dip"/> <CheckBox android:id="@+id/sc_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:focusable="false" android:layout_marginRight="6dip"/> </RelativeLayout> </span>
  1. <spanstyle="font-size:18px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ListView
  8. android:id="@+id/shortcuts_list"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentTop="true"
  13. android:layout_above="@+id/shortcuts_layout"
  14. />
  15. <LinearLayout
  16. android:id="@+id/shortcuts_layout"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:orientation="horizontal"
  20. android:layout_alignParentLeft="true"
  21. android:layout_alignParentBottom="true"
  22. >
  23. <Button
  24. android:id="@+id/shortcuts_add"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="1"
  28. android:text="@string/shortcuts_add_text"/>
  29. <Button
  30. android:id="@+id/shortcuts_cancel"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="1"
  34. android:text="@string/shortcuts_cancel_text"
  35. />
  36. </LinearLayout>
  37. </RelativeLayout>
  38. </span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/shortcuts_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_above="@+id/shortcuts_layout" /> <LinearLayout android:id="@+id/shortcuts_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" > <Button android:id="@+id/shortcuts_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/shortcuts_add_text"/> <Button android:id="@+id/shortcuts_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/shortcuts_cancel_text" /> </LinearLayout> </RelativeLayout> </span>
最后在Launcher.java的onActivityResult方法中添加如下代码,即大功告成
  1. <spanstyle="font-size:18px;">Bundlebundle=data.getExtras();
  2. ArrayList<Intent>intents=(ArrayList<Intent>)bundle.getSerializable("intents");
  3. if(intents!=null&&!intents.isEmpty()){
  4. intsize=intents.size();
  5. for(inti=0;i<size;i++){
  6. mAddItemCellInfo=newCellLayout.CellInfo();//Shortcuttosolvetheproblemofoverlapping
  7. completeAddApplication(this,intents.get(i),mAddItemCellInfo);
  8. }
  9. }
  10. break;</span>
<span style="font-size:18px;">Bundle bundle = data.getExtras(); ArrayList<Intent> intents = (ArrayList<Intent>) bundle.getSerializable("intents"); if (intents != null && !intents.isEmpty()) { int size = intents.size(); for (int i = 0; i < size; i++) { mAddItemCellInfo = new CellLayout.CellInfo(); // Shortcut to solve the problem of overlapping completeAddApplication(this, intents.get(i),mAddItemCellInfo); } } break;</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值