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 */
//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();
@Tarelochkin
Tarelochkin / ChooseImageFromGallery.java
Created August 18, 2017 19:55
Pick an image from the gallery and show it in the activity window
private void pickImage() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
startActivityForResult(i, PICK_PHOTO_FOR_PRODUCT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_PRODUCT && resultCode == Activity.RESULT_OK) {