Java tutorial
package android.support.v4.hardware.display; import android.content.Context; import android.os.Build.VERSION; import android.view.Display; import android.view.WindowManager; import java.util.WeakHashMap; public abstract class DisplayManagerCompat { public static final String DISPLAY_CATEGORY_PRESENTATION = "android.hardware.display.category.PRESENTATION"; private static final WeakHashMap<Context, DisplayManagerCompat> sInstances; private static class JellybeanMr1Impl extends DisplayManagerCompat { private final Object mDisplayManagerObj; public JellybeanMr1Impl(Context context) { this.mDisplayManagerObj = DisplayManagerJellybeanMr1.getDisplayManager(context); } public Display getDisplay(int i) { return DisplayManagerJellybeanMr1.getDisplay(this.mDisplayManagerObj, i); } public Display[] getDisplays() { return DisplayManagerJellybeanMr1.getDisplays(this.mDisplayManagerObj); } public Display[] getDisplays(String str) { return DisplayManagerJellybeanMr1.getDisplays(this.mDisplayManagerObj, str); } } private static class LegacyImpl extends DisplayManagerCompat { private final WindowManager mWindowManager; public LegacyImpl(Context context) { this.mWindowManager = (WindowManager) context.getSystemService("window"); } public Display getDisplay(int i) { Display defaultDisplay = this.mWindowManager.getDefaultDisplay(); return defaultDisplay.getDisplayId() == i ? defaultDisplay : null; } public Display[] getDisplays() { return new Display[] { this.mWindowManager.getDefaultDisplay() }; } public Display[] getDisplays(String str) { return str == null ? getDisplays() : new Display[0]; } } public abstract Display getDisplay(int i); public abstract Display[] getDisplays(); public abstract Display[] getDisplays(String str); static { sInstances = new WeakHashMap(); } DisplayManagerCompat() { } public static DisplayManagerCompat getInstance(Context context) { DisplayManagerCompat displayManagerCompat; synchronized (sInstances) { displayManagerCompat = (DisplayManagerCompat) sInstances.get(context); if (displayManagerCompat == null) { if (VERSION.SDK_INT >= 17) { displayManagerCompat = new JellybeanMr1Impl(context); } else { displayManagerCompat = new LegacyImpl(context); } sInstances.put(context, displayManagerCompat); } } return displayManagerCompat; } }