下面列出了怎么用android.hardware.display.DisplayManagerGlobal的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean updateLogicalDisplaysLocked() {
boolean changed = false;
for (int i = mLogicalDisplays.size(); i-- > 0; ) {
final int displayId = mLogicalDisplays.keyAt(i);
LogicalDisplay display = mLogicalDisplays.valueAt(i);
mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
display.updateLocked(mDisplayDevices);
if (!display.isValidLocked()) {
mLogicalDisplays.removeAt(i);
sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
changed = true;
} else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
handleLogicalDisplayChanged(displayId, display);
changed = true;
}
}
return changed;
}
private Display(DisplayManagerGlobal global, int displayId,
/*@NotNull*/ DisplayInfo displayInfo, DisplayAdjustments daj, Resources res) {
mGlobal = global;
mDisplayId = displayId;
mDisplayInfo = displayInfo;
mResources = res;
mDisplayAdjustments = mResources != null
? new DisplayAdjustments(mResources.getConfiguration())
: daj != null ? new DisplayAdjustments(daj) : null;
mIsValid = true;
// Cache properties that cannot change as long as the display is valid.
mLayerStack = displayInfo.layerStack;
mFlags = displayInfo.flags;
mType = displayInfo.type;
mAddress = displayInfo.address;
mOwnerUid = displayInfo.ownerUid;
mOwnerPackageName = displayInfo.ownerPackageName;
}
/**
* Set the level of color saturation to apply to the display.
*
* @param level The amount of saturation to apply, between 0 and 1 inclusive.
* 0 produces a grayscale image, 1 is normal.
* //@hide
*/
@TargetApi(Build.VERSION_CODES.P)
private static boolean setDisplaySaturationLevel(@FloatRange(from = 0, to = 1) float level) {
boolean ret = false;
try {
//Landroid/hardware/display/DisplayManagerGlobal;->setSaturationLevel(F)V,greylist-max-o
//noinspection ConstantConditions
DisplayManagerGlobal.getInstance().setSaturationLevel(level);
ret = true;
} catch (Throwable t) {
Log.e(TAG, "setDisplaySaturationLevel: ", t);
} finally {
Log.d(TAG, "main: setDisplaySaturationLevel(" + level + "): " + ret);
}
return ret;
}
/**
* Returns an adjusted {@link Display} object based on the inputs or null if display isn't
* available.
*
* @param displayId display Id.
* @param resources The {@link Resources} backing the display adjustments.
*/
public Display getAdjustedDisplay(final int displayId, Resources resources) {
synchronized (this) {
final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
if (dm == null) {
// may be null early in system startup
return null;
}
return dm.getCompatibleDisplay(displayId, resources);
}
}
/**
* Takes a screenshot.
*
* @return The screenshot bitmap on success, null otherwise.
*/
public Bitmap takeScreenshot() {
synchronized (mLock) {
throwIfNotConnectedLocked();
}
Display display = DisplayManagerGlobal.getInstance()
.getRealDisplay(Display.DEFAULT_DISPLAY);
Point displaySize = new Point();
display.getRealSize(displaySize);
int rotation = display.getRotation();
// Take the screenshot
Bitmap screenShot = null;
try {
// Calling out without a lock held.
screenShot = mUiAutomationConnection.takeScreenshot(
new Rect(0, 0, displaySize.x, displaySize.y), rotation);
if (screenShot == null) {
return null;
}
} catch (RemoteException re) {
Log.e(LOG_TAG, "Error while taking screnshot!", re);
return null;
}
// Optimization
screenShot.setHasAlpha(false);
return screenShot;
}
private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
if (displayId == Display.DEFAULT_DISPLAY) {
recordTopInsetLocked(display);
}
sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
}
private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
boolean isDefault = (deviceInfo.flags
& DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
isDefault = false;
}
if (!isDefault && mSingleDisplayDemoMode) {
Slog.i(TAG, "Not creating a logical display for a secondary display "
+ " because single display demo mode is enabled: " + deviceInfo);
return null;
}
final int displayId = assignDisplayIdLocked(isDefault);
final int layerStack = assignLayerStackLocked(displayId);
LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
display.updateLocked(mDisplayDevices);
if (!display.isValidLocked()) {
// This should never happen currently.
Slog.w(TAG, "Ignoring display device because the logical display "
+ "created from it was not considered valid: " + deviceInfo);
return null;
}
configureColorModeLocked(display, device);
if (isDefault) {
recordStableDisplayStatsIfNeededLocked(display);
recordTopInsetLocked(display);
}
mLogicalDisplays.put(displayId, display);
// Wake up waitForDefaultDisplay.
if (isDefault) {
mSyncRoot.notifyAll();
}
sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
return display;
}
private static float getRefreshRate() {
DisplayInfo di = DisplayManagerGlobal.getInstance().getDisplayInfo(
Display.DEFAULT_DISPLAY);
return di.getMode().getRefreshRate();
}
public Display getDefaultDisplay() {
return DisplayManagerGlobal.getInstance().getRealDisplay(
Display.DEFAULT_DISPLAY);
}
public static Bitmap takeScreenshot(int rotation, int screenWidth,
int screenHeight) {
Display display = DisplayManagerGlobal.getInstance().getRealDisplay(
Display.DEFAULT_DISPLAY);
Point displaySize = new Point();
display.getRealSize(displaySize);
final int displayWidth = screenWidth;
final int displayHeight = screenHeight;
final float screenshotWidth;
final float screenshotHeight;
switch (rotation) {
case UiAutomation.ROTATION_FREEZE_0: {
screenshotWidth = displayWidth;
screenshotHeight = displayHeight;
}
break;
case UiAutomation.ROTATION_FREEZE_90: {
screenshotWidth = displayHeight;
screenshotHeight = displayWidth;
}
break;
case UiAutomation.ROTATION_FREEZE_180: {
screenshotWidth = displayWidth;
screenshotHeight = displayHeight;
}
break;
case UiAutomation.ROTATION_FREEZE_270: {
screenshotWidth = displayHeight;
screenshotHeight = displayWidth;
}
break;
default: {
return null;
}
}
Bitmap screenShot = null;
try {
screenShot = SurfaceControl.screenshot((int) screenshotWidth,
(int) screenshotHeight);
if (screenShot == null) {
return null;
}
} catch (Exception re) {
return null;
}
if (rotation != UiAutomation.ROTATION_FREEZE_0) {
Bitmap unrotatedScreenShot = Bitmap.createBitmap(displayWidth,
displayHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(unrotatedScreenShot);
canvas.translate(unrotatedScreenShot.getWidth() / 2,
unrotatedScreenShot.getHeight() / 2);
canvas.rotate(getDegreesForRotation(rotation));
canvas.translate(-screenshotWidth / 2, -screenshotHeight / 2);
canvas.drawBitmap(screenShot, 0, 0, null);
canvas.setBitmap(null);
screenShot.recycle();
screenShot = unrotatedScreenShot;
}
// Optimization
screenShot.setHasAlpha(false);
return screenShot;
}
/**
* Internal method to create a display.
* The display created with this method will have a static {@link DisplayAdjustments} applied.
* Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
* or {@link android.hardware.display.DisplayManager#getDisplay}
* to get a display object.
*
* @hide
*/
public Display(DisplayManagerGlobal global, int displayId, /*@NotNull*/ DisplayInfo displayInfo,
DisplayAdjustments daj) {
this(global, displayId, displayInfo, daj, null /*res*/);
}
/**
* Internal method to create a display.
* The display created with this method will be adjusted based on the adjustments in the
* supplied {@link Resources}.
*
* @hide
*/
public Display(DisplayManagerGlobal global, int displayId, /*@NotNull*/ DisplayInfo displayInfo,
Resources res) {
this(global, displayId, displayInfo, null /*daj*/, res);
}