11package com.freerdp.freerdpcore.presentation;
13import android.content.Context;
14import android.graphics.Bitmap;
15import android.os.Handler;
16import android.os.Looper;
17import android.view.View;
18import android.view.ViewGroup;
19import android.widget.FrameLayout;
20import android.widget.ImageView;
22import java.util.HashMap;
24class RailWindowManager
26 private final Context context;
27 private final FrameLayout container;
28 private final SessionView sessionView;
29 private final Handler ui =
new Handler(Looper.getMainLooper());
31 private final HashMap<Long, ImageView> windows =
new HashMap<>();
32 private final HashMap<Long, int[]> rects =
new HashMap<>();
33 private final HashMap<Long, Bitmap> bitmaps =
new HashMap<>();
34 private long[] zOrder =
null;
35 private long activeWindowId = 0;
37 RailWindowManager(Context context, FrameLayout container, SessionView sessionView)
39 this.context = context;
40 this.container = container;
41 this.sessionView = sessionView;
42 sessionView.setOnZoomChangedListener(zoom -> repositionAll());
45 void onWindowUpdate(
long windowId,
int width,
int height,
int[] pixels)
49 ui.post(() -> handleWindowUpdate(windowId, width, height, pixels));
52 void onWindowMove(
long windowId,
int x,
int y,
int w,
int h)
54 ui.post(() -> handleWindowMove(windowId, x, y, w, h));
57 void onWindowHide(
long windowId)
60 ImageView iv = windows.get(windowId);
62 iv.setVisibility(View.INVISIBLE);
66 void onWindowDestroy(
long windowId)
68 ui.post(() -> handleWindowDestroy(windowId));
75 sessionView.setRailMode(
false);
79 void onMonitoredDesktop(
long[] windowIds,
long active)
81 ui.post(() -> handleMonitoredDesktop(windowIds, active));
90 private void handleWindowUpdate(
long windowId,
int width,
int height,
int[] pixels)
92 if (container ==
null)
95 Bitmap bm = bitmaps.get(windowId);
96 boolean newBitmap = bm ==
null || bm.getWidth() != width || bm.getHeight() != height;
99 bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
100 bitmaps.put(windowId, bm);
102 bm.setPixels(pixels, 0, width, 0, 0, width, height);
103 ImageView iv = windows.get(windowId);
106 iv =
new ImageView(context);
107 iv.setScaleType(ImageView.ScaleType.FIT_XY);
108 iv.setVisibility(View.INVISIBLE);
109 container.addView(iv);
110 windows.put(windowId, iv);
117 iv.setImageBitmap(bm);
118 ViewGroup.LayoutParams lp = iv.getLayoutParams();
127 if (rects.containsKey(windowId))
129 position(iv, windowId);
130 iv.setVisibility(View.VISIBLE);
131 sessionView.setRailMode(
true);
135 private void handleWindowMove(
long windowId,
int x,
int y,
int w,
int h)
137 if (x != -1 || y != -1)
138 rects.put(windowId,
new int[] { x, y, w, h });
139 ImageView iv = windows.get(windowId);
144 ViewGroup.LayoutParams lp = iv.getLayoutParams();
145 if (lp.width != w || lp.height != h)
152 position(iv, windowId);
153 iv.setVisibility(View.VISIBLE);
154 sessionView.setRailMode(
true);
158 private void handleWindowDestroy(
long windowId)
160 rects.remove(windowId);
161 bitmaps.remove(windowId);
162 ImageView iv = windows.remove(windowId);
163 if (iv !=
null && container !=
null)
164 container.removeView(iv);
167 private void handleMonitoredDesktop(
long[] windowIds,
long active)
169 if (windowIds !=
null)
172 if (windowIds.length > 0)
173 activeWindowId = windowIds[0];
176 activeWindowId = active;
181 private void restack()
183 if (container ==
null)
186 for (
int i = zOrder.length - 1; i >= 0; i--)
188 ImageView iv = windows.get(zOrder[i]);
192 if (activeWindowId != 0 &&
193 (zOrder ==
null || zOrder.length == 0 || zOrder[0] != activeWindowId))
195 ImageView iv = windows.get(activeWindowId);
201 private void repositionAll()
203 for (HashMap.Entry<Long, ImageView> entry : windows.entrySet())
204 position(entry.getValue(), entry.getKey());
207 private void position(ImageView iv,
long windowId)
209 int[] rect = rects.get(windowId);
212 float zoom = sessionView !=
null ? sessionView.getZoom() : 1.0f;
217 iv.setX(rect[0] * zoom);
218 iv.setY(rect[1] * zoom);
221 private void clearWindows()
223 if (container !=
null)
224 for (ImageView iv : windows.values())
225 container.removeView(iv);