Skip to content

Instantly share code, notes, and snippets.

@Tarelochkin
Tarelochkin / createTempImageFile.java
Created February 24, 2018 10:57
Create a file with a filename containg current date
static File createTempImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalCacheDir();
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
/**
* The custom SearchView provides following features:
* 1) Located below the App Name with the effect of 'multi-level' toolbar.
* 2) Expanded to the full width of the screen with even margin from the both edges.
* 3) The search icon is located inside the text field (not really) as opposed to the default 'outing'
* when implementing setIconifiedByDefault(false);
* 4) Uses semi-transparent background, icons and he hint text.
* 5) Default underline of the text field is removed.
*/
//process current time in milliseconds into "00:00 am" format
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat(getString(R.string.timeFormat), Locale.getDefault());
String currentTimeString = formatter.format(currentTime);
//process a number of minutes into "hh:mm" format
long totalMinutesCount = 123;
long hoursCount = TimeUnit.MINUTES.toHours(totalMinutesCount);
long minutesLessHoursCount =
totalMinutesCount - TimeUnit.HOURS.toMinutes(hoursCount);
@Tarelochkin
Tarelochkin / FirebaseJob.java
Created October 7, 2017 08:57
Create and schedule com.firebase.jobdispatcher.Job
Driver driver = new GooglePlayDriver(context);
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);
/* Create the Job to periodically create reminders to drink water */
Job constraintReminderJob = dispatcher.newJobBuilder()
/* The Service that will be used to write to preferences */
.setService(WaterReminderFirebaseJobService.class)
/*
* Set the UNIQUE tag used to identify this Job.
*/
private static final int WATER_REMINDER_NOTIFICATION_ID = 1138;
/**
* This pending intent id is used to uniquely reference the pending intent
*/
private static final int WATER_REMINDER_PENDING_INTENT_ID = 3417;
private static final int ACTION_DRINK_PENDING_INTENT_ID = 1;
private static final int ACTION_IGNORE_PENDING_INTENT_ID = 14;
public static void remindUserBecauseCharging(Context context) {
int count = PreferenceUtilities.getChargingReminderCount(this);
//the first count parameter selects the appropriate plural string and the second count parameter is inserted into the %d placeholder.
String formattedChargingReminders = getResources().getQuantityString(R.plurals.charge_notification_count, count, count);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
// Called when a user swipes left or right on a ViewHolder
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
// Here is where you'll implement swipe to delete
@Tarelochkin
Tarelochkin / scaled_image_xml
Created September 9, 2017 11:50
Scale Image to fill ImageView width and keep aspect ratio
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
@Tarelochkin
Tarelochkin / ImagetoDatabase.java
Created August 18, 2017 20:02
Save and retrieve an image to/from a database
// save to db
private void saveItem() {
ContentValues values = new ContentValues();
values.put(InventoryEntry.COLUMN_IMAGE, BitmapUtility.bitmapToBlob(mBitmap));
}
// retrieve from db (via CursorLoader)
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
int imageIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_IMAGE);
byte[] imageBlob = cursor.getBlob(imageIndex);
@Tarelochkin
Tarelochkin / ResizeBitmap.java
Created August 18, 2017 19:57
Resize Bitmap according to the maximum allowed size (width or height) in dp
public final class BitmapUtility {
private BitmapUtility() {
}
// scale the bitmap for a given maximum dimension
// (width or height depending on the image's aspect ratio)
public static Bitmap scaleBitmap(Bitmap bitmap, int dp, Context context) {
int width = bitmap.getWidth();