【转】Launcher2分析

转自:http://blogold.chinaunix.net/u/24632/showart_2320672.html

今天开始launcher2分析系列,Launcher2的代码路径为:$ANDROID_SRC/packages/apps/Launcher2

项目构成:

AndroidManifest.xml         项目Launcher2的描述文件

CleanSpec.mk                android项目授权文件?

NOTICE                      apache授权协议

Android.mk                  Launcher2编译的makefile

MODULE_LICENSE_APACHE2  空文件

proguard.flags          -keep clashh

res目录                 描述文件以及icon资源的位置

src目录                     源代码目录

先看AndroidManifest.xml文件,该文件是对Launcher2的配置文件

<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.android.launcher"
    android:sharedUserId="@string/sharedUserId"
>
<!--package配置我们应用程序的包名 -->
<original-package android:name="com.android.launcher2" />
<!--对系统资源访问的权限控制 -->
<permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
        android:protectionLevel="normal"
        android:label="@string/permlab_install_shortcut"
        android:description="@string/permdesc_install_shortcut" />
<permission
        android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"
        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
        android:protectionLevel="normal"
        android:label="@string/permlab_uninstall_shortcut"
        android:description="@string/permdesc_uninstall_shortcut"/>
<permission
        android:name="com.android.launcher.permission.READ_SETTINGS"
        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
        android:protectionLevel="normal"
        android:label="@string/permlab_read_settings"
        android:description="@string/permdesc_read_settings"/>
<permission
        android:name="com.android.launcher.permission.WRITE_SETTINGS"
        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
        android:protectionLevel="normal"
        android:label="@string/permlab_write_settings"
        android:description="@string/permdesc_write_settings"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.BIND_APPWIDGET" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
<!--对我们应用程序的配置 -->
<application
        android:name="com.android.launcher2.LauncherApplication"
        android:process="@string/process"
        android:label="@string/application_name"
        android:icon="@drawable/ic_launcher_home">

    <!--配置应用程序额的名字,进程,标签,和图标

        label的值为values/strings.xml中application_name 键值对的值

        icon为drawable目录下名为的ic_launcher_home的图片

        实际上该图片的位置位于drawable-hdpi(高分辨率)目录下,是个小房子

    -->
<activity
            android:name="com.android.launcher2.Launcher"
            android:launchMode="singleTask"
            android:clearTaskOnLaunch="true"
            android:stateNotNeeded="true"
            android:theme="@style/Theme"
            android:screenOrientation="nosensor"
            android:windowSoftInputMode="stateUnspecified|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>
<!--一个项目可能有很多activity,设置intent-filter可以先启动该activity -->
<activity
            android:name="com.android.launcher2.WallpaperChooser"
            android:label="@string/pick_wallpaper"
            android:icon="@drawable/ic_launcher_wallpaper"
            android:screenOrientation="nosensor"
            android:finishOnCloseSystemDialogs="true">
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!--设置wallpapaer的activity -->
<!-- Intent received used to install shortcuts from other applications -->
<receiver
            android:name="com.android.launcher2.InstallShortcutReceiver"
            android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>
<!--安装快捷方式的intent -->
<!-- Intent received used to uninstall shortcuts from other applications -->
<receiver
            android:name="com.android.launcher2.UninstallShortcutReceiver"
            android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.UNINSTALL_SHORTCUT" />
</intent-filter>
</receiver>
<!--设置删除快捷方式的intent -->
<!-- The settings provider contains Home -->

好吧,现在我们来看res目录里的布局文件,布局文件都放在layout*目录里。

本以为launcher的layout都放在layout目录里,由于屏幕放置方式的不同会对桌面造成一定的影响,所以google的android项目组就决定因地制宜。比如当你横着放置屏幕的时候就会使用layout-land目录里的文件来对系统launcher进行布局,竖着屏幕的时候会使用layout-port内的布局文件来对launcher来布局。

横竖屏幕切换之际,会重新进行布局。那我们就以layout-land目录为例来看吧。

layout-land/launcuer.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<com.android.launcher2.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.launcher2.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.launcher2.Workspace>
<!-- 上面这几行描述了几个工作区的屏幕,描述代码为layout-land/workspace_screen

      而模拟器上能看到左右各两个小白点可以控制工作区的移动

-->

<!-- 应该对应的是ClippedImageView类-->
<com.android.launcher2.ClippedImageView
        android:id="@+id/previous_screen"
        android:layout_width="93dip"
        android:layout_height="@dimen/button_bar_height"
        android:layout_gravity="bottom|left"
        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" />
<!-- 定义了previousScreen 按钮,即左下脚的白色小点,用来控制移动-->
<com.android.launcher2.ClippedImageView
        android:id="@+id/next_screen"
        android:layout_width="93dip"
        android:layout_height="@dimen/button_bar_height"
        android:layout_gravity="bottom|right"
        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.launcher2.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/half_status_bar_height"
        android:layout_gravity="right|center_vertical"
        android:scaleType="center"
        android:src="@drawable/delete_zone_selector"
        android:visibility="invisible"
        launcher:direction="vertical"
/>
<!-- 定义Trash放置的位置 右侧,中间平放-->
<RelativeLayout
        android:id="@+id/all_apps_button_cluster"
        android:layout_height="fill_parent"
        android:layout_width="@dimen/button_bar_height_portrait"
        android:layout_gravity="right|center_vertical"
        android:layout_marginBottom="@dimen/half_status_bar_height"
>
<!-- 定义右侧 靠近屏幕边缘的三个按钮,中间一个是all-apps

     之下是phone按钮,之上是浏览器按钮,绑定响应函数

-->
<com.android.launcher2.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"
/>
<ImageView
            android:id="@+id/hotseat_left"
            style="@style/HotseatButton.Left"
            android:layout_below="@id/all_apps_button"
            android:src="@drawable/hotseat_phone"
            android:onClick="launchHotSeat"
/>
<!-- onClick的值为响应方法-->
<ImageView
            android:id="@+id/hotseat_right"
            style="@style/HotseatButton.Right"
            android:layout_above="@id/all_apps_button"
            android:src="@drawable/hotseat_browser"
            android:onClick="launchHotSeat"
/>
<!-- 对浏览器进行的描述-->
</RelativeLayout>
</com.android.launcher2.DragLayer>

posted @ 2011-04-18 09:41  朱旭东  阅读(2109)  评论(0编辑  收藏  举报