Skip to content

Instantly share code, notes, and snippets.

View jaydeepw's full-sized avatar

JW jaydeepw

View GitHub Profile
@jaydeepw
jaydeepw / enableHTML5AppCache.java
Last active October 5, 2023 11:39
Enabling HTML5 AppCache in Android Webview programatically.
private void enableHTML5AppCache() {
webView.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
// Step 1: Integrate the api_sdk as well in app/build.gradle
compile ('com.trustvision:tv_api_sdk:1.0.17@aar') {
transitive = true
}
// Step 2:
// If the image is in base64, convert it to byte array
// use something like below
// byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
// Step 1: Integrate the api_sdk as well in app/build.gradle
compile ('com.trustvision:tv_api_sdk:1.0.17@aar') {
transitive = true
}
// Step 2:
// If the image is in base64, convert it to byte array
// use something like below
// byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
@jaydeepw
jaydeepw / wifi-wake-lock-android
Last active June 14, 2018 13:47
Holding and releasing WiFi wake locks in android.
// create a class member variable.
WifiManager.WifiLock mWifiLock = null;
/***
* Calling this method will aquire the lock on wifi. This is avoid wifi
* from going to sleep as long as <code>releaseWifiLock</code> method is called.
**/
private void holdWifiLock() {
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
@jaydeepw
jaydeepw / pick-capture-image-android.java
Last active December 27, 2015 03:49
To take a photograph using camera, gallery and other apps and receive the image data back in your activity.
// For some help with the Utils class, you may want to take a look
// at the following android-utils library.
// https://github.com/jaydeepw/android-utils
private Uri mOutputFileUri;
private void openPhotoChooser() {
// Determine Uri of camera image to save
@jaydeepw
jaydeepw / DateUtils.java
Created November 20, 2015 06:55
dat-string
/**
* Returns a string describing a day relative to the current day. For example if the day is
* today this function returns "Today", if the day was a week ago it returns "7 days ago", and
* if the day is in 2 weeks it returns "in 14 days".
*
* @param r the resources
* @param day the relative day to describe in UTC milliseconds
* @param today the current time in UTC milliseconds
*/
private static final String getRelativeDayString(Resources r, long day, long today) {
@jaydeepw
jaydeepw / DateUtils.java
Created November 20, 2015 06:49
relative-time
/**
* Returns a string describing 'time' as a time relative to 'now'.
* <p>
* Time spans in the past are formatted like "42 minutes ago". Time spans in
* the future are formatted like "in 42 minutes".
* <p>
* Can use {@link #FORMAT_ABBREV_RELATIVE} flag to use abbreviated relative
* times, like "42 mins ago".
*
* @param time the time to describe, in milliseconds
@jaydeepw
jaydeepw / is-android-service-running
Created May 15, 2012 06:43
Android: Checking if a Service is already running in the Android system.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@jaydeepw
jaydeepw / blackberry-network-connection-string.java
Created April 24, 2012 16:18
Getting a connection string while making a network connection in BlackBerry.
// Get most appropriate connection method
private String getConnectionString() {
String suffix = null;
if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS))
{
// BES is available
suffix = ";deviceside=false";
}
@jaydeepw
jaydeepw / IUtils.java
Created August 10, 2015 12:23
Converting Retrofit Response to String.
@Nullable
/**
* Converts {@link retrofit.client.Response#body} to simple {@link java.lang.String} representation.
* This can be used to log message or manually parse JSON or any other purpose.
* @param response
* @return
*/
public static String toString(@NonNull Response response) {
if (response == null) {