/***
  Copyright (c) 2013 CommonsWare, LLC
  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.
  
  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.cwac.preso;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.view.Display;

public class PresentationHelper implements
    DisplayManager.DisplayListener {
  public interface Listener {
    void showPreso(Display display);

    void clearPreso(boolean switchToInline);
  }

  private Listener listener=null;
  private DisplayManager mgr=null;
  private Context ctxt=null;
  private Display current=null;
  private boolean isFirstRun=true;
  private boolean isEnabled=true;

  public PresentationHelper(Context ctxt, Listener listener) {
    this.ctxt=ctxt;
    this.listener=listener;
  }

  public void onResume() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      mgr=
          (DisplayManager)ctxt.getSystemService(Context.DISPLAY_SERVICE);

      handleRoute();
      mgr.registerDisplayListener(this, null);
    }
  }

  public void onPause() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      listener.clearPreso(false);
      current=null;

      mgr.unregisterDisplayListener(this);
    }
  }

  public void enable() {
    isEnabled=true;
    handleRoute();
  }

  public void disable() {
    isEnabled=false;

    if (current != null) {
      listener.clearPreso(true);
      current=null;
    }
  }

  public boolean isEnabled() {
    return(isEnabled);
  }

  private void handleRoute() {
    if (isEnabled()) {
      Display[] displays=
          mgr.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);

      if (displays.length == 0) {
        if (current != null || isFirstRun) {
          listener.clearPreso(true);
          current=null;
        }
      }
      else {
        Display display=displays[0];

        if (display != null && display.isValid()) {
          if (current == null) {
            listener.showPreso(display);
            current=display;
          }
          else if (current.getDisplayId() != display.getDisplayId()) {
            listener.clearPreso(true);
            listener.showPreso(display);
            current=display;
          }
          else {
            // no-op: should already be set
          }
        }
        else if (current != null) {
          listener.clearPreso(true);
          current=null;
        }
      }

      isFirstRun=false;
    }
  }

  @Override
  public void onDisplayAdded(int displayId) {
    handleRoute();
  }

  @Override
  public void onDisplayChanged(int displayId) {
    handleRoute();
  }

  @Override
  public void onDisplayRemoved(int displayId) {
    handleRoute();
  }
}
@@ -36,16 +36,19 @@
 re, LLC%0A
+  %0A
   Licens
@@ -116,14 +116,14 @@
  may
+%0A 
  not
-%0A 
  use
@@ -190,17 +190,17 @@
 tain
+%0A 
  a copy
-%0A 
  of 
@@ -213,16 +213,20 @@
 cense at
+%0A   
  http://
@@ -260,17 +260,18 @@
 ENSE-2.0
-.
+%0A 
  Unless 
@@ -278,18 +278,16 @@
 required
-%0A 
  by appl
@@ -330,16 +330,18 @@
 software
+%0A 
  distrib
@@ -354,18 +354,16 @@
 nder the
-%0A 
  License
@@ -398,16 +398,18 @@
 %22 BASIS,
+%0A 
  WITHOUT
@@ -433,18 +433,16 @@
 NDITIONS
-%0A 
  OF ANY 
@@ -473,16 +473,18 @@
 implied.
+%0A 
  See the
@@ -508,18 +508,16 @@
 specific
-%0A 
  languag
@@ -543,16 +543,18 @@
 ions and
+%0A 
  limitat
@@ -581,101 +581,8 @@
 se.%0A
-  %0A  From _The Busy Coder's Guide to Android Development_%0A    http://commonsware.com/Android%0A
  */%0A
@@ -1025,37 +1025,8 @@
 ll;%0A
-  private Context ctxt=null;%0A
   pr
@@ -1190,28 +1190,8 @@
 ) %7B%0A
-    this.ctxt=ctxt;%0A
     
@@ -1218,39 +1218,8 @@
 er;%0A
-  %7D%0A%0A  public void onResume() %7B
 %0A   
@@ -1371,16 +1371,124 @@
 RVICE);%0A
+    %7D%0A  %7D%0A%0A  public void onResume() %7B%0A    if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN_MR1) %7B
 %0A      h

initialize the DisplayManager in constructor, rather than waiting