一、概述

现在越来越多的Android设备有多个屏幕,双屏异显应用场景最多的应该就是类似于收银平台那种设备,在主屏上店员能够对点商品进行选择录入,副屏则是展示给我们的账单详情,但是它只通过了一个软件系统就实现了双屏异显这个功能,而Presentation正是这其中的关键。

二、Presentation分析

  1. 简述:首先从它的继承关系上来看Presentation是继承自Dialog的,就是说它其实就是一种特殊的Dialog用于在第二个屏幕上显示内容的,它在创建时会和它的目标展示屏幕相关联,包括它的context和一些配置参数等。
  2. Context:然而这里提到的context和它的容器所处的context会有不同,它会用自身的context去加载presentation的布局和相关资源以此来确保在目标屏幕上能够展示正确的大小和获取合适的屏幕密度。
  3. 自动移除:虽说presentation和它相关联的Activity的context不同,但是他们也不是完全分离的关系,当和presentation相关联的屏幕被移除后,presentation也会自动的被移除,所以当Activity处于pause和resume的状态时Presentation也需要特别注意当前显示的内容的状态。
  4. 屏幕选择:因为有时候我们的Android设备有多个屏幕,所以选择合适的屏幕去展示就显得非常重要了,所以在我们显示Presentation的时候需要去让我们的系统去选择合适的屏幕来进行展示,以下是两种方式去选择一个合适的屏幕。 

 

  这是我们选择展示presentation最简单的一种方式,media router是一种系统层级的服务,它能够追踪到系统当中所有可用的音频和视屏route,当有路径被选中或取消选中,还有当适合用presentation进行显示的时候的route改变的时候它会发送一个通知,然后应用本身会监控这个通知自动的去选择presentation的展示或者隐藏,这里的推荐使用的presentation display其实只是media router推荐的,如果我们的应用需要在第二个屏幕上进行显示就使用,如果不用的话就用本地来展示内容。  

  •  利用media router去选择presentation的显示屏幕
  • 利用display manager去选择persentation的显示屏幕

DisplayManager能够监控到我们系统当中的所有连接上的显示设备,然而不是所有的设备都适用于作为副屏来进行内容展示的,比如当一个Activity想显示一个presentation在主屏幕上,其实效果就会相当于在主Activity当中显示了一个特殊的Dialog,所以当我们选择这种方式去选用Presentation去显示的时候就必须给它绑定一个可用的display。

三、源码分析

 首先来看它的构造函数

public Presentation(Context outerContext, Display display, int theme) {
        super(createPresentationContext(outerContext, display, theme), theme, false);

        mDisplay = display;
        mDisplayManager = (DisplayManager)getContext().getSystemService(DISPLAY_SERVICE);

        final Window w = getWindow();
        final WindowManager.LayoutParams attr = w.getAttributes();
        attr.token = mToken;
        w.setAttributes(attr);
        w.setGravity(Gravity.FILL);
        w.setType(TYPE_PRESENTATION);
        setCanceledOnTouchOutside(false);
    }

在它的形参中第一个Context参数非常重要,这是应用正在展示presentation的一个context,它是Presentation自己创建的它自己的一个context,基于这个context才能正确的在它所关联的屏幕上展示合适的信息。然后代码里面设置了这个window的相关属性。

接着我们看看它自身的Context的创建

private static Context createPresentationContext(
            Context outerContext, Display display, int theme) {
        //首先判断传入的context和display是否为空,为空则抛出异常
        if (outerContext == null) {
            throw new IllegalArgumentException("outerContext must not be null");
        }
        if (display == null) {
            throw new IllegalArgumentException("display must not be null");
        }
        
        Context displayContext = outerContext.createDisplayContext(display);

        //这里是对它的主题的判断,为0即为默认主题
        if (theme == 0) {
            TypedValue outValue = new TypedValue();
            displayContext.getTheme().resolveAttribute(
                    com.android.internal.R.attr.presentationTheme, outValue, true);
            theme = outValue.resourceId;
        }

        // Derive the display's window manager from the outer window manager.
        // We do this because the outer window manager have some extra information
        // such as the parent window, which is important if the presentation uses
        // an application window type.
        final WindowManagerImpl outerWindowManager =
                (WindowManagerImpl)outerContext.getSystemService(WINDOW_SERVICE);
        final WindowManagerImpl displayWindowManager =
                outerWindowManager.createPresentationWindowManager(displayContext);

        //因为ContextThemeWrapper的父类是我们的Context
        //所以这里最终返回的就是Presentation给我们创建好的Context
        return new ContextThemeWrapper(displayContext, theme) {
            
            //在这个方法中又返回的是Object对象
            @Override
            public Object getSystemService(String name) {
                if (WINDOW_SERVICE.equals(name)) {
                    return displayWindowManager;
                    //如果和这个传入的name相同的话返回的就是上面windowManager创建出来的WindowManager的一个具体实现
                }
                //否则就返回的是Context这个抽象类中的一种服务类型
                return super.getSystemService(name);
            }
        };
    }

接着我们继续看一下Presentation对屏幕增加、移除和改变的监听

private final DisplayListener mDisplayListener = new DisplayListener() {
        @Override
        public void onDisplayAdded(int displayId) {
        }

        @Override
        public void onDisplayRemoved(int displayId) {
            if (displayId == mDisplay.getDisplayId()) {
                handleDisplayRemoved();
            }
        }

        @Override
        public void onDisplayChanged(int displayId) {
            if (displayId == mDisplay.getDisplayId()) {
                handleDisplayChanged();
            }
        }
    };

这里我们看到它对add并没有进行处理,所以我们进一步去看一下onDisplayRemoved中的关键handlerDisplayRemoved和onDisplayChanged中的核心handlerDisplayChanged的实现

handlerDisplayChanged:

private void handleDisplayChanged() {
        onDisplayChanged();

        // We currently do not support configuration changes for presentations
        // (although we could add that feature with a bit more work).
        // If the display metrics have changed in any way then the current configuration
        // is invalid and the application must recreate the presentation to get
        // a new context.
        if (!isConfigurationStillValid()) {
            Log.i(TAG, "Presentation is being dismissed because the "
                    + "display metrics have changed since it was created.");
            cancel();
        }
    }

在这个方法中,我们遗憾的发现Google程序员并没有对当前屏幕配置发生改变后做特殊的处理,所以当我们的屏幕尺寸等信息改变时,我们的presentation必须重新Create去重新获取context,再重新进行显示。

@Override
    protected void onStart() {
        super.onStart();
        mDisplayManager.registerDisplayListener(mDisplayListener, mHandler);

        // Since we were not watching for display changes until just now, there is a
        // chance that the display metrics have changed.  If so, we will need to
        // dismiss the presentation immediately.  This case is expected
        // to be rare but surprising, so we'll write a log message about it.
        if (!isConfigurationStillValid()) {
            Log.i(TAG, "Presentation is being dismissed because the "
                    + "display metrics have changed since it was created.");
            mHandler.sendEmptyMessage(MSG_CANCEL);
        }
    }

同时在onStart中我们也能发现,因为它暂时还没有对display change进行监听,而这里又是有可能会改变的,所以在这种情况下它是打印了一个log去通知一下。 

handlerDisplayRemoved这个方法的话是系统自动去发送一个消息,然后调用后把presentation自动取消掉。

四、总结

在基本分析了Presentation之后,主要要注意一下它的Context以及和Activity之间绑定的关系,其实从简单来看,它就是一个Dialog,不过是可以显示在多个屏幕上。当然上述分析深度尚浅,更深入的理解和一些问题要在后续工作中多使用再继续观察。