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 / 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 / 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 / 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 / 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 / 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 / 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() {