javauis/nokiauiapi_qt/javasrc/com/nokia/mj/impl/nokiauiapi/SoftNotificationImpl.java
author hgs
Mon, 04 Oct 2010 11:29:25 +0300
changeset 78 71ad690e91f5
permissions -rw-r--r--
v2.2.17_1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     1
/*
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     2
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     3
 * All rights reserved.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     5
 * under the terms of "Eclipse Public License v1.0"
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     8
 *
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
     9
 * Initial Contributors:
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    11
 *
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    12
 * Contributors:
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    13
 *
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    14
 * Description:
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    15
 *
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    16
 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    17
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    18
package com.nokia.mj.impl.nokiauiapi;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    19
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    20
import java.io.ByteArrayInputStream;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    21
import java.io.IOException;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    22
import java.io.OutputStream;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    23
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    24
import org.eclipse.swt.internal.extension.DisplayExtension;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    25
import org.eclipse.swt.widgets.Internal_PackageSupport;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    26
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    27
import com.nokia.mj.impl.nokiauiapi.OS;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    28
import com.nokia.mid.ui.SoftNotificationException;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    29
import com.nokia.mid.ui.SoftNotificationListener;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    30
import com.nokia.mj.impl.fileutils.FileUtility;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    31
import com.nokia.mj.impl.rt.support.Finalizer;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    32
import com.nokia.mj.impl.rt.support.ApplicationInfo;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    33
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    34
/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    35
 * SoftNotificationImpl implements the functionality specified in
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    36
 * com.nokia.mid.ui.SoftNotification class.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    37
 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    38
public final class SoftNotificationImpl extends com.nokia.mid.ui.SoftNotification {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    39
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    40
	// Native CSoftNotification handle
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    41
	private int iSoftNotificationHandle = 0;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    42
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    43
	// Observer of this class set by the API user. An array is used to allow
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    44
	// listener synchronization.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    45
	private SoftNotificationListener[] iListener;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    46
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    47
	// Listener event when user accepted notification
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    48
	private final static int EVENT_ACCEPT = 1;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    49
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    50
	// Listener event when user dismissed notification
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    51
	private final static int EVENT_DISMISS = 2;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    52
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    53
	// Error message prefix for exceptions.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    54
	private final static String ERROR_MESSAGE_PREFIX = "Failed to ";
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    55
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    56
	// The class will have native resources so register it for
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    57
	// finalization.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    58
	private Finalizer mFinalizer;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    59
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    60
	private int err;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    61
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    62
	private int id;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    63
	
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    64
	private int midletUid;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    65
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    66
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    67
	 * Constructor. New instance with old identifier.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    68
	 * 
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    69
	 * @param aNotificationId
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    70
	 *            Identification of previous soft notification.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    71
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    72
	public SoftNotificationImpl(int aNotificationId) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    73
		initialize(aNotificationId);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    74
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    75
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    76
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    77
	 * Constructor. New instance.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    78
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    79
	public SoftNotificationImpl() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    80
		initialize(0);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    81
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    82
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    83
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    84
	 * Initializes the instance.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    85
	 * 
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    86
	 * @param aNotificationId
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    87
	 *            Id of the soft notification.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    88
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    89
	protected void initialize(int aNotificationId) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    90
		final int iNotificationId = aNotificationId;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    91
		iListener = new SoftNotificationListener[1];
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    92
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    93
		Runnable r = new Runnable() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    94
			public void run() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    95
				String uidString = ApplicationInfo.getInstance().getUid().toString();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    96
				// removing "[" and "]" brackets
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    97
				uidString = uidString.substring(1,uidString.length()-1);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    98
				// converting to decimal
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
    99
				midletUid =(int)Long.parseLong(uidString,16);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   100
				iSoftNotificationHandle = OS.createNativePeer(midletUid, iNotificationId,
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   101
						SoftNotificationImpl.this);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   102
			}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   103
		};
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   104
		// if display is created already, execute it in UI thread no matter
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   105
		// what thread it is called from. Otherwise assume LCDUI application is
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   106
		// called
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   107
		// and create a display. eSWT is not allowed to call before display
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   108
		// creation
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   109
		if (DisplayExtension.getDisplayInstance() != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   110
			DisplayExtension.getDisplayInstance().syncExec(r);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   111
		} else {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   112
			com.nokia.mj.impl.nokialcdui.LCDUIInvoker
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   113
					.eSWTUIThreadRunnerSyncExec(r);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   114
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   115
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   116
		if (iSoftNotificationHandle <= 0) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   117
			throw new OutOfMemoryError();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   118
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   119
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   120
		mFinalizer = ((mFinalizer != null) ? mFinalizer : new Finalizer() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   121
			public void finalizeImpl() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   122
				close();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   123
			}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   124
		});
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   125
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   126
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   127
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   128
	 * Called when the object is finalized by the garbage collector.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   129
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   130
	public void close() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   131
		if (DisplayExtension.getDisplayInstance() != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   132
			Internal_PackageSupport.getDisplayInstance().syncExec(new Runnable() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   133
				public void run() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   134
					if (mFinalizer != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   135
						OS.destroy(iSoftNotificationHandle);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   136
					}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   137
					mFinalizer = null;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   138
				}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   139
			});
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   140
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   141
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   142
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   143
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   144
	 * Notification callback, called from the native side.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   145
	 * 
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   146
	 * @param aEventArg
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   147
	 *            Occurred event.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   148
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   149
	private void notificationCallback(int aEventArg) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   150
		// Synchronize the listener usage since the user may want to set it
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   151
		// to null during execution of this function.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   152
		synchronized (iListener) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   153
			SoftNotificationListener listener = iListener[0];
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   154
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   155
			if (listener != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   156
				if (aEventArg == EVENT_ACCEPT) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   157
					listener.notificationSelected(this);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   158
				} else if (aEventArg == EVENT_DISMISS) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   159
					listener.notificationDismissed(this);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   160
				}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   161
			}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   162
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   163
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   164
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   165
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   166
	 * Checks the given error value. Throws SoftNotificationException if the
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   167
	 * given error value is other than NativeError.KErrNone.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   168
	 * 
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   169
	 * @param aError
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   170
	 *            Error value to be checked.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   171
	 * @param aErrorMessage
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   172
	 *            Message to be included in the exception.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   173
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   174
	private final void checkError(int aError, String aErrorMessage)
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   175
			throws SoftNotificationException {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   176
		if (aError != 0) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   177
			throw new SoftNotificationException(ERROR_MESSAGE_PREFIX
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   178
					+ aErrorMessage, aError);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   179
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   180
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   181
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   182
	// Functions from the base class.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   183
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   184
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   185
	 * See class SoftNotification for comments
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   186
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   187
	public int getId() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   188
		if (DisplayExtension.getDisplayInstance() != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   189
			Internal_PackageSupport.getDisplayInstance().syncExec(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   190
					new Runnable() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   191
						public void run() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   192
							id = 0;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   193
							id = OS.getId(iSoftNotificationHandle);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   194
						}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   195
					});
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   196
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   197
		return id;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   198
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   199
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   200
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   201
	 * See class SoftNotification for comments
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   202
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   203
	public void post() throws SoftNotificationException {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   204
		if (DisplayExtension.getDisplayInstance() != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   205
			Internal_PackageSupport.getDisplayInstance().syncExec(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   206
					new Runnable() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   207
						public void run() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   208
							err = 0;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   209
							err = OS
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   210
									.showSoftNotification(iSoftNotificationHandle);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   211
						}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   212
					});
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   213
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   214
		checkError(err, "add soft notification");
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   215
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   216
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   217
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   218
	 * See class SoftNotification for comments
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   219
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   220
	public void remove() throws SoftNotificationException {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   221
		if (DisplayExtension.getDisplayInstance() != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   222
			Internal_PackageSupport.getDisplayInstance().syncExec(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   223
					new Runnable() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   224
						public void run() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   225
							err = 0;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   226
							err = OS
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   227
									.removeSoftNotification(iSoftNotificationHandle);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   228
						}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   229
					});
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   230
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   231
		checkError(err, "remove notification");
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   232
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   233
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   234
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   235
	 * See class SoftNotification for comments
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   236
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   237
	public void setListener(SoftNotificationListener aListener) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   238
		// Synchronize the listener setting since the user may set it
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   239
		// to null during it is used elsewhere in this class.
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   240
		synchronized (iListener) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   241
			iListener[0] = aListener;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   242
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   243
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   244
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   245
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   246
	 * See class SoftNotification for comments
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   247
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   248
	public void setText(String aPrimaryText, String aSecondaryText)
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   249
			throws SoftNotificationException {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   250
		final String iPrimaryText = aPrimaryText;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   251
		final String iSecondaryText = aSecondaryText;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   252
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   253
		if (DisplayExtension.getDisplayInstance() != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   254
			Internal_PackageSupport.getDisplayInstance().syncExec(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   255
					new Runnable() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   256
						public void run() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   257
							err = 0;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   258
							err = OS.setText(iSoftNotificationHandle,
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   259
									iPrimaryText == null ? "" : iPrimaryText,
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   260
									iSecondaryText == null ? ""
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   261
											: iSecondaryText);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   262
						}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   263
					});
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   264
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   265
		checkError(err, "set note text");
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   266
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   267
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   268
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   269
	 * See class SoftNotification for comments
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   270
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   271
	public void setSoftkeyLabels(String aSoftkey1Label, String aSoftkey2Label)
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   272
			throws SoftNotificationException {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   273
		int err = 0;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   274
		checkError(err, "set softkeys");
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   275
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   276
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   277
	private String stringReplace(String source, String searchStr,
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   278
			String replacementStr) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   279
		StringBuffer strBuffer = new StringBuffer();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   280
		int pos = source.indexOf(searchStr);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   281
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   282
		while (pos != -1) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   283
			strBuffer.append(source.substring(0, pos)).append(replacementStr);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   284
			source = source.substring(pos + searchStr.length());
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   285
			pos = source.indexOf(searchStr);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   286
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   287
		strBuffer.append(source);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   288
		return strBuffer.toString();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   289
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   290
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   291
	/**
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   292
	 * See class SoftNotification for comments
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   293
	 */
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   294
	public void setImage(byte[] aImageData) throws SoftNotificationException {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   295
		if (aImageData != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   296
			final byte[] iImageData = aImageData;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   297
			if (DisplayExtension.getDisplayInstance() != null) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   298
				Internal_PackageSupport.getDisplayInstance().syncExec(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   299
						new Runnable() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   300
							public void run() {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   301
								err = 0;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   302
								String imagePath = "";
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   303
								try {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   304
									String directoryPath = ApplicationInfo
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   305
											.getInstance().getRootPath();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   306
									// Replace "\private\102033e6\apps" with
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   307
									// "resource\apps\java"
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   308
									directoryPath = stringReplace(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   309
											directoryPath, "private", "public");
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   310
									directoryPath = directoryPath
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   311
											+ "softnotification\\"
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   312
											+ midletUid + "_"
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   313
											+ getId();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   314
									String imageName = midletUid
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   315
											+ "_" + getId();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   316
									FileUtility target = new FileUtility(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   317
											directoryPath);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   318
									if (!target.exists()) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   319
										target.mkdirs();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   320
									}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   321
									target = new FileUtility(directoryPath
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   322
											+ "\\" + imageName);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   323
									if (!target.exists()) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   324
										target.createNewFile();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   325
									}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   326
									OutputStream fos = target
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   327
											.openOutputStream();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   328
									ByteArrayInputStream in = new ByteArrayInputStream(
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   329
											iImageData);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   330
									byte[] buf = new byte[1024];
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   331
									int len;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   332
									while ((len = in.read(buf)) > 0) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   333
										fos.write(buf, 0, len);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   334
									}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   335
									in.close();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   336
									fos.close();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   337
									target = null;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   338
									imagePath = directoryPath + "\\"
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   339
											+ imageName;
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   340
								} catch (IOException ex) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   341
									ex.printStackTrace();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   342
								} catch (Throwable t) {
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   343
									t.printStackTrace();
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   344
								}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   345
								err = OS.setImagePath(iSoftNotificationHandle,
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   346
										imagePath);
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   347
							}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   348
						});
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   349
			}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   350
		}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   351
		checkError(err, "set image");
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   352
	}
71ad690e91f5 v2.2.17_1
hgs
parents:
diff changeset
   353
}