/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/importcontacts/Importer.java

  • Committer: edam
  • Date: 2012-12-19 17:50:33 UTC
  • Revision ID: tim@ed.am-20121219175033-61acsxzjulqpnian
made contacts backend throw exceptions when it can't create contacts on the device; specified Locale in lower/upper case conversions; stripped old Contacts types from Importer (and replaced with our own types, which are now converted in the backend; switched using Integer() constructors to Integer.valueOf(); rewrote the main importer routine a bit

Show diffs side-by-side

added added

removed removed

4
4
 * Copyright (C) 2009 to 2012 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
 
7
 * to as "this program"). For more information, see
8
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
141
141
                protected HashMap< String, PreferredDetail > _numbers = null;
142
142
                protected HashMap< String, PreferredDetail > _emails = null;
143
143
                protected HashMap< String, TypeDetail > _addresses = null;
144
 
                protected HashSet< String > _notes = null;
145
144
 
146
145
                private ContactsCache.CacheIdentifier _cache_identifier = null;
147
146
 
185
184
 
186
185
                        // if this is the first organisation added, or it's a preferred
187
186
                        // organisation and the current primary organisation isn't, then
188
 
                        // record this as the primary organisation
 
187
                        // record this as the primary organisation.
189
188
                        if( _primary_organisation == null ||
190
189
                                ( is_preferred && !_primary_organisation_is_preferred ) )
191
190
                        {
239
238
                        // and the current primary number isn't, or this number is on equal
240
239
                        // standing with the primary number in terms of preference and it is
241
240
                        // a voice number and the primary number isn't, then record this as
242
 
                        // the primary number
 
241
                        // the primary number.
243
242
                        if( _primary_number == null ||
244
243
                                ( is_preferred && !_primary_number_is_preferred ) ||
245
244
                                ( is_preferred == _primary_number_is_preferred &&
291
290
 
292
291
                        // if this is the first email added, or it's a preferred email and
293
292
                        // the current primary organisation isn't, then record this as the
294
 
                        // primary email
 
293
                        // primary email.
295
294
                        if( _primary_email == null ||
296
295
                                ( is_preferred && !_primary_email_is_preferred ) )
297
296
                        {
345
344
                        return _addresses;
346
345
                }
347
346
 
348
 
                protected void addNote( String note )
349
 
                {
350
 
                        if( _notes == null ) _notes = new HashSet< String >();
351
 
                        if( !_notes.contains( note ) )
352
 
                                _notes.add( note );
353
 
                }
354
 
 
355
 
                public boolean hasNotes()
356
 
                {
357
 
                        return _notes != null && _notes.size() > 0;
358
 
                }
359
 
 
360
 
                public HashSet< String > getNotes()
361
 
                {
362
 
                        return _notes;
363
 
                }
364
 
 
365
347
                protected void finalise()
366
348
                        throws ContactNotIdentifiableException
367
349
                {
368
 
                        // Ensure that if there is a primary number, it is preferred so
369
 
                        // that there is always one preferred number.  Android will assign
 
350
                        // ensure that if there is a primary number, it is preferred so
 
351
                        // that there is always one preferred number. Android will assign
370
352
                        // preference to one anyway so we might as well decide one sensibly.
371
353
                        if( _primary_number != null ) {
372
354
                                PreferredDetail data = _numbers.get( _primary_number );
390
372
 
391
373
                        // create a cache identifier from this contact data, which can be
392
374
                        // used to look-up an existing contact
393
 
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
 
375
                        _cache_identifier = ContactsCache.createIdentifier( this );
394
376
                        if( _cache_identifier == null )
395
377
                                throw new ContactNotIdentifiableException();
396
378
                }
443
425
                        setProgressMessage( R.string.doit_caching );
444
426
 
445
427
                        // create the appropriate backend
446
 
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
447
 
                                _backend = new ContactsContractBackend( _doit );
448
 
                        else
 
428
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
429
//                              _backend = new ContactsContractBackend( _doit );
 
430
//                      else
449
431
                                _backend = new ContactsBackend( _doit );
450
432
 
451
433
                        // create a cache of existing contacts and populate it
627
609
        /**
628
610
         * Should we skip a contact, given whether it exists or not and the current
629
611
         * merge setting?  This routine handles throwing up a prompt, if required.
630
 
         *
631
612
         * @param contact_detail the display name of the contact
632
613
         * @param exists true if this contact matches one in the cache
633
614
         * @param merge_setting the merge setting to use
687
668
        {
688
669
                checkAbort();
689
670
 
690
 
                // It is expected that we use contact.getCacheIdentifier() here.  The
 
671
                // It is expected that we use contact.getCacheIdentifier() here. The
691
672
                // contact we are passed should have been successfully finalise()d,
692
673
                // which includes generating a valid cache identifier.
693
674
                ContactsCache.CacheIdentifier cache_identifier =
760
741
                                importContactAddresses( id, contact.getAddresses() );
761
742
                        if( contact.hasOrganisations() )
762
743
                                importContactOrganisations( id, contact.getOrganisations() );
763
 
                        if( contact.hasNotes() )
764
 
                                importContactNotes( id, contact.getNotes() );
765
744
                }
766
745
                catch( Backend.ContactCreationException e )
767
746
                {
780
759
                        String number = i.next();
781
760
                        ContactData.PreferredDetail data = datas.get( number );
782
761
 
783
 
                        // We don't want to add this number if it's crap, or it already
784
 
                        // exists (which would cause a duplicate to be created).  We don't
785
 
                        // take in to account the type when checking for duplicates.  This
786
 
                        // is intentional: types aren't really very reliable.  We assume
787
 
                        // that if the number exists at all, it doesn't need importing.
788
 
                        // Because of this, we also can't update the cache (which we don't
789
 
                        // need to anyway, so it's not a problem).
 
762
                        // we don't want to add this number if it's crap, or it already
 
763
                        // exists (which would cause a duplicate to be created). We don't
 
764
                        // take in to account the type when checking for duplicates. This is
 
765
                        // intentional: types aren't really very reliable. We assume that
 
766
                        // if the number exists at all, it doesn't need importing. Because
 
767
                        // of this, we also can't update the cache (which we don't need to
 
768
                        // anyway, so it's not a problem).
790
769
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
791
770
                                continue;
792
771
 
811
790
                        ContactData.PreferredDetail data = datas.get( email );
812
791
 
813
792
                        // we don't want to add this email address if it exists already or
814
 
                        // we would introduce duplicates
 
793
                        // we would introduce duplicates.
815
794
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
816
795
                                continue;
817
796
 
874
853
                }
875
854
        }
876
855
 
877
 
        private void importContactNotes( Long id,
878
 
                HashSet< String > datas )
879
 
                throws ContactCreationException
880
 
        {
881
 
                // add notes
882
 
                Iterator< String > i = datas.iterator();
883
 
                while( i.hasNext() ) {
884
 
                        String note = i.next();
885
 
 
886
 
                        // we don't want to add this note if it exists already or we would
887
 
                        // introduce duplicates
888
 
                        if( _contacts_cache.hasAssociatedNote( id, note ) )
889
 
                                continue;
890
 
 
891
 
                        // add note
892
 
                        _backend.addContactNote( id, note );
893
 
 
894
 
                        // and add this note to the cache to prevent a addition of duplicate
895
 
                        // date from another file
896
 
                        _contacts_cache.addAssociatedNote( id, note );
897
 
                }
898
 
 
899
 
        }
900
 
 
901
856
        synchronized protected void checkAbort() throws AbortImportException
902
857
        {
903
858
                if( _abort ) {