Skip to content

Instantly share code, notes, and snippets.

View vishalratna-microsoft's full-sized avatar

Vishal Ratna vishalratna-microsoft

  • Microsoft
  • Bangalore
View GitHub Profile
public class SampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
if(BuildConfig.DEBUG) {
Snippet.install(new Snippet.MeasuredExecutionPath());
Snippet.newFilter("SampleFilter");
Snippet.addFlag(Snippet.FLAG_METADATA_LINE | Snippet.FLAG_METADATA_THREAD_INFO);
}
@vishalratna-microsoft
vishalratna-microsoft / SomeActivity.kt
Created December 11, 2021 15:45
Snippet capture API
override fun onCreate(savedInstanceState: Bundle?) {
Snippet.capture { // starts the measurement
var prefs = NewsApplication.get(this).getPrefs()
val component = NewsApplication.get(this).applicationComponent.plus(NewsFeedModule())
component.inject(this)
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_news_feed)
viewModel = ViewModelProvider(this, viewModelFactory).get(NewsFeedActivityViewModelImpl::class.java)
registerLiveDataObservers()
} // ends the measuremement here.
@vishalratna-microsoft
vishalratna-microsoft / NewsApplication.java
Last active December 11, 2021 15:55
Start and end the measurement using tags.
class NewsApplication : Application() {
override fun onCreate() {
super.onCreate()
if(BuildConfig.DEBUG) {
Snippet.install(Snippet.MeasuredExecutionPath())
Snippet.newFilter("Carausell")
Snippet.addFlag(Snippet.FLAG_METADATA_LINE or Snippet.FLAG_METADATA_THREAD_INFO)
}
@vishalratna-microsoft
vishalratna-microsoft / NewActivity.kt
Created December 11, 2021 16:00
End the tag based measurement in Activity class
override fun onCreate(savedInstanceState: Bundle?) {
var prefs = NewsApplication.get(this).getPrefs()
prefs = NewsApplication.get(this).getPrefs()
val component = NewsApplication.get(this).applicationComponent.plus(NewsFeedModule())
component.inject(this)
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_news_feed)
viewModel = ViewModelProvider(this, viewModelFactory).get(NewsFeedActivityViewModelImpl::class.java)
registerLiveDataObservers()
@vishalratna-microsoft
vishalratna-microsoft / LazyLifecycleCallbacks.kt
Created April 5, 2022 09:08
LazyLifecycleCallbacks contract
import android.view.View
interface LazyLifecycleCallbacks {
/**
* Lazy version of activity onCreate() callback. Should be used for one time initialisations that could be done after the
* screen has finished rendering. Should not be used for complementary calls that set/reset their state in
* onCreate/onDestroy
*/
fun onLazyCreate()
@vishalratna-microsoft
vishalratna-microsoft / Barrier.java
Last active June 23, 2022 11:04
Code for Barrier
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@vishalratna-microsoft
vishalratna-microsoft / LazyLifecycleManager.kt
Last active June 28, 2022 07:27
Code for LazyLifecycleManager base class
import android.app.Activity
import android.os.Handler
import android.os.Looper
import androidx.annotation.MainThread
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import *******.Closure
import *******.Once
import *******.LazyLifecycleManagerType.ViewBased
import java.lang.ref.WeakReference
import java.util.concurrent.atomic.AtomicInteger
/**
* Construct that helps to restrict the code execution frequency to 1. It will not allow the snippet passed through the close to be executed more than once.
* The first client to execute run() will execute this and other calls to run() will not be respected till reset is called.
*/
class Once {
private val mCounter = AtomicInteger(0)
@vishalratna-microsoft
vishalratna-microsoft / MultiTriggerBomb.kt
Created June 18, 2022 19:20
MultiTriggerBomb implementation
import android.os.Handler
import android.os.Looper
import android.util.Log
import javax.annotation.concurrent.ThreadSafe
/**
* [MultiTriggerBomb] is a construct that executes a piece of code represented as [charge], only if
* 'k' triggers are down('k' conditions become true) or the timeout has happened(in case 'k' conditions do not become true), which ever happens first.
* To plant a bomb, call [MultiTriggerBomb.plant] on the object. Before [MultiTriggerBomb.plant] is called, bomb is in inert state, pressing down the triggers
* has no impact. The timeout counter starts the moment the [MultiTriggerBomb.plant] is called.
@vishalratna-microsoft
vishalratna-microsoft / ViewBasedLazyLifecycleManager.kt
Last active December 6, 2022 14:32
ViewBased implementation of LazyLifecycleManager
import android.util.Log
import android.view.ViewTreeObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import ************.Barrier
import javax.inject.Inject
/**
* ViewBasedLazyLifecycleManager is small construct to ensure that we initialise not so necessary