/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: Tim Marston
  • Date: 2013-06-22 17:29:31 UTC
  • Revision ID: tim@ed.am-20130622172931-ujydoni23t3a543b
minor style tweaks

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;
144
145
 
145
146
                private ContactsCache.CacheIdentifier _cache_identifier = null;
146
147
 
184
185
 
185
186
                        // if this is the first organisation added, or it's a preferred
186
187
                        // organisation and the current primary organisation isn't, then
187
 
                        // record this as the primary organisation.
 
188
                        // record this as the primary organisation
188
189
                        if( _primary_organisation == null ||
189
190
                                ( is_preferred && !_primary_organisation_is_preferred ) )
190
191
                        {
238
239
                        // and the current primary number isn't, or this number is on equal
239
240
                        // standing with the primary number in terms of preference and it is
240
241
                        // a voice number and the primary number isn't, then record this as
241
 
                        // the primary number.
 
242
                        // the primary number
242
243
                        if( _primary_number == null ||
243
244
                                ( is_preferred && !_primary_number_is_preferred ) ||
244
245
                                ( is_preferred == _primary_number_is_preferred &&
290
291
 
291
292
                        // if this is the first email added, or it's a preferred email and
292
293
                        // the current primary organisation isn't, then record this as the
293
 
                        // primary email.
 
294
                        // primary email
294
295
                        if( _primary_email == null ||
295
296
                                ( is_preferred && !_primary_email_is_preferred ) )
296
297
                        {
344
345
                        return _addresses;
345
346
                }
346
347
 
 
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
 
347
365
                protected void finalise()
348
366
                        throws ContactNotIdentifiableException
349
367
                {
350
 
                        // ensure that if there is a primary number, it is preferred so
351
 
                        // that there is always one preferred number. Android will assign
 
368
                        // Ensure that if there is a primary number, it is preferred so
 
369
                        // that there is always one preferred number.  Android will assign
352
370
                        // preference to one anyway so we might as well decide one sensibly.
353
371
                        if( _primary_number != null ) {
354
372
                                PreferredDetail data = _numbers.get( _primary_number );
372
390
 
373
391
                        // create a cache identifier from this contact data, which can be
374
392
                        // used to look-up an existing contact
375
 
                        _cache_identifier = ContactsCache.createIdentifier( this );
 
393
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
376
394
                        if( _cache_identifier == null )
377
395
                                throw new ContactNotIdentifiableException();
378
396
                }
609
627
        /**
610
628
         * Should we skip a contact, given whether it exists or not and the current
611
629
         * merge setting?  This routine handles throwing up a prompt, if required.
 
630
         *
612
631
         * @param contact_detail the display name of the contact
613
632
         * @param exists true if this contact matches one in the cache
614
633
         * @param merge_setting the merge setting to use
668
687
        {
669
688
                checkAbort();
670
689
 
671
 
                // It is expected that we use contact.getCacheIdentifier() here. The
 
690
                // It is expected that we use contact.getCacheIdentifier() here.  The
672
691
                // contact we are passed should have been successfully finalise()d,
673
692
                // which includes generating a valid cache identifier.
674
693
                ContactsCache.CacheIdentifier cache_identifier =
741
760
                                importContactAddresses( id, contact.getAddresses() );
742
761
                        if( contact.hasOrganisations() )
743
762
                                importContactOrganisations( id, contact.getOrganisations() );
 
763
                        if( contact.hasNotes() )
 
764
                                importContactNotes( id, contact.getNotes() );
744
765
                }
745
766
                catch( Backend.ContactCreationException e )
746
767
                {
759
780
                        String number = i.next();
760
781
                        ContactData.PreferredDetail data = datas.get( number );
761
782
 
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).
 
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).
769
790
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
770
791
                                continue;
771
792
 
790
811
                        ContactData.PreferredDetail data = datas.get( email );
791
812
 
792
813
                        // we don't want to add this email address if it exists already or
793
 
                        // we would introduce duplicates.
 
814
                        // we would introduce duplicates
794
815
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
795
816
                                continue;
796
817
 
853
874
                }
854
875
        }
855
876
 
 
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
 
856
901
        synchronized protected void checkAbort() throws AbortImportException
857
902
        {
858
903
                if( _abort ) {