File: WindowContextController.java

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 326,096 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,183; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 120; sed: 19
file content (156 lines) | stat: -rw-r--r-- 6,481 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.window;

import static java.lang.annotation.RetentionPolicy.SOURCE;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.IWindowManager;
import android.view.WindowManager.LayoutParams.WindowType;

import com.android.internal.annotations.VisibleForTesting;

import java.lang.annotation.Retention;

/**
 * The controller to manage {@link WindowContext}, such as attaching to a window manager node or
 * detaching from the current attached node. The user must call
 * {@link #attachToDisplayArea(int, int, Bundle)}, call {@link #attachToWindowToken(IBinder)}
 * after that if necessary, and then call {@link #detachIfNeeded()} for release.
 *
 * @hide
 */
public class WindowContextController {
    private static final boolean DEBUG_ATTACH = false;
    private static final String TAG = "WindowContextController";

    /**
     * {@link AttachStatus#STATUS_ATTACHED} to indicate that the {@code mToken} is associated with a
     * {@link com.android.server.wm.DisplayArea}. Note that {@code mToken} is able to attach a
     * WindowToken after this flag sets to {@link AttachStatus#STATUS_ATTACHED}.
     */
    @VisibleForTesting
    public int mAttachedToDisplayArea = AttachStatus.STATUS_INITIALIZED;

    /**
     * Status to indicate that the Window Context attach to a
     * {@link com.android.server.wm.DisplayArea}.
     */
    @Retention(SOURCE)
    @IntDef({AttachStatus.STATUS_INITIALIZED, AttachStatus.STATUS_ATTACHED,
            AttachStatus.STATUS_DETACHED, AttachStatus.STATUS_FAILED})
    public @interface AttachStatus{
        /**
         * The Window Context haven't attached to a {@link com.android.server.wm.DisplayArea}.
         */
        int STATUS_INITIALIZED = 0;
        /**
         * The Window Context has already attached to a {@link com.android.server.wm.DisplayArea}.
         */
        int STATUS_ATTACHED = 1;
        /**
         * The Window Context has detached from a {@link com.android.server.wm.DisplayArea}.
         */
        int STATUS_DETACHED = 2;
        /**
         * The Window Context fails to attach to a {@link com.android.server.wm.DisplayArea}.
         */
        int STATUS_FAILED = 3;
    }
    @NonNull
    private final WindowTokenClient mToken;

    /**
     * Window Context Controller constructor
     *
     * @param token The token used to attach to a window manager node. It is usually from
     *              {@link Context#getWindowContextToken()}.
     */
    public WindowContextController(@NonNull WindowTokenClient token) {
        mToken = token;
    }

    /**
     * Attaches the {@code mToken} to a {@link com.android.server.wm.DisplayArea}.
     *
     * @param type The window type of the {@link WindowContext}
     * @param displayId The {@link Context#getDisplayId() ID of display} to associate with
     * @param options The window context launched option
     * @throws IllegalStateException if the {@code mToken} has already been attached to a
     * DisplayArea.
     */
    public void attachToDisplayArea(@WindowType int type, int displayId, @Nullable Bundle options) {
        if (mAttachedToDisplayArea == AttachStatus.STATUS_ATTACHED) {
            throw new IllegalStateException("A Window Context can be only attached to "
                    + "a DisplayArea once.");
        }
        mAttachedToDisplayArea = mToken.attachToDisplayArea(type, displayId, options)
                ? AttachStatus.STATUS_ATTACHED : AttachStatus.STATUS_FAILED;
        if (mAttachedToDisplayArea == AttachStatus.STATUS_FAILED) {
            Log.w(TAG, "attachToDisplayArea fail, type:" + type + ", displayId:"
                    + displayId);
        } else if (DEBUG_ATTACH) {
            Log.d(TAG, "attachToDisplayArea success, type:" + type + ", displayId:"
                    + displayId);
        }
    }

    /**
     * Switches to attach the window context to a window token.
     * <p>
     * Note that the context should have been attached to a
     * {@link com.android.server.wm.DisplayArea} by {@link #attachToDisplayArea(int, int, Bundle)}
     * before attaching to a window token, and the window token's type must match the window
     * context's type.
     * </p><p>
     * A {@link WindowContext} can only attach to a specific window manager node, which is either a
     * {@link com.android.server.wm.DisplayArea} by calling
     * {@link #attachToDisplayArea(int, int, Bundle)} or the latest attached {@code windowToken}
     * although this API is allowed to be called multiple times.
     * </p>
     * @throws IllegalStateException if the {@code mClientToken} has not yet attached to
     * a {@link com.android.server.wm.DisplayArea} by
     * {@link #attachToDisplayArea(int, int, Bundle)}.
     *
     * @see WindowProviderService#attachToWindowToken(IBinder))
     * @see IWindowManager#attachWindowContextToWindowToken(IBinder, IBinder)
     */
    public void attachToWindowToken(IBinder windowToken) {
        if (mAttachedToDisplayArea != AttachStatus.STATUS_ATTACHED) {
            throw new IllegalStateException("The Window Context should have been attached"
                    + " to a DisplayArea. AttachToDisplayArea:" + mAttachedToDisplayArea);
        }
        mToken.attachToWindowToken(windowToken);
    }

    /** Detaches the window context from the node it's currently associated with. */
    public void detachIfNeeded() {
        if (mAttachedToDisplayArea == AttachStatus.STATUS_ATTACHED) {
            mToken.detachFromWindowContainerIfNeeded();
            mAttachedToDisplayArea = AttachStatus.STATUS_DETACHED;
            if (DEBUG_ATTACH) {
                Log.d(TAG, "Detach Window Context.");
            }
        }
    }
}