下面列出了android.hardware.display.DisplayManager#getDisplay ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public IInputForwarder createInputForwarder(int displayId) throws RemoteException {
if (!checkCallingPermission(android.Manifest.permission.INJECT_EVENTS,
"createInputForwarder()")) {
throw new SecurityException("Requires INJECT_EVENTS permission");
}
final DisplayManager displayManager = mContext.getSystemService(DisplayManager.class);
final Display display = displayManager.getDisplay(displayId);
if (display == null) {
throw new IllegalArgumentException(
"Can't create input forwarder for non-existent displayId: " + displayId);
}
final int callingUid = Binder.getCallingUid();
final int displayOwnerUid = display.getOwnerUid();
if (callingUid != displayOwnerUid) {
throw new SecurityException(
"Only owner of the display can forward input events to it.");
}
return new InputForwarder(displayId);
}
public void onResume() {
if (mVirtualDisplay == null) {
DisplayManager manager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY);
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
defaultDisplay.getMetrics(mDefaultMetrics);
mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", mWidth, mHeight,
mDefaultMetrics.densityDpi, mSurface, flags);
}
if (mPresentation == null) {
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay());
mPresentation.show();
if (mContentView != null) {
mPresentation.setContentView(mContentView);
}
}
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
if (mDefaultDisplay == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().
getSystemService(Context.DISPLAY_SERVICE);
mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
display = mDefaultDisplay;
}
return new WindowManagerImpl(display);
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
if (mDefaultDisplay == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().
getSystemService(Context.DISPLAY_SERVICE);
mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
display = mDefaultDisplay;
}
return new WindowManagerImpl(display);
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().getSystemService(
Context.DISPLAY_SERVICE);
display = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
return new WindowManagerImpl(display);
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().getSystemService(
Context.DISPLAY_SERVICE);
display = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
return new WindowManagerImpl(display);
}
public Object getService(ContextImpl ctx) {
Display display = ctx.mDisplay;
if (display == null) {
if (mDefaultDisplay == null) {
DisplayManager dm = (DisplayManager)ctx.getOuterContext().
getSystemService(Context.DISPLAY_SERVICE);
mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
display = mDefaultDisplay;
}
return new WindowManagerImpl(display);
}
public BurnInProtectionHelper(Context context, int minHorizontalOffset,
int maxHorizontalOffset, int minVerticalOffset, int maxVerticalOffset,
int maxOffsetRadius) {
mMinHorizontalBurnInOffset = minHorizontalOffset;
mMaxHorizontalBurnInOffset = maxHorizontalOffset;
mMinVerticalBurnInOffset = minVerticalOffset;
mMaxVerticalBurnInOffset = maxVerticalOffset;
if (maxOffsetRadius != BURN_IN_MAX_RADIUS_DEFAULT) {
mBurnInRadiusMaxSquared = maxOffsetRadius * maxOffsetRadius;
} else {
mBurnInRadiusMaxSquared = BURN_IN_MAX_RADIUS_DEFAULT;
}
mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
context.registerReceiver(mBurnInProtectionReceiver,
new IntentFilter(ACTION_BURN_IN_PROTECTION));
Intent intent = new Intent(ACTION_BURN_IN_PROTECTION);
intent.setPackage(context.getPackageName());
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mBurnInProtectionIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
DisplayManager displayManager =
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
mDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
displayManager.registerDisplayListener(this, null /* handler */);
mCenteringAnimator = ValueAnimator.ofFloat(1f, 0f);
mCenteringAnimator.setDuration(CENTERING_ANIMATION_DURATION_MS);
mCenteringAnimator.setInterpolator(new LinearInterpolator());
mCenteringAnimator.addListener(this);
mCenteringAnimator.addUpdateListener(this);
}
void attach(IWallpaperEngineWrapper wrapper) {
if (DEBUG) Log.v(TAG, "attach: " + this + " wrapper=" + wrapper);
if (mDestroyed) {
return;
}
mIWallpaperEngine = wrapper;
mCaller = wrapper.mCaller;
mConnection = wrapper.mConnection;
mWindowToken = wrapper.mWindowToken;
mSurfaceHolder.setSizeFromLayout();
mInitializing = true;
mSession = WindowManagerGlobal.getWindowSession();
mWindow.setSession(mSession);
mLayout.packageName = getPackageName();
mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
mDisplayManager.registerDisplayListener(mDisplayListener, mCaller.getHandler());
mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
mDisplayState = mDisplay.getState();
if (DEBUG) Log.v(TAG, "onCreate(): " + this);
onCreate(mSurfaceHolder);
mInitializing = false;
mReportedVisible = false;
updateSurface(false, false, false);
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void startRecording() {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay;
if (dm != null) {
defaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
} else {
throw new IllegalStateException("Cannot display manager?!?");
}
if (defaultDisplay == null) {
throw new RuntimeException("No display found.");
}
// Get the display size and density.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int screenDensity = metrics.densityDpi;
prepareVideoEncoder(screenWidth, screenHeight);
try {
File outputFile = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES) + "/grafika", "Screen-record-" +
Long.toHexString(System.currentTimeMillis()) + ".mp4");
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
muxer = new MediaMuxer(outputFile.getCanonicalPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
} catch (IOException ioe) {
throw new RuntimeException("MediaMuxer creation failed", ioe);
}
// Start the video input.
mediaProjection.createVirtualDisplay("Recording Display", screenWidth,
screenHeight, screenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR/* flags */, inputSurface,
null /* callback */, null /* handler */);
}
int runGetConfig(PrintWriter pw) throws RemoteException {
int days = -1;
boolean asProto = false;
boolean inclDevice = false;
String opt;
while ((opt=getNextOption()) != null) {
if (opt.equals("--days")) {
days = Integer.parseInt(getNextArgRequired());
if (days <= 0) {
throw new IllegalArgumentException("--days must be a positive integer");
}
} else if (opt.equals("--proto")) {
asProto = true;
} else if (opt.equals("--device")) {
inclDevice = true;
} else {
getErrPrintWriter().println("Error: Unknown option: " + opt);
return -1;
}
}
Configuration config = mInterface.getConfiguration();
if (config == null) {
getErrPrintWriter().println("Activity manager has no configuration");
return -1;
}
DisplayManager dm = mInternal.mContext.getSystemService(DisplayManager.class);
Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
if (asProto) {
final ProtoOutputStream proto = new ProtoOutputStream(getOutFileDescriptor());
config.writeResConfigToProto(proto, GlobalConfigurationProto.RESOURCES, metrics);
if (inclDevice) {
writeDeviceConfig(proto, GlobalConfigurationProto.DEVICE, null, config, dm);
}
proto.flush();
} else {
pw.println("config: " + Configuration.resourceQualifierString(config, metrics));
pw.println("abi: " + TextUtils.join(",", Build.SUPPORTED_ABIS));
if (inclDevice) {
writeDeviceConfig(null, -1, pw, config, dm);
}
if (days >= 0) {
final List<Configuration> recentConfigs = getRecentConfigurations(days);
final int recentConfigSize = recentConfigs.size();
if (recentConfigSize > 0) {
pw.println("recentConfigs:");
for (int i = 0; i < recentConfigSize; i++) {
pw.println(" config: " + Configuration.resourceQualifierString(
recentConfigs.get(i)));
}
}
}
}
return 0;
}