Skip to content

Instantly share code, notes, and snippets.

View JosiasSena's full-sized avatar
🏠
Working from home

Josias Sena JosiasSena

🏠
Working from home
View GitHub Profile
@JosiasSena
JosiasSena / RxJavaEventBus
Last active July 14, 2016 17:51
RxJavaEventBusExample
public class EventBus {
private final Subject<Boolean, Boolean> eventBus = new SerializedSubject<>(PublishSubject.<Boolean>create());
private EventBus() {
}
private static class SingleTonHelper {
private static final EventBus INSTANCE = new EventBus();
}
@JosiasSena
JosiasSena / uncheckAll
Last active July 14, 2016 17:52
Check/Uncheck all supported devices in the playstore with this JavaScript snippet
// Rather than going one by one and unchecking each and every one, run this JS query from
//the console on chrome or firefox that shows up after "inspecting the code" and then just
// check the ones that you want after wards.
switches = document.getElementsByClassName("M0PRCSC-Rc-g M0PRCSC-Rc-h");
for(i = 0; i < switches.length; i++) {
if (switches[i].getAttribute("aria-checked") == "false") {
switches[i].click()
}
/**
* The calculation (value * scale + 0.5f) is a widely used to convert from dps to pixel units
* based on density scale
* see developer.android.com (Supporting Multiple Screen Sizes)
*/
public static int convertDpToPixels(int dp, DisplayMetrics displayMetrics) {
return Math.round(dp * ((displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)));
}
Note: to get the display metrics do: getContext().getResources().getDisplayMetrics();
@JosiasSena
JosiasSena / to_lower.sh
Created July 15, 2016 23:10
Set all file names in current directory to lowercase
#!/usr/bin/env bash
for file in *; do mv "$file” "`echo $file | tr "[:upper:]" "[:lower:]"`"; done
@JosiasSena
JosiasSena / reflection
Created July 15, 2016 23:14
Reflection example in Java/Android
// without reflection
ClassName className = new ClassName();
className.unsubscribe();
// with reflection
Object className = ClassName.class.newInstance();
Method m = className.getClass().getDeclaredMethod("unsubscribe", new Class[0]);
m.invoke(className);
@JosiasSena
JosiasSena / javaJDK
Created July 15, 2016 23:15
Find out the path of the latest java JDK by running the following command
/usr/libexec/java_home
@JosiasSena
JosiasSena / genFbHash.txt
Last active July 15, 2016 23:17
Generate KeyHash for Facebook SDK
Generate KeyHash for Facebook SDK
1. First open a terminal (open a command prompt in windows).
2. Navigate in the terminal to the directory where your Android debug.keystore is stored.
3. Mostly it will located under “/Users/user_name/.android/” (In Windows will be C:\Documents and Settings\.android).
4. Once you are in the “.android” directory, run the following command.keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64

Note: “androiddebugkey” can be the username for the keystone file and “debug.keystore” can be the name of the keynote file
1. When it prompts you for a password, type android and hit Enter
@JosiasSena
JosiasSena / hideKeyboard
Created July 15, 2016 23:22
Hide keyboard in Android
final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
@JosiasSena
JosiasSena / getStringFromAssetFile
Created July 15, 2016 23:22
Load string from a file in Asset folder
public static String getStringFromAssetFile(final Context context, final String filename) {
final int MAX_BUFF = 1024 * 100;
final InputStream is;
final InputStreamReader isr;
final BufferedReader br;
String resultData = "";
try {
final AssetManager am = context.getAssets();
@JosiasSena
JosiasSena / isGpsEnabled
Created July 15, 2016 23:23
check if GPS is enabled
/**
* Returns the current enabled/disabled status of the GPS provider.
* <p/>
* If the user has enabled GPS in the Settings menu, true
* is returned otherwise false is returned
*/
public static boolean isGpsEnabled(final Context context) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}