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
var doAction = function() {
// do something when ctrl-enter is pressed
}
$(document).ready(function() {
var ctrlDown = false,
ctrlKey = 17,
cmdKey = 91,
enterKey = 13;
@Phocacius
Phocacius / LanguageUtils.php
Last active December 31, 2020 13:11
PHP utility class to get the preferred language of the current user. The language will be read from the HTTP AcceptLanguage, but can be overridden by GET and POST parameters. Preferences can also be saved in a cookie.
<?php
class LanguageUtils {
public static $COOKIE_LIFETIME = 10 * 365 * 24 * 3600; // 10 years
public static $LANGUAGE_KEY = "lang"; // key used as cookie name and to check $_POST and $_GET
/**
* Get the user language based on the following priorities:
* - "$LANGUAGE_KEY" parameter in $_POST data
@Phocacius
Phocacius / FileProvider.kt
Created November 28, 2017 14:17
Custom ContentProvider for Android to enable files stored in the internal storage to be opened by external apps
import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.os.ParcelFileDescriptor
import java.io.File
import java.io.FileNotFoundException
/**
* Created by thorstenhack on 28.11.17.
@Phocacius
Phocacius / download_material_icon.py
Created August 28, 2017 13:35
Python script to download Google material icons and add them to an Android project automatically in several densities.
#! /usr/bin/env python
# Simple command script to download and add Google Material
# Design Icons ( https://material.io/icons/ ) to
# to your Android project.
import sys
import os
# Constants
@Phocacius
Phocacius / responsive-iframe.js
Created December 4, 2016 00:36
automatically adapts the width of an iframe to the available width, needs ratio set via width/height attributes and width set to 100% via CSS
/* Responsive iFrames
- - - - - - - - - - - - - - - - - - - - */
var $iframes = $('.iframe-responsive');
if($iframes.length) {
var adaptIframes = function() {
$iframes.each(function(index, item) {
var $iframe = $(item);
var ratio = $iframes.attr('width') / $iframes.width();
$iframe.css('height', $iframes.attr('height') / ratio);
});
@Phocacius
Phocacius / GlobalStorage.php
Last active December 1, 2015 15:50
tiny php class to store tiny amounts of persistent data into a JSON file
<?php
// helper class to store tiny amounts of data that should persist across sessions
class GlobalStorage {
private static $instance;
public static function getInstance() {
if(!self::$instance) {
self::$instance = new GlobalStorage();
@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();
@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 / 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 / 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) {