javamanager/javacaptain/extensionplugins/autostarter/src.s60/autostarter.cpp
branchRCL_3
changeset 83 26b2b12093af
parent 77 7cee158cb8cd
child 84 0553e2305d00
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
     1 /*
       
     2 * Copyright (c) 2010 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:  autostarter is Java Captain Symbian plugin that starts
       
    15 *               auto start Java applications after they have been installed
       
    16 *               and when the device boots.
       
    17 *
       
    18 */
       
    19 
       
    20 #include <apgcli.h> // for RApaLsSession
       
    21 #include <e32base.h>
       
    22 #include <f32file.h>
       
    23 #include <javastorage.h>
       
    24 #include <javastorageentry.h>
       
    25 #include <javastoragenames.h>
       
    26 #include <memory>
       
    27 
       
    28 #include "commsendpoint.h"
       
    29 #include "coreinterface.h"
       
    30 #include "javaprocessconstants.h"
       
    31 #include "javasymbianoslayer.h"
       
    32 #include "javauid.h"
       
    33 #include "javauids.h"
       
    34 #include "logger.h"
       
    35 #include "preinstallerstartermessages.h"
       
    36 #include "rtcinterface.h"
       
    37 #include "rtcmessages.h"
       
    38 
       
    39 #include "autostarter.h"
       
    40 
       
    41 
       
    42 using namespace java::storage;
       
    43 using namespace java::util;
       
    44 
       
    45 
       
    46 /**
       
    47  * Return pointer to ExtensionPluginInterface implementation for this
       
    48  * extension dll
       
    49  */
       
    50 java::captain::ExtensionPluginInterface* getExtensionPlugin()
       
    51 {
       
    52     return new java::captain::AutoStarter();
       
    53 }
       
    54 
       
    55 namespace java  // codescanner::namespace
       
    56 {
       
    57 namespace captain  // codescanner::namespace
       
    58 {
       
    59 
       
    60 /**
       
    61  * Empty contructor
       
    62  */
       
    63 AutoStarter::AutoStarter() : mCore(0)
       
    64 {
       
    65 }
       
    66 
       
    67 /**
       
    68  * Empty destructor
       
    69  */
       
    70 AutoStarter::~AutoStarter()
       
    71 {
       
    72 }
       
    73 
       
    74 /**
       
    75  * Implement PluginInterface method
       
    76  */
       
    77 void AutoStarter::startPlugin(CoreInterface* core)
       
    78 {
       
    79     LOG(EJavaCaptain, EInfo, "AutoStarter plugin started");
       
    80 
       
    81     mCore = core;
       
    82 }
       
    83 
       
    84 /**
       
    85  * Implement PluginInterface method
       
    86  */
       
    87 void AutoStarter::stopPlugin()
       
    88 {
       
    89     mCore = 0;
       
    90 }
       
    91 
       
    92 /**
       
    93  * Implement ExtensionPluginInterface method
       
    94  */
       
    95 EventConsumerInterface* AutoStarter::getEventConsumer()
       
    96 {
       
    97     return this;
       
    98 }
       
    99 
       
   100 /**
       
   101  * Handle Java Captain events sent by Boot event provider or
       
   102  * preinstaller starter
       
   103  *
       
   104  * Implement EventConsumerInterface method
       
   105  */
       
   106 void AutoStarter::event(const std::string& eventProvider,
       
   107                        java::comms::CommsMessage& aMsg)
       
   108 {
       
   109     LOG1(EJavaCaptain, EInfo, "AutoStarter::event received, id %d", aMsg.getMessageId());
       
   110 
       
   111     if (eventProvider == PREINSTALLER_EVENT_PROVIDER)
       
   112     {
       
   113         int exitStatus = 0;
       
   114         getPreinstallerExitedMessageParams(aMsg, exitStatus);
       
   115         LOG1(
       
   116             EJavaCaptain,
       
   117             EInfo,
       
   118             "AutoStarter::event() preinstaller exited event received (status=%d)",
       
   119             exitStatus);
       
   120 
       
   121             // Start auto-start MIDlets
       
   122             TRAPD(err, checkMIDletsToBeStartedL())
       
   123             if (KErrNone != err)
       
   124             {
       
   125                 ELOG1(EJavaCaptain, "AutoStarter::checkMIDletsToBeStartedL: leaved (%d)", err);
       
   126             }
       
   127     }
       
   128 }
       
   129 
       
   130 /**
       
   131  *
       
   132  *
       
   133  */
       
   134 void AutoStarter::checkMIDletsToBeStartedL()
       
   135 {
       
   136     // Find all MIDlets with Nokia-MIDlet-auto-start
       
   137     // APPLICATION_TABLE, if (AUTORUN == AUTOSTART_TRUE) || (AUTORUN == AUTOSTART_ONCE),
       
   138     // call startMIDletL(ID)
       
   139 
       
   140     LOG(EJavaCaptain, EInfo, "AutoStarter::checkMIDletsToBeStartedL called");
       
   141 
       
   142     std::auto_ptr<JavaStorage> js(JavaStorage::createInstance());
       
   143     try
       
   144     {
       
   145         js->open(JAVA_DATABASE_NAME);
       
   146 
       
   147         // In Java Storage there is 'APPLICATION_TABLE' table that contains
       
   148         // the Uid of the application in ID column. The value in column AUTORUN is
       
   149         // one of the following AUTOSTART_FALSE (0), AUTOSTART_TRUE (1) or
       
   150         // AUTOSTART_ONCE (2)
       
   151 
       
   152         JavaStorageEntry attribute;
       
   153         JavaStorageApplicationEntry_t findPattern;
       
   154         JavaStorageApplicationList_t  foundEntries;
       
   155 
       
   156         // Get all IDs from APPLICATION_TABLE where AUTORUN is AUTOSTART_TRUE
       
   157         attribute.setEntry(AUTORUN, L"1");
       
   158         findPattern.insert(attribute);
       
   159         attribute.setEntry(ID, L"");
       
   160         findPattern.insert(attribute);
       
   161 
       
   162         js->search(APPLICATION_TABLE, findPattern, foundEntries);
       
   163         findPattern.clear();
       
   164 
       
   165         // Iterate through all found applications
       
   166         JavaStorageApplicationList_t::const_iterator appIterator;
       
   167         for (appIterator = foundEntries.begin(); appIterator != foundEntries.end(); ++appIterator)
       
   168         {
       
   169             std::wstring applicationId = appIterator->begin()->entryValue();
       
   170 
       
   171             LOG1WSTR(EJavaCaptain, EInfo,
       
   172                 "AutoStarter::checkMIDletsToBeStartedL : going to start MIDlet, uid is %s",
       
   173                 applicationId);
       
   174 
       
   175             startMIDletL(applicationId);
       
   176         }
       
   177         foundEntries.clear();
       
   178 
       
   179 
       
   180         // Get all IDs from APPLICATION_TABLE where AUTORUN is AUTOSTART_ONCE
       
   181         attribute.setEntry(AUTORUN, L"2");
       
   182         findPattern.insert(attribute);
       
   183         attribute.setEntry(ID, L"");
       
   184         findPattern.insert(attribute);
       
   185 
       
   186         js->search(APPLICATION_TABLE, findPattern, foundEntries);
       
   187         findPattern.clear();
       
   188 
       
   189         // Iterate through all found applications
       
   190         for (appIterator = foundEntries.begin(); appIterator != foundEntries.end(); ++appIterator)
       
   191         {
       
   192             std::wstring applicationId = appIterator->begin()->entryValue();
       
   193 
       
   194             LOG1WSTR(EJavaCaptain, EInfo,
       
   195                 "AutoStarter::checkMIDletsToBeStartedL : going to start MIDlet (once), uid is %s",
       
   196                 applicationId);
       
   197 
       
   198             if (startMIDletL(applicationId))
       
   199             {
       
   200                 // after starting MIDlet must set AUTORUN value to AUTOSTART_FALSE
       
   201                 JavaStorageApplicationEntry_t matchEntries;
       
   202 
       
   203                 JavaStorageApplicationEntry_t updateEntries;
       
   204                 JavaStorageEntry attribute;
       
   205 
       
   206                 attribute.setEntry(ID, applicationId);
       
   207                 matchEntries.insert(attribute);
       
   208 
       
   209                 attribute.setEntry(AUTORUN, L"0");
       
   210                 updateEntries.insert(attribute);
       
   211 
       
   212                 js->update(APPLICATION_TABLE, updateEntries, matchEntries);
       
   213             }
       
   214         }
       
   215         foundEntries.clear();
       
   216     }
       
   217     catch (JavaStorageException& e)
       
   218     {
       
   219         ELOG1(EJavaCaptain,
       
   220             "AutoStarter::checkMIDletsToBeStartedL: Java Storage exception %s", e.what());
       
   221     }
       
   222 
       
   223     try
       
   224     {
       
   225         js->close();
       
   226     }
       
   227     catch (JavaStorageException& e2)
       
   228     {
       
   229         WLOG1(EJavaCaptain,
       
   230             "AutoStarter::checkMIDletsToBeStartedL: Java Storage exception "
       
   231             "when closing storage %s", e2.what());
       
   232     }
       
   233 }
       
   234 
       
   235 
       
   236 /**
       
   237  *
       
   238  *  @return true if launch succeeded, false is starting failed
       
   239  */
       
   240 bool AutoStarter::startMIDletL(const std::wstring& aUid)
       
   241 {
       
   242     // Try to check whether the MIDlet is present (or whether it has been
       
   243     // installed to a removable media that is not present now)
       
   244     if (!isMIDletPresent(aUid))
       
   245     {
       
   246         return false;
       
   247     }
       
   248 
       
   249     // Start MIDlet
       
   250     rtcLaunchInfo launchInfo(aUid);
       
   251 
       
   252     bool launchSuccess = mCore->getRtc()->launch(launchInfo);
       
   253     if (!launchSuccess)
       
   254     {
       
   255         ELOG(EJavaCaptain, "AutoStarter::startMIDletL: launching MIDlet failed");
       
   256     }
       
   257 
       
   258     return launchSuccess;
       
   259 }
       
   260 
       
   261 
       
   262 /**
       
   263  * Try to check whether the MIDlet is present (or whether it has been
       
   264  * installed to a removable media that is not present now)
       
   265  *
       
   266  * @return false if it is certain that the MIDlet is not present, true otherwise
       
   267  */
       
   268 bool AutoStarter::isMIDletPresent(const std::wstring& aUid)
       
   269 {
       
   270     RApaLsSession apaSession;
       
   271     TInt err = apaSession.Connect();
       
   272     if (KErrNone != err)
       
   273     {
       
   274         // Cannot check presence from AppArc, try to start the MIDlet anyway
       
   275         ELOG1(EJavaCaptain,
       
   276               "AutoStarter::isMIDletPresent: RApaLsSession Connect error %d", err);
       
   277         return true;
       
   278     }
       
   279     CleanupClosePushL(apaSession);
       
   280 
       
   281     TUid appUid;
       
   282     Uid javaUid(aUid);
       
   283     err = uidToTUid(javaUid, appUid);
       
   284     if (KErrNone != err)
       
   285     {
       
   286         WLOG1(EJavaCaptain,
       
   287             "AutoStarter::isMIDletPresent: Cannot convert %S to TUid", aUid.c_str());
       
   288         CleanupStack::PopAndDestroy(&apaSession); // apaSession
       
   289         return true;
       
   290     }
       
   291 
       
   292     TUid appTypeUid;
       
   293     err = apaSession.GetAppType(appTypeUid, appUid);
       
   294     if (KErrNone != err)
       
   295     {
       
   296         if (KErrNotFound == err)
       
   297         {
       
   298             // The application is not present
       
   299             WLOG1(EJavaCaptain,
       
   300                 "AutoStarter::isMIDletPresent: trying to start MIDlet %S that is not present",
       
   301                 aUid.c_str());
       
   302             CleanupStack::PopAndDestroy(&apaSession); // apaSession
       
   303             return false;
       
   304         }
       
   305 
       
   306         // Cannot check presence from AppArc, try to start the MIDlet anyway
       
   307         ELOG1(EJavaCaptain,
       
   308             "AutoStarter::isMIDletPresent: RApaLsSession GetAppType error %d", err);
       
   309         CleanupStack::PopAndDestroy(&apaSession); // apaSession
       
   310         return true;
       
   311     }
       
   312     else if (appTypeUid.iUid != KMidletApplicationTypeUid)
       
   313     {
       
   314         // The application is present but it is NOT a MIDlet
       
   315         WLOG1(EJavaCaptain,
       
   316             "AutoStarter::isMIDletPresent: tried to start application %S that is not MIDlet",
       
   317             aUid.c_str());
       
   318         CleanupStack::PopAndDestroy(&apaSession); // apaSession
       
   319         return false;
       
   320     }
       
   321 
       
   322     CleanupStack::PopAndDestroy(&apaSession); // apaSession
       
   323     return true;
       
   324 }
       
   325 
       
   326 
       
   327 } // namespace captain
       
   328 } // namespace java