| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.test.taskembed; |
| 18 |
|
| 19 |
import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW; |
| 20 |
import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
| 21 |
|
| 22 |
import android.app.ActivityOptions; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.Intent; |
| 25 |
import android.graphics.Rect; |
| 26 |
import android.os.Bundle; |
| 27 |
import android.view.SurfaceControl; |
| 28 |
import android.view.SurfaceHolder; |
| 29 |
import android.view.SurfaceView; |
| 30 |
import android.window.WindowContainerToken; |
| 31 |
import android.window.WindowContainerTransaction; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class TaskView extends SurfaceView { |
| 38 |
private WindowContainerToken mWc; |
| 39 |
private Context mContext; |
| 40 |
private SurfaceControl mLeash; |
| 41 |
private TaskOrganizerMultiWindowTest.Organizer mOrganizer; |
| 42 |
private Intent mIntent; |
| 43 |
private boolean mLaunched = false; |
| 44 |
|
| 45 |
TaskView(Context c, TaskOrganizerMultiWindowTest.Organizer organizer, |
| 46 |
Intent intent) { |
| 47 |
super(c); |
| 48 |
mContext = c; |
| 49 |
mOrganizer = organizer; |
| 50 |
mIntent = intent; |
| 51 |
getHolder().addCallback( |
| 52 |
new SurfaceHolder.Callback() { |
| 53 |
@Override |
| 54 |
public void surfaceCreated(SurfaceHolder holder) {} |
| 55 |
|
| 56 |
@Override |
| 57 |
public void surfaceChanged(SurfaceHolder holder, |
| 58 |
int format, int width, int height) { |
| 59 |
if (!mLaunched) { |
| 60 |
launchOrganizedActivity(mIntent, width, height); |
| 61 |
mLaunched = true; |
| 62 |
} else { |
| 63 |
resizeTask(width, height); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void surfaceDestroyed(SurfaceHolder holder) {} |
| 69 |
} |
| 70 |
); |
| 71 |
setZOrderOnTop(true); |
| 72 |
} |
| 73 |
|
| 74 |
private void launchOrganizedActivity(Intent i, int width, int height) { |
| 75 |
mContext.startActivity(i, makeLaunchOptions(width, height)); |
| 76 |
} |
| 77 |
|
| 78 |
private Bundle makeLaunchOptions(int width, int height) { |
| 79 |
ActivityOptions o = ActivityOptions.makeBasic(); |
| 80 |
o.setLaunchWindowingMode(WINDOWING_MODE_MULTI_WINDOW); |
| 81 |
o.setLaunchBounds(new Rect(0, 0, width, height)); |
| 82 |
o.setTaskOverlay(true, true); |
| 83 |
o.setTaskAlwaysOnTop(true); |
| 84 |
return o.toBundle(); |
| 85 |
} |
| 86 |
|
| 87 |
void resizeTask(int width, int height) { |
| 88 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 89 |
wct.setBounds(mWc, new Rect(0, 0, width, height)).setHidden(mWc, false); |
| 90 |
try { |
| 91 |
mOrganizer.applySyncTransaction(wct, mOrganizer.mTransactionCallback); |
| 92 |
} catch (Exception e) { |
| 93 |
|
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void hideTask() { |
| 98 |
if (mWc == null) { |
| 99 |
return; |
| 100 |
} |
| 101 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 102 |
wct.setWindowingMode(mWc, WINDOWING_MODE_UNDEFINED).setHidden(mWc, true); |
| 103 |
try { |
| 104 |
mOrganizer.applySyncTransaction(wct, mOrganizer.mTransactionCallback); |
| 105 |
} catch (Exception e) { |
| 106 |
|
| 107 |
} |
| 108 |
releaseLeash(); |
| 109 |
} |
| 110 |
|
| 111 |
void reparentTask(WindowContainerToken wc, SurfaceControl leash) { |
| 112 |
mWc = wc; |
| 113 |
mLeash = leash; |
| 114 |
reparentLeash(); |
| 115 |
} |
| 116 |
|
| 117 |
void reparentLeash() { |
| 118 |
SurfaceControl.Transaction t = new SurfaceControl.Transaction(); |
| 119 |
t.reparent(mLeash, getSurfaceControl()) |
| 120 |
.show(mLeash) |
| 121 |
.apply(); |
| 122 |
} |
| 123 |
|
| 124 |
void releaseLeash() { |
| 125 |
SurfaceControl.Transaction t = new SurfaceControl.Transaction(); |
| 126 |
t.remove(mLeash).apply(); |
| 127 |
} |
| 128 |
} |