Skip to content

Instantly share code, notes, and snippets.

@theyann
theyann / WidgetProvider.java - onUpdate
Last active November 14, 2015 12:38
AppWidgetProvider for configurable widget, onUpdate example
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
WidgetEntryDao dao = new WidgetEntryDao(new DbHelper(context));
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
@theyann
theyann / EmailIntent.java
Last active November 14, 2015 17:03
Two ways to open email apps from intent, pre-filling datas. These two methods
// creating the intent with all the information we want to pre-fill
Intent intent = new Intent(Intent.ACTION_SEND);
// array of destination email addresses
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "email1@company.com", "email2@othercompany.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "my subject");
intent.putExtra(Intent.EXTRA_TEXT, "some message body");
// This type narrows down the amount of apps that will be displayed in the app choice dialog
intent.setType("message/rfc822"); // it should be mostly email apps
@theyann
theyann / WidgetProvider.java - onDelete
Created November 14, 2015 17:04
AppWidgetProvider for configurable widget, onDelete example
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
WidgetEntryDao dao = new WidgetEntryDao(new DbHelper(context));
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
dao.deleteByWidgetId(appWidgetIds[i]);
}
@theyann
theyann / PrefUtils.java
Created December 11, 2015 20:47
PrefUtils is a base class intended to simplify and clarify the code relative to dealing with SharedPreferences. It is very simple and it is meant to be.
package X.Y.Z;
import android.content.SharedPreferences;
/**
* Created by ylemin on 21/11/15.
*
* This class is a bunch of recurring methods used when dealing with SharedPreferences.
* It is intended to be a base class which receives the SharedPreferences instance from the child class.
* There should only be one instance of SharedPreferences per app.
@theyann
theyann / StringHelper.java
Created December 11, 2015 20:51
StringHelper is a utility class using a similar interface and proposing similar functionalities than commons lang StringUtils. Internally it is using Android system methods.
package X.Y.Z;
import android.text.TextUtils;
import android.util.Base64;
/**
* Created by ylemin on 07/09/15.
*
* This class is similar in functionalities as the commons.lang.StringUtils, but using as much
* of the Android built-in tools and helpers as possible
@theyann
theyann / KeyStoreCompat.java
Created April 15, 2017 20:38
KeyStoreCompat is a simple class that takes care of dealing with KeyStore and using the AndroidKeyStore when it can, or another type if it can't. You're welcome.
package YOUR_PACKAGE;
import android.content.Context;
import android.os.Build;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;