javamanager/javainstaller/installer/javasrc.s60/com/nokia/mj/impl/installer/utils/PropertyProvider.java
branchRCL_3
changeset 24 6c158198356e
equal deleted inserted replaced
23:e5618cc85d74 24:6c158198356e
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.installer.utils;
       
    20 
       
    21 /**
       
    22  * Provides notifications whenever the subscribed property value changes.
       
    23  * <br>
       
    24  * One PropertyProvider instance can be used to subscribe events
       
    25  * for only one property. If events for more properties are needed
       
    26  * use separate PropertyProvider instance for each property.
       
    27  *
       
    28  * @see PropertyListener
       
    29  */
       
    30 public class PropertyProvider
       
    31 {
       
    32     /** Property category. */
       
    33     private int iCategory = 0;
       
    34     /** Property key. */
       
    35     private int iKey = 0;
       
    36     /** Property listener. */
       
    37     private PropertyListener iListener = null;
       
    38     /** Handle to native object. */
       
    39     private int iHandle = 0;
       
    40 
       
    41     /**
       
    42      * Constructor.
       
    43      */
       
    44     public PropertyProvider()
       
    45     {
       
    46     }
       
    47 
       
    48     /**
       
    49      * Subscribe to listen changes of specified property value.
       
    50      * One PropertyListener can subscribe to only one property
       
    51      * value at a time.
       
    52      *
       
    53      * @param aCategory property category
       
    54      * @param aKey property key
       
    55      * @param aListener listener to be notified.
       
    56      */
       
    57     public void subscribe(int aCategory, int aKey, PropertyListener aListener)
       
    58     {
       
    59         if (iHandle != 0)
       
    60         {
       
    61             InstallerException.internalError("PropertyProvider already in use.");
       
    62         }
       
    63         iCategory = aCategory;
       
    64         iKey = aKey;
       
    65         iListener = aListener;
       
    66         // Start a new thread which blocks until unsubscribe is called.
       
    67         final PropertyProvider provider = this;
       
    68         new Thread(new Runnable()
       
    69         {
       
    70             public void run()
       
    71             {
       
    72                 synchronized (provider)
       
    73                 {
       
    74                     iHandle = _subscribe(iCategory, iKey);
       
    75                     // Notify subscribe method after subscription
       
    76                     // result is available.
       
    77                     provider.notify();
       
    78                 }
       
    79                 if (iHandle > 0)
       
    80                 {
       
    81                     // Subscription succeeded, start to process events.
       
    82                     _processEvents(iHandle, provider);
       
    83                 }
       
    84             }
       
    85         }, "PropertyProviderThread").start();
       
    86         synchronized (this)
       
    87         {
       
    88             try
       
    89             {
       
    90                 if (iHandle == 0)
       
    91                 {
       
    92                     // Wait until subscription has been completed.
       
    93                     wait();
       
    94                 }
       
    95             }
       
    96             catch (InterruptedException ie)
       
    97             {
       
    98             }
       
    99         }
       
   100         // Check if subscription failed.
       
   101         if (iHandle < 0)
       
   102         {
       
   103             Log.logError("PropertyProvider.subscribe failed with code " + iHandle);
       
   104             iHandle = 0;
       
   105         }
       
   106     }
       
   107 
       
   108     /**
       
   109      * Unubscribe from listening changes.
       
   110      */
       
   111     public void unsubscribe()
       
   112     {
       
   113         final PropertyProvider provider = this;
       
   114         new Thread(new Runnable()
       
   115         {
       
   116             public void run()
       
   117             {
       
   118                 synchronized (provider)
       
   119                 {
       
   120                     if (iHandle <= 0)
       
   121                     {
       
   122                         Log.logWarning(
       
   123                             "PropertyProvider.unsubscribe: no subscription.");
       
   124                         return;
       
   125                     }
       
   126                     int err = _unsubscribe(iHandle);
       
   127                     if (err < 0)
       
   128                     {
       
   129                         Log.logError(
       
   130                             "PropertyProvider.unsubscribe failed with code " +
       
   131                             err);
       
   132                     }
       
   133                     else
       
   134                     {
       
   135                         iHandle = 0;
       
   136                     }
       
   137                 }
       
   138             }
       
   139         }, "PropertyProviderUnsubscribeThread").start();
       
   140     }
       
   141 
       
   142     /**
       
   143      * Called from native when value for the subscribed property changes.
       
   144      */
       
   145     private void valueChanged(int aValue)
       
   146     {
       
   147         if (iListener != null)
       
   148         {
       
   149             iListener.valueChanged(iCategory, iKey, aValue);
       
   150         }
       
   151     }
       
   152 
       
   153     /**
       
   154      * Subscribe to listen changes of specified property value.
       
   155      *
       
   156      * @param aCategory property category
       
   157      * @param aKey property key
       
   158      * @return handle to native side object or Symbian error
       
   159      * code (negative number)
       
   160      */
       
   161     private static native int _subscribe(int aCategory, int aKey);
       
   162 
       
   163     /**
       
   164      * Starts to process events. This call blocks until unsubscribe is called.
       
   165      *
       
   166      * @param aHandle handle to native side object
       
   167      * @param aProvider PropertyProvider class instance to be notified
       
   168      * @return 0 or Symbian error code (negative number)
       
   169      */
       
   170     private static native int _processEvents(int aHandle, PropertyProvider aProvider);
       
   171 
       
   172     /**
       
   173      * Unubscribe from listening changes.
       
   174      *
       
   175      * @param aHandle handle to native side object
       
   176      * @return 0 or Symbian error code (negative number)
       
   177      */
       
   178     private static native int _unsubscribe(int aHandle);
       
   179 }