Skip to content

Instantly share code, notes, and snippets.

View yrom's full-sized avatar
😹

Yrom Wang yrom

😹
View GitHub Profile
@yrom
yrom / selector-snippet.java
Created October 19, 2014 15:12
code snippet for java.nio.channels.Selector. http://tutorials.jenkov.com/java-nio/selectors.html
// the remote server
SocketAddress remote = new InetSocketAddress(Inet4Address.getByName(hostName), port);
// open a selector
Selector selector = Selector.open();
// open a channel
SocketChannel channel = SocketChannel.open();
// non-blocking
channel.configureBlocking(false);
// pending connect
channel.connect(remote);
@yrom
yrom / gist:9918a8d5244a2f8ced11
Created May 21, 2014 09:19
Android: Check for Available Features at Runtime
// Check if android.hardware.telephony feature is available.
if (getPackageManager().hasSystemFeature("android.hardware.telephony")) {
Log.d("Mobile Test", "Running on phone");
// Check if android.hardware.touchscreen feature is available.
} else if (getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {
Log.d("Tablet Test", "Running on devices that don't support telphony but have a touchscreen.");
} else {
Log.d("TV Test", "Running on a TV!");
}
@yrom
yrom / gist:11167376
Created April 22, 2014 06:30
A hack to add the translation to the action bar (excerpt from https://github.com/umano/AndroidSlidingUpPanel/tree/master/demo)
public void setActionBarTranslation(float y) {
// Figure out the actionbar height
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
// A hack to add the translation to the action bar
ViewGroup content = ((ViewGroup) findViewById(android.R.id.content).getParent());
int children = content.getChildCount();
@yrom
yrom / ForceCacheing.java
Created October 24, 2013 06:49
Volley: 强制缓存
...
protected Response<?> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
... // parseing json
return Response.success(obj, cache(response, 60));
} catch (Exception e) {
@yrom
yrom / UsingCookiesRequest.java
Created October 24, 2013 06:28
Volley: 带cookies的 request
/*
* Copyright (C) 2013 YROM.NET
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@yrom
yrom / CustomUARequest.java
Last active December 26, 2015 09:49
Volley: Custom UA Request
/*
* Copyright (C) 2013 YROM.NET
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software