Archive for the ‘.NET’ Category

Mono Android: Working with SQLite

Saturday, December 31st, 2011

If you are using Mono for Android and need to use SQLite you are in luck since the platform fully supports this scenario.  In fact you don’t even need to do anything special, and this post also applies if you are developing for Android in Java using Eclipse.  What’s different however is Eclipse will let you copy files from your Android device, in Mono you are using Visual Studio which at this time does not have that capability.

The preferred method (but not the only one) of creating, and managing a SQLite DB in Android is to create a class and inherit from SQLiteOpenHelper, which is in the namespace ‘Android.Database.Sqlite’, you will also need to add the references to assemblies: ‘Mono.Android’, ‘Mono.Data.Sqlite’

Below is an example of where you inherit SQLiteOpenHelper and then override two methods: ‘OnCreate’ and ‘OnUpgrade’

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Database : Android.Database.Sqlite.SQLiteOpenHelper, IDatabase
{
private const string DATABASE_NAME = "MyDatabase";
private const int DATABASE_VERSION = 1;
public Database(Android.Content.Context context)
: base(context, DATABASE_NAME, null, DATABASE_VERSION)
{
}
public override void OnCreate(SQLiteDatabase db)
{
db.ExecSQL(@"CREATE TABLE MyTable (id INT PRIMARY KEY, comments TEXT NOT NULL");
}
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.ExecSQL("DROP TABLE IF EXISTS MyTable");
OnCreate(db);
}
}

It is important to note that OnCreate is ONLY called when the database is accessed for the very first time, subsequent calls are ignored. If the DB already exists, it is ignored, OnUpgrade is called only if the version number has been incremented, the database already exists and when the database is accessed for the first time per that instance.  Also to note is that I am taking an extremely lazy approach to upgrade where I simply drop the tables and re-create them, obviously not ideal for real world scenario because you would lose all your data.

At the top of the file I am defining two constants, DATABASE_NAME and DATABASE_VERSION which I mentioned above briefly is used for upgrade scenarios. The name is the actual name the DB file will end up being names on the Android file system.  In order to browse the file system you will need to have your Android device rooted or you will need to have a developer Android device that you obtained directly from Google.

These instructions pertain to (Windows users only, the commands will be similar however on OSX)

  1. Open cmd
  2. Type ‘adb shell’, if you get an error type ‘adb kill-server’ and try again.
  3. Type ‘su’ (for super user access, your phone must be rooted or be dev phone)
  4. Once you are in the shell, type ls (you will notice its a linux shell)
  5. Type ‘cd data/data’, it will list out all of the directories
  6. Type ‘ls data’, and find your applications directory
  7. Type cd <com.company.appname>
  8. Type cd ‘databases’
  9. Type ‘ls’ to look at your databases
  10. Note the name of your database matches your constant defined above (if everything worked…)
  11. Type ‘pwd’ (print working directory)
  12. Copy the path of this directory to Notepad or somewhere safe

To browse the database you can download SQLite Database Browser for free, and do the following.

  1. In order to browse the database your must copy the db file from your Android device onto your computer, I created a batch file for this and I run it when I need to take a look at something.
  2. Open ‘cmd’ prompts
  3. Type ‘adb pull <path of db file> <local path>’ for example ‘adb pull /data/data/com.elucidsoft.myapp/databases/MyDatabase D:\MyDatabase
  4. Open up SQLite Database Browser
  5. File -> Open -> D:\MyDatabase
  6. Whenever you need to see the new version you need to File -> Close Database, and repeat step 3, otherwise it can not copy over an in-use database file

I hope this helps you when working with SQLite on Android, it definitely helps me!

InfiniteListView Mono Android

Wednesday, August 31st, 2011

Been working with Mono Android a lot lately and thought I would give a quick example at something that I think is very useful.  I have created a quick example of how to create an InfiniteListView using Mono for Android and the .NET Framework.

It’s pretty simple, but at the same time kind of hard to get right.  You can download the example here. It is commented in the sections that I think are relevant to describe exactly what to watch out for and how/why it is that way.

More Mono Android Debugging Tips

Friday, August 26th, 2011
  1. Install the new Mono Android release 1.0.3, which substantially improves debugging performance and various other problems.  You can read more about it, and download it from here.
  2. If you followed the instructions of my previous post, I have gathered even more information on how to improve the logcat experience.  Create a bat file on your desktop and add the following lines:
cmd /C “C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe” adb shell setprop debug.mono.trace E:all
cmd /K “C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe” logcat

The first command enables verbose logging for mono, note that I do not know if this will decrease performance but I can only imagine it will since it has to log more often and in more places.  If you are testing performance in your app I suggest you do not keep this enabled.  If you are testing memory usage, or you want the FULL details of every single stack trace, crash, etc than definitely keep this setting.

Now to launch logcat I simply double click the bat file, much easier….

Sneak Peek at Next WP7 Game and the future…

Tuesday, December 28th, 2010

Been working very hard on a new Windows Phone 7 game, and at the same time I have pushed two new versions of Hanoi.  I have to admit that I am not doing a very good job at keeping this blog in the loop on what is going on with everything.  With that said, I am going to make 2011 my new attempt at trying to keep things more updated.

To the left is a sneak peek at the new game, it should be out very shortly.  Its about 75% completed, its fully functional, I only need to add some polish to the main menu, etc. If you can guess what it is from that then you definitely get some brownie points! ;)

This will be the last game that I write for a bit, I am going to focus on productivity and business applications for Windows Phone 7  at which point I may switch back and forth between the two of them.  I am also looking into Appcelerator to do some Android and iPhone work.  I have done Objective-C iPhone work in the past, but its pretty time consuming and I have been an Appcelerator beta tester for as before the company even existed but never really used any of their products.

Windows Phone 7 User Agents

Friday, November 19th, 2010

I ran into an issue where I needed to check for a Windows Phone 7 user agent, turns out however, each device has its own unique user agent.  Here are the following user agents I have been able to muster up so far, as of 03/24/2011:

Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) Asus;Galaxy6
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) HTC;WP7
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; Microsoft Corporation; CEPC)
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) LG;LG-E900
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) SAMSUNG;SGH-i707
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; SAMSUNG; Taylor)
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; TOSHIBA; TSUNAGI)
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; SAMSUNG; SGH-i917)
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; XBLWP7; ZuneWP7)
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) LG;LG-E900h)
Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; LG-E900)

If you find any additional ones, post in the comments and I will add it!

Update: For Mango devices and for best practices around using the User Agent string from Windows Phone 7 please read the following Blog post from Microsoft.

WP7 Lessons Learned Part 1: Using IsTrial() Properly

Friday, November 12th, 2010

If you are developing for Windows Phone 7, and planning on publishing to the marketplace it is a good idea to support a trial mode of your product.  You will get a much wider audience if you support trial than if you don’t, Microsoft themselves said they found it increased sales and drove further demand.  In this post I am going to discuss how I went about implementing IsTrial following the Microsoft best practices, how I setup my development environment to make it extremely easy to switch between testing my game in trial mode and paid mode and other best practices to follow.

First and foremost, when using IsTrial() you should be aware of the Microsoft best practices as follows are some generalized ones:

  • Do not rely on usage time limited trials to protect your application’s value.
    Typically, it is best to protect the value of your full mode application by limiting trial access to key code paths. A user may uninstall and retry an application without restriction so a trial design that offers full mode behavior for a limited time provides only inconvenience as a barrier to reuse.
  • Cache license state if you check trial state frequently.
    Note that the IsTrial() and the Guide.IsTrialMode methods are designed to be event-driven. A typical call takes approximately 60 milliseconds or more.
  • Check the IsTrial() state when your application loads or resumes.
    You can avoid some potential trial design flaws especially if you cache the IsTrial() state.
  • Provide a way for users to buy your trial application before the end-of-trial.
  • Make sure that you help users understand why they want to buy your application, perhaps, by implementing your trial limit at a point in the application where they will naturally want more.
    For example, let users experience the first level of game play and require them to purchase the application to play higher levels, retain points, or to connect to a gaming service.

See the Microsoft site for more details.

One of the most important things to pay attention to here is where they state you should “Cache the IsTrial() state..”, since each call to IsTrial() could take up to 60 milliseconds if you call this in a loop or if you call this method often it could potentially affect performance of your application.  It may also cause your application to fail certification if they later enforce restrictions on calling this method a number of times.

For instance in my applicaiton Hanoi, I call IsTrial() one time on startup, and one time on resume.  That’s it, the rest of the time I am reading a cached value and I have found this to be extremely useful.  Here is how I have implemented my version of a Cached version IsTrial().

Here is the following code snippet for how to set it up in your App.cs:

public partial class App : Application    {
public static bool IsTrial = true;

private static void LoadIsTrial()
{
IsTrial = new LicenseInformation().IsTrial();

#if DEBUG_TRIAL
IsTrial = true;
#endif
}

private void Application_Launching(object sender, LaunchingEventArgs e)
{
LoadIsTrial();
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
LoadIsTrial();
}
}

Now in my opinion, this is very elegant because to access the cached variable all you need to do is App.IsTrial and you are accessing the cached version of IsTrial instead of calling the method, and you are guaranteed to have an up to date value.  Next as you may have noticed is I have some #if #endif statements in their, which is what makes debugging this a breeze, also note that when this gets compiled in release mode anything inside those lines will get optimized out of the final code, so theres no chance it will ever run.  I also assumed the IsTrial cached version is equal to True, I do this to play it safe in the event that something does go wrong the user will get the benefit of the doubt.

Below is how to setup Visual Studio so you can easily switch between Trial and Paid:

Note: If you are walking through you may notice I have already setup mine called “Debug_Trial”, just ignore that and follow along with the screen shots.

The first thing you need to do is open up the Configuration Manager in Visual Studio, so click on the drop down where it says “Debug” or “Release” and click “Configuration Manager”

Next, in the configuration manager there is another drop down listing all of the Active Solution Configurations, click on “<New…>”

Next, change the “Copy settings from:” drop down to “Debug” or “Release” whichever one you are creating.  If you are creating a profile to test IsTrial in debug mode than select debug and vice versa, then give it a name. I chose Debug_Trial.

Last but least, since this probably the most important step.  Go to the properties of your project, click the “Configuration” drop down and select the configuration you just created.  Then paste into the “Conditional compilation symbols” box “DEBUG_TRIAL” as it appears above, make sure there is a space between SILVERLIGHT and no leading space.

Now when you need to test your Trial mode, just go to the configuration menu select the “Debug_Trial” and then hit F5 or click Run.  If you want to test the paid, just change it to “Debug” and do the same.  This is the easiest way to implement this that I have found.  Requires nearly zero code, and once you have this setup you never have to touch it again!

Hanoi for WP7 Final Release

Tuesday, October 12th, 2010

After a lot of work, and still more work work down the road the final release of Towers of Hanoi is nearly complete!  A ton of new features have been added and I have a lot of features planned for future releases, I was going to try and submit early into the early access program that Microsoft is offering but I recently decided to go ahead and add the next wave of features to it for final release!

So what will Hanoi have for its final release?

  • 50+ high definition professional backgrounds
  • Theme support for the backgrounds so you can display Winter or Summer or both types of imagery
  • Rich settings support so you can control exactly how you want the game to work, you can turn off the toolbar, adjust how often the background rotates, disable sounds, etc.
  • High definition ambient background music (can be disabled in settings)
  • Speed Hanoi which can be enabled in settings (disabled by default), which allows you to tap the disc and then tap the destination instead of having to drag it.  This is for those who are trying to get the quickest times possible!
  • Time Trials with count down timer for each level so you can best your previous times
  • Local High Scores
  • Ability to select which level you want to play

It will be available for $0.99 USD and will have a Trial Mode, the trial version will be limited to basics.

Trial Version

  • 7 Levels (7 discs)
  • 7 high definition backgrounds (will not rotate during levels, only 1 per level)
  • Local high scores
  • Can not select level to play, must progress from each level

Future Versions

  • Worldwide High Scores so you can compete with other people from around the world
  • Profiles so you can setup different game styles such as “Relaxing” and “Competitive”
  • Different styles and colors of discs and shelves to choose such as marble or metal materials
  • Import your own photos to use as background images, and import your own sounds to use for background music

I will be posting a new video, and screen shots of everything very soon as these new additions progress so keep in touch!

Quick preview of Tic-Tac-Toe for WP7

Tuesday, August 3rd, 2010

So I have been working on lots of Windows Phone 7 games and applications, and so far all of them I plan to sell, albeit cheaply like .99 cents or something around there, but I wanted to make a free one as well just to kind of give something to the community.  So I decided to write a Tic-Tac-Toe game, but I wanted to spruce it up a bit so I put my Photoshop skills to work and I have come up with the following.  This is a quick preview, the game is near completion actually I just need to start putting the bits and pieces together.  If you are curious its play against the computer only right now, the AI is done using MiniMax with some tweaks to make it slightly dumb so you can can actually win a game or two and the computer seems very intelligent at decision making.

I’ll probably add multiple chalk colors and such to make it more fun, this game is obviously aimed at the younger crowd but adults can still have fun with it as well!

Take a look at one my other Windows Phone 7 Games I posted called Towers of Hanoi!

Orientation Bug in Windows Phone 7 Beta Dev Tools

Saturday, July 24th, 2010

Last week I posted a video about a bug I was seeing involving the orientation of the emulator in the Windows PHone 7 Beta tools. Well this has been officially confirmed as a bug that Microsoft is currently looking into, and also confirmed that this issue does not appear to happen on an actual device. This is good news!

Here is the video of the orientation bug I was seeing with on my Hanoi App:

And here is the response from a Microsoft employee I got on the forums staying he was able to reproduce the problem and is looking into it.

Towers of Hanoi for Windows Phone 7

Saturday, June 26th, 2010

So I have been developing a couple Windows Phone 7 Applications in Silverlight in anticipation for the October release of the phone, below is a preview of the first game I have written that is a Towers of Hanoi game.  It was relatively easy to do, only took me total of 2 weeks development time and that was with 2 full time clients at the same time.  I could have easily pulled this off in 3-4 days time, which just goes to show you the sheer amount of power that Silverlight is offering up here on this platform!

So what do you think?  I am going to slowly add features to it overtime but this is the final version of this game.  I have 3 other applications that I am working on for Windows Phone 7 with the goal of release 4 fully fledged apps by October.  Not all of them are games, but most of them will end up probably being games for the time being.

Update: I forgot to mention that I borrowed a bunch of concepts for this game from one of my favorite iPhone Towers of Hanoi games by NimbleBit. If you have an iPhone I seriously recommend getting that version as its awesome!