Skip to content

Instantly share code, notes, and snippets.

View AshuTyagi16's full-sized avatar

Ashu Tyagi AshuTyagi16

View GitHub Profile
public class LocationManagerService extends ILocationManager.Stub {
private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
String packageName) {
IBinder binder = listener.asBinder();
Receiver receiver = mReceivers.get(binder);
if (receiver == null) {
receiver = new Receiver(listener, null, pid, uid, packageName);
mReceivers.put(binder, receiver);
try {
fun Drawable.updateTint(color: Int) {
DrawableCompat.wrap(this)?.let {
DrawableCompat.setTint(it, color)
}
}
private fun changeDrawableColor(holder: OutgoingChatViewHolder) {
holder.itemView.post {
ContextCompat.getDrawable(this, R.drawable.bg_round_corner_outgoing)
?.let { incomingBgDrawable ->
val color = animatedColor.with(getFloatRange(holder.itemView))
incomingBgDrawable.updateTint(color)
holder.itemView.tvOutgoingMessage.background = incomingBgDrawable
}
}
}
@AshuTyagi16
AshuTyagi16 / AnimatedColor.kt
Created November 16, 2020 09:22
Class to animate between two colors based on progress
import android.graphics.Color
class AnimatedColor(private val startColor: Int, private val endColor: Int) {
private val startHSV: FloatArray
private val endHSV: FloatArray
private val move = FloatArray(3)
init {
private fun getFloatRange(view: View): Float {
return 1f - (view.absY() / resources.displayMetrics.heightPixels.toFloat())
}
//This function returns the y-cord of the view..
fun View.absY(): Float {
val location = IntArray(2)
getLocationOnScreen(location)
return (location[1].toFloat() - height.toFloat())
}
rvChat.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
recyclerView.forEachVisibleHolder { holder: RecyclerView.ViewHolder ->
if (holder is OutgoingChatViewHolder) {
changeDrawableColor(holder)
}
}
}
})
@SystemService(Context.POWER_SERVICE)
public final class PowerManager {
final IPowerManager mService;
public final class WakeLock {
@UnsupportedAppUsage
private int mFlags;
@UnsupportedAppUsage
private String mTag;
import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import androidx.appcompat.app.AppCompatActivity
class ExampleActivity : AppCompatActivity() {
private lateinit var wakeLock: PowerManager.WakeLock
inline fun <reified T : RecyclerView.ViewHolder> RecyclerView.forEachVisibleHolder(
action: (T) -> Unit
) {
for (i in 0 until childCount) {
action(getChildViewHolder(getChildAt(i)) as T)
}
}
rvApps.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
recyclerView.forEachVisibleHolder { holder: AppViewHolder ->
holder.rotation
// Update the velocity.
// The velocity is calculated by the vertical scroll offset.
.setStartVelocity(holder.currentVelocity - dx * SCROLL_ROTATION_MAGNITUDE)
// Start the animation. This does nothing if the animation is already running.
.start()
}