Monday, April 22, 2013

1.0.0.65 - What's new

To get the latest version, run "Check for updates" from the Device Center or download.

Fixed: Support4Demos sample fails to compile with Community edition license

The Community Edition postfixes the application label with " (by dot42.com)". This conflicts with using a resource ID for the application label. This has been fixed in the code sample (it now uses a literal).

Improved: Improved performance of the dot42 compiler

Various performance improvements.

Saturday, April 20, 2013

1.0.0.64 - What's new?

To get the latest version, run "Check for updates" from the Device Center or download.

New: Reference any Java library

Until this version we only supported referencing a jar file (java library) that acts as a stub for an Android package (APK) that is already on the device. In this version we introduce support for referencing any Java library. This includes the Android support library. This is a major step indeed.

For convenience, we have added assemblies for the Android support library so that you do not have to download it yourself. Just add a reference to dot42.AndroidSupportLibrary.v4.dll (or dot42.AndroidSupportLibrary.v13.dll) and you are ready to use the entire support library.

We ported the Support4Demos code sample that comes with the Android support library to C# to demonstrate the use of this library. See <MyDocuments>\dot42\Samples\Various\Support4Demos.

New: Improved framework support

We added support for several .NET types (including System.TimeSpan) and improved and fixed the support of many base types such as System.Double and System.Float. With these improvements come various compiler fixes related to structs. Note that this does not mean that structs are now fully supported.

New: Improved Android manifest support

We added support for the <provider> element of the AndroidManifest.xml file by means of the Dot42.Manifest.ProviderAttribute. Attributes of he <provider> element correspond to properties of the ProviderAttribute.

Previously, all manfiest element attributes that were defined in an Android version later than the minimum Android version of your project, were not available as properties of the corresponding attributes in the Dot42.Manifest namespace. Using them resulted in a compile error. As of this version, all properties are now available. This allows you to use newer properties if you set your target SDK version beyond the minimum version. Note that you cannot use properties beyond your target SDK version.

New: Improved ApkSpy

The opcodes of instructions in both dex files as well as java files are now visible in the Apk Spy:


Change: Target SDK version 

The previous version added a property on the PackageAttribute to set the Target SDK version. This property has been removed. You should now specify the Target SDK version in your Visual Studio (MSBuild) project. Go to the project properties and select the Android tab to change this version.


Change: Android framework interfaces starting with 'I' 

Few interfaces in the Android framework follow the .NET interface naming pattern of INameOfInterface. Previously, these interface were prefixed with an 'I' like all other interfaces resulting in a double II. As of this version, these interfaces are no longer prefixed with an additional 'I'. For example Android.Os.IIBinder is now Android.Os.IBinder.

Fixed: ADB installation task did not report all possible installation errors

It does now.

Fixed: Enum types with underlying type other than int causes verify errors

The following line caused a verify error:
public enum MyEum : sbyte {...}
This has been fixed.

Fixed: Many small compiler issues

While adding support for java libraries (and testing this) we ran into many small compiler issues. We fixed those.

Monday, April 15, 2013

Call a JSON Web Service

This article demonstrates how to consume the Airport Service of the Federal Aviation Administration using the standard Android HTTP client API and the standard Android JSON parser. The FAA offers a tutorial describing their web services.

While this article includes code snippets only, the full code sample is included in dot42 update 1.0.0.61 or higher. See folder [MyDocuments]\dot42\Samples\Various\AirportInfo.

Send a GET request to the Airport Service

To get the airport status for airport OAK, including known delays and weather data, you would make a GET request to the following URL:

http://services.faa.gov/airport/status/OAK?format=application/json

The above URL encodes two input arguments: First, the three letter airport code (in this case OAK). Second, the format of the data that you would like to get back (in this case JSON).

The following code makes this request:
string iataCode = "OAK";
var uri = string.Format("http://services.faa.gov/airport/status/{0}?format=application/json", iataCode);
var client = AndroidHttpClient.NewInstance("AirportInfo");
var request = new HttpGet(uri);
var response = client.Execute(request); // send the request

Parse the HTTP response

When the above request is made to the service, it will return a response consisting of plain text formatted according the JSON format. Android offers class JSONObject to parse the name/value pairs.

First, we must extract the JSON string from the response:
var content = response.GetEntity().GetContent();
var reader = new BufferedReader(new InputStreamReader(content));
var builder = new StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
{
  builder.Append(line);
}
string jsonString = builder.ToString();
Then we wrap a JSONObject around the string:
JSONObject json = new JSONObject(jsonString);
And now we can query named values like this:
string state= json.GetString("state");
string city = json.GetString("city");

The full code


The full code sample is included in dot42 update 1.0.0.61 or higher. See folder [MyDocuments]\dot42\Samples\Various\AirportInfo.

Thursday, April 11, 2013

1.0.0.62: What's new?

To get the latest version, run "Check for updates" from the Device Center or download.

New: Added simple GPS sample showing how to query the current location.

This sample shows the use of the GPS based location service. Read more.

New: Added support for specifying the target SDK version.

[do not use property TargetSdkVersion of PackageAttribute - this property will be replaced by a Project property in the next update]

The minimum Android version supported by your application is determined by the "Android version" specified in your dot42 project (see project properties - Android tab).

We have added a new property called TargetSdkVersion to the Package attribute. With this property you can specify the target SDK version of your application. This the maximum Android version for which forward-compatibility will be enabled. This property is typically used to enable the Android Holo theme when your app runs on a modern device. The app will still run on the minimum Android version.

This property corresponds to the android:targetSdkVersion attribute of the uses-sdk element in the Android manifest file.

You can use this property as follows:
[assembly: Package(TargetSdkVersion = "v4.0.3")]

Change: Reachable detection phase of the compiler has changed.

The dot42 compiler includes the minimum number of types and members that are required to have a working application. These types and member are identified during the reachable detection phase. This phase has changed. Before all public types were included. In this release only those types that are marked with an ApplicationRoot attribute (or derived attribute such as Activity) are included.

Fixed: Rare verify error

The major compiler redesign of 1.0.0.61 introduced a verify error in rare cases involving calling methods. This has been fixed.

A GPS Client

This article walks through a basic dot42 code sample that uses the location services such as GPS on an Android device. While this article includes code snippets only, the full code sample is included in dot42 update 1.0.0.62 or higher. See folder [MyDocuments]\dot42\Samples\Sensors\SimpleGps.

Declare permissions

An application that uses location services requires certain permissions. You need INTERNET and ACCESS_COARSE_LOCATION to access the network-based location provider only. For more accurate GPS you need ACCESS_FINE_LOCATION permission. Declaring ACCESS_FINE_LOCATION implies ACCESS_COARSE_LOCATION.
Traditional Android developing declares permissions in AndroidManifest.xml as follows:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  ...
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.INTERNET" />
  ...
</manifest>
However, using dot42 you declare the same set of permissions using the assembly attribute UsesPermission as follows:
[assembly: UsesPermission(Android.Manifest.Permission.ACCESS_COARSE_LOCATION)]
[assembly: UsesPermission(Android.Manifest.Permission.INTERNET)]

LocationManager

Class LocationManager is the starting point for accessing location services. You retrieve an instance as follows:
LocationManager service =
  (LocationManager) GetSystemService(LOCATION_SERVICE);
The next step is to check whether GPS has been enabled on the device:
bool enabled =
  service.IsProviderEnabled(LocationManager.GPS_PROVIDER);
On my Asus Transformer Pad, this corresponds to the following setting:


If GPS is not enabled you may choose to fall back to the network-based location provider. You would check availability as follows:
bool enabled =
  service.IsProviderEnabled(LocationManager.NETWORK_PROVIDER);

Get location provider

After we have determined what provider is enabled, we can retrieve it and ask for the location. Assuming that GPS has been enabled, we would retrieve the provider and location as follows:
// get the provider
Criteria criteria = new Criteria();
criteria.Accuracy = Criteria.ACCURACY_FINE;
provider = service.GetBestProvider(criteria, false);
// request the location (returns null if provider is disabled)
Location location = service.GetLastKnownLocation(provider);
If GPS is not enabled, you may request a provider with course accuracy like this:
criteria.Accuracy = Criteria.ACCURACY_COARSE;

Receive location updates

The above code requests the last known location. The current location may however change and we would like to be notified when it does. This can be done by registering a listener. You typically do this in the OnResume of your activity:
protected override void OnResume()
{
  base.OnResume();
  if (enabled) service.RequestLocationUpdates(
    provider, // as returned by GetBestProvider
    1000, // minimum update period in ms
    1, // minimum update distance in m
    this);
}
In the Activity overridable OnPause, you would remove your listener as follows:
protected override void OnPause()
{
  base.OnPause();
  if (enabled) service.RemoveUpdates(this);
}
Note that this refers to the current Activity and it is therefore required to implement the listener interface ILocationListener:
[Activity]
public class MainActivity : ILocationListener
{
  ...
}
ILocationListener has 4 methods of which OnLocationChanged is the most interesting:
public void OnLocationChanged(Location location)
{
  double latitude = location.GetLatitude();
  double longtitude = location.GetLongitude();
  // update UI
}

The full code

The full code sample is included in dot42 update 1.0.0.62 or higher. See folder [MyDocuments]\dot42\Samples\Sensors\SimpleGps.

Wednesday, April 10, 2013

1.0.0.61 : What's new?

To get the latest version, run "Check for updates" from the Device Center or download.

New: Added AirportInfo sample.

This sample shows how to fetch data from a JSON webservice using the standard Android HTTP client API and the standard Android JSON parser.

New: Abstract interface implementation methods can now be overriden.

Various abstract classes in the Android framework implement interfaces while leaving several interface methods unimplemented (abstract).

To satisfy the C# compiler, these methods must be added. We have now added them as abstract methods (where possible), instead of an explicit interface implementation as it was before.

In very few cases it is not possible to add these methods as abstract methods because it causes a naming conflict with existing members (nested classes). In those cases we still use the old style of explicit implementation.

New: Primitive number types now implement IFormattable.

All .NET primitive number types (e.g. UInt32) implement IFormattable. The corresponding Android types do not implement this interface. We have improved the dot42 compiler to work around this issue such that the Android primitive number types will behave as if they implement IFormattable so that they will work with e.g. String.Format.

New: Variable argument methods now have a "params" parameter.

Android methods with variable arguments (such as Android.Content.Context.GetString) now have a "params" array as the last parameter instead of a normal array.

Change: String.Format improved.

We have extended support of String.Format formatting rules.

Fixed: Including layout files in a Visual Studio project does not always set the build type.

When you include an xml file in a Visual Studio project, dot42 tries to guess the build type for that file. We have greatly improved this for layout xml file.

Fixed: Various UInt64/UInt32 fixes

Various verify and compilation errors related to unsigned integers have been solved.

Fixed: Convert.ToBase64String

No comment needed.

Fixed: Method with generic out parameter may fail when called with primitive or enum value.

This includes Dictionary<int, string>.TryGetValue(...) or Dictonary<some_enum, T>.TryGetValue(...).

Fixed: Extension methods on primitive types may cause verify errors

The way that we support primitive types and boxed primitive types has changed slightly to avoid various verify errors when compiling methods that have been added to primitive types.

Change: Major internal overhaul of the compiler

The result of this change is not yet visible, but it will be in one of the next releases.

We have made a large number of internal changes in order to compile existing java code in addition to compiling .NET code to DEX and combine the two worlds.

As a side effect, we made optimized our compiler and fixed many minor issues.