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 / wakeUpPhone
Created July 15, 2016 23:27
Wakes up device/turns on screen. For example if a notification is received this will make sure that the devices screen lights up
public static void wakeUpPhone(Context context, String tag) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock((
PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP), tag);
wakeLock.acquire();
}
@JosiasSena
JosiasSena / removeWhiteSpaces
Created July 15, 2016 23:26
Removes all white spaces from string
/**
* Removes all white spaces from string
*
* @param text - text to remove white spaces from
* @return - text without any whitespaces
*/
public static String removeWhiteSpaces(String text) {
return text.replaceAll("\\s+", "");
}
@JosiasSena
JosiasSena / gist:c257013bb222ad2430334468bd442ea1
Created July 15, 2016 23:26
Check if the string contains a number or a special character
/**
* Check if the string contains a number or a special character
*
* @param text - string to check
* @return - true if it contains a number or a special character, else false
*/
public boolean textContainsSpecialCharsOrNumbers(String text) {
return !text.matches("^[a-z A-Z]+$");
}
@JosiasSena
JosiasSena / getCurrentDevicePhoneNumber
Created July 15, 2016 23:25
Get the current devices phone number
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = telephonyManager.getLine1Number();
@JosiasSena
JosiasSena / goToAppSettings
Created July 15, 2016 23:24
go to the settings for the current application
public static void goToAppSettings(final Context context) {
final Uri uri = Uri.fromParts("package", context.getPackageName(), null);
final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri);
context.startActivity(intent);
}
@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);
}
@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 / 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 / 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 / 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