javamanager/javainstaller/installer/javasrc.s60/com/nokia/mj/impl/installer/applicationregistrator/SifNotifier.java
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Wed, 13 Oct 2010 14:23:59 +0300
branchRCL_3
changeset 83 26b2b12093af
parent 77 7cee158cb8cd
permissions -rw-r--r--
Revision: v2.2.17 Kit: 201041

/*
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/


package com.nokia.mj.impl.installer.applicationregistrator;

import com.nokia.mj.impl.installer.utils.InstallerException;
import com.nokia.mj.impl.installer.utils.Log;

/**
 * Sends installation and uninstallation progress notifications
 * to platform's software installation framework.
 */
public final class SifNotifier
{
    /** Install operation. */
    public static final int OP_INSTALL = 1;
    /** Uninstall operation. */
    public static final int OP_UNINSTALL = 2;
    /** Update operation. */
    public static final int OP_UPDATE = 3;

    /** Indicates installaion or uninstallation without
        specific suboperation. */
    public static final int SUB_OP_NO = 1;
    /** OCSP phase during installation. */
    public static final int SUB_OP_OCSP = 2;
    /** Download phase during installation. */
    public static final int SUB_OP_DOWNLOAD = 3;

    /** Operation being notified. */
    private int iOperation = 0;
    /** Global component id for the application. */
    private String iGlobalComponentId = null;
    /** Component name (i.e. suite name). */
    private String iComponentName = null;
    /** Application names. */
    private String[] iApplicationNames = null;
    /** Applications icons. */
    private String[] iApplicationIcons = null;
    /** Component initial size. */
    private int iComponentSize = 0;
    /** Icon dir. */
    private String iIconDir = null;
    /** Component icon. */
    private String iComponentIcon = null;

    /** Sending progress notifications is only allowed between start
     *  and end notifications. */
    private boolean iNotifyProgressAllowed = false;

    /** Native object handle. */
    private int iHandle = 0;

    /*** ----------------------------- PUBLIC ------------------------------ */

    /**
     * Constructor.
     */
    public SifNotifier()
    {
        init();
    }

    /**
     * Returns true if SIF progress notifications are enabled, false otherwise.
     */
    public static boolean enabled()
    {
        return _sifNotifierEnabled();
    }

    /*
     * Notifies SIF that installation or uninstallation has started.
     *
     * @throws InstallerException in case an error occurs
     */
    public void notifyStart(
        int aOperation, String aGlobalComponentId, String aComponentName,
        String[] aApplicationNames, String[] aApplicationIcons,
        int aComponentSize, String aIconDir, String aComponentIcon)
    {
        iOperation = aOperation;
        iGlobalComponentId = aGlobalComponentId;
        iComponentName = aComponentName;
        iApplicationNames = aApplicationNames;
        iApplicationIcons = aApplicationIcons;
        iComponentSize = aComponentSize;
        iIconDir = aIconDir;
        iComponentIcon = aComponentIcon;

        if (iHandle == 0)
        {
            InstallerException.internalError(
                "SifNotifier.notifyStart: notifier has not been initialized");
        }
        int ret = _notifyStart(
                      iHandle, aGlobalComponentId, aComponentName,
                      aApplicationNames, aApplicationIcons,
                      aComponentSize, aIconDir, aComponentIcon);
        if (ret < 0)
        {
            Log.logError("Notifying SIF start failed with code " + ret +
                         ", " + getInfoString());
            InstallerException.internalError(
                "Notifying SIF start failed with code " + ret);
        }
        iNotifyProgressAllowed = true;
    }

    /**
     * Notifies SIF that installation or uinstallation has ended.
     *
     * @throws InstallerException in case an error occurs
     */
    public void notifyEnd(
        int aErrCategory, int aErrCode, String aErrMsg, String aErrMsgDetails)
    {
        if (iHandle == 0)
        {
            InstallerException.internalError(
                "SifNotifier.notifyEnd: notifier has not been initialized");
        }
        iNotifyProgressAllowed = false;
        int ret = _notifyEnd(
                      iHandle, iGlobalComponentId, aErrCategory, aErrCode,
                      aErrMsg, aErrMsgDetails);
        if (ret < 0)
        {
            Log.logError("Notifying SIF end failed with code " + ret +
                         ", " + getInfoString() +
                         ", ErrCategory: " + aErrCategory +
                         ", ErrCode: " + aErrCode +
                         ", ErrMsg: " + aErrMsg +
                         ", ErrMsgDetails: " + aErrMsgDetails);
            InstallerException.internalError(
                "Notifying SIF end failed with code " + ret);
        }
    }

    /**
     * Notifies SIF about installation or uninstallation progress.
     *
     * @throws InstallerException in case an error occurs
     */
    public void notifyProgress(int aSubOperation, int aCurrent, int aTotal)
    {
        if (!iNotifyProgressAllowed)
        {
            return;
        }
        if (iHandle == 0)
        {
            InstallerException.internalError(
                "SifNotifier.notifyProgress: notifier has not been initialized");
        }
        int ret = _notifyProgress(
                      iHandle, iGlobalComponentId, iOperation, aSubOperation,
                      aCurrent, aTotal);
        if (ret < 0)
        {
            Log.logError("Notifying SIF progress failed with code " + ret +
                         ", " + getInfoString() +
                         ", SubOp: " + aSubOperation +
                         ", Current: " + aCurrent +
                         ", Total: " + aTotal);
            InstallerException.internalError(
                "Notifying SIF progress failed with code " + ret);
        }
    }

    /**
     * Destroys SifNotifier. This method releawse native resources and
     * must be called after SifNotifier is no longer used.
     *
     * @throws InstallerException in case an error occurs
     */
    public void destroy()
    {
        if (iHandle == 0)
        {
            InstallerException.internalError(
                "SifNotifier.destroy: notifier has not been initialized");
        }
        int ret = _destroy(iHandle);
        if (ret < 0)
        {
            InstallerException.internalError(
                "Destroying SIF notifier failed with code " + ret);
        }
        iHandle = 0;
    }

    /*** ----------------------------- PACKAGE ---------------------------- */

    /**
     * Initializes SifNotifier. This method must be called before any
     * other methods are called.
     *
     * @throws InstallerException if the notifier cannot be initialized
     */
    void init()
    {
        int ret = _init();
        if (ret < 0)
        {
            InstallerException.internalError(
                "Initializing SifNotifier failed with code " + ret);
        }
        iHandle = ret;
    }

    /*** ----------------------------- PRIVATE ---------------------------- */

    /**
     * Returns notification info string used in logging.
     */
    private String getInfoString()
    {
        StringBuffer buf = new StringBuffer();
        buf.append("Operation: ").append(iOperation);
        buf.append(", GlobalComponentId: ").append(iGlobalComponentId);
        buf.append(", ComponentName: ").append(iComponentName);
        for (int i = 0; i < iApplicationNames.length; i++)
        {
            buf.append(", ApplicationName: ").append(iApplicationNames[i]);
        }
        buf.append(", ComponentSize: ").append(iComponentSize);
        return buf.toString();
    }

    /*** ----------------------------- NATIVE ----------------------------- */

    /**
     * Returns true if SIF progress notifications are enabled, false otherwise.
     */
    private static native boolean _sifNotifierEnabled();

    /**
     * Notifies SIF about installation/uinstallation start.
     *
     * @param aHandle
     * @param aGlobalComponentId
     * @param aComponentName
     * @param aApplicationNames
     * @param aApplicationIcons
     * @param aComponentSize
     * @param aIconDir
     * @param aComponentIcon
     * @return Symbian error code (negative number) if operation fails,
     * otherwise 0
     */
    private static native int _notifyStart(
        int aHandle, String aGlobalComponentId, String aComponentName,
        String[] aApplicationNames, String[] aApplicationIcons,
        int aComponentSize, String aIconDir, String aComponentIcon);

    /**
     * Notifies SIF about installation/uinstallation completion.
     *
     * @param aHandle
     * @param aGlobalComponentId
     * @param aErrCategory
     * @param aErrCode
     * @param aErrMsg
     * @param aErrMsgDetails
     * @return Symbian error code (negative number) if operation fails,
     * otherwise 0
     */
    private static native int _notifyEnd(
        int aHandle, String aGlobalComponentId,
        int aErrCategory, int aErrCode,
        String aErrMsg, String aErrMsgDetails);

    /**
     * Notifies SIF about installation/uinstallation progress.
     *
     * @param aHandle
     * @param aGlobalComponentId
     * @param aOperation
     * @param aSubOperation
     * @param aCurrent
     * @param aTotal
     * @return Symbian error code (negative number) if operation fails,
     * otherwise 0
     */
    private static native int _notifyProgress(
        int aHandle, String aGlobalComponentId, int aOperation,
        int aSubOperation, int aCurrent, int aTotal);

    /**
     * Initializes SifNotifier. This method must be called before any
     * other methods are called.
     *
     * @return Symbian error code (negative number) if operation fails,
     * otherwise handle to the natie side object
     */
    private static native int _init();

    /**
     * Cleans up SifNotifier. This method must be called after SifNotifier
     * is no longer used.
     *
     * @param aHandle
     * @return Symbian error code (negative number) if operation fails,
     * otherwise 0
     */
    private static native int _destroy(int aHandle);

}