Skip to content

Instantly share code, notes, and snippets.

View kyze8439690's full-sized avatar
📱
Working

Yang Hui kyze8439690

📱
Working
View GitHub Profile
@kyze8439690
kyze8439690 / SettingFragment.kt
Created February 9, 2022 16:50
Get itemView in androidx.preference.PreferenceFragmentCompat
findPreference<Preference>(getString(R.string.setting_settings_key))
?.setOnPreferenceClickListener { pref ->
@SuppressLint("RestrictedApi")
if (listView.adapter is PreferenceGroupAdapter) {
val adapter = listView.adapter as PreferenceGroupAdapter
val position = adapter.getPreferenceAdapterPosition(pref)
if (position != RecyclerView.NO_POSITION) {
val itemView = listView.findViewHolderForAdapterPosition(position)?.itemView
if (itemView != null) {
// do something
@kyze8439690
kyze8439690 / build.gradle.kts
Created December 21, 2021 16:08
Protobuf gradle plugin with gradle kotlin dsl
import com.google.protobuf.gradle.*
plugins {
id("com.google.protobuf") version "0.8.18"
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.19.1"
}
@kyze8439690
kyze8439690 / watch-fds.sh
Created April 16, 2021 09:32 — forked from jhansche/watch-fds.sh
Simple script to monitor the number open file descriptors for an Android application.
#!/bin/sh
# Usage: ./watch-fds.sh <application_id> [delay_secs = 5]
APP_ID=${1:?missing application id}
DELAY=$(( ${2:-5} ))
DEVICE_LIMIT=$(( $(adb shell ulimit -n) ))
WARN_THRESHOLD=$(( ${DEVICE_LIMIT} / 3 ))
echo "Will warn at ${WARN_THRESHOLD}"
GetObjectClass -> DeleteLocalRef
FindClass -> DeleteLocalRef
NewString -> DeleteLocalRef
NewStringUTF -> DeleteLocalRef
NewObject -> DeleteLocalRef
NewBooleanArray -> DeleteLocalRef
NewByteArray -> DeleteLocalRef
NewCharArray -> DeleteLocalRef
NewShortArray -> DeleteLocalRef
NewIntArray -> DeleteLocalRef
@kyze8439690
kyze8439690 / PermissionUtils
Created February 3, 2016 09:03
android 6.0 request permission callback pattern
import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import java.util.Map;
@kyze8439690
kyze8439690 / ActivityLifecycleLogger.java
Last active January 12, 2016 03:57
Activity Lifecycle Logger, not support onActivityResult()
//call if(BuildConfig.DEBUG) registerActivityLifecycleCallbacks(new ActivityLifecycleLogger()) in your own Application class.
public class ActivityLifecycleLogger implements Application.ActivityLifecycleCallbacks {
private static final String TAG = "LifeCycleLogger";
private static final Class[] LOG_CYCLE_ACTIVITIES = new Class[] {
//add activities you want to log here
MainActivity.class
};
@kyze8439690
kyze8439690 / FilterIntentChooser.java
Created October 14, 2015 06:53
FilterIntentChooser
Intent[] targetedShareIntents = new Intent[infos.size()];
for (int i = 0; i < infos.size(); i++) {
String packageName = infos.get(i).activityInfo.packageName;
Intent targetedShareIntent = new Intent(Intent.ACTION_VIEW, uri);
targetedShareIntent.setPackage(packageName);
targetedShareIntents[i] = targetedShareIntent;
}
Intent chooserIntent = Intent.createChooser(new Intent(), "打开网页");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents);
@kyze8439690
kyze8439690 / NestedAppBarLayout.java
Created June 24, 2015 03:23
NestedAppBarLayout, make AppBarLayout scrollable in CoordinatorLayout, merge code from NestedScrollView, lots of thing to improve...
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.VelocityTrackerCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ScrollerCompat;
import android.util.AttributeSet;
@kyze8439690
kyze8439690 / SelectorImageView.java
Created April 12, 2015 09:33
ImageView that will draw a selector on top of image, support normal drawable and the newest RippleDrawable.
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.widget.ImageView;
import me.yugy.app.timeline.R;
@kyze8439690
kyze8439690 / .gitignore
Created March 4, 2015 03:01
android studio project .gitignore file
.gradle
.idea
*.iml
build
gradle*
local.properties