Sunday 19 October 2014

ISO 8601 week numbering in visual basic 6


I wrote this tiny code snippet to calculate the number of the week according to the ISO 8601 standard.

While almost surely I did not follow the best algorithm available, the code is easy to understand and arises from a couple of wikipedia articles.

   1 Private Function Iso8601WeekNumber(Data As Date) As Integer
   2 
   3     Dim Years53Weeks As String
   4     Dim NumOfWeekFstJan As Integer
   5     Dim WDiff As Integer    
   6            
   7     'here I determine which day of the week is the first of January, see-> http://en.wikipedia.org/wiki/Seven-day_week#Week_numbering 
   8     If Weekday(CDate("01/01/" & year(Data)), vbMonday) = 1 Or _
   9         Weekday(CDate("01/01/" & year(Data)), vbMonday) = 2 Or _
  10          Weekday(CDate("01/01/" & year(Data)), vbMonday) = 3 Or _
  11           Weekday(CDate("01/01/" & year(Data)), vbMonday) = 4 Then
  12           NumOfWeekFstJan = 1
  13     ElseIf Weekday(CDate("01/01/" & year(Data)), vbMonday) = 5 Then
  14                   NumOfWeekFstJan = 53
  15     ElseIf Weekday(CDate("01/01/" & year(Data)), vbMonday) = 7 Then
  16              NumOfWeekFstJan = 52
  17     ElseIf YearHas53Weeks(year(Data) - 1) Then
  18         NumOfWeekFstJan = 53
  19     Else
  20         NumOfWeekFstJan = 52
  21     End If
  22       
  23     Iso8601WeekNumber = NumOfWeekFstJan
  24     
  25     ' number of weeks from Data and the first of January
  26     WDiff = DateDiff("ww", CDate("01/01/" & year(Data)), Data, vbMonday)
  27      
  28     If WDiff > 0 Then
  29         If Iso8601WeekNumber = 1 Then
  30             Iso8601WeekNumber = Iso8601WeekNumber + WDiff
  31         Else ' first of January is within week # 52 0r 53
  32             If WDiff > 0 Then
  33                 Iso8601WeekNumber = WDiff
  34             End If
  35         End If
  36     End If
  37     
  38     If Iso8601WeekNumber = 53 And Month(Data) > 8 And YearHas53Weeks(year(Data)) = False Then
  39         Iso8601WeekNumber = 1
  40     End If    
  41 
  42 End Function
  43 
  44 Private Function YearHas53Weeks(year As Integer) As Boolean
  45     
  46     YearHas53Weeks = False
  47     
  48     If Weekday(CDate("01/01/" & year), vbMonday) = 4 Then
  49         YearHas53Weeks = True
  50     ElseIf Weekday(CDate("01/01/" & year), vbMonday) = 3 And isLeapYear(CInt(year)) Then
  51         YearHas53Weeks = True
  52     End If
  53 
  54 End Function
  55 
  56 'http://www.codeproject.com/Questions/337216/leap-year-problem-in-visual-basic
  57 Function isLeapYear(year As Integer)
  58     isLeapYear = (year Mod 4 = 0 And year Mod 100 <> 0) Or year Mod 400 = 0
  59 End Function
  60 

In the lines 8-21 the routine determines the week number of the first of January. From Wikipedia we read that if the year begins on Monday,Tuesday, Wednesday or Thursay the first of January is in the week number 1, if it begins on Friday the first of January is in the week number 53, if it begins on Sunday the first of January is in the week number 52 and if it begins on Saturday the first of January is in the week number 52 or 53 depending on how many weeks the year before has got.

The function YearHas53Weeks tell us how many weeks are inside an year. The code simply says :
If the year starts on a Thursday or is a leap year that starts on a Wednesday, that year will have 53 weeks.
In the 26-th line I use a VB function to get the number of weeks from the first of January and the date passed as parameter and store the value in the WDiff variable.

Then the code makes some checks, basically it adds WDiff to the week number of the first of January but of course if the 01/01 is on the the 52th or 53th week of the previous year we must add WDiff less 1.

The last check (lines 38-40) states that if we got 53 as the result of the last calculation ad the date that we passed as input is at the end of the year, well that 53 is acceptable only if the year contains 53 week and so, if  it this was not the case, the 53-th becomes the first of the next year.

Monday 6 October 2014

Install programmatically an Android .apk file from qt 5 (liveupdate).

A few days ago I managed to install an .apk file from qt using the Java code I found here http://stackoverflow.com/questions/4967669/android-install-apk-programmatically

I modified the code just to use it within qt 5 using the QAndroidJniObject API which allows to interface your code with Java.

Here it is the resulting code snippet:

   1 QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");   
   2 if (activity.isValid()){
   3     QAndroidJniObject kindOfActivity = QAndroidJniObject::fromString(QLatin1String("android.intent.action.VIEW"));
   4     QAndroidJniObject apkFile = QAndroidJniObject::fromString(QLatin1String("file:///pathToApk/new.apk"));
   5     QAndroidJniObject mimetype = QAndroidJniObject::fromString(QLatin1String("application/vnd.android.package-archive"));
   6     QAndroidJniObject intent("android/content/Intent","(Ljava/lang/String;)V",kindOfActivity.object());
   7     QAndroidJniObject myUri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri","parse","(Ljava/lang/String;)Landroid/net/Uri;",apkFile.object());
   8     intent = intent.callObjectMethod("setDataAndType","(Landroid/net/Uri;Ljava/lang/String;)Landroid/content/Intent;",myUri.object(),mimetype.object());
   9     intent = intent.callObjectMethod("setFlags","(I)Landroid/content/Intent;",0x10000000);
  10     activity.callObjectMethod("startActivity","(Landroid/content/Intent;)V",intent.object());
  11 }

The value 0x10000000  corresponds to the FLAG_ACTIVITY_NEW_TASK constant of the Intent class.

The usage of the QAndroidJniObject class and how to correctly submit the method and parameters signatures are explained here http://qt-project.org/doc/qt-5/qandroidjniobject.html

The lines of code like this:
QAndroidJniObject kindOfActivity = QAndroidJniObject::fromString(QLatin1String("android.intent.action.VIEW"));
 are used to build Java Objects from the correponding Qt counterparts (QString to String in this example).

To use these objects as parameters to any Java method, you must call the .object() method of the QAndroidJniObject class as in
QAndroidJniObject myUri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri","parse","(Ljava/lang/String;)Landroid/net/Uri;",apkFile.object());