Skip to content

Instantly share code, notes, and snippets.

@jie-meng
jie-meng / FindFilesRecursively.py
Last active February 16, 2021 03:02
Python function: findFilesRecursively
import os
def findFilesRecursively(path, pred = None, ls = None):
if ls == None:
ls = []
for p in os.listdir(path):
p = os.path.join(path, p)
if os.path.isdir(p):
findFilesRecursively(p, pred, ls)
@jie-meng
jie-meng / Deal with CORS
Last active February 1, 2020 10:18
express deal with cors
const app = express();
app.all('*',function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
if (req.method == 'OPTIONS') {
console.log('---------------->');
res.send(200); /let options req pass quickly/
@jie-meng
jie-meng / ftp.lua
Last active June 4, 2017 10:51
lua ftp using curl
local file_ext = require('file_ext')
local table_ext = require('table_ext')
local tmp_file = 'listfile.tmp.000'
function downloadDir(server, user, password, server_path, local_path)
if os.execute(string.format('curl -l "%s/%s/" --user %s:%s > %s', server, server_path, user, password, tmp_file)) then
local lines = file_ext.readLines(tmp_file)
util.pathRemove(tmp_file)
table_ext.foreach(lines, function (k, v)
@jie-meng
jie-meng / BankCardNumberEditText.java
Last active October 29, 2021 05:21
Android EditText with space every 4 characters
//Util methods
public static void editTextSetContentMemorizeSelection(EditText editText, CharSequence charSequence) {
int selectionStart = editText.getSelectionStart();
int selectionEnd = editText.getSelectionEnd();
editText.setText(charSequence.toString());
if (selectionStart > charSequence.toString().length()) {
selectionStart = charSequence.toString().length();
}
if (selectionStart < 0) {
@jie-meng
jie-meng / ShowSoftKeyboardDialogFragment.java
Last active July 18, 2024 08:40
Android show soft keyboard in DialogFragment when startup
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(getLayout(), container);
editText.requestFocus();
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return rootView;
}
@jie-meng
jie-meng / ScrollViewClipPadding.java
Created October 10, 2017 06:56
Make ScrollView clip padding when scroll
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize"
android:clipToPadding="false">
@jie-meng
jie-meng / EditTextInputFilter.java
Created October 10, 2017 09:40
EditText input filter
editText.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!somePattern.matcher(source).matches()) {
//return empty to refuse
return "";
} else {
//return null if accept
return null;
@jie-meng
jie-meng / ViewPagerInvalidateActionBar.java
Created October 11, 2017 07:29
ViewPager invalidate ActionBar
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
invalidateActionbar(position);
}
@jie-meng
jie-meng / values.xml
Created October 11, 2017 08:13
Android Multiline Snackbar To override the predefined value used for that in values.xml of the app
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="design_snackbar_text_max_lines">5</integer>
</resources>
@jie-meng
jie-meng / ApplyDimToActivity.java
Created October 11, 2017 14:29
Apply dim to Activity
public static void applyDim(@NonNull Activity activity){
float dimAmount = 0.5f;
ViewGroup rootView = (ViewGroup) (activity.getWindow().getDecorView().getRootView());
Drawable dim = new ColorDrawable(Color.BLACK);
dim.setBounds(0, 0, rootView.getWidth(), rootView.getHeight());
dim.setAlpha((int) (255 * dimAmount));
ViewGroupOverlay overlay = rootView.getOverlay();
overlay.add(dim);
}