下面列出了android.view.Display#getDisplayId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onRoutePresentationDisplayChanged(Object routeObj) {
int index = findSystemRouteRecord(routeObj);
if (index >= 0) {
SystemRouteRecord record = mSystemRouteRecords.get(index);
Display newPresentationDisplay =
MediaRouterJellybeanMr1.RouteInfo.getPresentationDisplay(routeObj);
int newPresentationDisplayId = (newPresentationDisplay != null
? newPresentationDisplay.getDisplayId() : -1);
if (newPresentationDisplayId
!= record.mRouteDescriptor.getPresentationDisplayId()) {
record.mRouteDescriptor =
new MediaRouteDescriptor.Builder(record.mRouteDescriptor)
.setPresentationDisplayId(newPresentationDisplayId)
.build();
publishRoutes();
}
}
}
@Override
public Context createDisplayContext(Display display) {
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}
int displayId = display.getDisplayId();
ContextImpl context = new ContextImpl();
context.init(mPackageInfo, null, mMainThread);
context.mDisplay = display;
DisplayAdjustments daj = getDisplayAdjustments(displayId);
context.mResources = mResourcesManager.getTopLevelResources(mPackageInfo.getResDir(),
displayId, null, daj.getCompatibilityInfo(), null);
return context;
}
private SecondaryDisplay createPresentation() {
if (mMediaRouter == null) return null;
final MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
final Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;
if (presentationDisplay != null) {
if (BuildConfig.DEBUG)
Log.i(TAG, "Showing presentation on display: " + presentationDisplay);
final SecondaryDisplay presentation = new SecondaryDisplay(mActivity, presentationDisplay);
presentation.setOnDismissListener(mOnDismissListener);
try {
presentation.show();
mPresentationId = presentationDisplay.getDisplayId();
return presentation;
} catch (WindowManager.InvalidDisplayException ex) {
if (BuildConfig.DEBUG)
Log.w(TAG, "Couldn't show presentation! Display was removed in " + "the meantime.", ex);
mPresentationId = -1;
}
} else if (BuildConfig.DEBUG) Log.i(TAG, "No secondary display detected");
return null;
}
private void initialize() {
mIdMap = new SparseArray<>();
Display display;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mBackend = new DisplayListenerBackendImpl();
// Make sure the display map contains the built-in primary display.
// The primary display is never removed.
display = getDisplayManager().getDisplay(Display.DEFAULT_DISPLAY);
// Android documentation on Display.DEFAULT_DISPLAY suggests that the above
// method might return null. In that case we retrieve the default display
// from the application context and take it as the primary display.
if (display == null) display = getDefaultDisplayForContext(getContext());
} else {
mBackend = new DisplayListenerAPI16();
display = getDefaultDisplayForContext(getContext());
}
mMainSdkDisplayId = display.getDisplayId();
addDisplay(display); // Note this display is never removed.
mBackend.startListening();
}
@Override
public Context createDisplayContext(Display display) {
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}
ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
mActivityToken, mUser, mFlags, mClassLoader);
final int displayId = display.getDisplayId();
context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
null, getDisplayAdjustments(displayId).getCompatibilityInfo()));
context.mDisplay = display;
return context;
}
@Override
public Context createDisplayContext(Display display) {
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}
ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
mActivityToken, mUser, mFlags, mClassLoader);
final int displayId = display.getDisplayId();
context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
null, getDisplayAdjustments(displayId).getCompatibilityInfo()));
context.mDisplay = display;
return context;
}
@Override
public Display getDisplay(int displayId) {
Display display = mWindowManager.getDefaultDisplay();
if (display.getDisplayId() == displayId) {
return display;
}
return null;
}
@Override
public Context createDisplayContext(Display display) {
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}
ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
mActivityToken, mUser, mFlags, mClassLoader);
final int displayId = display.getDisplayId();
context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
null, getDisplayAdjustments(displayId).getCompatibilityInfo()));
context.mDisplay = display;
return context;
}
@Override
public Display getDisplay(int displayId) {
Display display = mWindowManager.getDefaultDisplay();
if (display.getDisplayId() == displayId) {
return display;
}
return null;
}
@Override
public Display getDisplay(int displayId) {
Display display = mWindowManager.getDefaultDisplay();
if (display.getDisplayId() == displayId) {
return display;
}
return null;
}
DisplayAndroid getDisplayAndroid(Display display) {
int sdkDisplayId = display.getDisplayId();
DisplayAndroid displayAndroid = mIdMap.get(sdkDisplayId);
if (displayAndroid == null) {
displayAndroid = addDisplay(display);
}
return displayAndroid;
}
@Override
public Display getDisplay(int displayId) {
Display display = mWindowManager.getDefaultDisplay();
if (display.getDisplayId() == displayId) {
return display;
}
return null;
}
@Override
public Display getDisplay(int displayId) {
Display display = mWindowManager.getDefaultDisplay();
if (display.getDisplayId() == displayId) {
return display;
}
return null;
}
public Display getDisplay(int i)
{
Display display = a.getDefaultDisplay();
if (display.getDisplayId() == i)
{
return display;
} else
{
return null;
}
}
@Override
public Context createDisplayContext(Display display) {
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}
ContextImpl context = new ContextImpl(this, mMainThread, mPackageInfo, mSplitName,
mActivityToken, mUser, mFlags, mClassLoader);
final int displayId = display.getDisplayId();
context.setResources(createResources(mActivityToken, mPackageInfo, mSplitName, displayId,
null, getDisplayAdjustments(displayId).getCompatibilityInfo()));
context.mDisplay = display;
return context;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void createPresentation() {
if (mMediaRouter == null || mEnableCloneMode)
return;
// Get the current route and its presentation display.
MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(
MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;
if (presentationDisplay != null) {
// Show a new presentation if possible.
Log.i(TAG, "Showing presentation on display: " + presentationDisplay);
mPresentation = new SecondaryDisplay(this, LibVLC(), presentationDisplay);
mPresentation.setOnDismissListener(mOnDismissListener);
try {
mPresentation.show();
mPresentationDisplayId = presentationDisplay.getDisplayId();
} catch (WindowManager.InvalidDisplayException ex) {
Log.w(TAG, "Couldn't show presentation! Display was removed in "
+ "the meantime.", ex);
mPresentation = null;
}
} else
Log.i(TAG, "No secondary display detected");
}
private ContextImpl(ContextImpl container, ActivityThread mainThread,
LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted,
Display display, Configuration overrideConfiguration, int createDisplayWithId) {
mOuterContext = this;
mMainThread = mainThread;
mActivityToken = activityToken;
mRestricted = restricted;
if (user == null) {
user = Process.myUserHandle();
}
mUser = user;
mPackageInfo = packageInfo;
mResourcesManager = ResourcesManager.getInstance();
final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
? createDisplayWithId
: (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
CompatibilityInfo compatInfo = null;
if (container != null) {
compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
}
if (compatInfo == null) {
compatInfo = (displayId == Display.DEFAULT_DISPLAY)
? packageInfo.getCompatibilityInfo()
: CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
}
mDisplayAdjustments.setCompatibilityInfo(compatInfo);
mDisplayAdjustments.setConfiguration(overrideConfiguration);
mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
: ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments);
Resources resources = packageInfo.getResources(mainThread);
if (resources != null) {
if (displayId != Display.DEFAULT_DISPLAY
|| overrideConfiguration != null
|| (compatInfo != null && compatInfo.applicationScale
!= resources.getCompatibilityInfo().applicationScale)) {
resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(),
packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(),
packageInfo.getApplicationInfo().sharedLibraryFiles, displayId,
overrideConfiguration, compatInfo);
}
}
mResources = resources;
if (container != null) {
mBasePackageName = container.mBasePackageName;
mOpPackageName = container.mOpPackageName;
} else {
mBasePackageName = packageInfo.mPackageName;
ApplicationInfo ainfo = packageInfo.getApplicationInfo();
if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
// Special case: system components allow themselves to be loaded in to other
// processes. For purposes of app ops, we must then consider the context as
// belonging to the package of this process, not the system itself, otherwise
// the package+uid verifications in app ops will fail.
mOpPackageName = ActivityThread.currentPackageName();
} else {
mOpPackageName = mBasePackageName;
}
}
mContentResolver = new ApplicationContentResolver(this, mainThread, user);
}
InputInterceptor(Display display) {
InputChannel[] channels = InputChannel.openInputChannelPair("drag");
mServerChannel = channels[0];
mClientChannel = channels[1];
mService.mInputManager.registerInputChannel(mServerChannel, null);
mInputEventReceiver = new DragInputEventReceiver(mClientChannel,
mService.mH.getLooper(), mDragDropController);
mDragApplicationHandle = new InputApplicationHandle(null);
mDragApplicationHandle.name = "drag";
mDragApplicationHandle.dispatchingTimeoutNanos =
WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, null,
display.getDisplayId());
mDragWindowHandle.name = "drag";
mDragWindowHandle.inputChannel = mServerChannel;
mDragWindowHandle.layer = getDragLayerLocked();
mDragWindowHandle.layoutParamsFlags = 0;
mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
mDragWindowHandle.dispatchingTimeoutNanos =
WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
mDragWindowHandle.visible = true;
mDragWindowHandle.canReceiveKeys = false;
mDragWindowHandle.hasFocus = true;
mDragWindowHandle.hasWallpaper = false;
mDragWindowHandle.paused = false;
mDragWindowHandle.ownerPid = Process.myPid();
mDragWindowHandle.ownerUid = Process.myUid();
mDragWindowHandle.inputFeatures = 0;
mDragWindowHandle.scaleFactor = 1.0f;
// The drag window cannot receive new touches.
mDragWindowHandle.touchableRegion.setEmpty();
// The drag window covers the entire display
mDragWindowHandle.frameLeft = 0;
mDragWindowHandle.frameTop = 0;
mDragWindowHandle.frameRight = mDisplaySize.x;
mDragWindowHandle.frameBottom = mDisplaySize.y;
// Pause rotations before a drag.
if (DEBUG_ORIENTATION) {
Slog.d(TAG_WM, "Pausing rotation during drag");
}
mService.pauseRotationLocked();
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int length = appWidgetIds.length;
for (int i = 0; i < length; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_WIDGET_LAUNCH, true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.miracast_widget);
views.setOnClickPendingIntent(R.id.widget_layout_parent, pendingIntent);
final DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = displayManager.getDisplays();
boolean displaySet = false;
int currentDisplay = -1;
for(int j = 0; j < displays.length; j++){
Display display = displays[j];
if(display.getDisplayId() != Display.DEFAULT_DISPLAY){
views.setTextViewText(R.id.widget_text, display.getName());
views.setTextColor(R.id.widget_text, context.getResources().getColor(android.R.color.holo_blue_bright));
currentDisplay = display.getDisplayId();
displaySet = true;
// Track this
MiracastApplication application
= (MiracastApplication) context.getApplicationContext();
Tracker tracker = application.getDefaultTracker();
sendEventDisplayFound(tracker);
}
}
if(!displaySet){
views.setTextViewText(R.id.widget_text, "Cast Screen");
views.setTextColor(R.id.widget_text, context.getResources().getColor(android.R.color.white));
}
MiracastDisplayListener displayListener = new MiracastDisplayListener(currentDisplay, views, displayManager, appWidgetManager, appWidgetId, context);
displayManager.registerDisplayListener(displayListener, null);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
public static DisplayInfo getDisplayInfo(Context context, boolean fromTaskbar) {
context = getDisplayContext(context);
int displayID = getTaskbarDisplayID(context);
DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
Display currentDisplay = null;
for(Display display : dm.getDisplays()) {
if(display.getDisplayId() == displayID) {
currentDisplay = display;
break;
}
}
if(currentDisplay == null)
return new DisplayInfo(0, 0, 0, 0);
DisplayMetrics metrics = new DisplayMetrics();
currentDisplay.getMetrics(metrics);
DisplayMetrics realMetrics = new DisplayMetrics();
currentDisplay.getRealMetrics(realMetrics);
DisplayInfo info = new DisplayInfo(metrics.widthPixels, metrics.heightPixels, metrics.densityDpi, 0);
if(isChromeOs(context)) {
SharedPreferences pref = getSharedPreferences(context);
if(!pref.getBoolean(PREF_CHROME_OS_CONTEXT_MENU_FIX, true)) {
info.width = realMetrics.widthPixels;
info.height = realMetrics.heightPixels;
}
return info;
}
// Workaround for incorrect display size on devices with notches in landscape mode
if(fromTaskbar && getDisplayOrientation(context) == Configuration.ORIENTATION_LANDSCAPE)
return info;
boolean sameWidth = metrics.widthPixels == realMetrics.widthPixels;
boolean sameHeight = metrics.heightPixels == realMetrics.heightPixels;
if(sameWidth && !sameHeight) {
info.width = realMetrics.widthPixels;
info.height = realMetrics.heightPixels - getNavbarHeight(context);
}
if(!sameWidth && sameHeight) {
info.width = realMetrics.widthPixels - getNavbarHeight(context);
info.height = realMetrics.heightPixels;
}
return info;
}