javacommons/utils/src.s60/properties.cpp
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <memory>
       
    20 #include <jni.h>
       
    21 #include <f32file.h>
       
    22 #include <sysversioninfo.h>
       
    23 #include <versioninfo.h>
       
    24 #include <sysutil.h>
       
    25 #include <hal.h>
       
    26 
       
    27 #ifdef RD_JAVA_UI_QT
       
    28 
       
    29 #include <QLocale>
       
    30 
       
    31 #else // RD_JAVA_UI_QT
       
    32 
       
    33 #include <bautils.h>
       
    34 #include <barsc.h>
       
    35 #include <avkon.rsg>
       
    36 #include <AknUtils.h>
       
    37 
       
    38 #endif // RD_JAVA_UI_QT
       
    39 
       
    40 #include "javaenvinfo.h"
       
    41 #include "commonproperties.h"
       
    42 #include "javacommonutils.h"
       
    43 #include "exceptionbase.h"
       
    44 #include "logger.h"
       
    45 #include "s60commonutils.h"
       
    46 
       
    47 using namespace java::util;
       
    48 
       
    49 jstring getS60LocaleL(JNIEnv* env);
       
    50 
       
    51 #ifndef RD_JAVA_UI_QT
       
    52 
       
    53 HBufC* getS60LocaleTempImplL();
       
    54 
       
    55 #endif // RD_JAVA_UI_QT
       
    56 
       
    57 HBufC* MicroEditionPlatformL();
       
    58 TInt getHardwareVersionL(TDes& aHardwareType);
       
    59 
       
    60 const TUint KBufferSize = 32;
       
    61 _LIT(KMicroeditionPlatform,                     "S60");
       
    62 _LIT(KMicroeditionPlatformValueDefaultPrefix,   "Series60");
       
    63 _LIT(KMicroeditionPlatformName,      "sw_platform=");
       
    64 _LIT(KMicroeditionPlatformVersion,   "sw_platform_version=");
       
    65 _LIT(KMicroeditionPlatformJavaVersion, "java_build_version=");
       
    66 
       
    67 jstring java::util::getLocaleImpl(JNIEnv* env)
       
    68 {
       
    69     JELOG2(EUtils);
       
    70 
       
    71 #ifdef RD_JAVA_UI_QT
       
    72 
       
    73     QString localeName = QLocale::system().name();
       
    74 
       
    75     jstring loc = env->NewString(localeName.utf16(), localeName.size());
       
    76     if (!loc)
       
    77     {
       
    78         std::bad_alloc();
       
    79     }
       
    80     return loc;
       
    81 
       
    82 #else // RD_JAVA_UI_QT
       
    83     jstring loc = 0;
       
    84     HBufC* buf = 0;
       
    85     bool usingTempSolution = false;
       
    86     TRAPD(err, buf = AknLangUtils::DisplayLanguageTagL());
       
    87     if (buf == 0 && err == KErrNotSupported)
       
    88     {
       
    89         //At the moment DisplayLanguageTagL must be called from
       
    90         //UI thread. Once this is fixed by the Avkon, we must
       
    91         //use temporary solution.
       
    92         usingTempSolution = true;
       
    93         TRAP(err, buf = getS60LocaleTempImplL());
       
    94     }
       
    95     if (buf == 0)
       
    96     {
       
    97         std::string errorStr = "Could not solve locale when using ";
       
    98         if (usingTempSolution)
       
    99         {
       
   100             errorStr.append("temp");
       
   101         }
       
   102         else
       
   103         {
       
   104             errorStr.append("original");
       
   105         }
       
   106         errorStr.append(" solution. Leave code = ");
       
   107         errorStr.append(JavaCommonUtils::intToString(err));
       
   108         throw ExceptionBase(errorStr,
       
   109                             __FILE__,__FUNCTION__,__LINE__);
       
   110     }
       
   111     const jchar* stringPtr = buf->Ptr();
       
   112     const jsize stringLength = buf->Length();
       
   113     loc = env->NewString(stringPtr, stringLength);
       
   114     delete buf;
       
   115     return loc;
       
   116 
       
   117 #endif // RD_JAVA_UI_QT
       
   118 }
       
   119 
       
   120 jstring java::util::getPlatformImpl(JNIEnv* aEnv)
       
   121 {
       
   122     JELOG2(EUtils);
       
   123 
       
   124 
       
   125     jstring platform = NULL;
       
   126     std::auto_ptr<HBufC> propertyValue(0);
       
   127 
       
   128     TRAP_IGNORE(propertyValue.reset(MicroEditionPlatformL()));
       
   129     if (propertyValue.get())
       
   130     {
       
   131         platform = S60CommonUtils::NativeToJavaString(*aEnv,*propertyValue.get());
       
   132     }
       
   133     return platform;
       
   134 }
       
   135 
       
   136 #ifndef RD_JAVA_UI_QT
       
   137 
       
   138 HBufC* getS60LocaleTempImplL()
       
   139 {
       
   140     JELOG2(EUtils);
       
   141     _LIT(KFileName, "z:\\resource\\avkon.rsc");
       
   142     TFileName fileName(KFileName);
       
   143     RFs iRFs;
       
   144     CleanupClosePushL(iRFs);
       
   145     User::LeaveIfError(iRFs.Connect());
       
   146     BaflUtils::NearestLanguageFile(iRFs, fileName);
       
   147     RResourceFile resourceFile;
       
   148     CleanupClosePushL(resourceFile);
       
   149     resourceFile.OpenL(iRFs, fileName);
       
   150     resourceFile.ConfirmSignatureL(0);
       
   151     HBufC8* textBuf8 = resourceFile.AllocReadLC(R_QTN_LANGUAGE_RFC3066_TAG);
       
   152     const TPtrC16 ptrBuf8((TText16*) textBuf8->Ptr(), (textBuf8->Length() + 1) >> 1);
       
   153     HBufC16* tag = ptrBuf8.AllocL();
       
   154     CleanupStack::PopAndDestroy(3);
       
   155     return tag;
       
   156 
       
   157 }
       
   158 
       
   159 #endif // RD_JAVA_UI_QT
       
   160 
       
   161 HBufC* GetPlatformVersionL()
       
   162 {
       
   163     VersionInfo::TPlatformVersion platformVersion;
       
   164     User::LeaveIfError(VersionInfo::GetVersion(platformVersion));
       
   165     TBuf<KBufferSize> version;
       
   166 
       
   167     version.AppendNum(platformVersion.iMajorVersion);
       
   168     version.Append('.');
       
   169     version.AppendNum(platformVersion.iMinorVersion);
       
   170 
       
   171     return version.AllocL();
       
   172 }
       
   173 
       
   174 HBufC* MicroEditionPlatformL()
       
   175 {
       
   176     // First try to get the actual device name
       
   177     // If the device name is not found the default value is used: Series60/
       
   178     HBufC *productName= NULL;
       
   179 
       
   180     TBuf<KBufferSize> hardwareType(0);
       
   181     int retCode = getHardwareVersionL(hardwareType);
       
   182 
       
   183     if (retCode == KErrNone && hardwareType.Length() > 0)
       
   184     {
       
   185         TInt slashAfterManufacturer = hardwareType.Locate('/');
       
   186         TInt spaceAfterManufacturer = hardwareType.Locate(' ');
       
   187         TPtrC productNameShort = hardwareType;
       
   188 
       
   189         if ((slashAfterManufacturer != KErrNotFound) ||
       
   190                 (spaceAfterManufacturer != KErrNotFound))
       
   191         {
       
   192             if (slashAfterManufacturer == KErrNotFound)
       
   193             {
       
   194                 productNameShort.Set(hardwareType.Left(
       
   195                                          spaceAfterManufacturer));
       
   196             }
       
   197             else if (spaceAfterManufacturer == KErrNotFound)
       
   198             {
       
   199                 productNameShort.Set(hardwareType.Left(
       
   200                                          slashAfterManufacturer));
       
   201             }
       
   202             else
       
   203             {
       
   204                 productNameShort.Set(hardwareType.Left(
       
   205                                          slashAfterManufacturer < spaceAfterManufacturer ?
       
   206                                          slashAfterManufacturer :
       
   207                                          spaceAfterManufacturer));
       
   208             }
       
   209         }
       
   210 
       
   211         productName = HBufC::NewL(productNameShort.Length());
       
   212         productName->Des().Copy(productNameShort);
       
   213     }
       
   214 
       
   215     if (!productName)
       
   216     {
       
   217         productName = KMicroeditionPlatformValueDefaultPrefix().AllocLC();
       
   218     }
       
   219     else
       
   220     {
       
   221         CleanupStack::PushL(productName);
       
   222     }
       
   223 
       
   224     HBufC* platformName = NULL;
       
   225 
       
   226     TBuf<KSysUtilVersionTextLength> swVersion;
       
   227     if (SysUtil::GetSWVersion(swVersion) == KErrNone && swVersion.Length()>0)
       
   228     {
       
   229         // Assume syntax V xx.xx\ndd-mm-yy\nNHL-vvv\n(c)Vendor
       
   230         TInt NLloc = swVersion.Locate('\n');
       
   231         TPtrC verPtr = swVersion.Mid(2, NLloc-2); // skip "V " in the beginning
       
   232 
       
   233         // Get platform minor and major version numbers
       
   234         HBufC* platformVersionValue = GetPlatformVersionL();
       
   235         CleanupStack::PushL(platformVersionValue);
       
   236 
       
   237         // Get platform properties and values
       
   238         HBufC* platformNameProperty = KMicroeditionPlatformName().AllocLC();
       
   239         HBufC* platformNameValue = KMicroeditionPlatform().AllocLC();
       
   240         HBufC* platformVersionProperty = KMicroeditionPlatformVersion().AllocLC();
       
   241         HBufC* platformJavaVersionProperty = KMicroeditionPlatformJavaVersion().AllocLC();
       
   242 
       
   243         // Get Java version number
       
   244         TVersion versionInfo = Java::JavaEnvInfo::Version();
       
   245         TVersionName versionName;
       
   246         versionName.AppendNum(versionInfo.iMajor);
       
   247         versionName.Append('.');
       
   248         versionName.AppendNum(versionInfo.iMinor);
       
   249         versionName.Append('.');
       
   250         versionName.AppendNum(versionInfo.iBuild);
       
   251         HBufC* platformJavaVersionValue = versionName.AllocL();
       
   252         CleanupStack::PushL(platformJavaVersionValue);
       
   253 
       
   254         // Calculate length of property string
       
   255         TUint propertyLength = productName->Length() + 1 + verPtr.Length() + 1;
       
   256         propertyLength += platformNameProperty->Length();
       
   257         propertyLength += platformNameValue->Length() + 1;
       
   258         propertyLength += platformVersionProperty->Length();
       
   259         propertyLength += platformVersionValue->Length() + 1;
       
   260         propertyLength += platformJavaVersionProperty->Length();
       
   261         propertyLength += platformJavaVersionValue->Length();
       
   262 
       
   263         // Concatenate properties
       
   264         platformName = HBufC::NewL(propertyLength);
       
   265         platformName->Des().Append(*productName);
       
   266         platformName->Des().Append('/');
       
   267         platformName->Des().Append(verPtr);
       
   268         platformName->Des().Append('/');
       
   269         platformName->Des().Append(*platformNameProperty);
       
   270         platformName->Des().Append(*platformNameValue);
       
   271         platformName->Des().Append(';');
       
   272         platformName->Des().Append(*platformVersionProperty);
       
   273         platformName->Des().Append(*platformVersionValue);
       
   274         platformName->Des().Append(';');
       
   275         platformName->Des().Append(*platformJavaVersionProperty);
       
   276         platformName->Des().Append(*platformJavaVersionValue);
       
   277 
       
   278         CleanupStack::PopAndDestroy(platformJavaVersionValue);
       
   279         CleanupStack::PopAndDestroy(platformJavaVersionProperty);
       
   280         CleanupStack::PopAndDestroy(platformVersionProperty);
       
   281         CleanupStack::PopAndDestroy(platformNameValue);
       
   282         CleanupStack::PopAndDestroy(platformNameProperty);
       
   283         CleanupStack::PopAndDestroy(platformVersionValue);
       
   284     }
       
   285 
       
   286     CleanupStack::PopAndDestroy(productName);
       
   287     return platformName;
       
   288 }
       
   289 
       
   290 jint java::util::getFreeMemoryImpl(JNIEnv*)
       
   291 {
       
   292     JELOG2(EUtils);
       
   293     int freeMemory = -1;
       
   294     HAL::Get(HAL::EMemoryRAMFree, freeMemory);
       
   295     return freeMemory;
       
   296 }
       
   297 
       
   298 TInt getHardwareVersionL(TDes& aHardwareType)
       
   299 {
       
   300     SysVersionInfo::TProductVersion pVersion;
       
   301 
       
   302     RFs fs;
       
   303     User::LeaveIfError(fs.Connect());
       
   304     CleanupClosePushL(fs);
       
   305     TInt rc = SysVersionInfo::GetVersionInfo(pVersion, fs);
       
   306 
       
   307     if (rc == KErrNone)
       
   308     {
       
   309         aHardwareType.Append(pVersion.iManufacturer);
       
   310         aHardwareType.Append(pVersion.iModel);
       
   311         aHardwareType.Append('/');
       
   312         aHardwareType.Append(pVersion.iProduct);
       
   313         aHardwareType.Append(pVersion.iRevision);
       
   314         aHardwareType.Append(';');
       
   315     }
       
   316 
       
   317     CleanupStack::PopAndDestroy(&fs);
       
   318     return rc;
       
   319 }
       
   320