Skip to content

Instantly share code, notes, and snippets.

View twiceyuan's full-sized avatar

twiceYuan twiceyuan

View GitHub Profile
@twiceyuan
twiceyuan / ByteBufferSource.kt
Created December 24, 2023 15:58
[ByteBufferSource] 直接接收 ByteArray 作为数据来源的 okio Source 实现 #Kotlin #Okio
/**
* 直接接收 ByteArray 作为数据来源的 okio Source 实现
*/
class ByteBufferSource : Source {
private val buffer = Buffer()
private val queue: BlockingQueue<ByteArray> = ArrayBlockingQueue(1024)
private val isClosed = AtomicBoolean(false)
fun onDataReceived(data: ByteArray) {
queue.put(data)
@twiceyuan
twiceyuan / init.gradle.kts
Created July 3, 2023 02:08 — forked from bennyhuo/init.gradle.kts
How to config mirrors for repositories in Gradle without changing the source code of your project?
fun RepositoryHandler.enableMirror() {
all {
if (this is MavenArtifactRepository) {
val originalUrl = this.url.toString().removeSuffix("/")
urlMappings[originalUrl]?.let {
logger.lifecycle("Repository[$url] is mirrored to $it")
this.setUrl(it)
}
}
}
@twiceyuan
twiceyuan / libs.versions.toml
Last active September 17, 2024 13:37
[常用三方库定义 VersionCatalog] #Gradle #Pinned
# Library 定义
[libraries]
# AndroidX 系列
androidx-core = "androidx.core:core:1.13.1"
androidx-legacy = "androidx.legacy:legacy-support-v4:1.0.0"
androidx-fragment = "androidx.fragment:fragment-ktx:1.8.3"
androidx-appcompat = "androidx.appcompat:appcompat:1.7.0"
androidx-recyclerview = "androidx.recyclerview:recyclerview:1.3.2"
androidx-transition = "androidx.transition:transition:1.5.1"
@twiceyuan
twiceyuan / note.md
Created November 4, 2021 09:32
[重启 macOS 音频服务] #macOS
sudo kill -9 `ps ax|grep 'coreaudio[a-z]' | awk '{print $1}'`

经常遇到 macOS 在 Zoom 或者飞书语音时连着的 AirPods 怎么都没办法用,重启下音频服务就行了。

垃圾苹果

@twiceyuan
twiceyuan / SingleLiveEvent.kt
Last active October 27, 2019 16:51 — forked from hadilq/SingleLiveEvent.kt
[单次事件的 LiveData] #Android
class SingleLiveEvent<T> : MutableLiveData<T>() {
private val observers = CopyOnWriteArraySet<ObserverWrapper<T>>()
@MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
val wrapper = ObserverWrapper(observer)
observers.add(wrapper)
super.observe(owner, wrapper)
}
@twiceyuan
twiceyuan / note.kt
Created August 30, 2019 09:12
[键盘显示隐藏监听] #Android
class KeyboardVisibilityListener(
private val activity: Activity,
private val rootLayout: ViewGroup,
private val listener: (isShow: Boolean) -> Unit
) {
private val keyboardLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
val heightDiff = rootLayout.rootView.height - rootLayout.height
val contentViewTop = activity.window.findViewById<View>(ID_ANDROID_CONTENT).top
listener(heightDiff > contentViewTop)
}
@twiceyuan
twiceyuan / note.md
Last active June 5, 2019 02:21
[Module 打包时附带 ProGuard 配置] #Android

defaultConfig 下添加:

consumerProguardFiles 'proguard-rules.pro'

或者也可以在特定的配置下

@twiceyuan
twiceyuan / note.kt
Created May 17, 2019 09:59
[nonce generator] 随机字符串生成器 #Kotlin
fun generateNonce(size: Int): String {
val nonceScope = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
val scopeSize = nonceScope.length
val nonceItem: (Int) -> Char = { nonceScope[(scopeSize * Math.random()).toInt()] }
return Array(size, nonceItem).joinToString("")
}
@twiceyuan
twiceyuan / note.md
Last active May 17, 2019 02:53
[Kotlin/Java 可见性修饰符] Kotlin 和 Java 在可见性修饰符上的比较 #Kotlin #Java

在 Java 中经常会用到 public/protected/private 来修饰一个成员变量或者方法。public、private 的含义相当明确,前者代表无论何时都可以在任何地方引用这个变量或方法,而后者代表只要在类之外,一概不允许引用这个变量或方法。而 protected 我们一开始被告知它是让成员在包内可见的,然而另外一个特性经常被选择性忽略,就是它能让这个成员可在包外的子类访问。例如我们定义下面这个类:

package com.example.package1;

public class A {
  void defaultMethod() {
    //... 
  }
 
@twiceyuan
twiceyuan / note.md
Created March 29, 2019 03:47
[Uri 获取 Byte 数组] #Android

很多上传操作需要文件对象或者 byte 数组,但是 Uri 获取文件对象是不方便的(content 类型的 Uri),但是相对于获取 InputStream 是方便的:

InputStream iStream = getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);

然后 InputStream 获取目标文件的 bytes