Skip to content

Instantly share code, notes, and snippets.

View ok3141's full-sized avatar

Oleksii ok3141

View GitHub Profile
@ok3141
ok3141 / dagger-android-view.md
Created April 10, 2018 09:35 — forked from ronshapiro/dagger-android-view.md
dagger.android for views in ~10 minutes

1. You can implement View.getActivity() like this:

public interface ViewWithActivity {
  // Using android-gradle-plugin 3.0, which has the desugar step for default methods on interfaces
  default Activity getActivity() {
    // https://android.googlesource.com/platform/frameworks/support/+/03e0f3daf3c97ee95cd78b2f07bc9c1be05d43db/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java#276
    Context context = getContext();
    while (context instanceof ContextWrapper) {
      if (context instanceof Activity) {
@ok3141
ok3141 / gist:c977c4c37831a62c3f695ee29c102768
Last active December 23, 2017 12:19
MacBook OSX disable saving password for Git
@ok3141
ok3141 / Android_enableManifestComponent.java
Created November 14, 2017 10:04 — forked from sdecima/Android_enableManifestComponent.java
How to dynamically enable/disable an Android Component declared in the manifest.
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
public class Android_enableManifestComponent {
public static void enableManifestComponent(Context context, Class<?> component, boolean enable) {
ComponentName receiver = new ComponentName(context, component);
PackageManager pm = context.getPackageManager();
@ok3141
ok3141 / gradle-maven-helper.gradle
Created October 31, 2017 15:13
Gradle Maven helper
apply plugin: 'maven'
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
repository(url: mavenLocal().url)
pom.groupId = project.group
pom.artifactId = project.name
pom.version = project.version
@ok3141
ok3141 / StaticContext.java
Created August 22, 2017 04:31
Android. Get static Context
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
@SuppressLint("PrivateApi")
public class StaticContext {
@Nullable
public static Context getContext() {
try {
return (Context) Class.forName("android.app.ActivityThread")
@ok3141
ok3141 / Toaster.java
Last active August 16, 2017 08:04
Helper for class Toast in Android
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;
@SuppressWarnings({"unused", "PrivateApi", "StaticFieldLeak", "SameParameterValue", "WeakerAccess"})
public final class Toaster implements Handler.Callback {
private static Context context;
private static Handler handler;
@ok3141
ok3141 / AndroidManifest.xml
Last active July 27, 2017 09:03
Declare ContentProvider in AndroidManifest.xml like Firebase
<?xml version="1.0"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase">
<application>
<provider
android:authorities="${applicationId}.firebaseinitprovider"
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:exported="false"
android:initOrder="100"
/>
@ok3141
ok3141 / Test24.java
Created March 21, 2017 20:27
What will be the output?
public class Test24 {
private static final Object[] lock = { "Zero", new Integer(1), Double.valueOf(2) };
static void notifyAndPrint(int who, int what) {
synchronized (lock[who]) {
System.out.print(what);
lock[who].notifyAll();
}
}
@ok3141
ok3141 / SomeWhere.java
Last active March 10, 2017 12:59
Save current position with scroll offset of RecyclerView
// NOTE in this example setStackFromEnd(true) was used
// so we operate here with 'bottom', 'last' properties/methods
public void setNewData(List<Object> newData) {
Object currentItem = null;
Rect parentRect = new Rect();
if (!recyclerView.getGlobalVisibleRect(parentRect)) {
parentRect = null;
}
@ok3141
ok3141 / Kmp.java
Last active March 10, 2017 10:32
KMP substring search algorithm
public class Kmp {
public static int indexOf(String t, String p) {
return compile(p).matcher(t).find();
}
public static Pattern compile(String p) {
return new Pattern(p);
}
public static class Pattern {