Launcher和Setting是客户需求经常改动的地方,不过其代码量也不容小觑。今天就初略来看一下,以下内容都是本人查阅资料加上自己的理解得出,由于自己水平有限,如果误导还请指出:
先从AndroidManifest文件入手,Launcher3的工程名是ToggleWeightWatcher,包名是com.android.launcher3。
关于权限,Launcher3的权限有permission和uses-permission两种。
permission是自定义的权限,uses-permission是调用系统的permission。其中自定义permission有几个属性:
android:permissionGroup 可选,为Permission进行分组,可以由以下常量定义:
ACCOUNTS: 账户管理相关
COST_MONEY:让用户花钱但不需要通过与他们直接牵涉
DEVELOPMENT_TOOLS:开发相关
HARDWARE_CONTROLS:直接访问硬件设备
LOCATION: 访问用户当前位置
MESSAGE: 信息相关
NETWORK: 访问网络服务相关
PERSONAL_INFO:访问用户私人数据相关
PHONE_CALLS:拨号相关
STORAGE:SD卡相关
SYSTEM_TOOLS:系统API有关
android:protectionLevel必有,
normal:低风险权限,只要申请了就可以使用(在AndroidManifest.xml中添加<uses-permission>标签),安装时不需要用户确认;
dangerous:高风险权限,安装时需要用户的确认才可使用;
signature:只有当申请权限的应用程序的数字签名与声明此权限的应用程序的数字签名相同时(如果是申请系统权限,则需要与系统签名相同),才能将权限授给它;
signatureOrSystem:签名相同,或者申请权限的应用为系统应用(在system image中)
其他的android:label, android:name, android:description是描述性信息
- <permission
- android:name="com.android.launcher3.permission.PRELOAD_WORKSPACE"
- android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
- android:protectionLevel="system|signature" />
- <permission
- android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
- android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
- android:protectionLevel="dangerous"
- 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="dangerous"
- android:label="@string/permlab_uninstall_shortcut"
- android:description="@string/permdesc_uninstall_shortcut"/>
- <permission
- android:name="com.android.launcher3.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.launcher3.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"/>
- <permission
- android:name="com.android.launcher3.permission.RECEIVE_LAUNCH_BROADCASTS"
- android:protectionLevel="signature"
- />
- <uses-permission android:name="android.permission.CALL_PHONE" />
- <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.BIND_APPWIDGET" />
- <uses-permission android:name="android.permission.GET_ACCOUNTS" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.READ_MEDIA_STORAGE" />
- <uses-permission android:name="android.permission.ADVANCED_WIDGET_API"/>
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
- <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
- <uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" />
- <uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" />
- <uses-permission android:name="com.android.launcher3.permission.RECEIVE_LAUNCH_BROADCASTS" />
- <!-- M: hide apps activity requires this permission to get package size. -->
- <uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
下面逐一看一下各个权限:
PRELOAD_WORKSPACE
INSTALL_SHORTCUT / UNINSTALL_SHORTCUT 创建 / 删除快捷方式
READ_SETTINGS / WRITE_SETTINGS 读取 / 写入设置配置
RECEIVE_LAUNCH_BROADCASTS 接收启动广播
CALL_PHONE 拨打电话
SET_WALLPAPER 设置壁纸
SET_WALLPAPER_HINTS 设置壁纸(桌面)提示,目前不懂这个什么用,是初次启动Launcher的使用提示吗?
VIBRATE 震动
BIND_APPWIDGET 访问AppWidget数据
GET_ACCOUNTS 访问账号服务的账号列表
READ_EXTERNAL_STORAGE 读取外部存储
READ_MEDIA_STORAGE 读取媒体存储
ADVANCED_WIDGET_API 高级Widget接口
GET_PACKAGE_SIZE 获取应用包大小
接着看一下AndroidManifest文件中的<application>部分:
这里面声明的都是显式Activity,Service,Provider之类的。
先上源码:
- <application
- android:name="com.android.launcher3.LauncherApplication"
- android:label="@string/application_name"
- android:icon="@mipmap/ic_launcher_home"
- android:hardwareAccelerated="true"
- android:largeHeap="@bool/config_largeHeap"
- android:supportsRtl="true">
- <activity
- android:name="com.android.launcher3.Launcher"
- android:launchMode="singleTask"
- android:clearTaskOnLaunch="true"
- android:stateNotNeeded="true"
- android:theme="@style/Theme"
- android:windowSoftInputMode="adjustPan"
- android:screenOrientation="nosensor">
- <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>
- <!-- M: hide apps activity used to configure apps to be hidden in apps list. -->
- <activity android:name="com.android.launcher3.HideAppsActivity"
- android:label="@string/hideapps"
- android:icon="@drawable/ic_launcher_home"
- android:screenOrientation="nosensor" >
- <intent-filter>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.android.launcher3.ToggleWeightWatcher"
- android:label="@string/toggle_weight_watcher"
- android:enabled="@bool/debug_memory_enabled"
- android:icon="@mipmap/ic_launcher_home">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.android.launcher3.WallpaperPickerActivity"
- android:theme="@style/Theme.WallpaperCropper"
- android:label="@string/pick_wallpaper"
- android:icon="@mipmap/ic_launcher_wallpaper"
- android:finishOnCloseSystemDialogs="true"
- android:process=":wallpaper_chooser"
- android:configChanges="orientation|keyboard|keyboardHidden|screenSize">
- <intent-filter>
- <action android:name="android.intent.action.SET_WALLPAPER" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.android.launcher3.WallpaperCropActivity"
- android:theme="@style/Theme.WallpaperCropper"
- android:label="@string/crop_wallpaper"
- android:icon="@mipmap/ic_launcher_wallpaper"
- android:finishOnCloseSystemDialogs="true"
- android:process=":wallpaper_chooser">
- <intent-filter>
- <action android:name="android.service.wallpaper.CROP_AND_SET_WALLPAPER" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="image/*" />
- </intent-filter>
- </activity>
- <!-- Debugging tools -->
- <activity
- android:name="com.android.launcher3.MemoryDumpActivity"
- android:theme="@android:style/Theme.NoDisplay"
- android:label="@string/debug_memory_activity"
- android:enabled="@bool/debug_memory_enabled"
- android:excludeFromRecents="true"
- android:icon="@mipmap/ic_launcher_home"
- >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:name="com.android.launcher3.MemoryTracker"
- android:enabled="@bool/debug_memory_enabled"
- >
- </service>
- <!-- Intent received used to prepopulate the default workspace. -->
- <receiver
- android:name="com.android.launcher3.PreloadReceiver"
- android:permission="com.android.launcher3.permission.PRELOAD_WORKSPACE">
- <intent-filter>
- <action android:name="com.android.launcher3.action.PRELOAD_WORKSPACE" />
- </intent-filter>
- </receiver>
- <!-- Intent received used to install shortcuts from other applications -->
- <receiver
- android:name="com.android.launcher3.InstallShortcutReceiver"
- android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
- <intent-filter>
- <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
- </intent-filter>
- </receiver>
- <!-- Intent received used to uninstall shortcuts from other applications -->
- <receiver
- android:name="com.android.launcher3.UninstallShortcutReceiver"
- android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT">
- <intent-filter>
- <action android:name="com.android.launcher.action.UNINSTALL_SHORTCUT" />
- </intent-filter>
- </receiver>
- <!-- New user initialization; set up initial wallpaper -->
- <receiver
- android:name="com.android.launcher3.UserInitializeReceiver"
- android:exported="false">
- <intent-filter>
- <action android:name="android.intent.action.USER_INITIALIZE" />
- </intent-filter>
- </receiver>
- <receiver android:name="com.android.launcher3.PackageChangedReceiver" >
- <intent-filter>
- <action android:name="android.intent.action.PACKAGE_CHANGED"/>
- <action android:name="android.intent.action.PACKAGE_REPLACED"/>
- <action android:name="android.intent.action.PACKAGE_REMOVED"/>
- <data android:scheme="package"></data>
- </intent-filter>
- </receiver>
- <!-- The settings provider contains Home's data, like the workspace favorites -->
- <provider
- android:name="com.android.launcher3.LauncherProvider"
- android:authorities="com.android.launcher3.settings"
- android:exported="true"
- android:writePermission="com.android.launcher3.permission.WRITE_SETTINGS"
- android:readPermission="com.android.launcher3.permission.READ_SETTINGS" />
- <meta-data android:name="android.nfc.disable_beam_default"
- android:value="true" />
- </application>
com.android.launcher3.Launcher Activity,主Activity
com.android.launcher3.ToggleWeightWatcher Activity,启动Activity
com.android.launcher3.HideAppsActivity Activity,隐藏应用抽屉程序清单中应用的Activity。
com.android.launcher3.WallpaperPickerActivity Activity,选择壁纸的Activity
com.android.launcher3.WallpaperCropActivity Activity,裁剪壁纸的Activity
com.android.launcher3.MemoryDumpActivity Activity,这个是调试用的,可忽略
com.android.launcher3.MemoryTracker Service,调试用
com.android.launcher3.PreloadReceiver Receiver,用来响应预加载Launcher默认工作空间
com.android.launcher3.InstallShortcutReceiver Receiver,用来响应添加其他应用的快捷方式
com.android.launcher3.UninstallShortcutReceiver Receiver,和上一个相反,用来响应去除其他应用的快捷方式
com.android.launcher3.UserInitializeReceiver Receiver,用来响应新用户初始化,设置初始壁纸
com.android.launcher3.PackageChangedReceiver Receiver, 用来响应包变化
com.android.launcher3.LauncherProvider Provider,包括主页屏的数据,比如图标和AppWidget的位置、大小等。
AllAppList
这是一个比较核心的类,所有应用的信息都在这儿了。
下面看一下AllAppList类的成员变量和成员函数:
data是所有应用的清单:
- public ArrayList<AppInfo> data =
- new ArrayList<AppInfo>(DEFAULT_APPLICATIONS_NUMBER);
added是自上次 更新(notify()广播)后新 增加的应用清单:
- public ArrayList<AppInfo> added =
- new ArrayList<AppInfo>(DEFAULT_APPLICATIONS_NUMBER);
- public ArrayList<AppInfo> removed = new ArrayList<AppInfo>();
modified是自上次更新以来,所修改的应用清单:
- public ArrayList<AppInfo> modified = new ArrayList<AppInfo>();
mIconCache 图标缓存
mAppFilter 应用过滤器
- private IconCache mIconCache;
- private AppFilter mAppFilter;
新添加一个应用及其信息到list中,如果已存在则不添加:
- public void add(AppInfo info) {
- if (LauncherLog.DEBUG) {
- LauncherLog.d(TAG, "Add application in app list: app = " + info.componentName
- + ", title = " + info.title);
- }
- if (mAppFilter != null && !mAppFilter.shouldShowApp(info.componentName)) {
- return;
- }
- if (findActivity(data, info.componentName)) {
- LauncherLog.i(TAG, "Application " + info + " already exists in app list, app = " + info);
- return;
- }
- data.add(info);
- added.add(info);
- }
清空列表中的应用信息,包括data,added,removed,modified:
- public void clear() {
- if (LauncherLog.DEBUG) {
- LauncherLog.d(TAG, "clear all data in app list: app size = " + data.size());
- }
- data.clear();
- // TODO: do we clear these too?
- added.clear();
- removed.clear();
- modified.clear();
- }
- public void addPackage(Context context, String packageName) {
- final List<ResolveInfo> matches = findActivitiesForPackage(context, packageName);
- if (LauncherLog.DEBUG) {
- LauncherLog.d(TAG, "addPackage: packageName = " + packageName + ", matches = " + matches.size());
- }
- if (matches.size() > 0) {
- for (ResolveInfo info : matches) {
- add(new AppInfo(context.getPackageManager(), info, mIconCache, null));
- }
- }
- }
- public void removePackage(String packageName) {
- final List<AppInfo> data = this.data;
- if (LauncherLog.DEBUG) {
- LauncherLog.d(TAG, "removePackage: packageName = " + packageName + ", data size = " + data.size());
- }
- for (int i = data.size() - 1; i >= 0; i--) {
- AppInfo info = data.get(i);
- final ComponentName component = info.intent.getComponent();
- if (packageName.equals(component.getPackageName())) {
- removed.add(info);
- data.remove(i);
- }
- }
更新包信息,若不存在,直接添加应用信息;若已存在,则先删除旧信息,然后添加新信息。
- public void updatePackage(Context context, String packageName) {
- final List<ResolveInfo> matches = findActivitiesForPackage(context, packageName);
- if (LauncherLog.DEBUG) {
- LauncherLog.d(TAG, "updatePackage: packageName = " + packageName + ", matches = " + matches.size());
- }
- if (matches.size() > 0) {
- // Find disabled/removed activities and remove them from data and add them
- // to the removed list.
- for (int i = data.size() - 1; i >= 0; i--) {
- final AppInfo applicationInfo = data.get(i);
- final ComponentName component = applicationInfo.intent.getComponent();
- if (packageName.equals(component.getPackageName())) {
- if (!findActivity(matches, component)) {
- removed.add(applicationInfo);
- mIconCache.remove(component);
- data.remove(i);
- }
- }
- }
- // Find enabled activities and add them to the adapter
- // Also updates existing activities with new labels/icons
- int count = matches.size();
- for (int i = 0; i < count; i++) {
- final ResolveInfo info = matches.get(i);
- final String pkgName = info.activityInfo.applicationInfo.packageName;
- final String className = info.activityInfo.name;
- AppInfo applicationInfo = findApplicationInfoLocked(
- info.activityInfo.applicationInfo.packageName,
- info.activityInfo.name);
- if (applicationInfo == null) {
- add(new AppInfo(context.getPackageManager(), info, mIconCache, null));
- } else {
- mIconCache.remove(applicationInfo.componentName);
- mIconCache.getTitleAndIcon(applicationInfo, info, null);
- modified.add(applicationInfo);
- }
- }
- } else {
- // Remove all data for this package.
- for (int i = data.size() - 1; i >= 0; i--) {
- final AppInfo applicationInfo = data.get(i);
- final ComponentName component = applicationInfo.intent.getComponent();
- if (packageName.equals(component.getPackageName())) {
- if (LauncherLog.DEBUG) {
- LauncherLog.d(TAG, "Remove application from launcher: component = " + component);
- }
- removed.add(applicationInfo);
- mIconCache.remove(component);
- data.remove(i);
- }
- }
- }
- }
供MAIN/LAUNCHER通过包名packageName查询包管理器:
- static List<ResolveInfo> findActivitiesForPackage(Context context, String packageName) {
- final PackageManager packageManager = context.getPackageManager();
- final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
- mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
- mainIntent.setPackage(packageName);
- final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
- return apps != null ? apps : new ArrayList<ResolveInfo>();
- }
- /**
- * Returns whether <em>apps</em> contains <em>component</em>.
- */
- private static boolean findActivity(List<ResolveInfo> apps, ComponentName component) {
- final String className = component.getClassName();
- for (ResolveInfo info : apps) {
- final ActivityInfo activityInfo = info.activityInfo;
- if (activityInfo.name.equals(className)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Returns whether <em>apps</em> contains <em>component</em>.
- */
- private static boolean findActivity(ArrayList<AppInfo> apps, ComponentName component) {
- final int N = apps.size();
- for (int i=0; i<N; i++) {
- final AppInfo info = apps.get(i);
- if (info.componentName.equals(component)) {
- return true;
- }
- }
- return false;
- }
根据指定的包名packageName,类明className返回一个Component对象:
- private AppInfo findApplicationInfoLocked(String packageName, String className) {
- for (AppInfo info: data) {
- final ComponentName component = info.intent.getComponent();
- if (packageName.equals(component.getPackageName())
- && className.equals(component.getClassName())) {
- return info;
- }
- }
- return null;
- }
- static boolean loadTopPackage(final Context context) {
- boolean bRet = false;
- if (sTopPackages != null) {
- return bRet;
- }
- sTopPackages = new ArrayList<TopPackage>();
- try {
- XmlResourceParser parser = context.getResources().getXml(R.xml.default_toppackage);
- AttributeSet attrs = Xml.asAttributeSet(parser);
- XmlUtils.beginDocument(parser, TAG_TOPPACKAGES);
- final int depth = parser.getDepth();
- int type = -1;
- while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
- && type != XmlPullParser.END_DOCUMENT) {
- if (type != XmlPullParser.START_TAG) {
- continue;
- }
- TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopPackage);
- sTopPackages.add(new TopPackage(a.getString(R.styleable.TopPackage_topPackageName),
- a.getString(R.styleable.TopPackage_topClassName), a.getInt(
- R.styleable.TopPackage_topOrder, 0)));
- LauncherLog.d(TAG, "loadTopPackage: packageName = "
- + a.getString(R.styleable.TopPackage_topPackageName)
- + ", className = "
- + a.getString(R.styleable.TopPackage_topClassName));
- a.recycle();
- }
- } catch (XmlPullParserException e) {
- LauncherLog.w(TAG, "Got XmlPullParserException while parsing toppackage.", e);
- } catch (IOException e) {
- LauncherLog.w(TAG, "Got IOException while parsing toppackage.", e);
- }
- return bRet;
- }
根据应用信息appInfo匹配默认顶部应用的对应应用,返回该应用下标:
- static int getTopPackageIndex(final AppInfo appInfo) {
- int retIndex = -1;
- if (sTopPackages == null || sTopPackages.isEmpty() || appInfo == null) {
- return retIndex;
- }
- for (TopPackage tp : sTopPackages) {
- if (appInfo.componentName.getPackageName().equals(tp.packageName)
- && appInfo.componentName.getClassName().equals(tp.className)) {
- retIndex = tp.order;
- break;
- }
- }
- return retIndex;
- }
- void reorderApplist() {
- final long sortTime = DEBUG_LOADERS_REORDER ? SystemClock.uptimeMillis() : 0;
- if (sTopPackages == null || sTopPackages.isEmpty()) {
- return;
- }
- ensureTopPackageOrdered();
- final ArrayList<AppInfo> dataReorder = new ArrayList<AppInfo>(
- DEFAULT_APPLICATIONS_NUMBER);
- for (TopPackage tp : sTopPackages) {
- int loop = 0;
- for (AppInfo ai : added) {
- if (DEBUG_LOADERS_REORDER) {
- LauncherLog.d(TAG, "reorderApplist: remove loop = " + loop);
- }
- if (ai.componentName.getPackageName().equals(tp.packageName)
- && ai.componentName.getClassName().equals(tp.className)) {
- if (DEBUG_LOADERS_REORDER) {
- LauncherLog.d(TAG, "reorderApplist: remove packageName = "
- + ai.componentName.getPackageName());
- }
- data.remove(ai);
- dataReorder.add(ai);
- dumpData();
- break;
- }
- loop++;
- }
- }
- for (TopPackage tp : sTopPackages) {
- int loop = 0;
- int newIndex = 0;
- for (AppInfo ai : dataReorder) {
- if (DEBUG_LOADERS_REORDER) {
- LauncherLog.d(TAG, "reorderApplist: added loop = " + loop + ", packageName = "
- + ai.componentName.getPackageName());
- }
- if (ai.componentName.getPackageName().equals(tp.packageName)
- && ai.componentName.getClassName().equals(tp.className)) {
- newIndex = Math.min(Math.max(tp.order, 0), added.size());
- if (DEBUG_LOADERS_REORDER) {
- LauncherLog.d(TAG, "reorderApplist: added newIndex = " + newIndex);
- }
- /// M: make sure the array list not out of bound
- if (newIndex < data.size()) {
- data.add(newIndex, ai);
- } else {
- data.add(ai);
- }
- dumpData();
- break;
- }
- loop++;
- }
- }
- if (added.size() == data.size()) {
- added = (ArrayList<AppInfo>) data.clone();
- LauncherLog.d(TAG, "reorderApplist added.size() == data.size()");
- }
- if (DEBUG_LOADERS_REORDER) {
- LauncherLog.d(TAG, "sort and reorder took " + (SystemClock.uptimeMillis() - sortTime) + "ms");
- }
- }
Dump掉data列表中的应用信息:
- void dumpData() {
- int loop2 = 0;
- for (AppInfo ai : data) {
- if (DEBUG_LOADERS_REORDER) {
- LauncherLog.d(TAG, "reorderApplist data loop2 = " + loop2);
- LauncherLog.d(TAG, "reorderApplist data packageName = "
- + ai.componentName.getPackageName());
- }
- loop2++;
- }
- }
确保top_package.xml中条目有序,以防特殊情况下top_package.xml会使数组列表越界:
- static void ensureTopPackageOrdered() {
- ArrayList<TopPackage> tpOrderList = new ArrayList<TopPackage>(DEFAULT_APPLICATIONS_NUMBER);
- boolean bFirst = true;
- for (TopPackage tp : sTopPackages) {
- if (bFirst) {
- tpOrderList.add(tp);
- bFirst = false;
- } else {
- for (int i = tpOrderList.size() - 1; i >= 0; i--) {
- TopPackage tpItor = tpOrderList.get(i);
- if (0 == i) {
- if (tp.order < tpOrderList.get(0).order) {
- tpOrderList.add(0, tp);
- } else {
- tpOrderList.add(1, tp);
- }
- break;
- }
- if ((tp.order < tpOrderList.get(i).order)
- && (tp.order >= tpOrderList.get(i - 1).order)) {
- tpOrderList.add(i, tp);
- break;
- } else if (tp.order > tpOrderList.get(i).order) {
- tpOrderList.add(i + 1, tp);
- break;
- }
- }
- }
- }
- if (sTopPackages.size() == tpOrderList.size()) {
- sTopPackages = (ArrayList<TopPackage>) tpOrderList.clone();
- tpOrderList = null;
- LauncherLog.d(TAG, "ensureTopPackageOrdered done");
- } else {
- LauncherLog.d(TAG, "some mistake may occur when ensureTopPackageOrdered");
- }
- }
- public void addApp(final AppInfo info) {
- if (LauncherLog.DEBUG_EDIT) {
- LauncherLog.d(TAG, "Add application to data list: app = " + info.componentName);
- }
- if (findActivity(data, info.componentName)) {
- LauncherLog.i(TAG, "The app " + info + " is already exist in data list.");
- return;
- }
- data.add(info);
- }
AppInfo
这个类也很关键,不过相对AllAppList来讲要简单很多,从代码量上就可以看出来。主要是App的详细信息,包括图标,初次安装时间之类的,下面细看AppInfo的成员变量和函数:
图标:
- Bitmap iconBitmap;
应用的初次安装时间:
- long firstInstallTime;
应用图标是否显示,true为显示。
- boolean isVisible = true;
无参初始化,为应用创建快捷方式:
- AppInfo() {
- itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
- }
得到应用的图标和标题:
- public AppInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
- HashMap<Object, CharSequence> labelCache) {
- final String packageName = info.activityInfo.applicationInfo.packageName;
- this.componentName = new ComponentName(packageName, info.activityInfo.name);
- this.container = ItemInfo.NO_ID;
- this.setActivity(componentName,
- Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
- this.setFlagAndInstallTime(pm, packageName);
- iconCache.getTitleAndIcon(this, info, labelCache);
- }
- public static int initFlags(PackageInfo pi) {
- int appFlags = pi.applicationInfo.flags;
- int flags = 0;
- if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
- flags |= DOWNLOADED_FLAG;
- if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
- flags |= UPDATED_SYSTEM_APP_FLAG;
- }
- }
- return flags;
- }
- public AppInfo(AppInfo info) {
- super(info);
- componentName = info.componentName;
- title = info.title.toString();
- intent = new Intent(info.intent);
- flags = info.flags;
- firstInstallTime = info.firstInstallTime;
- /// M: copy value from source applcation info.
- pos = info.pos;
- isVisible = info.isVisible;
- stateChanged = info.stateChanged;
- }
- final void setActivity(ComponentName className, int launchFlags) {
- intent = new Intent(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- intent.setComponent(className);
- intent.setFlags(launchFlags);
- itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
- }
- public void setFlagAndInstallTime(final PackageManager pm, String packageName) {
- try {
- PackageInfo pi = pm.getPackageInfo(packageName, 0);
- flags = initFlags(pi);
- firstInstallTime = initFirstInstallTime(pi);
- } catch (NameNotFoundException e) {
- Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName);
- }
- }
- public void setFlagAndInstallTime(final PackageManager pm) {
- String packageName = getPackageName();
- setFlagAndInstallTime(pm, packageName);
- }
返回快捷方式要启动应用的包名,如不存在则返回一个空字符串:
- String getPackageName() {
- String packageName = "";
- if (intent != null) {
- packageName = intent.getPackage();
- if (packageName == null && intent.getComponent() != null) {
- packageName = intent.getComponent().getPackageName();
- }
- }
- return packageName;
- }
今天就先这两个类吧,已经不短了。不过源码真心长,真心多,真心眼花缭乱,慢慢啃吧。
今天来看一下CropView和DynamicGrid两部分。
CropView是设置壁纸时通过缩放手势截取图片,DynamicGrid则是根据设备来指定图标显示的行列,下面细看一下:
CropView
为了便于查看,吸取前两篇的教训,直接把注释写在代码里,成员变量:
- private ScaleGestureDetector mScaleGestureDetector;
- private long mTouchDownTime;
- private float mFirstX, mFirstY;<span style="white-space:pre"> </span>// 初始坐标位置
- private float mLastX, mLastY;<span style="white-space:pre"> </span>// 结束坐标位置
- private float mCenterX, mCenterY;<span style="white-space:pre"> </span>// 中间坐标位置
- private float mMinScale;
- private boolean mTouchEnabled = true;
- private RectF mTempEdges = new RectF();
- private float[] mTempPoint = new float[] { 0, 0 };
- private float[] mTempCoef = new float[] { 0, 0 };
- private float[] mTempAdjustment = new float[] { 0, 0 };
- private float[] mTempImageDims = new float[] { 0, 0 };
- private float[] mTempRendererCenter = new float[] { 0, 0 };
- TouchCallback mTouchCallback;
- Matrix mRotateMatrix;
- Matrix mInverseRotateMatrix;
成员函数:
CropView的构造函数:
- public CropView(Context context) {
- this(context, null);
- }
- public CropView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mScaleGestureDetector = new ScaleGestureDetector(context, this);
- mRotateMatrix = new Matrix();
- mInverseRotateMatrix = new Matrix();
- }
得到图像的分辨率:
- private float[] getImageDims() {
- final float imageWidth = mRenderer.source.getImageWidth();<span style="white-space:pre"> </span>// 图像宽度
- final float imageHeight = mRenderer.source.getImageHeight();<span style="white-space:pre"> </span>// 图像高度
- float[] imageDims = mTempImageDims;
- imageDims[0] = imageWidth;
- imageDims[1] = imageHeight;
- mRotateMatrix.mapPoints(imageDims);
- imageDims[0] = Math.abs(imageDims[0]);
- imageDims[1] = Math.abs(imageDims[1]);
- return imageDims;
- }
- private void getEdgesHelper(RectF edgesOut) {
- final float width = getWidth();
- final float height = getHeight();
- final float[] imageDims = getImageDims();
- final float imageWidth = imageDims[0];
- final float imageHeight = imageDims[1];
- float initialCenterX = mRenderer.source.getImageWidth() / 2f;
- float initialCenterY = mRenderer.source.getImageHeight() / 2f;
- float[] rendererCenter = mTempRendererCenter;
- rendererCenter[0] = mCenterX - initialCenterX;
- rendererCenter[1] = mCenterY - initialCenterY;
- mRotateMatrix.mapPoints(rendererCenter);
- rendererCenter[0] += imageWidth / 2;
- rendererCenter[1] += imageHeight / 2;
- final float scale = mRenderer.scale;
- float centerX = (width / 2f - rendererCenter[0] + (imageWidth - width) / 2f)
- * scale + width / 2f;
- float centerY = (height / 2f - rendererCenter[1] + (imageHeight - height) / 2f)
- * scale + height / 2f;
- float leftEdge = centerX - imageWidth / 2f * scale;
- float rightEdge = centerX + imageWidth / 2f * scale;
- float topEdge = centerY - imageHeight / 2f * scale;
- float bottomEdge = centerY + imageHeight / 2f * scale;
- edgesOut.left = leftEdge;
- edgesOut.right = rightEdge;
- edgesOut.top = topEdge;
- edgesOut.bottom = bottomEdge;
- }
- public RectF getCrop() {
- final RectF edges = mTempEdges;
- getEdgesHelper(edges);
- final float scale = mRenderer.scale;
- float cropLeft = -edges.left / scale;
- float cropTop = -edges.top / scale;
- float cropRight = cropLeft + getWidth() / scale;
- float cropBottom = cropTop + getHeight() / scale;
- return new RectF(cropLeft, cropTop, cropRight, cropBottom);
- }
设置TileSource,根据TileSource更新Scale:
- public void setTileSource(TileSource source, Runnable isReadyCallback) {
- super.setTileSource(source, isReadyCallback);
- mCenterX = mRenderer.centerX;
- mCenterY = mRenderer.centerY;
- mRotateMatrix.reset();
- mRotateMatrix.setRotate(mRenderer.rotation);
- mInverseRotateMatrix.reset();
- mInverseRotateMatrix.setRotate(-mRenderer.rotation);
- updateMinScale(getWidth(), getHeight(), source, true);
- }
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- updateMinScale(w, h, mRenderer.source, false);
- }
- public void setScale(float scale) {
- synchronized (mLock) {
- mRenderer.scale = scale;
- }
- }
- private void updateMinScale(int w, int h, TileSource source,
- boolean resetScale) {
- synchronized (mLock) {
- if (resetScale) {
- mRenderer.scale = 1;
- }
- if (source != null) {
- final float[] imageDims = getImageDims();
- final float imageWidth = imageDims[0];
- final float imageHeight = imageDims[1];
- mMinScale = Math.max(w / imageWidth, h / imageHeight);
- mRenderer.scale = Math.max(mMinScale, mRenderer.scale);
- }
- }
- }
核心的来了,触摸响应:
- public boolean onTouchEvent(MotionEvent event) {
- int action = event.getActionMasked();
- final boolean pointerUp = action == MotionEvent.ACTION_POINTER_UP;
- final int skipIndex = pointerUp ? event.getActionIndex() : -1;
- // Determine focal point
- float sumX = 0, sumY = 0;
- final int count = event.getPointerCount();
- for (int i = 0; i < count; i++) {
- if (skipIndex == i)
- continue;
- sumX += event.getX(i);
- sumY += event.getY(i);
- }
- final int div = pointerUp ? count - 1 : count;
- float x = sumX / div;
- float y = sumY / div;
- if (action == MotionEvent.ACTION_DOWN) {<span style="white-space:pre"> </span>// 响应按下事件
- mFirstX = x;
- mFirstY = y;
- mTouchDownTime = System.currentTimeMillis();
- if (mTouchCallback != null) {
- mTouchCallback.onTouchDown();
- }
- } else if (action == MotionEvent.ACTION_UP) {<span style="white-space:pre"> </span>// 响应抬起事件
- ViewConfiguration config = ViewConfiguration.get(getContext());
- float squaredDist = (mFirstX - x) * (mFirstX - x) + (mFirstY - y)
- * (mFirstY - y);
- float slop = config.getScaledTouchSlop()
- * config.getScaledTouchSlop();
- long now = System.currentTimeMillis();
- if (mTouchCallback != null) {
- // only do this if it's a small movement
- if (squaredDist < slop
- && now < mTouchDownTime
- + ViewConfiguration.getTapTimeout()) {
- mTouchCallback.onTap();
- }
- mTouchCallback.onTouchUp();
- }
- }
- if (!mTouchEnabled) {
- return true;
- }
- synchronized (mLock) {<span style="white-space:pre"> </span>// 同步锁
- mScaleGestureDetector.onTouchEvent(event);<span style="white-space:pre"> </span>// 手势缩放
- switch (action) {
- case MotionEvent.ACTION_MOVE:
- float[] point = mTempPoint;
- point[0] = (mLastX - x) / mRenderer.scale;
- point[1] = (mLastY - y) / mRenderer.scale;
- mInverseRotateMatrix.mapPoints(point);
- mCenterX += point[0];
- mCenterY += point[1];
- updateCenter();<span style="white-space:pre"> </span>// 更新中心点
- invalidate();
- break;
- }
- if (mRenderer.source != null) {
- // Adjust position so that the wallpaper covers the entire area
- // of the screen
- final RectF edges = mTempEdges;
- getEdgesHelper(edges);
- final float scale = mRenderer.scale;
- float[] coef = mTempCoef;
- coef[0] = 1;
- coef[1] = 1;
- mRotateMatrix.mapPoints(coef);
- float[] adjustment = mTempAdjustment;
- mTempAdjustment[0] = 0;
- mTempAdjustment[1] = 0;
- if (edges.left > 0) {
- adjustment[0] = edges.left / scale;
- } else if (edges.right < getWidth()) {
- adjustment[0] = (edges.right - getWidth()) / scale;
- }
- if (edges.top > 0) {
- adjustment[1] = FloatMath.ceil(edges.top / scale);
- } else if (edges.bottom < getHeight()) {
- adjustment[1] = (edges.bottom - getHeight()) / scale;
- }
- for (int dim = 0; dim <= 1; dim++) {
- if (coef[dim] > 0)
- adjustment[dim] = FloatMath.ceil(adjustment[dim]);
- }
- mInverseRotateMatrix.mapPoints(adjustment);
- mCenterX += adjustment[0];
- mCenterY += adjustment[1];
- updateCenter();
- }
- }
- mLastX = x;
- mLastY = y;
- return true;
- }
DynamicGrid
动态网格?先看两个核心类:
查询获取设备信息,宽和高以及分辨率:
- class DeviceProfileQuery {
- float widthDps;
- float heightDps;
- float value;
- PointF dimens;
- DeviceProfileQuery(float w, float h, float v) {
- widthDps = w;
- heightDps = h;
- value = v;
- dimens = new PointF(w, h);
- }
- }
下面这个就长了,洋洋洒洒几百行:
- class DeviceProfile {
- String name;
- float minWidthDps;
- float minHeightDps;
- float numRows;
- float numColumns;
- float iconSize;
- float iconTextSize;
- float numHotseatIcons;
- float hotseatIconSize;
- boolean isLandscape;
- boolean isTablet;
- boolean isLargeTablet;
- boolean transposeLayoutWithOrientation;
- int desiredWorkspaceLeftRightMarginPx;
- int edgeMarginPx;
- Rect defaultWidgetPadding;
- int widthPx;
- int heightPx;
- int availableWidthPx;
- int availableHeightPx;
- int iconSizePx;<span style="white-space:pre"> </span>// 图标大小
- int iconTextSizePx;<span style="white-space:pre"> </span>// 文字大小
- int cellWidthPx;
- int cellHeightPx;
- int folderBackgroundOffset;<span style="white-space:pre"> </span>// 文件夹背景
- int folderIconSizePx;<span style="white-space:pre"> </span>// 文件夹图标像素大小
- int folderCellWidthPx;
- int folderCellHeightPx;
- int hotseatCellWidthPx;<span style="white-space:pre"> </span>// 固定热键宽度
- int hotseatCellHeightPx;<span style="white-space:pre"> </span>// 固定热键高度
- int hotseatIconSizePx;
- int hotseatBarHeightPx;<span style="white-space:pre"> </span>// 固定热键上端的分割线
- int hotseatAllAppsRank;
- int allAppsNumRows;<span style="white-space:pre"> </span>// 应用行数
- int allAppsNumCols;<span style="white-space:pre"> </span>// 应用列数
- int searchBarSpaceWidthPx;<span style="white-space:pre"> </span>// 搜索条
- int searchBarSpaceMaxWidthPx;
- int searchBarSpaceHeightPx;
- int searchBarHeightPx;
- int pageIndicatorHeightPx;
- private static String APP_COUNTX = SystemProperties.get("ro.appscellcountx.value");
- private static String APP_COUNTY = SystemProperties.get("ro.appscellcounty.value");
- private static int MAX_ROW = 12;<span style="white-space:pre"> </span>// 最大行
- private static int MAX_COLS = 12;<span style="white-space:pre"> </span>// 最大列
- private static int MIN_ROW = 4;<span style="white-space:pre"> </span>// 最小行
- private static int MIN_COLS = 4;<span style="white-space:pre"> </span>// 最小列
- private static boolean IS_CONFIG = !TextUtils.isEmpty(APP_COUNTX) & !TextUtils.isEmpty(APP_COUNTY);
- private static boolean IS_AVAILABLY = (Integer.parseInt(APP_COUNTX) > MIN_ROW) &
- (Integer.parseInt(APP_COUNTX) < MAX_ROW) &
- (Integer.parseInt(APP_COUNTY) > MIN_COLS) &
- (Integer.parseInt(APP_COUNTY) < MAX_COLS);
- DeviceProfile(String n, float w, float h, float r, float c,
- float is, float its, float hs, float his) {
- // Ensure that we have an odd number of hotseat items (since we need to place all apps)
- if (!AppsCustomizePagedView.DISABLE_ALL_APPS && hs % 2 == 0) {<span style="white-space:pre"> </span>// 热键数目必须是奇数,因为中间是应用抽屉的入口
- throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
- }
- name = n;
- minWidthDps = w;
- minHeightDps = h;
- numRows = r;
- numColumns = c;
- iconSize = is;
- iconTextSize = its;
- numHotseatIcons = hs;
- hotseatIconSize = his;
- }
- DeviceProfile(Context context,
- ArrayList<DeviceProfile> profiles,
- float minWidth, float minHeight,
- int wPx, int hPx,
- int awPx, int ahPx,
- Resources resources) {
- DisplayMetrics dm = resources.getDisplayMetrics();
- ArrayList<DeviceProfileQuery> points =
- new ArrayList<DeviceProfileQuery>();
- transposeLayoutWithOrientation =
- resources.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
- minWidthDps = minWidth;
- minHeightDps = minHeight;
- ComponentName cn = new ComponentName(context.getPackageName(),
- this.getClass().getName());
- defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
- edgeMarginPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
- desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
- pageIndicatorHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
- // 列
- for (DeviceProfile p : profiles) {
- points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows));
- }
- numRows = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
- // 行
- points.clear();
- for (DeviceProfile p : profiles) {
- points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numColumns));
- }
- numColumns = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
- // 图标大小
- points.clear();
- for (DeviceProfile p : profiles) {
- points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize));
- }
- iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
- iconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
- // 文字大小
- points.clear();
- for (DeviceProfile p : profiles) {
- points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize));
- }
- iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
- iconTextSizePx = DynamicGrid.pxFromSp(iconTextSize, dm);
- // 热键大小
- points.clear();
- for (DeviceProfile p : profiles) {
- points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numHotseatIcons));
- }
- numHotseatIcons = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
- // Interpolate the hotseat icon size
- points.clear();
- for (DeviceProfile p : profiles) {
- points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize));
- }
- // 热键
- hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
- hotseatIconSizePx = DynamicGrid.pxFromDp(hotseatIconSize, dm);
- hotseatAllAppsRank = (int) (numColumns / 2);
- // Calculate other vars based on Configuration
- updateFromConfiguration(resources, wPx, hPx, awPx, ahPx);
- // 搜索条
- searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
- searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
- searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
- searchBarSpaceHeightPx = searchBarHeightPx + 2 * edgeMarginPx;
- // 计算实际标题大小
- Paint textPaint = new Paint();
- textPaint.setTextSize(iconTextSizePx);
- FontMetrics fm = textPaint.getFontMetrics();
- cellWidthPx = iconSizePx;
- cellHeightPx = iconSizePx + (int) Math.ceil(fm.bottom - fm.top);
- // At this point, if the cells do not fit into the available height, then we need
- // to shrink the icon size
- /*
- Rect padding = getWorkspacePadding(isLandscape ?
- CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
- int h = (int) (numRows * cellHeightPx) + padding.top + padding.bottom;
- if (h > availableHeightPx) {
- float delta = h - availableHeightPx;
- int deltaPx = (int) Math.ceil(delta / numRows);
- iconSizePx -= deltaPx;
- iconSize = DynamicGrid.dpiFromPx(iconSizePx, dm);
- cellWidthPx = iconSizePx;
- cellHeightPx = iconSizePx + (int) Math.ceil(fm.bottom - fm.top);
- }
- */
- // 热键
- hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
- hotseatCellWidthPx = iconSizePx;
- hotseatCellHeightPx = iconSizePx;
- // 应用文件夹
- folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
- folderCellHeightPx = cellHeightPx + (int) ((3f/2f) * edgeMarginPx);
- folderBackgroundOffset = -edgeMarginPx;
- folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
- }
- void updateFromConfiguration(Resources resources, int wPx, int hPx,
- int awPx, int ahPx) {
- isLandscape = (resources.getConfiguration().orientation ==
- Configuration.ORIENTATION_LANDSCAPE);
- isTablet = resources.getBoolean(R.bool.is_tablet);
- isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
- widthPx = wPx;
- heightPx = hPx;
- availableWidthPx = awPx;
- availableHeightPx = ahPx;
- Rect padding = getWorkspacePadding(isLandscape ?
- CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
- int pageIndicatorOffset =
- resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset);
- if (IS_CONFIG && IS_AVAILABLY) {
- if (isLandscape) {
- allAppsNumRows = Integer.parseInt(APP_COUNTX);
- allAppsNumCols = Integer.parseInt(APP_COUNTY);
- } else {
- allAppsNumRows = Integer.parseInt(APP_COUNTY);
- allAppsNumCols = Integer.parseInt(APP_COUNTX);
- }
- } else {
- if (isLandscape) {
- allAppsNumRows = (availableHeightPx - pageIndicatorOffset - 4 * edgeMarginPx) /
- (iconSizePx + iconTextSizePx + 2 * edgeMarginPx);
- } else {
- allAppsNumRows = (int) numRows + 1;
- }
- allAppsNumCols = (availableWidthPx - padding.left - padding.right - 2 * edgeMarginPx) /
- (iconSizePx + 2 * edgeMarginPx);
- }
- }
- private float dist(PointF p0, PointF p1) {
- return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
- (p1.y-p0.y)*(p1.y-p0.y));
- }
- private float weight(PointF a, PointF b,
- float pow) {
- float d = dist(a, b);
- if (d == 0f) {
- return Float.POSITIVE_INFINITY;
- }
- return (float) (1f / Math.pow(d, pow));
- }
- private float invDistWeightedInterpolate(float width, float height,
- ArrayList<DeviceProfileQuery> points) {
- float sum = 0;
- float weights = 0;
- float pow = 5;
- float kNearestNeighbors = 3;
- final PointF xy = new PointF(width, height);
- ArrayList<DeviceProfileQuery> pointsByNearness = points;
- Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
- public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
- return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
- }
- });
- for (int i = 0; i < pointsByNearness.size(); ++i) {
- DeviceProfileQuery p = pointsByNearness.get(i);
- if (i < kNearestNeighbors) {
- float w = weight(xy, p.dimens, pow);
- if (w == Float.POSITIVE_INFINITY) {
- return p.value;
- }
- weights += w;
- }
- }
- for (int i = 0; i < pointsByNearness.size(); ++i) {
- DeviceProfileQuery p = pointsByNearness.get(i);
- if (i < kNearestNeighbors) {
- float w = weight(xy, p.dimens, pow);
- sum += w * p.value / weights;
- }
- }
- return sum;
- }
- Rect getWorkspacePadding(int orientation) {
- Rect padding = new Rect();
- if (orientation == CellLayout.LANDSCAPE &&
- transposeLayoutWithOrientation) {
- // Pad the left and right of the workspace with search/hotseat bar sizes
- padding.set(searchBarSpaceHeightPx, edgeMarginPx,
- hotseatBarHeightPx, edgeMarginPx);
- } else {
- if (isTablet()) {
- // Pad the left and right of the workspace to ensure consistent spacing
- // between all icons
- int width = (orientation == CellLayout.LANDSCAPE)
- ? Math.max(widthPx, heightPx)
- : Math.min(widthPx, heightPx);
- // XXX: If the icon size changes across orientations, we will have to take
- // that into account here too.
- int gap = (int) ((width - 2 * edgeMarginPx -
- (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
- padding.set(edgeMarginPx + gap,
- searchBarSpaceHeightPx,
- edgeMarginPx + gap,
- hotseatBarHeightPx + pageIndicatorHeightPx);
- } else {
- // Pad the top and bottom of the workspace with search/hotseat bar sizes
- padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
- searchBarSpaceHeightPx,
- desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
- hotseatBarHeightPx + pageIndicatorHeightPx);
- }
- }
- return padding;
- }
- // The rect returned will be extended to below the system ui that covers the workspace
- Rect getHotseatRect() {
- if (isVerticalBarLayout()) {
- return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
- Integer.MAX_VALUE, availableHeightPx);
- } else {
- return new Rect(0, availableHeightPx - hotseatBarHeightPx,
- availableWidthPx, Integer.MAX_VALUE);
- }
- }
- int calculateCellWidth(int width, int countX) {
- return width / countX;
- }
- int calculateCellHeight(int height, int countY) {
- return height / countY;
- }
- boolean isPhone() {
- return !isTablet && !isLargeTablet;
- }
- boolean isTablet() {
- return isTablet;
- }
- boolean isLargeTablet() {
- return isLargeTablet;
- }
- boolean isVerticalBarLayout() {
- return isLandscape && transposeLayoutWithOrientation;
- }
- public void layout(Launcher launcher) {
- FrameLayout.LayoutParams lp;
- Resources res = launcher.getResources();
- boolean hasVerticalBarLayout = isVerticalBarLayout();
- // Layout the search bar space
- View searchBar = launcher.getSearchBar();
- lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
- if (hasVerticalBarLayout) {
- // Vertical search bar
- lp.gravity = Gravity.TOP | Gravity.LEFT;
- lp.width = searchBarSpaceHeightPx;
- lp.height = LayoutParams.MATCH_PARENT;
- searchBar.setPadding(
- 0, 2 * edgeMarginPx, 0,
- 2 * edgeMarginPx);
- } else {
- // Horizontal search bar
- lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
- lp.width = searchBarSpaceWidthPx;
- lp.height = searchBarSpaceHeightPx;
- searchBar.setPadding(
- 2 * edgeMarginPx,
- 2 * edgeMarginPx,
- 2 * edgeMarginPx, 0);
- }
- searchBar.setLayoutParams(lp);
- // Layout the search bar
- View qsbBar = launcher.getQsbBar();
- LayoutParams vglp = qsbBar.getLayoutParams();
- vglp.width = LayoutParams.MATCH_PARENT;
- vglp.height = LayoutParams.MATCH_PARENT;
- qsbBar.setLayoutParams(vglp);
- // Layout the voice proxy
- View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy);
- if (voiceButtonProxy != null) {
- if (hasVerticalBarLayout) {
- // TODO: MOVE THIS INTO SEARCH BAR MEASURE
- } else {
- lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams();
- lp.gravity = Gravity.TOP | Gravity.END;
- lp.width = (widthPx - searchBarSpaceWidthPx) / 2 +
- 2 * iconSizePx;
- lp.height = searchBarSpaceHeightPx;
- }
- }
- // Layout the workspace
- View workspace = launcher.findViewById(R.id.workspace);
- lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
- lp.gravity = Gravity.CENTER;
- Rect padding = getWorkspacePadding(isLandscape
- ? CellLayout.LANDSCAPE
- : CellLayout.PORTRAIT);
- workspace.setPadding(padding.left, padding.top,
- padding.right, padding.bottom);
- workspace.setLayoutParams(lp);
- // Layout the hotseat
- View hotseat = launcher.findViewById(R.id.hotseat);
- lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
- if (hasVerticalBarLayout) {
- // Vertical hotseat
- lp.gravity = Gravity.RIGHT;
- lp.width = hotseatBarHeightPx;
- lp.height = LayoutParams.MATCH_PARENT;
- hotseat.setPadding(0, 2 * edgeMarginPx,
- 2 * edgeMarginPx, 2 * edgeMarginPx);
- } else if (isTablet()) {
- // Pad the hotseat with the grid gap calculated above
- int gridGap = (int) ((widthPx - 2 * edgeMarginPx -
- (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
- int gridWidth = (int) ((numColumns * cellWidthPx) +
- ((numColumns - 1) * gridGap));
- int hotseatGap = (int) Math.max(0,
- (gridWidth - (numHotseatIcons * hotseatCellWidthPx))
- / (numHotseatIcons - 1));
- lp.gravity = Gravity.BOTTOM;
- lp.width = LayoutParams.MATCH_PARENT;
- lp.height = hotseatBarHeightPx;
- hotseat.setPadding(2 * edgeMarginPx + gridGap + hotseatGap, 0,
- 2 * edgeMarginPx + gridGap + hotseatGap,
- 2 * edgeMarginPx);
- } else {
- // For phones, layout the hotseat without any bottom margin
- // to ensure that we have space for the folders
- lp.gravity = Gravity.BOTTOM;
- lp.width = LayoutParams.MATCH_PARENT;
- lp.height = hotseatBarHeightPx;
- hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
- 2 * edgeMarginPx, 0);
- }
- hotseat.setLayoutParams(lp);
- // 页面指示器(就是热键上端的小圆点)
- View pageIndicator = launcher.findViewById(R.id.page_indicator);
- if (pageIndicator != null) {
- if (hasVerticalBarLayout) {
- // Hide the page indicators when we have vertical search/hotseat
- pageIndicator.setVisibility(View.GONE);
- } else {
- // Put the page indicators above the hotseat
- lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
- lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
- lp.width = LayoutParams.WRAP_CONTENT;
- lp.height = LayoutParams.WRAP_CONTENT;
- lp.bottomMargin = hotseatBarHeightPx;
- pageIndicator.setLayoutParams(lp);
- }
- }
- }
- }
最后终于到我们的主类了:
- public class DynamicGrid {
- @SuppressWarnings("unused")<span style="white-space:pre"> </span>// 该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。
- private static final String TAG = "DynamicGrid";
- private DeviceProfile mProfile;
- private float mMinWidth;
- private float mMinHeight;
- public static float dpiFromPx(int size, DisplayMetrics metrics){
- float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT;
- return (size / densityRatio);
- }
- public static int pxFromDp(float size, DisplayMetrics metrics) {
- return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
- size, metrics));
- }
- public static int pxFromSp(float size, DisplayMetrics metrics) {
- return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
- size, metrics));
- }
- public DynamicGrid(Context context, Resources resources,
- int minWidthPx, int minHeightPx,
- int widthPx, int heightPx,
- int awPx, int ahPx) {
- DisplayMetrics dm = resources.getDisplayMetrics();
- ArrayList<DeviceProfile> deviceProfiles =
- new ArrayList<DeviceProfile>();
- boolean hasAA = !AppsCustomizePagedView.DISABLE_ALL_APPS;
- // Our phone profiles include the bar sizes in each orientation /* zhoumushui:下面定义了一些设备的信息,里面的9个参数分别是:设备名,最小宽度Dps,最小高度Dps,行数,列数,图标大小,图标中字体大小,固定热键(通常放拨号,短信等几个常用应用)数目,固定热键图标大小 */
- deviceProfiles.add(new DeviceProfile("Super Short Stubby",
- 255, 300, 2, 3, 48, 13, (hasAA ? 5 : 4), 48));
- deviceProfiles.add(new DeviceProfile("Shorter Stubby",
- 255, 400, 3, 3, 48, 13, (hasAA ? 5 : 4), 48));
- deviceProfiles.add(new DeviceProfile("Short Stubby",
- 275, 420, 3, 4, 48, 13, (hasAA ? 5 : 4), 48));
- deviceProfiles.add(new DeviceProfile("Stubby",
- 255, 450, 3, 4, 48, 13, (hasAA ? 5 : 4), 48));
- deviceProfiles.add(new DeviceProfile("Nexus S",
- 296, 491.33f, 4, 4, 48, 13, (hasAA ? 5 : 4), 48));
- /// M: add 294 X 460 profile
- deviceProfiles.add(new DeviceProfile("294 X 460",
- 294, 460, 4, 4, 50.34943f, 13, (hasAA ? 5 : 4), 49.566288f));
- deviceProfiles.add(new DeviceProfile("Nexus 4",<span style="white-space:pre"> </span>
- 359, 518, 4, 4, 60, 13, (hasAA ? 5 : 4), 56));
- // The tablet profile is odd in that the landscape orientation
- // also includes the nav bar on the side
- deviceProfiles.add(new DeviceProfile("WVGA",
- 407, 727, 4, 5, 68, 13, (hasAA ? 5 : 4), 56));
- deviceProfiles.add(new DeviceProfile("1024x600",
- 527, 951, 5, 6, 72, 14.4f, 7, 60));
- deviceProfiles.add(new DeviceProfile("Nexus 7",
- 575, 904, 6, 6, 72, 14.4f, 7, 60));
- // Larger tablet profiles always have system bars on the top & bottom
- deviceProfiles.add(new DeviceProfile("Nexus 10",
- 727, 1207, 5, 8, 80, 14.4f, 9, 64));
- deviceProfiles.add(new DeviceProfile("andows",
- 647, 1280, 5, 8, 56, 14.4f, 9, 60));
- deviceProfiles.add(new DeviceProfile("Nexus 7",
- 600, 960, 5, 5, 72, 14.4f, 5, 60));
- deviceProfiles.add(new DeviceProfile("Nexus 10",
- 800, 1280, 5, 5, 80, 14.4f, (hasAA ? 7 : 6), 64));
- deviceProfiles.add(new DeviceProfile("20-inch Tablet",
- 1527, 2527, 7, 7, 100, 20, 7, 72));
- mMinWidth = dpiFromPx(minWidthPx, dm);
- mMinHeight = dpiFromPx(minHeightPx, dm);
- mProfile = new DeviceProfile(context, deviceProfiles,
- mMinWidth, mMinHeight,
- widthPx, heightPx,
- awPx, ahPx,
- resources);
- }
- DeviceProfile getDeviceProfile() {
- return mProfile;
- }
- public String toString() {
- return "-------- DYNAMIC GRID ------- \n" +
- "Wd: " + mProfile.minWidthDps + ", Hd: " + mProfile.minHeightDps +
- ", W: " + mProfile.widthPx + ", H: " + mProfile.heightPx +
- " [r: " + mProfile.numRows + ", c: " + mProfile.numColumns +
- ", is: " + mProfile.iconSizePx + ", its: " + mProfile.iconTextSize +
- ", cw: " + mProfile.cellWidthPx + ", ch: " + mProfile.cellHeightPx +
- ", hc: "
- + mProfile.numHotseatIcons + ", his: "
- + mProfile.hotseatIconSizePx + "]";
- }
- }
WallpaperCropActivity:
最近两天改一个客户需求改得挺耗时,多数都用在了WallpaperCropActivity这个文件中。今天就顺带分析一下这个文件。
路径:packages/apps/Launcher3/src/com/android/launcher3/WallpaperCropActivity.java
这个Activity是Launcher设置壁纸的Activity,完成剪切壁纸大小,设置壁纸等操作。
就在代码里面加注释分析吧,方便一些。
因为我这边的代码是mediaTek定制过的,有些地方和Google官方不一样,所以就没用。以下代码是从网上找的Google官方的代码:
(其实前4篇都是mediaTek定制过的代码,当时也没有考虑到和Google的差异)
/* * Copyright (C) 2013 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. */ package com.android.launcher3; import android.app.ActionBar; import android.app.Activity; import android.app.WallpaperManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.graphics.BitmapRegionDecoder; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.graphics.RectF; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.FloatMath; import android.util.Log; import android.view.Display; import android.view.View; import android.view.WindowManager; import com.android.gallery3d.common.Utils; import com.android.gallery3d.exif.ExifInterface; import com.android.photos.BitmapRegionTileSource; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class WallpaperCropActivity extends Activity { private static final String LOGTAG = "Launcher3.CropActivity"; protected static final String WALLPAPER_WIDTH_KEY = "wallpaper.width"; protected static final String WALLPAPER_HEIGHT_KEY = "wallpaper.height"; private static final int DEFAULT_COMPRESS_QUALITY = 90;<span style="white-space:pre"> </span>// 默认的图片压缩质量 /** * The maximum bitmap size we allow to be returned through the intent. * Intents have a maximum of 1MB in total size. However, the Bitmap seems to * have some overhead to hit so that we go way below the limit here to make * sure the intent stays below 1MB.We should consider just returning a byte * array instead of a Bitmap instance to avoid overhead. */ public static final int MAX_BMAP_IN_INTENT = 750000; private static final float WALLPAPER_SCREENS_SPAN = 2f; protected CropView mCropView; protected Uri mUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); if (!enableRotation()) { setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT); } } protected void init() { setContentView(R.layout.wallpaper_cropper); mCropView = (CropView) findViewById(R.id.cropView); Intent cropIntent = getIntent(); final Uri imageUri = cropIntent.getData(); if (imageUri == null) { Log.e(LOGTAG, "No URI passed in intent, exiting WallpaperCropActivity"); finish(); return; } int rotation = getRotationFromExif(this, imageUri); mCropView.setTileSource(new BitmapRegionTileSource(this, imageUri, 1024, rotation), null); mCropView.setTouchEnabled(true); // Action bar // Show the custom action bar view final ActionBar actionBar = getActionBar(); actionBar.setCustomView(R.layout.actionbar_set_wallpaper); actionBar.getCustomView().setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { boolean finishActivityWhenDone = true; cropImageAndSetWallpaper(imageUri, null, finishActivityWhenDone); } }); } public boolean enableRotation() { return getResources().getBoolean(R.bool.allow_rotation); } public static String getSharedPreferencesKey() { return WallpaperCropActivity.class.getName(); } // As a ratio of screen height, the total distance we want the parallax effect to span // horizontally private static float wallpaperTravelToScreenWidthRatio(int width, int height) { float aspectRatio = width / (float) height; // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width // We will use these two data points to extrapolate how much the wallpaper parallax effect // to span (ie travel) at any aspect ratio: final float ASPECT_RATIO_LANDSCAPE = 16/10f; final float ASPECT_RATIO_PORTRAIT = 10/16f; final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; // To find out the desired width at different aspect ratios, we use the following two // formulas, where the coefficient on x is the aspect ratio (width/height): // (16/10)x + y = 1.5 // (10/16)x + y = 1.2 // We solve for x and y and end up with a final formula: final float x = (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; return x * aspectRatio + y; }
/*以下这个函数用来获取壁纸的长和宽,Google的源码逻辑还是要简单一些,没有做太多的定制。这个函数可以做很多扩展,比如说让横屏显示图片全部,让横屏和竖屏分别用两张分辨率不同长宽不同的图片等等。当然复杂了就容易有Bug,在锁屏界面进行转屏可能会出现闪屏的情况*/ static protected Point getDefaultWallpaperSize(Resources res, WindowManager windowManager) { Point minDims = new Point(); Point maxDims = new Point(); windowManager.getDefaultDisplay().getCurrentSizeRange(minDims, maxDims); int maxDim = Math.max(maxDims.x, maxDims.y); int minDim = Math.max(minDims.x, minDims.y); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { Point realSize = new Point(); windowManager.getDefaultDisplay().getRealSize(realSize); maxDim = Math.max(realSize.x, realSize.y); minDim = Math.min(realSize.x, realSize.y); } // We need to ensure that there is enough extra space in the wallpaper // for the intended // parallax effects final int defaultWidth, defaultHeight; if (isScreenLarge(res)) {// 屏幕最短边大于720则被判定为大屏幕设备 defaultWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim)); defaultHeight = maxDim; } else { defaultWidth = Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim); defaultHeight = maxDim; } return new Point(defaultWidth, defaultHeight); } public static int getRotationFromExif(String path) { return getRotationFromExifHelper(path, null, 0, null, null); } public static int getRotationFromExif(Context context, Uri uri) { return getRotationFromExifHelper(null, null, 0, context, uri); } public static int getRotationFromExif(Resources res, int resId) { return getRotationFromExifHelper(null, res, resId, null, null); } private static int getRotationFromExifHelper( String path, Resources res, int resId, Context context, Uri uri) { ExifInterface ei = new ExifInterface(); try { if (path != null) { ei.readExif(path); } else if (uri != null) { InputStream is = context.getContentResolver().openInputStream(uri); BufferedInputStream bis = new BufferedInputStream(is); ei.readExif(bis); } else { InputStream is = res.openRawResource(resId); BufferedInputStream bis = new BufferedInputStream(is); ei.readExif(bis); } Integer ori = ei.getTagIntValue(ExifInterface.TAG_ORIENTATION); if (ori != null) { return ExifInterface.getRotationForOrientationValue(ori.shortValue()); } } catch (IOException e) { Log.w(LOGTAG, "Getting exif data failed", e); } return 0; }
// 根据文件路径filePath设置壁纸 protected void setWallpaper(String filePath, final boolean finishActivityWhenDone) { int rotation = getRotationFromExif(filePath); BitmapCropTask cropTask = new BitmapCropTask( this, filePath, null, rotation, 0, 0, true, false, null); final Point bounds = cropTask.getImageBounds(); Runnable onEndCrop = new Runnable() { public void run() { updateWallpaperDimensions(bounds.x, bounds.y); if (finishActivityWhenDone) { setResult(Activity.RESULT_OK); finish(); } } }; cropTask.setOnEndRunnable(onEndCrop); cropTask.setNoCrop(true); cropTask.execute(); }
/*这个函数无疑是剪切图片,然后设置壁纸了,图片的outSize由上面的函数getDefaultWallpaperSize所返回,这个函数也可以进行扩充,根据resId可以对默认壁纸和壁纸库里的其他壁纸进行区分,然后设置不同的响应函数,虽然我觉的这个在实际应用中的意义不大,但是永远不能低估客户的想象力和创造力,我还就偏偏遇到了这样的客户需求。*/ protected void cropImageAndSetWallpaper( Resources res, int resId, final boolean finishActivityWhenDone) { // crop this image and scale it down to the default wallpaper size for // this device int rotation = getRotationFromExif(res, resId); Point inSize = mCropView.getSourceDimensions(); Point outSize = getDefaultWallpaperSize(getResources(), getWindowManager()); RectF crop = getMaxCropRect( inSize.x, inSize.y, outSize.x, outSize.y, false); Runnable onEndCrop = new Runnable() { public void run() { // Passing 0, 0 will cause launcher to revert to using the // default wallpaper size updateWallpaperDimensions(0, 0); if (finishActivityWhenDone) { setResult(Activity.RESULT_OK); finish(); } } }; BitmapCropTask cropTask = new BitmapCropTask(this, res, resId, crop, rotation, outSize.x, outSize.y, true, false, onEndCrop); cropTask.execute(); }
/*这个函数就是上面说到的判断是否属于大屏幕设备的函数了,可以看到Google的定义是屏幕最短边大于720就算是了。自然可以根据自己的需要来修改这个数值*/ private static boolean isScreenLarge(Resources res) { Configuration config = res.getConfiguration(); return config.smallestScreenWidthDp >= 720; }/*不同参数的cropImageAndSetWallpaper,可以看到参数相比上面的cropImageAndSetWallpaper由resId变成了<pre name="code" class="java">OnBitmapCroppedHandler,处理过程相对复杂一些。而且加入了是否是LTR布局的判断。*/
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">核心的部分也不是很多,其他不少地方都已经用英文标注了,相信看这篇文字的都是IT人,英语自然不在话下,就不充当翻译了。</span>
Launcher3去掉所有应用列表,横屏时左右两侧的留空:
// Layout AllApps AppsCustomizeTabHost host = (AppsCustomizeTabHost) launcher.findViewById(R.id.apps_customize_pane); if (host != null) { // Center the all apps page indicator int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f, (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX))); pageIndicator = host.findViewById(R.id.apps_customize_page_indicator); if (pageIndicator != null) { lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams(); lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; lp.width = LayoutParams.WRAP_CONTENT; lp.height = pageIndicatorHeight; pageIndicator.setLayoutParams(lp); } AppsCustomizePagedView pagedView = (AppsCustomizePagedView) host.findViewById(R.id.apps_customize_pane_content); padding = new Rect(); if (pagedView != null) { // Constrain the dimensions of all apps so that it does not span the full width // TChip ZJ Add START: 去掉所有应用列表,横屏时左右两侧的Margin int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) / (2 * (allAppsNumCols + 1)); int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) / (2 * (allAppsNumRows + 1)); paddingLR = Math.min(paddingLR, (int)((paddingLR + paddingTB) * 0.75f)); paddingTB = Math.min(paddingTB, (int)((paddingLR + paddingTB) * 0.75f)); paddingLR = (int)(paddingLR * 0.75f); paddingTB = (int)(paddingTB * 0.75f); // TChip ZJ Add END // TChip ZJ Minus START: 去掉所有应用列表,横屏时左右两侧的Margin /* int paddingLR = 2; int paddingTB = 2; */ // TChip ZJ Minus END int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR)); int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 4; // Only adjust the side paddings on landscape phones, or tablets if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) { padding.left = padding.right = gridPaddingLR; } // The icons are centered, so we can't just offset by the page indicator height // because the empty space will actually be pageIndicatorHeight + paddingTB padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB); pagedView.setAllAppsPadding(padding); pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight); } }
Launcher3的DeviceProfile的改变
最近真忙,没时间细写blog了。
对比了两个平台的Launcher3项目,虽然都是3,但是代码也不是一成不变的,新版本的Launcher3,DeviceProfile也从DynamicGrid中独立出来了。
之前加的很多属性不再适用,需要重新阅读源码,进行修改。呃,工作量不小。
在Launcher的主菜单中隐藏某个应用removePackage
在package/apps/Launcher3/src/com/android/launcher3/LauncherModel.java中的private void loadAllApps() {}函数中的mBgAllAppsList.reorderApplist();之前添加如下:
// ZJ Add START mBgAllAppsList.removePackage("com.mediatek.filemanager"); mBgAllAppsList.removePackage("com.android.chrome"); mBgAllAppsList.removePackage("com.android.providers.downloads.ui"); mBgAllAppsList.removePackage("com.android.deskclock"); mBgAllAppsList.removePackage("com.android.email"); mBgAllAppsList.removePackage("com.google.android.apps.maps"); mBgAllAppsList.removePackage("com.google.android.gm"); mBgAllAppsList.removePackage("com.android.soundrecorder"); mBgAllAppsList.removePackage("com.mediatek.FMRadio"); mBgAllAppsList.removePackage("com.google.android.inputmethod.korean"); // ZJ Add END mBgAllAppsList.reorderApplist();
mBgAllAppList是类AllAppList的对象,可以看下removePackage这个函数的实现:
/** * Remove the apps for the given apk identified by packageName. */ public void removePackage(String packageName) { final List<AppInfo> data = this.data; if (LauncherLog.DEBUG) { LauncherLog.d(TAG, "removePackage: packageName = " + packageName + ", data size = " + data.size()); } for (int i = data.size() - 1; i >= 0; i--) { AppInfo info = data.get(i); final ComponentName component = info.intent.getComponent(); if (packageName.equals(component.getPackageName())) { removed.add(info); data.remove(i); } } // This is more aggressive than it needs to be. mIconCache.flush(); }
转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui