1. 程式人生 > 程式設計 >Android Presentation實現雙屏異顯

Android Presentation實現雙屏異顯

一、概述

現在越來越多的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,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,不過是可以顯示在多個螢幕上。當然上述分析深度尚淺,更深入的理解和一些問題要在後續工作中多使用再繼續觀察。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。