-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: using PixelCopyCompat allows for the use of advanced PixelCop…
…y when the API is less than 26 change: the implementation of the `takeScreenshot()` method has been changed from `View#getDrawingCache()` to `View#drawToBitmap()`
- Loading branch information
1 parent
f5c74fa
commit 9cc4a80
Showing
7 changed files
with
155 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
circularrevealswitch/src/main/java/com/yenaly/circularrevealswitch/PixelCopyCompat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.yenaly.circularrevealswitch | ||
|
||
import android.annotation.SuppressLint | ||
import android.graphics.Bitmap | ||
import android.graphics.Rect | ||
import android.os.Build | ||
import android.os.Handler | ||
import android.view.PixelCopy | ||
import android.view.Surface | ||
import android.view.View | ||
import android.view.Window | ||
import androidx.annotation.RequiresApi | ||
|
||
object PixelCopyCompat { | ||
|
||
fun request( | ||
window: Window, srcRect: Rect?, dest: Bitmap, | ||
listener: PixelCopy.OnPixelCopyFinishedListener, | ||
listenerHandler: Handler, | ||
) { | ||
when { | ||
Build.VERSION.SDK_INT >= 26 -> Api26Impl.request( | ||
window, srcRect, dest, | ||
listener, listenerHandler | ||
) | ||
|
||
else -> Api24Impl.request( | ||
window, dest, listener, | ||
listenerHandler | ||
) | ||
} | ||
} | ||
|
||
fun request( | ||
window: Window, dest: Bitmap, | ||
listener: PixelCopy.OnPixelCopyFinishedListener, | ||
listenerHandler: Handler, | ||
) = request(window, null, dest, listener, listenerHandler) | ||
|
||
@RequiresApi(26) | ||
internal object Api26Impl { | ||
fun request( | ||
window: Window, srcRect: Rect?, dest: Bitmap, | ||
listener: PixelCopy.OnPixelCopyFinishedListener, | ||
listenerHandler: Handler, | ||
) = PixelCopy.request(window, srcRect, dest, listener, listenerHandler) | ||
} | ||
|
||
internal object Api24Impl { | ||
fun request( | ||
window: Window, dest: Bitmap, | ||
listener: PixelCopy.OnPixelCopyFinishedListener, | ||
listenerHandler: Handler, | ||
) { | ||
val insets = Rect() | ||
val surface = sourceForWindow(window, insets) | ||
PixelCopy.request(surface, dest, listener, listenerHandler) | ||
} | ||
|
||
@SuppressLint("PrivateApi") | ||
private fun sourceForWindow(source: Window?, outInsets: Rect): Surface { | ||
requireNotNull(source) { "source is null" } | ||
requireNotNull(source.peekDecorView()) { "Only able to copy windows with decor views" } | ||
var surface: Surface? = null | ||
val dv = source.peekDecorView() | ||
val root = dv?.let { | ||
View::class.java.getDeclaredField("mAttachInfo").apply { | ||
isAccessible = true | ||
}[it]?.let { attachInfo -> | ||
attachInfo.javaClass.getDeclaredField("mViewRootImpl").apply { | ||
isAccessible = true | ||
}[attachInfo] | ||
} | ||
} // as ViewRootImpl | ||
if (root != null) { | ||
surface = root.javaClass.getDeclaredField("mSurface").apply { | ||
isAccessible = true | ||
}[root] as Surface | ||
val windowAttrs = root.javaClass.getDeclaredField("mWindowAttributes").apply { | ||
isAccessible = true | ||
}[root] // as WindowManager.LayoutParams | ||
val surfaceInsets = windowAttrs.javaClass.getDeclaredField("surfaceInsets").apply { | ||
isAccessible = true | ||
}[windowAttrs] as Rect | ||
// val width = root.javaClass.getDeclaredField("mWidth").apply { | ||
// isAccessible = true | ||
// }[root] as Int | ||
// val height = root.javaClass.getDeclaredField("mHeight").apply { | ||
// isAccessible = true | ||
// }[root] as Int | ||
val width = dv.rootView.width | ||
val height = dv.rootView.height | ||
outInsets.set( | ||
surfaceInsets.left, surfaceInsets.top, | ||
width + surfaceInsets.left, height + surfaceInsets.top | ||
) | ||
} | ||
require(surface != null && surface.isValid) { "Window doesn't have a backing surface!" } | ||
return surface | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.