Developer Area

root/apps/blackberry/branches/1.5/blackberry/src/org/telnic/blackberry/util/listcallbacks/LookupResultsListCallback.java @ 584

Revision 584, 30.7 kB (checked in by jonmaycock, 10 months ago)
Line 
1/******************************************************************
2Copyright (c) 2008, Telnic Ltd.
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are
7met:
8
9*   Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11*   Redistributions in binary form must reproduce the above
12copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
13*   Neither the name of the Telnic Ltd. nor the names of its
14contributors may be used to endorse or promote products derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
18*****************************************************************/
19
20package org.telnic.blackberry.util.listcallbacks;
21
22import java.io.UnsupportedEncodingException;
23import java.util.Enumeration;
24import java.util.Vector;
25
26import net.rim.blackberry.api.browser.URLEncodedPostData;
27import net.rim.device.api.i18n.ResourceBundle;
28import net.rim.device.api.system.Bitmap;
29import net.rim.device.api.ui.Font;
30import net.rim.device.api.ui.Graphics;
31import net.rim.device.api.ui.component.ListField;
32import net.rim.device.api.ui.component.ListFieldCallback;
33
34import org.bouncycastle.util.encoders.Base64;
35import org.not.java.net.URLDecoder;
36import org.telnic.blackberry.TelnameApp;
37import org.telnic.blackberry.TelnameAppResource;
38import org.telnic.blackberry.dns.DNSLocResourceRecord;
39import org.telnic.blackberry.dns.DNSTelnameNaptrResourceRecord;
40import org.telnic.blackberry.options.Options;
41import org.telnic.blackberry.util.Logger;
42
43/**
44 * Provides drawing functionality for the list object displaying lookup results
45 *
46 * @author Ben Dowling, Jonathan Maycock
47 * @version $Id: LookupResultsListCallback.java,v 1.13 2009/11/23 15:33:27 jm2 Exp $
48 */
49public class LookupResultsListCallback implements ListFieldCallback
50{
51    /**
52     * Denotes the back item.
53     */
54    public static int NONE = 0;
55
56    /**
57     * Denotes the back item.
58     */
59    public static int BACK_ELEMENT = 1;
60
61    /**
62     *  Dentotes the keyword item.
63     */
64    public static int KEYWORD_ELEMENT = 2;
65
66    /** The tel icon. */
67    private Bitmap telIcon = Bitmap.getBitmapResource( "tel16.png" );
68   
69    /** The phone icon. */
70    private Bitmap phoneIcon = Bitmap.getBitmapResource( "phone_16x16.gif" );
71
72    /** The sms icon. */
73    private Bitmap smsIcon = Bitmap.getBitmapResource( "sms_16x16.gif" );
74   
75    /** The fax icon. */
76    private Bitmap faxIcon = Bitmap.getBitmapResource( "fax_16x16.gif" );
77   
78    /** The email icon. */
79    private Bitmap emailIcon = Bitmap.getBitmapResource( "email_16x16.gif" );
80   
81    /** The web icon. */
82    private Bitmap webIcon = Bitmap.getBitmapResource( "web_16x16.gif" );       
83
84    /** The gtalk im icon. */
85    private Bitmap gtalkIcon = Bitmap.getBitmapResource( "im_16x16.gif" );
86   
87    /** The xmppIcon im icon. */
88    private Bitmap xmppIcon = Bitmap.getBitmapResource( "im_16x16.gif" );
89
90    /** The xmppIcon im icon. */
91    private Bitmap xmppVoiceIcon = Bitmap.getBitmapResource( "voip_16x16.gif" );
92
93    /** The aim im icon. */
94    private Bitmap aimIcon = Bitmap.getBitmapResource( "im_16x16.gif" );
95
96    /** The aim im icon. */
97    private Bitmap aimVoiceIcon = Bitmap.getBitmapResource( "voip_16x16.gif" );
98   
99    /** The mobile icon. */
100    private Bitmap mobileIcon = Bitmap.getBitmapResource( "mobile_16x16.gif" );         
101
102    /** The ftp icon. */
103    private Bitmap ftpIcon = Bitmap.getBitmapResource( "ftp_16x16.gif" );
104
105    /** The sip icon. */
106    private Bitmap sipIcon = Bitmap.getBitmapResource( "voip_16x16.gif" );
107   
108    /** The h323 icon. */
109    private Bitmap h323Icon = Bitmap.getBitmapResource( "voip_16x16.gif" );
110
111    /** The msn im icon. */
112    private Bitmap msnInstantIcon = Bitmap.getBitmapResource( "im_16x16.gif" );
113   
114    /** The msn voice icon. */
115    private Bitmap msnVoiceIcon = Bitmap.getBitmapResource( "voip_16x16.gif" );
116   
117    /** The skype voice icon. */
118    private Bitmap skypeInstantIcon = Bitmap.getBitmapResource( "im_16x16.gif" );
119   
120    /** The skype voice icon. */
121    private Bitmap skypeVoiceIcon = Bitmap.getBitmapResource( "voip_16x16.gif" );
122
123    /** The yahoo icon. */
124    private Bitmap yahooIcon = Bitmap.getBitmapResource( "im_16x16.gif" );
125   
126    /** The icq icon. */
127    private Bitmap icqIcon = Bitmap.getBitmapResource( "im_16x16.gif" );
128
129    /** The ntn icon. */
130    private Bitmap ntnIcon = Bitmap.getBitmapResource( "link_16x16.gif" );
131
132    /** The ntn icon. */
133    private Bitmap ntnBackIcon = Bitmap.getBitmapResource( "link_16x16_rotated.gif" );
134
135    /** The world icon. */
136    private Bitmap worldIcon = Bitmap.getBitmapResource( "location_16x16.gif" );
137   
138    /** The note icon. */
139    private Bitmap noteIcon = Bitmap.getBitmapResource( "info_16x16.gif" );
140   
141    /** The user icon. */
142    private Bitmap userIcon = Bitmap.getBitmapResource( "personal_16x16.gif" );
143
144    /** The blank icon. */
145    private Bitmap blankIcon = Bitmap.getBitmapResource( "blank.gif" );
146
147    /**
148     * The keywords icon.
149     */
150    private Bitmap keyIcon = Bitmap.getBitmapResource( "keywords_16x16.gif" );
151
152    /**
153     * icon.
154     */
155    private Bitmap icon;
156
157    /**
158     * The element type.
159     */
160    private final Vector elementTypes = new Vector();
161
162    /** The list. */
163    private final Vector list = new Vector();
164
165    private Vector displayList = new Vector();
166
167    /** The parsed string. */
168    private String parsedString = "";
169
170    /** The service display string. */
171    private String serviceDisplayString = "";
172
173    /**
174     * Add.
175     *
176     * @param arg0 the arg0
177     * @param rr the rr
178     */
179    public void add (final ListField arg0, final DNSTelnameNaptrResourceRecord rr)
180    {
181        try
182        {
183            if (rr != null && !list.contains( rr ))
184            {
185                String val = parseNaptrString( rr );
186                arg0.insert( list.size() );
187                list.addElement( rr );           
188                displayList.addElement( val );           
189                elementTypes.addElement( new Integer( NONE ) );
190            }
191        }
192        catch (DiscardedNaptrException d)
193        {
194            //do nothing
195            return;
196        }
197    }
198
199
200    /**
201     * Add.
202     *
203     * @param arg0 the arg0
204     * @param rr the rr
205     */
206    public void add (final ListField arg0, final DNSLocResourceRecord rr)
207    {   
208        if (rr != null && !list.contains( rr ))
209        {
210            arg0.insert( list.size() );
211            list.addElement( rr );
212            displayList.addElement( rr.toString() );
213            elementTypes.addElement( new Integer( NONE ) );
214        }               
215    }
216
217    /**
218     * Add.  used to add the back option.
219     *
220     * @param arg0 the list field
221     * @param rr the string
222     */
223    public void add (final ListField arg0, final String rr, final int type)
224    {
225
226        if (rr != null && !list.contains( rr ))
227        {
228            arg0.insert( list.size() );
229            list.addElement( rr );
230            displayList.addElement( rr );
231            elementTypes.addElement( new Integer( type ) );
232
233        }
234    }
235
236    public boolean scrollingRequired(ListField arg0, int index)
237    {
238        String element = (String) displayList.elementAt( index );
239
240        if (index == arg0.getSelectedIndex() && (arg0.getFont().getAdvance( element ) > (arg0.getWidth() - telIcon.getWidth() - 2)))
241        {
242            return true;
243        }
244        return false;
245    }
246
247    public void updateText(ListField arg0, int index)
248    {
249        String element = (String) displayList.elementAt( index );
250
251        if (index == arg0.getSelectedIndex() && (arg0.getFont().getAdvance( element ) > (arg0.getWidth() - telIcon.getWidth() - 2)))
252        {
253            element = (String) displayList.elementAt( index );
254            displayList.setElementAt( element.substring( 1 ) + element.charAt( 0 ), index );
255        }
256    }
257
258    public void resetText(ListField arg0, int index)
259    {
260        displayList = new Vector();
261        for (int i = 0; i < list.size(); i++)
262        {
263            String element = "";
264            if (list.elementAt( i ) instanceof DNSTelnameNaptrResourceRecord)
265            {
266                final DNSTelnameNaptrResourceRecord rr = (DNSTelnameNaptrResourceRecord) list.elementAt( i );
267                try
268                {
269                    element = parseNaptrString( rr );
270                }
271                catch (DiscardedNaptrException e)
272                {
273                    Logger.logMessage( "Problem with naptr: " + e.getMessage(), Options.KEY_LOG_APPLICATION );
274                }
275            }
276            if (list.elementAt( i ) instanceof String)
277            {
278                element = (String) list.elementAt( i );
279            }
280            else if (list.elementAt( i ) instanceof DNSLocResourceRecord)
281            {
282                final DNSLocResourceRecord rr = (DNSLocResourceRecord) list.elementAt( i );
283                element = rr.toString();
284            }
285            displayList.addElement( element );
286        }
287
288
289        String element = (String) displayList.elementAt( index );
290        if ((arg0.getFont().getAdvance( element ) > (Graphics.getScreenWidth() - telIcon.getWidth() - 2)))
291        {
292            displayList.setElementAt( element + "  -----  ", index );
293        }
294    }
295
296    /*
297     * (non-Javadoc)
298     *
299     * @see net.rim.device.api.ui.component.ListFieldCallback#drawListRow(net.rim.device.api.ui.component.ListField,
300     *      net.rim.device.api.ui.Graphics, int, int, int)
301     */
302    public void drawListRow (final ListField arg0, final Graphics g, final int index, final int y,
303        final int w)
304    {
305        if (list.elementAt( index ) instanceof DNSTelnameNaptrResourceRecord)
306        {
307            final DNSTelnameNaptrResourceRecord rr = (DNSTelnameNaptrResourceRecord) list.elementAt( index );
308
309            final int space = 1;
310            try
311            {
312                Object[] vals = parseNaptrStringAndIcon( rr );
313                final String element = (String) vals[0];
314               
315                if (rr.isEncrypted())
316                {
317                    g.setFont( g.getFont().derive( Font.ITALIC ) );
318                }
319                else
320                {
321                    g.setFont( g.getFont().derive( Font.PLAIN ) );
322                }
323
324                g.drawBitmap( 0, y, icon.getWidth(), icon.getHeight(), icon, 0, 0 );
325                g.drawText( (String) displayList.elementAt( index ), icon.getWidth() + space, y, 0, w );
326            }
327            catch (DiscardedNaptrException e)
328            {
329                Logger.logMessage( "Problem with NAPTR: " + e.getMessage(), Options.KEY_LOG_APPLICATION );
330            }           
331        }
332        if (list.elementAt( index ) instanceof String)
333        {
334            final String rr = (String) list.elementAt( index );
335
336            final Integer x = (Integer) elementTypes.elementAt( index );
337
338            final int space = 1;
339            if (x.intValue() == BACK_ELEMENT)
340            {
341                icon = ntnBackIcon;
342            }
343            else if (x.intValue() == KEYWORD_ELEMENT)
344            {
345                icon = keyIcon;
346            }
347            else
348            {
349                icon = blankIcon;
350            }
351
352            g.drawBitmap( 0, y, icon.getWidth(), icon.getHeight(), icon, 0, 0 );
353            g.drawText( (String) displayList.elementAt( index ), icon.getWidth() + space, y, 0, w );
354        }
355        else if (list.elementAt( index ) instanceof DNSLocResourceRecord)
356        {
357            final DNSLocResourceRecord rr = (DNSLocResourceRecord) list.elementAt( index );
358            icon = worldIcon;
359            final int space = 1;
360            final String element = rr.toString();
361
362            g.drawBitmap( 0, y, icon.getWidth(), icon.getHeight(), icon, 0, 0 );
363            g.drawText( (String) displayList.elementAt( index ), icon.getWidth() + space, y, 0, w );
364        }
365    }
366
367    /**
368     * getLocIcon.
369     *
370     * @return loc icon
371     */
372    public Bitmap getLocIcon ()
373    {
374        return worldIcon;
375    }
376
377    /**
378     * returns the element type
379     */
380    public int getElementType(int index)
381    {
382        Integer x = (Integer) elementTypes.elementAt( index );
383        return x.intValue();
384    }
385
386
387    /**
388     * parse naptr string and icon.
389     *
390     * @param rr the rr
391     * @return object
392     */
393    public Object[] parseNaptrStringAndIcon (DNSTelnameNaptrResourceRecord rr)  throws DiscardedNaptrException
394    {
395        String element = "";
396        serviceDisplayString = "";
397
398        if (rr.isNonTerminal())
399        {
400            element = rr.getNTNDisplayName();
401            icon = ntnIcon;
402            serviceDisplayString = TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_NTN );
403        }
404        else
405        {           
406            String service = rr.getServiceType();
407           
408            element = rr.getUri();
409            Vector categories = rr.getCategories();
410           
411            //calculate LIH prefix
412            if (categories.size() > 0)
413            {
414                Enumeration e = categories.elements();
415                serviceDisplayString += parseLIH( (String) e.nextElement() );
416               
417                while (e.hasMoreElements())
418                {
419                    serviceDisplayString += " " + TelnameApp.getResourceBundle().getString( TelnameAppResource.AND ) + " ";
420                    serviceDisplayString += parseLIH( (String) e.nextElement() );
421                }
422               
423                serviceDisplayString += " ";               
424            }                                   
425                                                                           
426//          Phone**************************************************************************
427            if (service.indexOf( "voice:tel" ) >= 0)
428            {               
429                checkUriType( service, element );
430                icon = phoneIcon;
431                if (categories.contains( "x-mobile" ))
432                {
433                    icon = mobileIcon;
434                }
435                element = element.substring( element.indexOf( ":" ) + 1 );
436                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_PHONE );           
437            }
438            else if (service.indexOf( "fax:tel" ) >= 0)
439            {
440                checkUriType( service, element );
441                icon = faxIcon;
442                element = element.substring( element.indexOf( ":" ) + 1 );
443                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_FAX );               
444            }
445            else if (service.indexOf( "sms:tel" ) >= 0 || service.indexOf( "sms:mailto" ) >= 0)
446            {
447                checkUriType( service, element );
448                icon = smsIcon;
449                element = element.substring( element.indexOf( ":" ) + 1 );
450                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_SMS );
451            }
452            else if (service.indexOf( "ems:tel" ) >= 0 || service.indexOf( "ems:mailto" ) >= 0)
453            {
454                checkUriType( service, element );
455                icon = smsIcon;
456                element = element.substring( element.indexOf( ":" ) + 1 );
457                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_EMS );
458            }
459            else if (service.indexOf( "mms:tel" ) >= 0 || service.indexOf( "mms:mailto" ) >= 0)
460            {
461                checkUriType( service, element );
462                icon = smsIcon;
463                element = element.substring( element.indexOf( ":" ) + 1 );
464                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_MMS );
465            }
466
467            //AIM**************************************************************************
468            else if (service.indexOf( "x-im:aim" ) >= 0)
469            {
470                checkUriType( service, element );
471                icon = aimIcon;
472                element = element.substring( element.indexOf( ":" ) + 1 );
473                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_AIM_MESS );
474            }
475            else if (service.indexOf( "x-voice:aim" ) >= 0)
476            {
477                checkUriType( service, element );
478                icon = aimVoiceIcon;
479                element = element.substring( element.indexOf( ":" ) + 1 );
480                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_AIM_VOICE );
481            }           
482
483            // XMPP/GTALK*******************************************************************
484            else if (service.indexOf( "x-im:xmpp" ) >= 0)
485            {
486                checkUriType( service, element );
487                icon = xmppIcon;
488                element = element.substring( element.indexOf( ":" ) + 1 );
489                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_XMPP_MESS );
490            }
491            else if (service.indexOf( "x-voice:gtalk" ) >= 0)
492            {
493                checkUriType( service, element );
494                icon = xmppVoiceIcon;
495                element = element.substring( element.indexOf( ":" ) + 1 );
496                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_GTALK_VOICE );
497            }
498            else if (service.indexOf( "x-im:gtalk" ) >= 0)
499            {
500                checkUriType( service, element );
501                icon = gtalkIcon;
502                element = element.substring( element.indexOf( ":" ) + 1 );
503                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_GTALK_MESS );
504            }
505           
506//          SKYPE***************************************************************************           
507            else if (service.indexOf( "x-voice:skype" ) >= 0)
508            {
509                checkUriType( service, element );
510                icon = skypeVoiceIcon;
511                element = element.substring( element.indexOf( ":" ) + 1 );
512                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_SKYPE_VOICE );
513            }
514            else if (service.indexOf( "x-im:skype" ) >= 0)
515            {
516                checkUriType( service, element );
517                icon = skypeInstantIcon;
518                element = element.substring( element.indexOf( ":" ) + 1 );
519                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_SKYPE_IM );
520            }
521           
522//          MSN***************************************************************************
523            else if (service.indexOf( "x-im:msn" ) >= 0)
524            {
525                checkUriType( service, element );
526                icon = msnInstantIcon;
527                element = element.substring( element.indexOf( ":" ) + 1 );
528                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_MSN_MESS );
529            } 
530            else if (service.indexOf( "x-voice:msn" ) >= 0)
531            {
532                checkUriType( service, element );
533                icon = msnVoiceIcon;
534                element = element.substring( element.indexOf( ":" ) + 1 );
535                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_MSN_VOICE );
536            }
537//          YAHOO*******************************************************************
538            else if (service.indexOf( "x-im:ymsgr" ) >= 0)
539            {
540                checkUriType( service, element );
541                icon = yahooIcon;
542                element = element.substring( element.indexOf( ":" ) + 1 );
543                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_YAHOO_MESS );
544            }
545//          ICQ*******************************************************************
546            else if (service.indexOf( "x-im:icq" ) >= 0)
547            {
548                checkUriType( service, element );
549                icon = icqIcon;
550                element = element.substring( element.indexOf( ":" ) + 1 );
551                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_ICQ_MESS );
552            }           
553           
554//          OTHER*******************************************************************
555            else if (service.indexOf( "sip" )>= 0)
556            {
557                icon = sipIcon;
558                element = element.substring( element.indexOf( ":" ) + 1 );
559                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_SIP );
560            }
561            else if (service.indexOf( "h323" )>= 0)
562            {
563                icon = h323Icon;
564                element = element.substring( element.indexOf( ":" ) + 1 );
565                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_H323 );
566            }
567            else if (service.indexOf( "x-crypto" ) >= 0)
568            {
569                icon = telIcon;
570                element = "Encrypted";
571                serviceDisplayString = "";
572            }           
573            else if (service.indexOf( "email:mailto" ) >= 0)
574            {
575                checkUriType( service, element );
576                icon = emailIcon;
577                element = element.substring( element.indexOf( ":" ) + 1 );
578                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_EMAIL );
579            }
580            else if (service.indexOf( "web:http" ) >= 0)
581            {
582                checkUriType( service, element );
583                icon = webIcon;
584                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_WEB_SITE );
585            }
586            else if (service.indexOf( "web:https" ) >= 0)
587            {
588                checkUriType( service, element );
589                icon = webIcon;
590                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_SECURE_WEB_SITE );
591            }
592            else if (service.indexOf( "ft:ftp" ) >= 0)
593            {
594                checkUriType( service, element );
595                icon = ftpIcon;
596                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_FTP );
597            }
598            else if (service.indexOf( "note:data" ) >= 0)
599            {
600                checkUriType( service, element );
601                icon = noteIcon;
602                element = decodeDataUri( element );
603                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_NOTE );               
604            }
605            else if (service.indexOf( "user:data" ) >= 0)
606            {
607                checkUriType( service, element );
608                icon = userIcon;
609                element = decodeDataUri( element );
610                serviceDisplayString += TelnameApp.getResourceBundle().getString( TelnameAppResource.SERVICE_TYPE_USER );               
611            }
612            else
613            {
614                icon = blankIcon;
615                               
616                if( service.startsWith( "x-" ))
617                {
618                    service = ( service.substring( 2 ) );
619                }   
620                if (service.indexOf( ":data" ) >= 0)
621                {
622                    element = decodeDataUri( element );
623                }
624                serviceDisplayString += service;
625            }
626        }
627        parsedString = element;
628
629        Object[] returnvals = { element, icon };
630        return returnvals;
631    }
632
633    /**
634     * parse naptr string
635     *
636     * @param rr
637     * @return string
638     */
639    public String parseNaptrString (DNSTelnameNaptrResourceRecord rr) throws DiscardedNaptrException
640    {
641        parseNaptrStringAndIcon( rr );
642        String element = parsedString;
643        return element;
644    }
645   
646    /**
647     * parses a LIH and returns the display string.
648     *
649     * @param string the LIH
650     * @return the display string
651     */
652    public String parseLIH(String lih)
653    {       
654        ResourceBundle bundle = TelnameApp.getResourceBundle();
655       
656        if (lih.equals( "x-main" ))
657        {
658            return bundle.getString( TelnameAppResource.LIH_CATEGORY_MAIN );
659        }
660        else if (lih.equals( "x-home" ))
661        {
662            return bundle.getString( TelnameAppResource.LIH_CATEGORY_HOME );
663        }
664        else if (lih.equals( "x-mobile" ))
665        {
666            return bundle.getString( TelnameAppResource.LIH_CATEGORY_MOBILE );
667        } 
668        else if (lih.equals( "x-work" ))
669        {
670            return bundle.getString( TelnameAppResource.LIH_CATEGORY_WORK );
671        } 
672        else if (lih.equals( "x-transit" ))
673        {
674            return bundle.getString( TelnameAppResource.LIH_CATEGORY_TRANSIT );
675        } 
676        else if (lih.equals( "x-prs" ))
677        {
678            return bundle.getString( TelnameAppResource.LIH_CATEGORY_PRS );
679        } 
680           
681        return "";
682    }
683
684    public String getServiceString (int index)
685    {
686        if (!(list.elementAt( index ) instanceof DNSTelnameNaptrResourceRecord))
687        {
688            return "";
689        }
690        else
691        {
692            try
693            {
694                DNSTelnameNaptrResourceRecord rr = (DNSTelnameNaptrResourceRecord) list.elementAt( index );
695                parseNaptrStringAndIcon( rr );
696                return serviceDisplayString;
697            }
698            catch (DiscardedNaptrException e)
699            {
700                Logger.logMessage( "Problem with NAPTR: " + e.getMessage(), Options.KEY_LOG_APPLICATION );
701                return "";
702            }
703        }
704    }
705
706
707    /*
708     * (non-Javadoc)
709     *
710     * @see net.rim.device.api.ui.component.ListFieldCallback#get(net.rim.device.api.ui.component.ListField,
711     *      int)
712     */
713    public Object get (final ListField arg0, final int index)
714    {
715        return list.elementAt( index );
716    }
717
718    public String getString (final ListField arg0, final int index)
719    {
720        String element = "";
721        if (list.elementAt( index ) instanceof DNSTelnameNaptrResourceRecord)
722        {
723            final DNSTelnameNaptrResourceRecord rr = (DNSTelnameNaptrResourceRecord) list.elementAt( index );
724            try
725            {
726                element = parseNaptrString( rr );
727            }
728            catch (DiscardedNaptrException e)
729            {
730                Logger.logMessage( "Problem with NAPTR: " + e.getMessage(), Options.KEY_LOG_APPLICATION );
731            }
732        }
733        if (list.elementAt( index ) instanceof String)
734        {
735            element = (String) displayList.elementAt( index );
736        }
737        else if (list.elementAt( index ) instanceof DNSLocResourceRecord)
738        {
739            final DNSLocResourceRecord rr = (DNSLocResourceRecord) list.elementAt( index );
740            element = rr.toString();
741        }
742        return element;
743    }
744
745    /**
746     * size.
747     *
748     * @return size
749     */
750    public int size ()
751    {
752        return list.size();
753    }
754
755    /*
756     * (non-Javadoc)
757     *
758     * @see net.rim.device.api.ui.component.ListFieldCallback#getPreferredWidth(net.rim.device.api.ui.component.ListField)
759     */
760    public int getPreferredWidth (final ListField arg0)
761    {
762        return 0;
763    }
764
765    private String decodeDataUri(String uri) throws DiscardedNaptrException
766    {
767        String returnData = "";
768       
769        if ( uri.startsWith( "data:," ) )
770        {
771            returnData = uri.substring( uri.indexOf( "," ) + 1 );
772            try
773            {
774                returnData = URLDecoder.decode( returnData, "UTF-8" );
775            }
776            catch (UnsupportedEncodingException e)
777            {               
778                e.printStackTrace();
779            }
780        }
781        else if ( uri.startsWith( "data:;base64," ) )
782        {
783            returnData = uri.substring( uri.indexOf( "," ) + 1 );           
784            try
785            {
786                returnData = new String (Base64.decode( returnData ), "UTF-8");
787            }
788            catch (UnsupportedEncodingException e)
789            {   
790                throw new DiscardedNaptrException("Problem Unencoding data uri: " + e.getMessage());               
791            } 
792        }
793        else
794        {
795            throw new DiscardedNaptrException("Unsupported Mime Type or Charset found.");
796        }
797       
798        return returnData;
799    }
800   
801    private void checkUriType(String service, String uri) throws DiscardedNaptrException
802    {
803        String serviceType = service.substring( service.indexOf( ":" ) + 1 );
804        if (!uri.startsWith( serviceType ))
805        {
806            throw new DiscardedNaptrException("URI scheme does not match service.");
807        }       
808    }
809
810    /*
811     * (non-Javadoc)
812     *
813     * @see net.rim.device.api.ui.component.ListFieldCallback#indexOfList(net.rim.device.api.ui.component.ListField,
814     *      java.lang.String, int)
815     */
816    public int indexOfList (final ListField arg0, final String arg1, final int arg2)
817    {
818        return 0;
819    }
820}
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us