Wednesday, 19 December 2012

Extracting contacts from a .sbu file

A while Back I had my Galaxy phone stolen to which I later bought a new one. However the problem was that the backup I had from the previous phone was in a .sbu file which I could not open or restored using the Kies application. After many tries and Googling, I came across this source edited it the used it to extract the contacts(VCARD data) from the SBU file.
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class SSVCardExtractor {

    private static final String VCARD_FILE_SUFFIX = ".vcf";

    private String outputCharset = "ascii";

    private String inputCharset = "UTF-16LE";

    private InputStream input;

    private VCardWriter output;

    private byte[] start;

    private byte[] end;

    private interface VCardWriter {

        public void write(String vcard) throws IOException;

    }

    public SSVCardExtractor(String inputPath, String outputPath,

            boolean separate, boolean suffixFix) throws IOException {

        if (!separate && suffixFix && !outputPath.endsWith(VCARD_FILE_SUFFIX)) {

            outputPath = outputPath + VCARD_FILE_SUFFIX;

        }


        final File outputFile = new File(outputPath);

        if (outputFile.exists()) {

            throw new IOException("Output path exists: " + outputFile);

        }

        if (separate) {

            outputFile.mkdirs();

            this.output = new VCardWriter() {

                private File directory = outputFile;

                private int index = 0;

                @Override

                public void write(String vcard) throws IOException {

                    File file = new File(directory, ++index + VCARD_FILE_SUFFIX);

                    FileOutputStream stream = new FileOutputStream(file);

                    try {

                        stream.write(vcard.getBytes(outputCharset));

                        stream.flush();

                    } finally {

                        stream.close();

                    }

                }

            };

        } else {

            final String outputPathCopy = new String(outputPath);

            this.output = new VCardWriter() {

                private OutputStream stream = new FileOutputStream(

                        outputPathCopy);

                @Override
                public void write(String vcard) throws IOException {

                    stream.write(vcard.getBytes(outputCharset));

                    stream.flush();

                }


                protected void finalize() throws Throwable {

                    try {

                        stream.close();

                    } catch (Exception e) {

                        // pass

                    }

                }

            };

        }

        this.input = new BufferedInputStream(new FileInputStream(inputPath));

        this.start = "BEGIN:VCARD".getBytes(inputCharset);

        this.end = "END:VCARD".getBytes(inputCharset);

    }

    public void extract() throws IOException {

        while (skipUntil(start, false) != null) {

            byte[] data = skipUntil(end, true);

            if (data == null) {

                throw new IOException("Unexpected end of stream");

            }

            String vcard = new String(start, inputCharset)

                    + new String(data, inputCharset) + "\r\n";

            output.write(vcard);

        }

    }

    private byte[] skipUntil(byte[] bytes, boolean store) throws IOException {

        int searchPosition = 0, current;

        ByteArrayOutputStream data = new ByteArrayOutputStream();

        while ((current = input.read()) >= 0) {

            if (store) {

                data.write(current);

            }

            if ((byte) current == bytes[searchPosition]) {

                if (++searchPosition == bytes.length) {

                    return data.toByteArray();

                }

            } else if (searchPosition != 0) {

                searchPosition = 0;

            }

        }

        return null;

    }


    private static void usageExit(String message) {

        PrintStream out = System.out;

        if (message != null) {

            out.println(message);

        }

        out.println("Usage: java SSVcardExtractor [OPTIONS] <SBU-FILE> <OUTPUT-PATH>");

        out.println("Options:");

        out.println(" -s, --separate       Write each vcard to separate file.");

        out.println("                      By default all vcards are written to one file.");

        out.println("     --no-suffix-fix  Do not add missing "

                + VCARD_FILE_SUFFIX + " suffix.");

        out.println("     --debug          Print debug messages.");

        out.println("     --accept-license Accept license.");

        System.exit(1);

    }

    public static void main(String[] args) {

        boolean separate = false;

        boolean debug = false;

        boolean suffixFix = true;

        boolean acceptLicense = false;

        List<String> arguments = new ArrayList<String>(2);

        for (String argument : args) {

            if (argument.equals("--separate") || argument.equals("-s")) {

                separate = true;

            } else if (argument.equals("--debug")) {

                debug = true;

            } else if (argument.equals("--no-suffix-fix")) {

                suffixFix = false;

            } else if (argument.equals("--accept-license")) {

                acceptLicense = true;

            } else if (argument.startsWith("-")) {

                usageExit("Invalid option: " + argument);

            } else {

                arguments.add(argument);

            }

        }

        if (arguments.size() != 2) {

            usageExit("Invalid argument count");

        }

        try {

            SSVCardExtractor extractor = new SSVCardExtractor(arguments.get(0),

                    arguments.get(1), separate, suffixFix);

            extractor.extract();

            System.out.println("Extract Complete...");

        } catch (IOException e) {

            System.out.println(e.getMessage());

            System.out.flush();

            if (debug) {

                e.printStackTrace();

            }

        }

    }

}






The SBU file is some kind of uncompressed archive file with UTF-16 encoding, all that this code does is to find the VCard start and End tags then writes the VCard items into a file. I then used the generated file to import the contacts to my phone.


How do you use the program?


1. Make sure you have java installed in your machine

2. Copy the code and paste it in a notepad, save the notepad as SSVCardExtractor.java

3. Copy the SBU file in the java bin folder “Program files/java/jdk1.6.0_25/bin”

4. Start the command window

5. Compile the program above as shown;
javac

6. Execute the program to extract the 20121120T112151.sbu which was my backup as follows;

java SSVCardExtractor 20121120T112151.sbu output.vcf

the extract file will be output.vcf

7. You can now use Kies to import the contacts from output.vcf file to your phone.


The Java sdk may be located on a different path on you computer, please confirm that.

Monday, 29 October 2012

Patch Installation error

I ran into a sticky situation while trying to apply a second patch to an application but failed, instead getting an error. The actual scenario was that my product was version 2.0 which was already patched with version 2.1, applying patch 2.2 failed but when patched directly with 2.2, it upgraded successfully.

This is the upgrade error I received while patching;

"The upgrade patch cannot be installed by the Windows Installer service because the program to be upgraded may be missing, or the upgrade patch may update a different version of the program. Verify that the program to be updated exists on your computer and that you have the correct upgrade patch."

Notice the error number is missing, that a fail to installshield.

Among other,s the reason why this error occurs is because the application to upgrade is either not installed, the Upgrade code has changed or the previous patch Package code may be similar to the new patch. In my case I discovered that the patch 2.1 was somehow changing the upgrade code making it impossible for the 2.2 patch to install.

My solution to this issue was to change the Transform Filter Settings on the previous packages page. I changed the ‘Match Upgrade Code’ setting to “NO” and left the “Match Product Code” setting as ‘YES’ this would make sure that the patch only upgrades the product version it is supposed to leaving out the upgrade code validation. This may be a crude method but it fixed my issue in the nick of time.

Here is an image to demonstrate.

image

Thursday, 11 October 2012

How to fix error 1911- Could not register type library for file.

I landed into problems when a patch I had created failed during installation and gave the error “Error 1911. Could not register type library for file x. Contact your support personnel”. This was not the first time I have dealt with the same installation error but this time it was different because it occurred on a patch.

Here are some tip on how you can fix this problem:

This error is likely to occur if the installation package extracts the wrong COM information  i.e an old library version could be registered at build time. The fix  to this is to clean the build machine, this involves using Regclean or any other such tool to remove registry information which could be out-dated or from an older version. Replace the components, register them afresh and redo the build.

If cleaning the build doesn't work, The second approach would be to check if there are double entries in the Typelib table in direct editor. If there is more than one entry, delete the one with the wrong Lib ID. You could also remove the dll from the project, restart the project then re-add the dll again otherwise completely remove the library file if you do not need register the file.

When dealing with a patch, maybe you should try finding out if the developer had broken the components compatibility. Patching components with broken binary compatibility can be a real pain. If true, ask the developer to restore compatibility and recompile the components.

Wednesday, 26 September 2012

Custom exception in C#

When working with large projects with many integrating modules you will find developing custom exceptions imperative. In this post I will demonstrate how to create a simple custom exception.

The main motivation behind creating custom exceptions is that it improves code quality through simplified and improved error handling. Custom exception achieves improved error handling through separating application specific errors and other exception. When developing application modules you get to create object with specific responsibilities, at the same time you think around violations that would occur example invalid data or violation of logical constraints and throw relevant exceptions.

Here is an example of a custom exception for a login module.

Define a class and inherit the Exception class

using System; 
using System.Runtime.Serialization;

namespace LoginException
{
public sealed class LoginException : Exception
{
public LoginException()
: base() { }

public LoginException(string message)
: base(message) { }

public LoginException(string format, params object[] args)
: base(string.Format(format, args)) { }

public LoginExeption(string message, Exception innerException)
: base(message, innerException) { }

public LoginException(string format, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) { }

public LoginException(string message, Boolean LogError ,Exception innerException)
: base(message, innerException) {

if (LogError){
LogMessage(innerException,message);
}
}
public LoginException(string format, Boolean LogError, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) {

if (LogError){
LogMessage(innerException, string.Format(format, args));
}
}


private void LogMessage(Exception ex,string message)
{

try
{
if (!System.IO.Directory.Exists(Properties.Resource1.LogsPath))
System.IO.Directory.CreateDirectory(Properties.Resource1.LogsPath);

//Open a file and save the error message.

System.IO.StreamWriter file = new System.IO.StreamWriter( Properties.Resource1.LogsPath + "\\log " + DateTime.Now.ToString("ddMMyyHHmmss") + ".txt");

file.WriteLine(message + Environment.NewLine + ex.StackTrace);
file.Close();
}
catch
{

}
finally { }
}

}

}





How do we implement the class after defining it:



1. You can use the custom exception with a try catch statements as done below.




try 
{
//Code to execute here
}
catch (LoginException ex)
{
//do something

}









note better that this exception will only catch exception of type LoginException. You can improve on this to handle any other regular exception.




try{

//Code to execute here

}
catch (LoginException ex)
{

//do something

}

catch (Exception ex)
{
throw new LoginException(“error login in”,True,ex)
}



Thursday, 23 August 2012

Creating Good Log Messages

Sometimes a bug emerges at a customer site and the support staff sends me a log session and a set of recreation steps to figure out the bug. With rich log messages, that provide indication of what and where the application failed, am able to understand the bug and ultimately fix it. This has lead me to understand how important log messages are in an application.

There are tones of article emphasising on the importance of good programming practices etc but very little about logging practices. On this post am going to describe a good log message based on my experience.

To start with, a few rules that you should follow when logging;

  1. Log messages should be short and precise.
  2. Avoid spelling mistakes in the logs.
  3. Provide fatality levels in your messages. This will help the support analyst to rate the bug/issues severity.
  4. Avoid fancy characters in the log messages .
  5. Avoid logging your messages to the database, this cost space and reduce performance.
  6. Always have a consistent format for your logs. this helps is keeping uniformity

Here is how a good log message should appear:

ERROR|| DATETIME: 22-Aug-2012 13:24:24 || DESCRIPTION: Could not write to output file 'C:\thisfolder\myfile\report1.txt', 'The directory name is invalid.' || SOURCE: Report.SaveReport || PROCESSID: 3456 || PCNAME: Mark-laptop||  USER: Mark || BUILD VER: 2.1.1 || OS :  Windows 7 Version 6.1 Build 7600

 

LOG MESSAGE BREAK DOWN

You will realise I have used a double slash to segment my message, this is to make it readable. Lets break down the sample message and look at it bit by bit.

SERVERITY– This is the first section of the message indicating ERROR. It is very helpful in determining the importance of the message at a glance.

DATETIME: It is the date and time when the error occurred. This is important in case you would like to know the frequency of error or even tag it to a certain external process or event.

DESCRIPTION:  This is a test message of what happened. This can be the runtime  message generated by the debugger or a custom message by the developer.

SOURCE: The code location that generated this message. This will save you a lot of time figuring out where you need to look in your code. In my case I have used the class_name.method_name format.

PROCESSID: In case you have several processes it good to know which particular one is being a nuisance.

PCNAME: This gives you the machine where the error occurred. You could also provide the server name just incase your logs come from different hosts.

USER: An identifier of the user logged in, this is important if you want to track a users actions within the session. You could use a session id instead of a username like I have done.

BUILD VER: This is the code version running. This is very important as the location in the log may be for a different version of code from what is currently running.

OS: Some bugs are tagged to Operating Systems therefore it is important o know what platform the user was on.

 

Most of the time we rely on the debugger to fix our bugs but suppose a bug occurs in site and you manage to fix it through the information provided by the logs. This is the reason why we should always improve the content to provide as much information as possible.To do this think about what information the log provided, evaluate if was it was helpful and find out if more content be appended to make it even more helpful.

Making the log rich enough may not be your priority because  as the developer you have the debugger,  but it a very important aspect for the support team and the customers too. You will be shocked at how may issues the support team can resolve at site just because they can make out what the problem is. This makes it easier for you as a developer and you can concentrate on more relevant issue.

Those are my thought about log messages. If you may have anything to add please drop me a comment.

Thursday, 26 July 2012

SQLite Database in Android

SQLite is a database embedded in Android OS, making it of course the default Android DB. The reason why it is a perfect choice for most mobile OSes including android is because its compact, configuration is not required, it’s serverless, thus making it a lightweight DB.

What are the things you should know about SQLite before we jump into its implementation on Android:

  1. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements.
  2. Only Three datatypes are supported in SQLite; Text, Integer and real (Double). All other type need to be converted to these types.
  3. Data type integrity is not maintained in SQLite such that you can write an integer into a string column and vice versa.
  4. Referential integrity is not maintained, meaning there is no Foreign Key constrains or Joins statements.
  5. SQLite Full Unicode support is optional and not installed by default.

For demonstration purpose’s, am going to create simple students database application. My Application will contain two tables; Students Table and a Department table.

Student Table columns:

  • StudentID
  • FirstName
  • SurName
  • DateOfBirth
  • DeptID

Department table columnts

  • DeptID
  • DeptName

 

SQLite on Android doesn’t have a management interface to create and manage databases from, so we are going to do this though code. To start with, we shall create a class to handle the Database and table creation. Here is the structure of the class:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class StudentsDataHelper extends SQLiteOpenHelper  {
   
   

//Tables Constants
private static final String Table_Students="Table_Students";
private static final String Table_Dept="Table_Dept";

//Student table columns Constants
private static final String Col_Studentid="StudentID";
private static final String Col_FirstName="FirstName";
private static final String Col_Surname="Surname";
private static final String Col_DateOfBirth="DateOfBirth";
private static final String Col_Stud_DeptID="DeptID";

//Dept table columns Contants
private static final String Col_DeptID="DeptID";
private static final String Col_DeptName="DeptName";

    private static final String DATABASE_NAME = "Students.db";
    private static int DATABASE_VERSION=1;

    public StudentsDataHelper(Context context){
        super(context, DATABASE_NAME,  null, DATABASE_VERSION);

       
    }
   
     @Override
    public void onCreate(SQLiteDatabase db) {
        //TODO Create table Student TABLE
        //TODO Create table Department TABLE
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Drop the Student Table
        //Drop the Department Table
        onCreate(db);
    }
   
}

 

The StudentsDataHelper class Inherits the‘SQLiteOpenHelper’ class . This class provides us with two override methods; onCreate() which is called by the framework when the database doesn’t exist and onUpgrade() called when the database version is increased in the application code, This method allows one to upgrade the database schema.

In the constructor , the Super() method of ‘SQLiteOpenHelper’, requires the following parameters:

  •  Context:-This is the context to which the database is attached.
  •  DatabaseName:- The name of the Database.
  • CursorFactory:- This can either be used as a tandard cursor which acts as a pointer to the results returned from the database. Or a Specialized Cursor done by extending the Cursor class to do additional operations or verifications on the database result.
  • Version:- The version of the database schema. The constructor creates a new blank databse with the specified name and version.

 

OnCreate() Method

On our overriden onCreate() method we shall put the standard SQL statements to create out table and execute them against the Database: Here it is;

@Override
public void onCreate(SQLiteDatabase db) {
     String CREATE_STUDENT_TABLE=String.format("CREATE TABLE IF NOT EXISTS %s " +
             "(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT,%s TEXT,%s TEXT,%s INTEGER)" ,Table_Students ,Col_Studentid, Col_FirstName, Col_Surname, Col_DateOfBirth,Col_Stud_DeptID);
    
     db.execSQL(CREATE_STUDENT_TABLE);
    
     String CREATE_DEPT_TABLE=String.format("CREATE TABLE IF NOT EXISTS %s (%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT)" ,Table_Dept ,Col_DeptID,Col_DeptName);
    
     db.execSQL(CREATE_DEPT_TABLE);
    
}

 

On this method you can create the tables, views and triggers. It is usually invoked only once when the database does not exist or the first time the application is ran. You will also realise later that I have invoked the method gain during the upgrade.

 

 onUpgrade() Method

Sometimes you want to change the Database schema; Add new table, Add columns, change datatypes. to do this you will require to override the onUpgrade() method:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Table_Students);
    db.execSQL("DROP TABLE IF EXISTS " + Table_Dept);
   
    //Drop any view or triggers if any
    onCreate(db);
}


In our method, we drop all the tables and the call the method onCreate()  to recreate our schema again. For this method to be executed the database version in the constructor will have to change.

private static int DATABASE_VERSION=2;

For my simple case here, I have dropped all the table but what if there is data that you want to conserve in the tables? You can create temporary tables and preserve the data  then retrieve it after you are done with the upgrade.

Friday, 29 June 2012

Where should new items appear in a listview?

Today I got a bug logged from QA which got me thinking a little bit.  To start with, the bug was about where the new items were appearing after adding them to the list. I have a listview where I the user add, edit and order items, however, whenever a new item is added, it appears at the top of the list. This is one of those issues you throw to the Design Analyst but then I decided to think through it though there was a potential risk of the designer saying “No this is not how it should be..”.

Back to my Issue; What is the best practice while adding items to a listview, should the item appear at the bottom or at the top of the a list?
I think the answer to this highly depends on the several things, One of the things is the sorting order. If the list has an existing sort, then it should be maintained; example if an associated timestamp (assuming that's the order) is chronological, then the new item will appear at the button and vice versa for the reverse chronological ordering. So if there is a sorting order then insert according to the sorting rules. In situations where the user has scrolled to a point that the first/last items cannot be viewed then auto-scroll to the new item and put the new item into context. The same should apply if the list is paged.

If you consider a scenario where the sort is not applied, then you might want to check what the other parts of the application do. This will allow you to maintain conformity within the application,  in addition, the user can intuitively tell where to find a new entry because of the consistency all through. This how I fixed my bug.

Some schools of thought would argue that adding the item at the top would be a good idea. It gives the user an immediate feedback that the item has been added to the list and reduces on the challenge of keeping focus at the bottom of the list.