Android T wallpaper相关流程

介绍

什么是壁纸?
壁纸即是一个壁纸服务,每换一张壁纸 ,就是将该图片写入壁纸文件,再启动一个壁纸服务读取该壁纸文件显示出来的过程。
我们常见的壁纸在客户端的代码在systemUI中ImageWallpaper,壁纸服务实现在SystemUI里面,所以其会跟随SystemUI进程的启动而启动,不论是系统应用壁纸,还是三方应壁纸,基本上都会去继承WallpaperService,这里我们主要讲解静态壁纸的显示和移除,主要涉及WallpaperService和WallpaperManagerService

学习思路

在我们完全不了解壁纸逻辑的情况下如何学习这个模块?前面我们学习WMS的添加等相关流程见:Android T WMS窗口相关流程,因此我们可以先从壁纸中添加和移除逻辑入手。
1.这里以添加为例,如果我们要添加窗口,必然会涉及addView,addToDisplay(客户端)或addWindow(服务端)的流程
2.ImageWallpaper在systemUI进程中,因此我们首先想到的是调用客户端中的添加窗口方式
3.在代码中搜索相关代码addView,addToDisplay
发现在WallpaperService代码中有相关的添加逻辑,在updateSurface方法中,以此跟踪流程
在这里插入图片描述
但是通过查找代码发现有很多地方都调用了这个updateSurface,怎么确定流程呢?
在这里插入图片描述

4.找token,壁纸的窗口属于非system window,因此可能会通过mLayout.token的方式添加token.
mLayout.token = mWindowToken;发现有token,看看是什么时候赋值的,发现其在attach()方法中有赋值
在这里插入图片描述
该方法在最后也调用了updateSurface()
5.继续看谁调用的attach(),看看我们发现了什么?
在这里插入图片描述
这里是跨进程通信,WallpaperService继承Service
6.最终我们找到了WallpaperManagerService
在这里插入图片描述
其调用了attach
在这里插入图片描述

后面有同样的方法查找,这里不在赘述

注:如果了解SurfaceControl可以在其show()方法中打印堆栈,追踪流程,里面有三个关键方法:show()、hide()、remove()

壁纸切换流程简述

在这里插入图片描述

其中attach方法和detach方法是作为异步binder调用
在这里插入图片描述
可以看到这里定义的是oneway。

问题

从代码中可以看出壁纸的移除和添加是异步调用,那么就有概率会出现,旧壁纸移除了,但是新壁纸还没有添加完成的情况,那么在切换过程中就可能会有闪黑现象,因此我们可以尝试把旧壁纸的移除逻辑放在新壁纸添加完成后。

frameworks/base/core/java/android/service/wallpaper/WallpaperService.java

**
     * The actual implementation of a wallpaper.  A wallpaper service may
     * have multiple instances running (for example as a real wallpaper
     * and as a preview), each of which is represented by its own Engine
     * instance.  You must implement {@link WallpaperService#onCreateEngine()}
     * to return your concrete Engine implementation.
     */
    public class Engine {
        ......
		void detach() {
			if (mCreated) {
                try {
                    if (DEBUG) Log.v(TAG, "Removing window and destroying surface "
                            + mSurfaceHolder.getSurface() + " of: " + this);

                    if (mInputEventReceiver != null) {
                        mInputEventReceiver.dispose();
                        mInputEventReceiver = null;
                    }
                    
                    //注释掉壁纸移除WindowState逻辑
                    //mSession.remove(mWindow);
                } catch (RemoteException e) {
                }
                mSurfaceHolder.mSurface.release();
                if (mBlastBufferQueue != null) {
                    mBlastBufferQueue.destroy();
                    mBlastBufferQueue = null;
                }
                if (mBbqSurfaceControl != null) {
                    //注释掉壁纸移除mBbqSurfaceControl逻辑
                    //new SurfaceControl.Transaction().remove(mBbqSurfaceControl).apply();
                    mBbqSurfaceControl = null;
                }
                mCreated = false;
            }
        }
        ......
    }

这个方法主要就是移除了壁纸的WindowState和SurfaceControl。

frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java

//创建WallpaperConnection.DisplayConnector对象,用来获取其成员变量
WallpaperConnection.DisplayConnector mPendingRemoveDisplayConnector= null;

class WallpaperConnection extends IWallpaperConnection.Stub
            implements ServiceConnection {

        /**
         * Collect needed info for a display.
         */
        @VisibleForTesting
        final class DisplayConnector {
            final int mDisplayId;
            final Binder mToken = new Binder();
            IWallpaperEngine mEngine;
            boolean mDimensionsChanged;
            boolean mPaddingChanged;
            ......

            void disconnectLocked() {
                if (DEBUG) Slog.v(TAG, "Removing window token: " + mToken);
                //注释掉壁纸移除WindowToken逻辑
                //removeWindowToken中传递的参数false,表示只移除当前WindowToken
                //如果传递参数为true,则还会移除挂在其下面的windowState
//                mWindowManagerInternal.removeWindowToken(mToken, false/* removeWindows */,
//                        mDisplayId);
                //保存当前DisplayConnector对象,后续用来获取mDisplayId和mToken
                mPendingRemoveDisplayConnector = this;
                try {
                    if (mEngine != null) {
                        mEngine.destroy();
                    }
                } catch (RemoteException e) {
                }
                mEngine = null;
            }
        }
        ......
        public void engineShown(IWallpaperEngine engine) {
            //start
            //判断mPendingRemoveDisplayConnector是否为空
            if (mPendingRemoveDisplayConnector != null) {
                //传递前面disconnectLocked方法中保存mPendingRemoveDisplayConnector的成员变量
                //removeWindowToken中传递了true参数,直接移除壁纸的WindowToken以及其windowState
                mWindowManagerInternal.removeWindowToken(mPendingRemoveDisplayConnector.mToken, 
                                true/* removeWindows */,mPendingRemoveDisplayConnector.mDisplayId);
                mPendingRemoveDisplayConnector = null;
                android.util.Log.i("Wallpaper"," engineShown mPendingRemoveDisplayConnector = " + mPendingRemoveDisplayConnector);
            //end
            synchronized (mLock) {
                if (mReply != null) {
                    TimingsTraceAndSlog t = new TimingsTraceAndSlog(TAG);
                    t.traceBegin("WPMS.mReply.sendResult");
                    final long ident = Binder.clearCallingIdentity();
                    try {
                        mReply.sendResult(null);
                    } catch (RemoteException e) {
                        Binder.restoreCallingIdentity(ident);
                        Slog.d(TAG, "failed to send callback!", e);
                    }
                    t.traceEnd();
                    mReply = null;
                }
            }
        }
        ......
    }

disconnectLocked()方法主要就是移除壁纸的WindowToken,engineShown()方法主要是用来显示壁纸。

最后编译代码即可 make framework (android 11之前的代码使用)
make framework-minus-apex(android 11及其之后的代码使用)

其他参考

android7.0 wallpaper显示流程

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值