Android StackBox 分屏效果

最近自己在折腾多窗口系统,结果从一位高手那得知,KitKat已经自带了分屏的代码,以及关键的数据结构:StackBox,自己乱折腾了下,真的在AOSP里跑起来了分屏的效果。

下面打算将原来自己乱折腾出的多窗口,利用StackBox改进下。

参考下面的文章跑起来StackBox分屏效果。

From Split Screens in Android Exist?

Plenty of Android phone vendors have been doing this for a long time - split screens.

I am talking about that feature (taken from desktop Os's) where you have several applications displayed on one screen.
Check out how these guys do it: http://www.onskreen.com/cornerstone (their solution was also supposed to be adopted by CyanogenMod a while ago).
I am sure there is still a lot of work ahead for the guys at Samsung etc. making their solution a little more user-friendly for mobile devices, but what I want to talk about is Google's own solution.
This new Android feature has been around for a while, but no one seems to care (you'll see why shortly). Somehow this newAPI seems to go unnoticed.
I only noticed it when I was comparing Kitkat's ActivityManagerService.java to JB's. I was wondering what these methods: createStack, resizeStackBoxand getStackswere all about so I started digging.

A full explanation about how Kitkat's ActivityManagerServiceand WindowsManagerServiceinteract is beyond the scope of this post.
I can only say two things:
  1. Since 4.4, there is more interaction between these two services (the WindowManagerService became stack-aware).
  2. This interaction is still far from finished (i.e. this split screen solution is not working properly yet - but it is a small step in the right direction).
* In the description below I refer to what the WindowManagerService calls a 'stack box' as 'stack'.
I am only doing this for my own convenience. What the ActivityManagerService refers to as a stack is actually a different type of object.

I started with a clean emulator and launched the Email app followed by the People app.
# am stack boxes

Pay attention to the output. You will notice that there are actually 2 activity stacks. Unlike JB, where there was one ActivityStack, in Kitkat we have 2:
  • HOME stack: This is the stack with id = 0. This stack is used by the Launcher activities. When several users run Lanchers on one device, they will all belong to this stack. Other than that, systemui activities are also launched in it.
  • Applications stack: The id for this stack could be any number. All activities that are neither Launcher apps or systemui activities are run here (for all users).
Both these stacks are controlled by the ActivityStackSupervisor.
In order to split the screen, all I had to do was:
# am stack create 3 2 4 0.5

And this is the result:
Figure 1 - People and Email apps share one screen
What do these numbers mean?
Syntax: am stack create <int1> <int2> <int3> <float1>
  • <int1>: TASK_ID - the id for the existing task that you want a separate stack for.
  • <int2>: RELATIVE_STACK_BOX_ID - an existing stack id. The postion of the new stack will be relative to this one.
  • <int3>: POSITION - the relative position of the stack. Could be any one of these values:
    • 0: before relative stack (depends on RTL/LTR configuration)
    • 1: after relative stack (depends on RTL/LTR configuration)
    • 2: to left of relative stack
    • 3: to right of relative stack
    • 4: above relative stack
    • 5: below relative stack
    • 6: displayed on a higher layer than the relative stack (unused)
    • 7: displayed on a lower layer than the relative stack (unused)
  • <float1>: WEIGHT - a number between 0.2 - 0.8 inclusive
What I actually did was move task 3 (Email) above stack id 2 (People) and gave each an equal rectangular area on the screen. Now the original stack 2 is split into 2 boxes: the first holds the Email activity and the second holds the People activity. Each one of them now has a different area on the screen.
If instead of 0.5 for weight, I used 0.7 as weight, this would be the result:
Figure 2- 70% of the screen is taken by Email app

As you can see in Figure 2, the weight parameter controls the relative amount of space each stack box takes up on the screen.
Problems
I haven't described the entire user feature set (you can also resize a stack and change tasks order inside a stack). However, I will not describe this since, at this point, when you try it at home you are likely to be disappointed. Why?
  • These features are a hidden API. Writing your own app to tweak these features requires reflection.
  • Even worse, the permissions required for this are with a permissionLevel of type 'signature'. So unless you are building your own ROM, or you have your ROM's signature you can't do this with your own app.
  • If you decide to use am on your device, then you must run it as a super-user. This means the device must be rooted (I described below another thing you should do if you choose this approach)
  • Most importantly, it doesn't work that well. You will need to take several shots at this before it works right for you.
Even when it works, sometimes the outcome is somewhat unexpected:
Figure 3 - This is what happened when I tried to split the Launcher stack (HOME stack)

If anyone wants to try the am commands on a rooted device, he'd be surprised to find out that the am command does not print anything to the screen when it is run with 'su'.
So before yo try anything, run this command:
# setprop log.redirect-stdio true
this way you can see what am prints in the logcat.
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值