Window Android 学习

来源:互联网 发布:linux删除mysql数据库 编辑:程序博客网 时间:2024/06/10 20:05


参考博客:

http://www.androiddesignpatterns.com/2013/07/binders-window-tokens.html

参考类:

  • com.android.server.wm.AppWindowToken
  • com.android.server.wm.WindowManagerService
  • android.view.IWindow
  • android.view.WindowManagerImpl

Window Tokens

If you've ever scrolled through the official documentation for Android's View class, chances are you've stumbled across thegetWindowToken() method and wondered what it meant. As its name implies, a window token is a special type of Binder token that the window manager uses to uniquely identify a window in the system. Window tokens are important for security because they make it impossible for malicious applications to draw on top of the windows of other applications. The window manager protects against this by requiring applications to pass their application's window token as part of each request to add or remove a window.3 If the tokens don't match, the window manager rejects the request and throws aBadTokenException. Without window tokens, this necessary identification step wouldn't be possible and the window manager wouldn't be able to protect itself from malicious applications.

By this point you might be wondering about the real-world scenarios in which you would need to obtain a window token. Here are some examples:

  • When an application starts up for the first time, the ActivityManagerService4 creates a special kind of window token called an application window token, which uniquely identifies the application's top-level container window.5 The activity manager gives this token to both the application and the window manager, and the application sends the token to the window manager each time it wants to add a new window to the screen. This ensures secure interaction between the application and the window manager (by making it impossible to add windows on top of other applications), and also makes it easy for the activity manager to make direct requests to the window manager. For example, the activity manager can say, "hide all of this token's windows", and the window manager will be able to correctly identify the set of windows which should be closed.

  • Developers implementing their own custom Launchers can interact with the live wallpaper window that sits directly behind them by calling the sendWallpaperCommand(IBinder windowToken, String action, int x, int y, int z, Bundle extras) method. To ensure that no other application other than the Launcher is able to interact with the live wallpaper, the framework requires developers to pass a window token as the first argument to the method. If the window token does not identify the current foreground activity window sitting on top of the wallpaper, the command is ignored and a warning is logged.

  • Applications can ask the InputMethodManager to hide the soft keyboard by calling thehideSoftInputFromWindow(IBinder windowToken, int flags) method, but must provide a window token as part of the request. If the token doesn't match the window token belonging to the window currently accepting input, theInputMethodManager will reject the request. This makes it impossible for malicious applications to force-close a soft keyboard opened by another application.

  • Applications which manually add new windows to the screen (i.e. using the addView(View, WindowManager.LayoutParams) method) may need to specify their application's window token by setting theWindowManager.LayoutParams.token field. It is very unlikely that any normal application would ever have to do this, since the getWindowManager() method returns a WindowManager which will automatically set the token's value for you. That said, if at some point in the future you encounter a situation in which you need to add a panel window to the screen from a background service, know that you would need to manually sign the request with your application window token in order to achieve it. :P


0 0
原创粉丝点击