|
1 /* |
|
2 * Copyright (c) 2002 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: This class implements functionalities defined in the |
|
15 * MetaDataControl interface (included in |
|
16 * javax.microedition.media.control package). |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 package com.nokia.microedition.media.control; |
|
22 |
|
23 import java.util.Vector; |
|
24 import java.util.Enumeration; |
|
25 |
|
26 // CLASS DEFINITION |
|
27 /** |
|
28 * The <code>MetaDataControl</code> class implements functionalities defined |
|
29 * in the <code>MetaDataControl</code> interface (included in |
|
30 * <code>javax.microedition.media.control</code> package). |
|
31 */ |
|
32 |
|
33 public class MetaDataControl |
|
34 extends ControlImpl |
|
35 implements javax.microedition.media.control.MetaDataControl |
|
36 { |
|
37 |
|
38 /** |
|
39 * The metadata keys received from the native side. It is assumed that |
|
40 * the keys will not change during the lifetime of a media clip or |
|
41 * stream. Key data is used so frequently that we want to buffer it |
|
42 * here at Java side. |
|
43 */ |
|
44 private String[] iKeys = null; |
|
45 private static final String[] DEFAULT_DATA = { MetaDataControl.AUTHOR_KEY, |
|
46 MetaDataControl.COPYRIGHT_KEY, |
|
47 MetaDataControl.DATE_KEY, |
|
48 MetaDataControl.TITLE_KEY |
|
49 }; |
|
50 private static final String DEFAULT_DATA_VALUE = "unknown"; |
|
51 |
|
52 // Static initialization |
|
53 |
|
54 /** |
|
55 * Creates an instance of <code>MetaDataControl</code>. |
|
56 */ |
|
57 public MetaDataControl() |
|
58 { |
|
59 } |
|
60 |
|
61 /** |
|
62 * Returns acceptable metadata keys. |
|
63 * |
|
64 * The keys can be used for getting specific metadata value associated to each key. |
|
65 * |
|
66 * @See <code>javax.microedition.media.control.MetaDataControl</code> |
|
67 * interface documentation for details. |
|
68 * |
|
69 * @return Acceptable key values. |
|
70 * |
|
71 */ |
|
72 public String[] getKeys() |
|
73 { |
|
74 checkState(); |
|
75 |
|
76 int keyCount = _getKeysCount(iEventSource, iControlHandle); |
|
77 |
|
78 if (keyCount < 0) // can't read key count |
|
79 { |
|
80 iKeys = DEFAULT_DATA; |
|
81 return iKeys; |
|
82 } |
|
83 |
|
84 Vector keys = new Vector(keyCount + 1); |
|
85 |
|
86 for (int i = 0; i < keyCount; i++) |
|
87 { |
|
88 String currentKey = _getKey(iEventSource, iControlHandle, i); |
|
89 if (currentKey == null) // can't read key value |
|
90 { |
|
91 iKeys = DEFAULT_DATA; |
|
92 return iKeys; |
|
93 } |
|
94 keys.addElement(currentKey); |
|
95 } |
|
96 |
|
97 // Check if the default keys are already in the key vector |
|
98 // and if not, then add them |
|
99 |
|
100 for (int i = 0; i < DEFAULT_DATA.length; i++) |
|
101 { |
|
102 checkAddKey(keys, DEFAULT_DATA[ i ]); |
|
103 } |
|
104 |
|
105 iKeys = new String[ keys.size()]; |
|
106 keys.copyInto(iKeys); |
|
107 |
|
108 return iKeys; |
|
109 } |
|
110 |
|
111 /** |
|
112 * Check if there is already a key in vector and if not, then |
|
113 * add one. |
|
114 * @param aKeys vector containing keys to be checked and which to add |
|
115 * new key |
|
116 * @param aKey key string to be added if not already present in aKeys |
|
117 */ |
|
118 private void checkAddKey(Vector aKeys, String aKey) |
|
119 { |
|
120 for (Enumeration e = aKeys.elements(); e.hasMoreElements();) |
|
121 { |
|
122 String currentElement = (String)e.nextElement(); |
|
123 if (currentElement.equals(aKey)) |
|
124 { |
|
125 return; |
|
126 } |
|
127 } |
|
128 // Otherwise add the key |
|
129 aKeys.addElement(new String(aKey)); |
|
130 } |
|
131 |
|
132 /** |
|
133 * Fetches a value of the given metadata key. |
|
134 * |
|
135 * See <code>javax.microedition.media.control.MetaDataControl</code> |
|
136 * interface documentation for details. |
|
137 * |
|
138 * @param aKey Key of the metadata value to be returned. |
|
139 * |
|
140 * @return Value of the metadata. |
|
141 * |
|
142 * @exception IllegalArgumentException Thrown if the given key is |
|
143 * <code>null</code> or invalid. |
|
144 */ |
|
145 public String getKeyValue(String aKey) |
|
146 { |
|
147 checkState(); |
|
148 if (null == aKey) |
|
149 { |
|
150 throw new IllegalArgumentException("Key cannot be null"); |
|
151 } |
|
152 |
|
153 getKeys(); // Buffer the keys if not present |
|
154 |
|
155 int arrLen = iKeys.length; |
|
156 int i = 0; |
|
157 |
|
158 // Check if the given key is valid and return the value. |
|
159 while (i < arrLen) |
|
160 { |
|
161 if (iKeys[ i ].equals(aKey)) |
|
162 { |
|
163 String value = _getKeyValue(iEventSource, iControlHandle, aKey); |
|
164 |
|
165 // if key is ok but value is null then this key does not really |
|
166 // exist and it must be checked whether it is one from DEFAULT_DATA |
|
167 if (value == null) |
|
168 { |
|
169 for (int j = 0; j < DEFAULT_DATA.length; j++) |
|
170 { |
|
171 if (aKey.equals(DEFAULT_DATA[ j ])) |
|
172 { |
|
173 return(DEFAULT_DATA_VALUE); |
|
174 } |
|
175 } |
|
176 } |
|
177 return value; |
|
178 } |
|
179 ++i; |
|
180 } |
|
181 |
|
182 // No match - invalid key. |
|
183 throw new IllegalArgumentException("Invalid non-null key"); |
|
184 } |
|
185 |
|
186 private native int _getKeysCount(int aEventSource, |
|
187 int aControlHandle); |
|
188 private native String _getKey(int aEventSource, |
|
189 int aControlHandle, |
|
190 int aIndex); |
|
191 private native String _getKeyValue(int aEventSource, |
|
192 int aControlHandle, |
|
193 String aKey); |
|
194 } //end of file |