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文件,以下是这个文件的全部代码:

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

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

view plain print ?
  1. <span style="font-size:18px;">        public static final int ICON_WIDTH_PX = 64;  
  2.         public static final int ICON_TEXTURE_WIDTH_PX = 74;  
  3.         public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;  
  4.   
  5.         public static final int ICON_HEIGHT_PX = 64;  
  6.         public static final int ICON_TEXTURE_HEIGHT_PX = 74;  
  7.         public static final int SELECTION_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键显示最近开启的应用时,图标还是原先默认的图标,这些就自己去改吧。
view plain print ?
  1. <span style="font-size:18px;">private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {  
  2.         CacheEntry entry = mCache.get(componentName);  
  3.         if (entry == null) {  
  4.             entry = new CacheEntry();  
  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.               
  12.             entry.icon = suBIcon(info);  
  13.         }  
  14.         return entry;  
  15.     }  
  16.       
  17.     /** 
  18.      * Replace the default Launcher icon 
  19.      * @param info 
  20.      * @return 
  21.      */  
  22.     private Bitmap suBIcon(ResolveInfo info){  
  23.         Drawable drawable = null;  
  24.         String pname = info.activityInfo.packageName;  
  25.           
  26.         if(pname.equals(Constant.PACKAGE_VIDEOPLAYER)){  
  27.             drawable = mResources.getDrawable(R.drawable.lu_videoplayer);  
  28.         }else if(pname.equals(Constant.PACKAGENAME_BROWSER)){  
  29.             drawable = mResources.getDrawable(R.drawable.lu_browser);  
  30.         }else if(pname.equals(Constant.PACKAGE_CALCULATOR)){  
  31.             drawable = mResources.getDrawable(R.drawable.lu_calculator2);  
  32.         }else if(pname.equals(Constant.PACKAGE_CALENDAR)){  
  33.             drawable = mResources.getDrawable(R.drawable.lu_calendar);  
  34.         }else if(pname.equals(Constant.PACKAGE_CAMERA)){  
  35.             drawable = mResources.getDrawable(R.drawable.lu_camera);  
  36.         }else if(pname.equals(Constant.PACKAGE_CLOCK)){  
  37.             drawable = mResources.getDrawable(R.drawable.lu_alarmclock);  
  38.         }else if(pname.equals(Constant.PACKAGE_EMAIL)){  
  39.             drawable = mResources.getDrawable(R.drawable.lu_email);  
  40.         }else if(pname.equals(Constant.PACKAGE_GALLERY)){  
  41.             drawable = mResources.getDrawable(R.drawable.lu_gallery);  
  42.         }else if(pname.equals(Constant.PACKAGE_MUSIC)){  
  43.             drawable = mResources.getDrawable(R.drawable.lu_music);  
  44.         }else if(pname.equals(Constant.PACKAGE_SETTINGS)){  
  45.             drawable = mResources.getDrawable(R.drawable.lu_settings);  
  46.         }  
  47.           
  48.         if(drawable!=null){  
  49.             return Utilities.createIconBitmap(drawable, mContext);  
  50.         }else{  
  51.             return Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager), mContext);  
  52.         }  
  53.     }</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

直接上代码:

view plain print ?
  1. <span style="font-size:18px;">package com.android.launcherex;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.content.pm.ActivityInfo;  
  9. import android.content.pm.PackageManager;  
  10. import android.content.pm.ResolveInfo;  
  11. import android.os.Bundle;  
  12. import android.os.Parcelable;  
  13. import android.util.Log;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.AdapterView;  
  17. import android.widget.AdapterView.OnItemClickListener;  
  18. import android.widget.Button;  
  19. import android.widget.ListView;  
  20.   
  21. import com.android.launcher.R;  
  22.   
  23. public class AddShortcutsActivity extends Activity implements OnClickListener,OnItemClickListener{  
  24.   
  25.     private AddShortcutsAdapter mShortcutsAdapter;  
  26.       
  27.     private final static String TAG = "AddShortcutsActivity";  
  28.       
  29.     private ListView lstShortcuts;  
  30.     private Button btnAddShortcuts;  
  31.     private Button btnCancelShortcuts;  
  32.       
  33.     private ArrayList<ShortcutsEntity> mShortcutsEntity = new ArrayList<ShortcutsEntity>();  
  34.       
  35.     @Override  
  36.     protected void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.add_shortcuts);  
  39.           
  40.         setupViews();  
  41.     }  
  42.       
  43.     private void setupViews(){  
  44.         lstShortcuts = (ListView) findViewById(R.id.shortcuts_list);  
  45. //      lstShortcuts.setItemsCanFocus(false);  
  46.         lstShortcuts.setChoiceMode(ListView.CHOICE_MODE_SINGLE);   
  47.           
  48.         btnAddShortcuts = (Button) findViewById(R.id.shortcuts_add);  
  49.         btnCancelShortcuts=(Button) findViewById(R.id.shortcuts_cancel);  
  50.           
  51.         btnAddShortcuts.setOnClickListener(this);  
  52.         btnCancelShortcuts.setOnClickListener(this);  
  53.         lstShortcuts.setOnItemClickListener(this);  
  54.           
  55.         mShortcutsEntity = getAllShortcuts();  
  56.           
  57.         mShortcutsAdapter = new AddShortcutsAdapter(mShortcutsEntity,this);  
  58.         lstShortcuts.setAdapter(mShortcutsAdapter);  
  59.           
  60.     }  
  61.   
  62.     public void onClick(View arg0) {  
  63.         switch(arg0.getId()){  
  64.         case R.id.shortcuts_add:  
  65.               
  66.             ArrayList<Intent> intents = getSelectedShortcuts();  
  67.             Intent intent = getIntent();  
  68.             // Read base intent from extras, otherwise assume default  
  69.             Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT);  
  70.               
  71.             if (parcel instanceof Intent) {  
  72.                 intent = (Intent) parcel;  
  73.             } else {  
  74.                 intent = new Intent(Intent.ACTION_MAIN, null);  
  75.                 intent.addCategory(Intent.CATEGORY_DEFAULT);  
  76.             }  
  77.               
  78.             Bundle bundle = new Bundle();  
  79.             bundle.putSerializable("intents", intents);  
  80.             intent.putExtras(bundle);  
  81.               
  82.             setResult(Activity.RESULT_OK,intent);  
  83.               
  84.               
  85.             finish();  
  86.               
  87.             break;  
  88.         case R.id.shortcuts_cancel:  
  89.             finish();  
  90.             break;  
  91.         }  
  92.     }  
  93.       
  94.     /** 
  95.      * Get all the applications 
  96.      * @return 
  97.      */  
  98.     private ArrayList<ShortcutsEntity> getAllShortcuts(){  
  99.           
  100.         ArrayList<ShortcutsEntity> entitys = new ArrayList<ShortcutsEntity>();  
  101.           
  102.         Intent intent = new Intent(Intent.ACTION_MAIN);  
  103.         intent.addCategory(Intent.CATEGORY_LAUNCHER);  
  104.           
  105.         PackageManager mPackageManager = this.getPackageManager();  
  106.         List<ResolveInfo> mAllApps = mPackageManager.queryIntentActivities(intent, 0);  
  107.           
  108.         int size = mAllApps.size();  
  109.           
  110.         for(int i=0;i<size;i++){  
  111.               
  112.             ShortcutsEntity entity = new ShortcutsEntity();  
  113.             ResolveInfo resolveInfo = mAllApps.get(i);  
  114.             entity.Title = resolveInfo.loadLabel(mPackageManager);  
  115.             entity.Icon = resolveInfo.loadIcon(mPackageManager);  
  116.             entity.Info = resolveInfo;  
  117.             entitys.add(entity);  
  118.               
  119.             Log.i(TAG, "entity.Title="+resolveInfo.loadLabel(mPackageManager));  
  120.         }  
  121.           
  122.         //TODO: sort  
  123.           
  124.         return entitys;  
  125.     }  
  126.   
  127.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  128.         mShortcutsAdapter.setSelectItem(arg2);  
  129.         mShortcutsAdapter.notifyDataSetChanged();  
  130.     }  
  131.       
  132.     /** 
  133.      * Been selected shortcuts 
  134.      * @return 
  135.      */  
  136.     private ArrayList<Intent> getSelectedShortcuts(){  
  137.           
  138.         ArrayList<Intent> selecteds = new ArrayList<Intent>();  
  139.           
  140.         ArrayList<Boolean> isCheck = AddShortcutsAdapter.isChecked;  
  141.         int checkSize =  isCheck.size();  
  142.         for(int i=0;i<checkSize;i++){  
  143.             if(isCheck.get(i)){  
  144.                 ShortcutsEntity entity = mShortcutsEntity.get(i);  
  145.                 ActivityInfo activityInfo = entity.Info.activityInfo;  
  146.                 Intent intent = new Intent();  
  147.                 intent.setClassName(activityInfo.applicationInfo.packageName,activityInfo.name);  
  148.                 selecteds.add(intent);  
  149.                 Log.i(TAG, "selected item.name="+mShortcutsEntity.get(i).Title);  
  150.             }  
  151.         }  
  152.           
  153.         return selecteds;  
  154.     }  
  155. }  
  156. </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>
view plain print ?
  1. <span style="font-size:18px;">package com.android.launcherex;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import com.android.launcher.R;  
  6.   
  7. import android.content.Context;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.BaseAdapter;  
  12. import android.widget.CheckBox;  
  13. import android.widget.ImageView;  
  14. import android.widget.TextView;  
  15.   
  16. public class AddShortcutsAdapter extends BaseAdapter {  
  17.   
  18.     private ArrayList<ShortcutsEntity> mShortcuts;  
  19.     private final LayoutInflater mInflater;  
  20.     private Context mContext;  
  21.     public static ArrayList<Boolean> isChecked;  
  22.       
  23.     public AddShortcutsAdapter(ArrayList<ShortcutsEntity> shortcuts,Context context)  
  24.     {  
  25.         mShortcuts = shortcuts;  
  26.         mContext = context;  
  27.         mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  28.           
  29.         isChecked = new ArrayList<Boolean>();  
  30.           
  31.         int size = mShortcuts.size();  
  32.         for(int i=0;i<size;i++){  
  33.             isChecked.add(false);  
  34.         }  
  35.     }  
  36.       
  37.     public int getCount() {  
  38.         if(mShortcuts!=null){  
  39.             return mShortcuts.size();  
  40.         }  
  41.         return 0;  
  42.     }  
  43.   
  44.     public Object getItem(int arg0) {  
  45.         if(mShortcuts!=null){  
  46.             return mShortcuts.get(arg0);  
  47.         }  
  48.         return null;  
  49.     }  
  50.   
  51.     public long getItemId(int arg0) {  
  52.         return 0;  
  53.     }  
  54.   
  55.     public View getView(int position, View convertView, ViewGroup parent) {  
  56.           
  57.         ViewHolder holder = null;  
  58.         ShortcutsEntity entity = mShortcuts.get(position);  
  59.           
  60.         if(convertView==null){  
  61.             holder = new ViewHolder();  
  62.             convertView = mInflater.inflate(R.layout.add_shortcuts_adapter, parent, false);  
  63.               
  64.             holder.imageView = (ImageView) convertView.findViewById(R.id.sc_icon);  
  65.             holder.textView = (TextView) convertView.findViewById(R.id.sc_title);  
  66.             holder.checkBox = (CheckBox) convertView.findViewById(R.id.sc_checkbox);  
  67.               
  68.             convertView.setTag(holder);  
  69.         }else{  
  70.             holder = (ViewHolder) convertView.getTag();  
  71.         }  
  72.           
  73.         if(entity!=null){  
  74.             holder.textView.setText(entity.Title);  
  75.             //TODO: Modify the picture size.  
  76.             holder.imageView.setBackgroundDrawable(Utilities.getDrawable(entity.Icon));  
  77.         }  
  78.           
  79.         if(isChecked!=null && !isChecked.isEmpty()){  
  80.             holder.checkBox.setChecked(isChecked.get(position));  
  81.         }  
  82.           
  83.         return convertView;  
  84.     }  
  85.       
  86.     public void setSelectItem(int item){  
  87.         if(isChecked.get(item)){  
  88.             isChecked.set(item, false);  
  89.         }else{  
  90.             isChecked.set(item,true);  
  91.         }  
  92.     }  
  93.       
  94.     private class ViewHolder{  
  95.         ImageView imageView;  
  96.         TextView textView;  
  97.         CheckBox checkBox;  
  98.     }  
  99. }  
  100. </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>
view plain print ?
  1. <span style="font-size:18px;">package com.android.launcherex;  
  2.   
  3. import android.content.pm.ResolveInfo;  
  4. import android.graphics.drawable.Drawable;  
  5.   
  6. public class ShortcutsEntity {  
  7.   
  8.     public CharSequence Title;  
  9.     public Drawable Icon;  
  10.     public ResolveInfo Info;  
  11. }  
  12. </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>
view plain print ?
  1. <span style="font-size:18px;"><?xml version="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>
view plain print ?
  1. <span style="font-size:18px;"><?xml version="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.       
  8.     <ListView   
  9.         android:id="@+id/shortcuts_list"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:layout_above="@+id/shortcuts_layout"  
  15.         />  
  16.     <LinearLayout   
  17.         android:id="@+id/shortcuts_layout"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:orientation="horizontal"  
  21.         android:layout_alignParentLeft="true"  
  22.         android:layout_alignParentBottom="true"  
  23.         >  
  24.         <Button   
  25.             android:id="@+id/shortcuts_add"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_weight="1"  
  29.             android:text="@string/shortcuts_add_text"/>  
  30.               
  31.         <Button   
  32.             android:id="@+id/shortcuts_cancel"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:layout_weight="1"  
  36.             android:text="@string/shortcuts_cancel_text"  
  37.             />  
  38.     </LinearLayout>  
  39. </RelativeLayout>  
  40. </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方法中添加如下代码,即大功告成
view plain print ?
  1. <span style="font-size:18px;">Bundle bundle = data.getExtras();  
  2.                     ArrayList<Intent> intents = (ArrayList<Intent>) bundle.getSerializable("intents");  
  3.                       
  4.                     if (intents != null && !intents.isEmpty()) {  
  5.                         int size = intents.size();  
  6.                         for (int i = 0; i < size; i++) {  
  7.                             mAddItemCellInfo = new CellLayout.CellInfo(); // Shortcut to solve the problem of overlapping  
  8.                             completeAddApplication(this, intents.get(i),mAddItemCellInfo);  
  9.                         }  
  10.                     }  
  11.                     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>

你可能感兴趣的:(android,android,HTC,扩展,ipad,ipad,Launcher,Launcher)