venerdì 3 luglio 2015

Create a custom dialog box with Modern UI 1.5.0

The Modern UIfor WPF is a good solution to create pretty WPF application.
Unfortunately the FirstFloor.ModernUI.Windows.Controls.ModernDialog class is usefull only to show simple text (alert dialog).

The following function helps you to create a custom dialog box using the ModernDialog class.

/// <summary>Simple input dialog box</summary>
/// <returns>null if the dialog box was closed with "cancel",
/// otherwise the user input<returns>
public static string ShowInputDialog(string initialText=null)
{
  // create the dialog content
  TextBox content = new TextBox();
  content.Text = initialText;

  // create the ModernUI dialog component with the buttons
  var dlg = new ModernDialog
  {
    Title = "Insert a text",
    Content = content,
    MinHeight = 0,
    MinWidth = 0,
    MaxHeight = 480,
    MaxWidth = 640,
  };
  dlg.Buttons = new Button[] { dlg.OkButton, dlg.CancelButton };

  // register the event to retrieve the result of the dialog box 
  string result = null;
  dlg.OkButton.Click += (object sender, RoutedEventArgs e) =>
  {
    result = content.Text;
  };

  dlg.ShowDialog();
  return result;
}

giovedì 2 luglio 2015

Retrieve the version of the .NET runtime

Sometimes the simplest questions remains unanswered: what's the .NET runtime version executes my app? .NET 4.0 or .Net 4.5?
Hier you find a simple C# function to retrieve the version of the .NET runtime on which your app is executed.

public static string RetrieveHumanRuntimeVersion()
{
 string text = Environment.Version.ToString();
 if (!text.StartsWith("4.0.30319.")) return text;
 int build, start = text.LastIndexOf('.');
 if (!Int32.TryParse(text.Substring(start + 1), out build)) return text;
 if (build <= 17000) return ".NET 4.0";
 if (build <= 19000) return ".NET 4.5";
 if (build <= 34000) return ".NET 4.5.1";
 return ".NET 4.5.2";
}

More details here

giovedì 2 aprile 2015

Simple uniq alternative

uniq is a Unix utility which, when fed a text file, outputs the file with adjacent identical lines collapsed to one.

Here a simple alternative

alias uniq='while read line; do if [ "$line" != "$PREVIOUS" ]; then echo $line; PREVIOUS=$line; fi; done'

domenica 1 febbraio 2015

Experience with the Google Play Developer API

If the Android developer console is a powerful tool, the manage of a multi language Android app is oft a long time-consuming procedure.
So I've tested the Google Play Developer Publishing API.

As described in the Getting Started page, I've linked my Google Play Developer Console to an API project and created a OAuth2 Client in the API Access page on the Google Play Developer Console .

Here there's the code of a simple bash script:
#!/bin/sh

PACKAGE_NAME=<package name of the Android app>
BASE_URL=https://www.googleapis.com/androidpublisher/v2/applications/$PACKAGE_NAME

# see https://play.google.com/apps/publish/#ApiAccessPlace
CLIENT_ID=<CLIENT ID value>
CLIENT_SECRET=<CLIENT SECRET value>
REDIRECT_URL=urn:ietf:wg:oauth:2.0:oob

#-----------------------------------------------
# https://developers.google.com/accounts/docs/OAuth2InstalledApp
CODE=$1
if [ "$CODE" = "" ]
then
	echo "opening the browser to obtain a client code ..."
	xdg-open "https://accounts.google.com/o/oauth2/auth\
?scope=https://www.googleapis.com/auth/androidpublisher\
&response_type=code\
&access_type=offline\
&redirect_uri=$REDIRECT_URL\
&client_id=$CLIENT_ID"
	exit 255
fi

#-----------------------------------------------------------------------
# https://developers.google.com/android-publisher/authorization
REQUEST_URL="https://accounts.google.com/o/oauth2/token"
REQUEST_BODY="grant_type=authorization_code\
&code=$CODE\
&client_id=$CLIENT_ID\
&client_secret=$CLIENT_SECRET\
&redirect_uri=$REDIRECT_URL"
RESPONSE="/tmp/response-auth.json"
echo "Retrieving the authorization ..."

curl -s -o $RESPONSE -X POST -d "$REQUEST_BODY" $REQUEST_URL
AUTHORIZATION=`grep "access_token" $RESPONSE | cut -d '"' -f 4`
if [ "$AUTHORIZATION" = "" ]; then exit 255; fi

#-----------------------------------------------
# https://developers.google.com/android-publisher/edits/workflow
REQUEST_URL="$BASE_URL/edits"
REQUEST_BODY='{"id": "?","expiryTimeSeconds": 9}'
RESPONSE="/tmp/response-editid.json"

echo "Acquiring a edit id ..."
curl -s -o $RESPONSE -X POST -d \
-H "Content-Type: application/json" "$REQUEST_BODY" \
"$REQUEST_URL?access_token=$AUTHORIZATION"
EDIT_ID=`grep "id" $RESPONSE | cut -d '"' -f 4`
if [ "$EDIT_ID" = "" ]; then exit 255; fi

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/listings/list
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/listings"

echo "Retrieving the app description ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/images/
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/listings/en-US/phoneScreenshots"

echo "Retrieving the phone screenshots ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/apks/list
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/apks"

echo "Retrieving the APKs list ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"