| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.pip; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.content.res.TypedArray; |
| 21 |
import android.graphics.Color; |
| 22 |
import android.graphics.Rect; |
| 23 |
import android.view.SurfaceControl; |
| 24 |
import android.view.SurfaceSession; |
| 25 |
import android.window.TaskSnapshot; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public abstract class PipContentOverlay { |
| 31 |
protected SurfaceControl mLeash; |
| 32 |
|
| 33 |
|
| 34 |
public abstract void attach(SurfaceControl.Transaction tx, SurfaceControl parentLeash); |
| 35 |
|
| 36 |
|
| 37 |
public void detach(SurfaceControl.Transaction tx) { |
| 38 |
if (mLeash != null && mLeash.isValid()) { |
| 39 |
tx.remove(mLeash); |
| 40 |
tx.apply(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public abstract void onAnimationUpdate(SurfaceControl.Transaction atomicTx, float fraction); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public abstract void onAnimationEnd(SurfaceControl.Transaction atomicTx, |
| 59 |
Rect destinationBounds); |
| 60 |
|
| 61 |
|
| 62 |
public static final class PipColorOverlay extends PipContentOverlay { |
| 63 |
private final Context mContext; |
| 64 |
|
| 65 |
public PipColorOverlay(Context context) { |
| 66 |
mContext = context; |
| 67 |
mLeash = new SurfaceControl.Builder(new SurfaceSession()) |
| 68 |
.setCallsite("PipAnimation") |
| 69 |
.setName(PipColorOverlay.class.getSimpleName()) |
| 70 |
.setColorLayer() |
| 71 |
.build(); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void attach(SurfaceControl.Transaction tx, SurfaceControl parentLeash) { |
| 76 |
tx.show(mLeash); |
| 77 |
tx.setLayer(mLeash, Integer.MAX_VALUE); |
| 78 |
tx.setColor(mLeash, getContentOverlayColor(mContext)); |
| 79 |
tx.setAlpha(mLeash, 0f); |
| 80 |
tx.reparent(mLeash, parentLeash); |
| 81 |
tx.apply(); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onAnimationUpdate(SurfaceControl.Transaction atomicTx, float fraction) { |
| 86 |
atomicTx.setAlpha(mLeash, fraction < 0.5f ? 0 : (fraction - 0.5f) * 2); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onAnimationEnd(SurfaceControl.Transaction atomicTx, Rect destinationBounds) { |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
private float[] getContentOverlayColor(Context context) { |
| 95 |
final TypedArray ta = context.obtainStyledAttributes(new int[] { |
| 96 |
android.R.attr.colorBackground }); |
| 97 |
try { |
| 98 |
int colorAccent = ta.getColor(0, 0); |
| 99 |
return new float[] { |
| 100 |
Color.red(colorAccent) / 255f, |
| 101 |
Color.green(colorAccent) / 255f, |
| 102 |
Color.blue(colorAccent) / 255f }; |
| 103 |
} finally { |
| 104 |
ta.recycle(); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public static final class PipSnapshotOverlay extends PipContentOverlay { |
| 111 |
private final TaskSnapshot mSnapshot; |
| 112 |
private final Rect mSourceRectHint; |
| 113 |
|
| 114 |
public PipSnapshotOverlay(TaskSnapshot snapshot, Rect sourceRectHint) { |
| 115 |
mSnapshot = snapshot; |
| 116 |
mSourceRectHint = new Rect(sourceRectHint); |
| 117 |
mLeash = new SurfaceControl.Builder(new SurfaceSession()) |
| 118 |
.setCallsite("PipAnimation") |
| 119 |
.setName(PipSnapshotOverlay.class.getSimpleName()) |
| 120 |
.build(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void attach(SurfaceControl.Transaction tx, SurfaceControl parentLeash) { |
| 125 |
final float taskSnapshotScaleX = (float) mSnapshot.getTaskSize().x |
| 126 |
/ mSnapshot.getHardwareBuffer().getWidth(); |
| 127 |
final float taskSnapshotScaleY = (float) mSnapshot.getTaskSize().y |
| 128 |
/ mSnapshot.getHardwareBuffer().getHeight(); |
| 129 |
tx.show(mLeash); |
| 130 |
tx.setLayer(mLeash, Integer.MAX_VALUE); |
| 131 |
tx.setBuffer(mLeash, mSnapshot.getHardwareBuffer()); |
| 132 |
|
| 133 |
tx.setPosition(mLeash, -mSourceRectHint.left, -mSourceRectHint.top); |
| 134 |
tx.setScale(mLeash, taskSnapshotScaleX, taskSnapshotScaleY); |
| 135 |
tx.reparent(mLeash, parentLeash); |
| 136 |
tx.apply(); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void onAnimationUpdate(SurfaceControl.Transaction atomicTx, float fraction) { |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void onAnimationEnd(SurfaceControl.Transaction atomicTx, Rect destinationBounds) { |
| 146 |
atomicTx.remove(mLeash); |
| 147 |
} |
| 148 |
} |
| 149 |
} |