1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
19import com.android.server.input.InputApplicationHandle;
20import com.android.server.input.InputWindowHandle;
21
22import android.os.Looper;
23import android.os.Process;
24import android.view.Display;
25import android.view.InputChannel;
26import android.view.InputEventReceiver;
27import android.view.WindowManagerPolicy;
28
29public final class FakeWindowImpl implements WindowManagerPolicy.FakeWindow {
30    final WindowManagerService mService;
31    final InputChannel mServerChannel, mClientChannel;
32    final InputApplicationHandle mApplicationHandle;
33    final InputWindowHandle mWindowHandle;
34    final InputEventReceiver mInputEventReceiver;
35    final int mWindowLayer;
36
37    boolean mTouchFullscreen;
38
39    public FakeWindowImpl(WindowManagerService service,
40            Looper looper, InputEventReceiver.Factory inputEventReceiverFactory,
41            String name, int windowType, int layoutParamsFlags,
42            boolean canReceiveKeys, boolean hasFocus, boolean touchFullscreen) {
43        mService = service;
44
45        InputChannel[] channels = InputChannel.openInputChannelPair(name);
46        mServerChannel = channels[0];
47        mClientChannel = channels[1];
48        mService.mInputManager.registerInputChannel(mServerChannel, null);
49
50        mInputEventReceiver = inputEventReceiverFactory.createInputEventReceiver(
51                mClientChannel, looper);
52
53        mApplicationHandle = new InputApplicationHandle(null);
54        mApplicationHandle.name = name;
55        mApplicationHandle.dispatchingTimeoutNanos =
56                WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
57
58        mWindowHandle = new InputWindowHandle(mApplicationHandle, null, Display.DEFAULT_DISPLAY);
59        mWindowHandle.name = name;
60        mWindowHandle.inputChannel = mServerChannel;
61        mWindowLayer = getLayerLw(windowType);
62        mWindowHandle.layer = mWindowLayer;
63        mWindowHandle.layoutParamsFlags = layoutParamsFlags;
64        mWindowHandle.layoutParamsType = windowType;
65        mWindowHandle.dispatchingTimeoutNanos =
66                WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
67        mWindowHandle.visible = true;
68        mWindowHandle.canReceiveKeys = canReceiveKeys;
69        mWindowHandle.hasFocus = hasFocus;
70        mWindowHandle.hasWallpaper = false;
71        mWindowHandle.paused = false;
72        mWindowHandle.ownerPid = Process.myPid();
73        mWindowHandle.ownerUid = Process.myUid();
74        mWindowHandle.inputFeatures = 0;
75        mWindowHandle.scaleFactor = 1.0f;
76
77        mTouchFullscreen = touchFullscreen;
78    }
79
80    void layout(int dw, int dh) {
81        if (mTouchFullscreen) {
82            mWindowHandle.touchableRegion.set(0, 0, dw, dh);
83        } else {
84            mWindowHandle.touchableRegion.setEmpty();
85        }
86        mWindowHandle.frameLeft = 0;
87        mWindowHandle.frameTop = 0;
88        mWindowHandle.frameRight = dw;
89        mWindowHandle.frameBottom = dh;
90    }
91
92    @Override
93    public void dismiss() {
94        synchronized (mService.mWindowMap) {
95            if (mService.removeFakeWindowLocked(this)) {
96                mInputEventReceiver.dispose();
97                mService.mInputManager.unregisterInputChannel(mServerChannel);
98                mClientChannel.dispose();
99                mServerChannel.dispose();
100            }
101        }
102    }
103
104    private int getLayerLw(int windowType) {
105        return mService.mPolicy.windowTypeToLayerLw(windowType)
106                * WindowManagerService.TYPE_LAYER_MULTIPLIER
107                + WindowManagerService.TYPE_LAYER_OFFSET;
108    }
109}
110