Archive

Archive for May, 2014

APK to BAR file from the command line

There are some very handy websites that allow you just upload a APK file, and you get a BAR file in return. Which is great, but sometimes you need some finer control over it. – such as having the BAR file signed with your own certificate rather than a random one provided by the website. 

If you already have your BlackBerry certs set up then the process is easy:

apk2bar “C:\your.apk” “C:\android\sdk”

blackberry-signer -storepass xxxx  “C:\your.bar”

blackberry-deploy -installApp -device 192.168.0.12 -password pass -package “C:\your.bar”

 

Categories: Uncategorized

Embed Prepopulated SQL server db in Windows Phone app

This is a easy way to embed a Prepopulated  SQL server database (read-only) into a Windows Phone app.

Include SDF into project at root level, mark it as “build-action: embed”

Using the SQL CE toolbox, generate a context class (http://sqlcetoolbox.codeplex.com/releases/view/104781), include this in your project.

Make a reference to system.data.linq

– I found I needed to comment out the lines containing “IDbConnection” but that might not be applicable.

Then the code for a select is simple:

using (dictContext db = new dictContext(“Data Source=appdata:/dict.sdf;File Mode=Read Only; Max Database Size = 512;”))
{
var translations = db.Dictionary.Where(d => d.Word == “CAT”);
MessageBox.Show(translations.First().Fr);
}

 

 

 

Categories: Uncategorized

Convert SQLite database to SQL Compact Edition

This is a script I came up with that takes data from a SQLite database, and writes it into a SQL CE (SDF) database. This is a very simple use-case, where there is only one table, no indexes, views, or primary keys. Also, the SDF file has been pre-created, and set up with the same schema as the SQLite database.

var db = new SQLiteConnection(@”Data Source=C:\source.db;Version=3″);
db.Open();
string sql = “PRAGMA table_info( yourtable);”;
SQLiteCommand command = new SQLiteCommand(sql, db);
SQLiteDataReader reader = command.ExecuteReader();
List<string> strColumnNames = new List<string>();
while (reader.Read())
{
strColumnNames.Add(reader[1].ToString());
}
db.Close();

db.Open();
sql = “select * from yourtable”;
command = new SQLiteCommand(sql, db);
reader = command.ExecuteReader();

var conn = new SqlCeConnection(@”Data Source = C:\destination.sdf”);
conn.Open();

SqlCeCommand cmd = conn.CreateCommand();

while (reader.Read())
{
try
{
sql = “insert into dictionary (” + String.Join(“,”, strColumnNames.ToArray()) + “) values (“;
var strColumnValues = new List<string>();
for (int i = 0; i < reader.FieldCount; i++)
{
strColumnValues.Add(“N'” + reader[i] + “‘”);
}
sql += String.Join(“,”, strColumnValues.ToArray());
sql += “)”;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
Console.WriteLine(sql);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

This requires the System.Data.SqlServerCe and System.Data.SQLite; namespaces

Categories: Uncategorized

Parse JSON in WP7

Windows Phone 7 may be quite obsolete at this point, but since WP8 still runs WP7 apps, it is worth trying to make your apps backwards-compatible where possible. 

Using Newtonsoft JSON.NET to parse JSON is a good option, but you have to get the right version, the latest version is only for Windows Phone 8. I found version 4.0.3 (40r3)  worked for me. 

Here is a brief example of how I used it:

public static void Search(string depart, string arrive, Action<JObject> callback)
{
var strUrl = “http://www.rome2rio.com/api/1.0/json/Search?key=xxxx&oName={0}&dName={1}”;
strUrl = string.Format(strUrl, depart, arrive);
WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri(strUrl), callback);
wc.DownloadStringCompleted += (sender, args) =>
{
var jo = JObject.Parse(args.Result);
callback(jo);
};
}

Of course, you need to use using Newtonsoft.Json.Linq;

Categories: Uncategorized

Determine Geoposition from IP address on Windows Phone

On Windows Phone, you can determine the current user’s lat/lon position using code such as:

GeoCoordinateWatcher gwatcher = new GeoCoordinateWatcher();
GeoCoordinate PositionNow = gwatcher.Position.Location;

if (!PositionNow.IsUnknown)
{
MessageBox.Show(“lat/lon:” + PositionNow.Latitude+ “,” + PositionNow.Longitude);
}

However, the position may not be known, so you may need a failover system, getting the user’s rough location from the IP address of the device, for that, I’ve used api.ipinfodb.com – Create your own account there, since I’m not sharing my key!

Here’s the code I used:

public static void GetLocationFromIp(Action<GeoCoordinate> callback)
{
var wc = new WebClient();
var url = new Uri(“http://api.ipinfodb.com/v3/ip-city/?key=xxxx&format=xml&#8221;);
wc.DownloadStringAsync(url, callback);
wc.DownloadStringCompleted += (sender, args) =>
{
var xdoc = XDocument.Parse(args.Result);
var longitude = xdoc.Descendants(“longitude”).First().Value;
var latitude = xdoc.Descendants(“latitude”).First().Value;
var dLng = Convert.ToDouble(longitude, CultureInfo.InvariantCulture);
var dLat = Convert.ToDouble(latitude, CultureInfo.InvariantCulture);
var geo = new GeoCoordinate(dLat, dLng);
callback(geo);
};
}

Neat., all all in one function.

I’ve then expanded upon this to provide reverse geocoding using geonames.org:

public static void ReverseGeocode(Action<string> callback, GeoCoordinate geo)
{
var wc = new WebClient();
var strUrl = “http://api.geonames.org/findNearbyPlaceName?formatted=true&lat={0}&lng={1}&username=xxxxx”;
strUrl = string.Format(strUrl, geo.Latitude, geo.Longitude);
var url = new Uri(strUrl);
wc.DownloadStringAsync(url, callback);
wc.DownloadStringCompleted += (sender, args) =>
{
var xdoc = XDocument.Parse(args.Result);
var name = xdoc.Descendants(“name”).First().Value;
callback(name);
};

}

Categories: Uncategorized

SDK default key was used. To publish developer own key should be used with Registration file.

If you submit an app to Tizen (Samsung Seller) and get this rejection back:

<Defect>
SDK default key was used. To publish developer own key should be used with Registration file
For more information, refer to IDE > Help Contents > Generating a certificate request
Or, Help Contents > Getting Started with Tizen wearable > Development Environment > Tizen SDK for wearable > Certificates
<Procedure>
<Expected Result>

Then it means that your Tizen app was not signed.

The procedure is as follows:

Generating a Certificate Request

There are 2 ways to generate a certificate request: you can make a certificate request from scratch, or you can use your existing Android keystore. When you develop a companion type application, the second option is better, since you must use the same author certificate for both the host and wearable application.

To make a certificate request from scratch to obtain an author certificate:

  1. Click the button on the Tizen IDE for Wearable toolbar.

    The Generate Certificate Request dialog opens.

  2. Enter the following information:

    • Mandatory information
      • Name
      • Email
      • Private key password
      • Password confirm
      • Developer type
      • Privilege level
      • DUID (Device Unique Identifier)
    • Optional information
      • Country
      • State
      • City
      • Organization
      • Department

To use your existing Android keystore to obtain an author certificate:

  1. Click the button on the Tizen IDE for Wearable toolbar.

    The Import Android Keystore and Generate Certificate Request dialog opens.

  2. Enter the following information:

    • Mandatory information
      • Alias

        Click Import, input the keystore with the password, and select the right alias.

      • Private key password
      • Name
      • Email
      • Developer type
      • Privilege level
      • DUID (Device Unique Identifier)
    • Optional information
      • Country
      • State
      • City
      • Organization
      • Department

Getting the DUID

To get the DUID (Device Unique Identifier):

  1. Connect your device to your host computer or launch the Emulator.
  2. In the Connection Explorer, check that the connected device or Emulator is visible.
  3. Select the device or Emulator, and click Properties.

    The DUID is visible on the Info panel

Sending a Certificate Request

You must first generate the developer key used for application signing and then send it for signing to the Samsung Developer Center. The following figure illustrates the registration process.

Figure: Sending a certificate request using PGP

 

To get the key signed, you must be encrypt it and send it to Samsung Developer Center using PGP (Pretty Good Privacy) encryption. You can generate your own PGP key using a PGP support tool. The tool normally generates a key pair consisting of a private key and public key.

Send your public key to gear2.sec@samsung.com with the encrypted request. After the Samsung Developer Center has signed it, you receive the registration XML file. Use your private key to decrypt the registration file.

The process and encryption tool required to generate the developer key vary depending on your operating system:

Signing the Developer Key (Windows®)

To send a developer key request to the Samsung Developer Center, you have to first encrypt the keys and email them using PGP (Pretty Good Privacy).

To send a developer key request to the Samsung Developer Center:

    1. To download the Gnu Privacy Assistant (GPA) PGP support application, go to www.gpg4win.org and download the GPA installer (gpg4win.exe). In the setup dialog, select the GPA component.

       

Other PGP tool support applications, such as Kleopatra, can be also used.
  1. To create your own PGP key:
    Note
    If you already have a PGP key, you can skip this step. You can use the existing key for the signing process.
    1. Launch the GPA application and go to Keys > New key.

       

    2. Enter your full name and the email address from which you are going to send your registration request.

    You have now created a PGP key, which you share with the Samsung Developer Center to initiate encrypted communication.

  2. To import the Samsung Developer Center PGP key:
    1. Go to a PGP key server, such as keyserver.pgp.com.

       

    2. Search the keys using the Samsung Developer Center email address (gear2.sec@samsung.com) or the key ID (4D8CE45C).

       

    3. To download the PGP key and save it on your system, click Download.

    4. Drag and drop the key directly to the GPA application or import it manually by going to Keys > Import Keys.

       

  3. To encrypt your developer certificate request:
      1. In the GPA application, go to Windows > File Manager.

         

      2. Click Open, select the created certificate request file (certificate-request.xml) from the /tizen-wearable-sdk-data/keystore directory, and click OK.
      3. Click Encrypt, select the Samsung Developer Center key named Gear2 Samsung from the Public Keys list, and click OK.
        Note
        Make sure the hidden attribute of the /tizen-wearable-sdk-data folder is not selected to show it in the GPA File Manager.
      4. Send the encrypted file (certificate-request.xml.gpg) and your public key to the Samsung Developer Center as an email attachment.

        In the mail, enter REQUEST FOR SIGNING as subject.

    Note
    PGP is not compatible with the Web mail system. Therefore, if you must encrypt your email message, go to Windows > Clipboard and paste your message in the clipboard window. To encrypt the message, click Encrypt and select the appropriate Samsung Developer Center key from the list.
  4. To decrypt your registration file after you receive it from the Samsung Developer Center:
    1. In the GPA application, go to Windows > File Manager.

       

    2. Click Open to find the files to decrypt. Select the file and click Decrypt.
    3. Enter your password to decrypt the registration file.

Registering Certificates

You can register an author certificate using the certificate-registeration.xml file received from the Samsung Developer Center.

To register a certificate:

  1. On the Tizen IDE for Wearable toolbar, click the Open icon button.

    The Register certificates dialog opens.

  2. Enter the certificate-registeration.xml file path.
  3. Enter your password.
  4. Click OK.

The author certificate and distributor certificate files are generated. A default security profile is also created.

Note

To install, without the Tizen IDE for Wearable, a wearable application on a wearable device on which no wearable applications have previously been installed:

  1. In the Connection Explorer, select the /home/developer folder.
  2. Copy the certificate-registration.xml file containing the DUID of the target device to the /home/developer folder of the Wearable device.

Adding Certificates to Security Profile

After registering, the author and distributor certificates are automatically added to the security profile.

To manually change the certificate settings:

  1. In the Tizen IDE for Wearable, go to Window > Preferences > Tizen SDK > Security Profiles.
  2. To add a signed profile, click Add in the Profiles panel.
  3. To edit the certificates:
    • In the Author Certificate panel, click Edit to set the author certificate path and password.
    • In the Distributor Certificates panel, select a certificate in the table and click Edit to modify the distributor certificate path and password.

Figure: Adding certificates

 

At least one distributor certificate is required and the default information is filled automatically when a profile is created. Another distributor certificate is optional and is used only for special purposes. In general, you are not required to modify distributor certificates.

 

– Then, click on the Profile, and click “set Active”, you will be prompted to clean all projects, and rebuild. (this last step wasn’t in the documentation)

To check if a WGT file is signed or not, then unzip the wgt file, and open the author-signature.xml file in a text editor, look for the X509Certificate node, and copy and paste it into http://www.sslshopper.com/certificate-decoder.html, you should see something like:

 

Certificate Information:

Common Name: Open Merchant Account Ltd
Organization: Open Merchant Account Ltd
Locality: Clonmany
State: Donegal
Country: IE
Valid From: October 31, 2012
Valid To: December 31, 2018
Issuer: Tizen Developers CA, Tizen Association
Key Size: 1024 bit
Serial Number: 0142c2e3a0f1

– I.e. the Common name should be your company, not Tizen.

 

 

Categories: Uncategorized

Alternative to sp_send_dbmail

If you’d like to send email from SQL server, but don’t, or can’t setup sp_send_dbmail on your SQL server database, then you can use this CLR stored procedure, which uses Mailgun as an outbound SMTP server.

using System;
using System.Net;
using Microsoft.SqlServer.Server;
using System.Net.Mail;
public partial class StoredProcedures
{
/// <summary>
/// Note: you will need to set
/// EXEC sp_dbcmptlevel ‘GeneralPurpose’, 90
/// ALTER DATABASE GeneralPurpose SET TRUSTWORTHY ON
/// </summary>
/// <param name=”subject”>The subject.</param>
/// <param name=”body”>The body.</param>
/// <param name=”to”>To.</param>
[SqlProcedure]
public static void CLR_SendEmail(string subject, string body, string to)
{
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential(“postmaster@xxxx.mailgun.org”, “xxxx”);
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(“postmaster@xxx.mailgun.org”);

smtpClient.Host = “smtp.mailgun.org”;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;

message.From = fromAddress;
message.Subject = subject;
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = body;
message.To.Add(to);

try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Error, could not send the message
SqlContext.Pipe.Send(ex.Message);
}
SqlContext.Pipe.Send(“Sent” + Environment.NewLine);
}
};

Categories: Uncategorized

Google Search API with Silverlight and System.Json

There are many ways to handle JSON in .NET, this approach uses a weakly-typed approach, which makes the parsing more flexible to schema changes.  I wanted to try the Microsoft approach and use System.JSON – This is all from a .NET 4 environment in Silverlight, with a non-blocking behaviour.

So, from the highest level, this is how I wanted the method to act. – from MainPage.xaml.cs

public MainPage()
{
InitializeComponent();
GoogleSearch.DoSearch(“Hello”, Callback);
}

private void Callback(List<GoogleSearchResult> googleSearchResults)
{
// to-do
}

So, in a separate class, I defined a GoogleSearchResult as follows:

public class GoogleSearchResult
{
public string Title;
public Uri Url;
}

Then, to make a HTTP call to the Google custom search API as follows:

public static void DoSearch(string query, Action<List<GoogleSearchResult>> callback)
{
var strUrl = “https://www.googleapis.com/customsearch/v1?&#8221;;
strUrl += “q=” + query;
strUrl += “&cx=…”;
strUrl += “&key=…..”;
var wc = new WebClient();
wc.DownloadStringAsync(new Uri(strUrl), callback);
wc.DownloadStringCompleted += DownloadStringCompleted;
}

Then defining the DownloadStringCompleted method as follows:

static void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var callback = (Action<List<GoogleSearchResult>>) e.UserState;
var json = JsonValue.Parse(e.Result);
if (json == null || (json[“items”] as JsonArray) == null)
{
callback(null);
return;
}
var lResults = json[“items”].Cast<JsonObject>().Select(jo =>
new GoogleSearchResult
{
Title = jo[“title”],
Url = new Uri(jo[“link”])
}).ToList();
callback(lResults);
}

Notice how the callback function is passed through in the UserState (same as userToken)

Categories: Uncategorized

Generate static graph images for embedding into emails

fsThe Google Graph API is great, but it’s Javascript based, so therefore unsuitable for embedding into emails, since most, if not all email clients will block the execution of Javascript.

The Google static chart API is an option, but it is officially deprecated, and may not be supported beyond 2015.

Here is an unusual solution, which can be used to generate an image from a web page, and then the resultant image can be embedded into a HTML based email.

First, I took Google’s simple “Pie chart” example, and replaced all the values with variables read from the query string: i.e.

function drawChart() {

var data = new google.visualization.DataTable();
data.addColumn(‘string’, getParameterByName(“datatitle”));
data.addColumn(‘number’, getParameterByName(“valuetitle”));
data.addRows(
eval(getParameterByName(“data”))
);

var options = {‘title’:getParameterByName(“title”),’width’:”100%”,’height’:”100%”};

var chart = new google.visualization.PieChart(document.getElementById(‘chart_div’));
chart.draw(data, options);
}

Put this online as a web-page, which can be called like this:

http://webtropy.site44.com/graph.html?title=Mobile+OS&datatitle=OS&valuetitle=Market+Share&data=%5B%5B%27iOS%27,15.6%5D,%5B%27Android%27,78.4%5D,%5B%27BlackBerry%27,1.9%5D,%5B%27Windows%27,3.2%5D%5D

Then using a thumbnail generator service, I can create an image from this:

http://immediatenet.com/t/fs?Size=1024×768&url=http://webtropy.site44.com/graph.html?title=Mobile+OS&datatitle=OS&valuetitle=Market+Share&data=%5B%5B%27iOS%27,15.6%5D,%5B%27Android%27,78.4%5D,%5B%27BlackBerry%27,1.9%5D,%5B%27Windows%27,3.2%5D%5D

– Which could then be embedded into an email using a simple <img> tag.

 

Categories: Uncategorized