Skip to content

Instantly share code, notes, and snippets.

View Phocacius's full-sized avatar

Thorsten Hack Phocacius

  • WhereGroup GmbH
  • Baden-Württemberg, Germany
View GitHub Profile
@Phocacius
Phocacius / RateAppManager.java
Last active October 29, 2016 11:23
Simple Android Java class that allows you to determine whether or not to show a "rate my app"-dialog and allows you to show the dialog that takes the user to the Play Store when the dialog is confirmed.
public class RateAppManager {
private static final String MS_PREFS = Prefs.class.getSimpleName();
private static SharedPreferences preferences;
private static final String APP_START_COUNT = "appStartCount";
private static final String APP_INSTALL_TIMESTAMP = "appInstallTimestamp";
private static final String RATE_DIALOG_SHOWN = "rateDialogShown";
private static SharedPreferences getPreferences() {
@Phocacius
Phocacius / MetricsCalculator.java
Created December 11, 2014 10:05
Android class that converts density to raw pixels and vice versa
public class MetricsCalculator {
public static float convertDpToPixel(float dp,Context ctx) {
Resources resources = ctx.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return dp * (metrics.densityDpi / 160f);
}
public static float convertPixelToDp(float px, Context ctx) {
Resources resources = ctx.getResources();
@Phocacius
Phocacius / calendar.php
Created December 30, 2014 19:07
A php function that outputs a calendar for a given month and year. Optionally, a heading (e.g. weekdays) can be displayed, and the current day is highlighted. The calendar can be styled via CSS.
<?php
function drawCalendar($year, $month, $heading = null, $forceSixthRow = false) {
$daysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
$daysInPrevious = date("t", mktime(0, 0, 0, $month-1, 1, $year));
$weekdayOfFirst = date("N", mktime(0, 0, 0, $month, 1, $year)) - 1;
$rows = ($forceSixthRow || $weekdayOfFirst + $daysInMonth > 35) ? 6 : 5;
echo '<table class="calendar" border="1">';
if($heading != null) {
for ($i=0; $i < 7; $i++) {
echo '<th>'.$heading[$i].'</th>';
@Phocacius
Phocacius / Sha1Hasher.cs
Created January 5, 2015 10:53
A tiny helper C# class that converts an arbitrary string to an hex encoded SHA1 hash.
using System;
using System.Security.Cryptography;
namespace MyNamespace
{
public class Sha1Hasher
{
public static string sha1Hash (string plaintext)
{
SHA1 sh1 = SHA1.Create ();
@Phocacius
Phocacius / WearableCommunicator.java
Created January 7, 2015 09:53
Simple to use communication handler between an Android handheld and an Android Wear device
public class WearableCommunicator {
private static WearableCommunicator communicator;
public static WearableCommunicator getInstance() {
if (communicator == null) {
communicator = new WearableCommunicator();
}
return communicator;
}
@Phocacius
Phocacius / DensityCalculator.java
Created January 9, 2015 08:34
Get the smallestWidth property in density pixels for the current device. >= 600 dp is a 7' tablet, >= 720 dp is a 10' tablet.
public int getSmallestWidthDensity(Activity context) {
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float scaleFactor = metrics.density;
float widthDp = widthPixels / scaleFactor;
float heightDp = heightPixels / scaleFactor;
@Phocacius
Phocacius / ViewHolder.java
Created January 9, 2015 11:03
A generic Android ViewHolder class that simplifies the standard ViewHolder pattern. Credit goes to http://www.piwai.info/android-adapter-good-practices/
public class ViewHolder {
@SuppressWarnings("unchecked")
public static <T extends View> T get(View view, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
@Phocacius
Phocacius / copygitrepos.sh
Created January 14, 2015 16:05
copies an arbitrary Git repo (including all branches and tags) and pushs it to another repo. The destination repo has to be completely empty!
mkdir tmp
echo 'Enter URL of old repo:'
read oldrepo
echo 'Enter URL of new repo:'
read newrepo
cd tmp
git clone --bare $oldrepo
REPO=`ls`
cd $REPO
git remote add destination $newrepo
@Phocacius
Phocacius / httppost.js
Created January 24, 2015 19:44
send http post via Javascript without Ajax
httpPost = function(url, params) {
var html = '<form action="' + url + '" method="post">';
for(key in params) {
html += '<input type="text" name="'+ key +'" value="'+ params[key] +'" />';
}
html += '</form>';
$(html).submit();
}
@Phocacius
Phocacius / MethodAnnotation.java
Created January 28, 2015 15:32
template for a method annotation processor
Method[] methods = this.getClass().getMethods();
for (Method method : methods) {
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
if (annotation != null) {
Object myAnnotationAttribute = annotation.value();
// do whatever you want here
try {
method.invoke(this, methodParameters);
} catch (IllegalAccessException|InvocationTargetException e) {
e.printStackTrace();