在launcher3(桌面程序)的hotseat中,可以通过修改dw_phone_hotseat.xml在指定位置加载指定app(仅限于已经指定category类型为APP_xxxx的app或者是系统APP(这里不描述关于系统APP事件定义方式))。如果想在hotseat中加载自己编写app,比较省事的一种方式是在系统中增加新的category类型,类型可以任意命名(这里以APP_SETTINGS做示范),下面介绍增加新的category类型以及hotseat中指定app的方法。
一.增加新的category类型:
首先打开frameworks/base/core/java/android/content/intent.java文件:
我们可以看到intent事件类型是在这个文件中定义,其中包括一些系统级app的启动事件、category类型定义等。在这里我们仿照已存在的category为APP_xxxx的类型 作为示例,首先在intent.java文件中搜索到 “CATEGORY_APP_EMAIL” 位置,可以看到如下信息:
@SdkConstant(SdkConstantType.INTENT_CATEGORY)
public static final String CATEGORY_APP_EMAIL = "android.intent.category.APP_EMAIL";
这是APP_EMAIL类型的定义方式,和ACTION_MAIN一起使用,
用来启动邮件应用程序(如果把APP_EMAIL写在dw_phone_hotseat.xml中,将在
hotseat指定位置加载app图标)
下面我们仿照“CATEGORY_APP_EMAIL”的形式增加自己的类型定义:
在“CATEGORY_APP_EMAIL”定义下方,增加下面一段代码:
/**
* 增加自定义类型,Settings,可以根据实际情况自定义名称
*/
@SdkConstant(SdkConstantType.INTENT_CATEGORY)
public static final String CATEGORY_APP_SETTINGS = "android.intent.category.APP_SETTINGS";
保存文件,新的category类型增加完成,下面启动新增加的类型:
打开frameworks/base/sevices/core/java/com/android/serives/pm/DefaultPermissionGrantPolicy.java文件:
这里把Intent类型以包的形式进行封装,然后放入运行时(包管理器服务)(动态加载Intent信息,在应用层可以获取到已经加载的Intent指针(在这里也可以称为实例))。我们继续模仿已存在的category为APP_xxxx的类型 作为示例,首先在DefaultPermissionGrantPolicy.java文件中搜索到 “CATEGORY_APP_EMAIL” 位置,可以看到如下信息:
// Email
Intent emailIntent = new Intent(Intent.ACTION_MAIN);
emailIntent.addCategory(Intent.CATEGORY_APP_EMAIL);
PackageParser.Package emailPackage = getDefaultSystemHandlerActivityPackageLPr(
emailIntent, userId);
if (emailPackage != null
&& doesPackageSupportRuntimePermissions(emailPackage)) {
grantRuntimePermissionsLPw(emailPackage, CONTACTS_PERMISSIONS, userId);
}
下面我们仿照“CATEGORY_APP_EMAIL”的形式增加自己的启动方式:
在“CATEGORY_APP_EMAIL”代码下方,增加下面一段代码:
//增加自定义种类Settings,根据实际情况使用自己定义的类型
Intent SettingsIntent = new Intent(Intent.ACTION_MAIN);
/**这里我们也是指定了APP主文件<intent-filter >内有
/*<action android:name="android.intent.action.MAIN" />
/*字段的节点将作为检索目标*/
SettingsIntent.addCategory(Intent.CATEGORY_APP_SETTINGS);
PackageParser.Package SettingsPackage = getDefaultSystemHandlerActivityPackageLPr(
SettingsIntent, userId);
if (SettingsPackage != null
&& doesPackageSupportRuntimePermissions(SettingsPackage)) {
grantRuntimePermissionsLPw(SettingsPackage, CONTACTS_PERMISSIONS, userId);
}
这里需要说明的是 “CONTACTS_PERMISSIONS” 字段,追踪 “CONTACTS_PERMISSIONS” 的定义处:
private static final Set<String> CONTACTS_PERMISSIONS = new ArraySet<>();
static {
CONTACTS_PERMISSIONS.add(Manifest.permission.READ_CONTACTS);
CONTACTS_PERMISSIONS.add(Manifest.permission.WRITE_CONTACTS);
CONTACTS_PERMISSIONS.add(Manifest.permission.GET_ACCOUNTS);
}
可以看到 “CONTACTS_PERMISSIONS” 字段的作用是设置自定义类型(在容器内)的权限,
比如自定义类型绑定了一个app,app在当前容器内启动时将有这些访问权限
使用者请根据实际情况来定义权限
保存文件,新的category类型增加完成,下面是编译工作:
1.在源码文件夹根位置执行 mm frameworks/base/core/;
2.执行 mm frameworks/base/sevices/;
二.hotseat中指定app的方法:
打开 packages/apps/Launcher3/res/xml/dw_phone_hotseat.xml文件:
<resolve
launcher:container="-101"
launcher:screen="0"
launcher:x="0"
launcher:y="0" >
<favorite launcher:uri="#Intent;action=android.intent.action.DIAL;end" />
<favorite launcher:uri="tel:123" />
<favorite launcher:uri="#Intent;action=android.intent.action.CALL_BUTTON;end" />
</resolve>
这是第一个APP节点:
launcher:screen="0" 表示当前第几个APP所对应在hotseat内的屏幕范围,0表示
第一个
launcher:x="0" 表示在hotseat容器内屏幕最左边的位置,最大为4,
其中2 为allapps按钮位置(在dw_phone_hotseat.xml中设置2将会被allapps按钮覆盖)
可以设置的参数为0,1,3,4 ,APP位置变化是从左向右
launcher:y="0" hotseat只有一行的情况下 launcher:y为0
<favorite launcher:uri="#Intent;action=android.intent.action.DIAL;end" />
这一句也就是指定加载的APP,当前这段表示加载的是系统APP(在intent.java中有定义)
下面附上在第一个APP位置加载我们自定义的APP:
<!--resolve
launcher:container="-101"
launcher:screen="0"
launcher:x="0"
launcher:y="0" >
<favorite launcher:uri="#Intent;action=android.intent.action.MAIN;category=android.intent.category.APP_SETTINGS;end" />
</resolve-->
在<favorite launcher:uri="#Intent;action=android.intent.action.MAIN;
category=android.intent.category.APP_SETTINGS;end" />这段代码中可以看到
我们指定了探索<favorite 中包含main字段的节点,category类型为APP_SETTINGS
的应用程序
保存文件,下面修改我们想要加载的app主文件:
打开packages/apps/Settings/AndroidManifest.xml文件:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
+ <category android:name="android.intent.category.APP_SETTINGS" />
</intent-filter>
找到包含<intent-filter>节点下包含<action android:name=
"android.intent.action.MAIN" />的节点,在这个节点中增加自定义的类型
<category android:name="android.intent.category.APP_SETTINGS" />
保存文件,重新编译修改过的APP,和Launcher3程序,并make snod更新到.img文件中,启动系统后可以看到hotseat第一个app是我们自定义的APP了,修改第二个及后面的APP和这种方式大致相同。
AOSP6.0.1相关文章:
AOSP6.0.1 launcher3入门篇—解析launcher.java文件!
AOSP6.0.1 launcher3入门篇-解析DeviceProject.java及相关文件!
AOSP6.0.1 launcher3入门篇—hotseat相关实现!