/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-10-20 17:51:32 UTC
  • Revision ID: tim@ed.am-20131020175132-lvqrbal1ztz5jepl
updated .bzrignore

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2013 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
                protected String _birthday = null;
144
146
 
145
147
                private ContactsCache.CacheIdentifier _cache_identifier = null;
146
148
 
184
186
 
185
187
                        // if this is the first organisation added, or it's a preferred
186
188
                        // organisation and the current primary organisation isn't, then
187
 
                        // record this as the primary organisation.
 
189
                        // record this as the primary organisation
188
190
                        if( _primary_organisation == null ||
189
191
                                ( is_preferred && !_primary_organisation_is_preferred ) )
190
192
                        {
238
240
                        // and the current primary number isn't, or this number is on equal
239
241
                        // standing with the primary number in terms of preference and it is
240
242
                        // a voice number and the primary number isn't, then record this as
241
 
                        // the primary number.
 
243
                        // the primary number
242
244
                        if( _primary_number == null ||
243
245
                                ( is_preferred && !_primary_number_is_preferred ) ||
244
246
                                ( is_preferred == _primary_number_is_preferred &&
290
292
 
291
293
                        // if this is the first email added, or it's a preferred email and
292
294
                        // the current primary organisation isn't, then record this as the
293
 
                        // primary email.
 
295
                        // primary email
294
296
                        if( _primary_email == null ||
295
297
                                ( is_preferred && !_primary_email_is_preferred ) )
296
298
                        {
344
346
                        return _addresses;
345
347
                }
346
348
 
 
349
                protected void addNote( String note )
 
350
                {
 
351
                        if( _notes == null ) _notes = new HashSet< String >();
 
352
                        if( !_notes.contains( note ) )
 
353
                                _notes.add( note );
 
354
                }
 
355
 
 
356
                public boolean hasNotes()
 
357
                {
 
358
                        return _notes != null && _notes.size() > 0;
 
359
                }
 
360
 
 
361
                public HashSet< String > getNotes()
 
362
                {
 
363
                        return _notes;
 
364
                }
 
365
 
 
366
                public void setBirthday( String birthday )
 
367
                {
 
368
                        _birthday = birthday;
 
369
                }
 
370
 
 
371
                public boolean hasBirthday()
 
372
                {
 
373
                        return _birthday != null;
 
374
                }
 
375
 
 
376
                public String getBirthday()
 
377
                {
 
378
                        return _birthday;
 
379
                }
 
380
 
347
381
                protected void finalise()
348
382
                        throws ContactNotIdentifiableException
349
383
                {
350
 
                        // ensure that if there is a primary number, it is preferred so
351
 
                        // that there is always one preferred number. Android will assign
 
384
                        // Ensure that if there is a primary number, it is preferred so
 
385
                        // that there is always one preferred number.  Android will assign
352
386
                        // preference to one anyway so we might as well decide one sensibly.
353
387
                        if( _primary_number != null ) {
354
388
                                PreferredDetail data = _numbers.get( _primary_number );
372
406
 
373
407
                        // create a cache identifier from this contact data, which can be
374
408
                        // used to look-up an existing contact
375
 
                        _cache_identifier = ContactsCache.createIdentifier( this );
 
409
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
376
410
                        if( _cache_identifier == null )
377
411
                                throw new ContactNotIdentifiableException();
378
412
                }
425
459
                        setProgressMessage( R.string.doit_caching );
426
460
 
427
461
                        // create the appropriate backend
428
 
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
429
 
//                              _backend = new ContactsContractBackend( _doit );
430
 
//                      else
 
462
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
463
                                _backend = new ContactsContractBackend( _doit );
 
464
                        else
431
465
                                _backend = new ContactsBackend( _doit );
432
466
 
433
467
                        // create a cache of existing contacts and populate it
609
643
        /**
610
644
         * Should we skip a contact, given whether it exists or not and the current
611
645
         * merge setting?  This routine handles throwing up a prompt, if required.
 
646
         *
612
647
         * @param contact_detail the display name of the contact
613
648
         * @param exists true if this contact matches one in the cache
614
649
         * @param merge_setting the merge setting to use
668
703
        {
669
704
                checkAbort();
670
705
 
671
 
                // It is expected that we use contact.getCacheIdentifier() here. The
 
706
                // It is expected that we use contact.getCacheIdentifier() here.  The
672
707
                // contact we are passed should have been successfully finalise()d,
673
708
                // which includes generating a valid cache identifier.
674
709
                ContactsCache.CacheIdentifier cache_identifier =
741
776
                                importContactAddresses( id, contact.getAddresses() );
742
777
                        if( contact.hasOrganisations() )
743
778
                                importContactOrganisations( id, contact.getOrganisations() );
 
779
                        if( contact.hasNotes() )
 
780
                                importContactNotes( id, contact.getNotes() );
 
781
                        if( contact.hasBirthday() )
 
782
                                importContactBirthday( id, contact.getBirthday() );
744
783
                }
745
784
                catch( Backend.ContactCreationException e )
746
785
                {
759
798
                        String number = i.next();
760
799
                        ContactData.PreferredDetail data = datas.get( number );
761
800
 
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).
 
801
                        // We don't want to add this number if it's crap, or it already
 
802
                        // exists (which would cause a duplicate to be created).  We don't
 
803
                        // take in to account the type when checking for duplicates.  This
 
804
                        // is intentional: types aren't really very reliable.  We assume
 
805
                        // that if the number exists at all, it doesn't need importing.
 
806
                        // Because of this, we also can't update the cache (which we don't
 
807
                        // need to anyway, so it's not a problem).
769
808
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
770
809
                                continue;
771
810
 
790
829
                        ContactData.PreferredDetail data = datas.get( email );
791
830
 
792
831
                        // we don't want to add this email address if it exists already or
793
 
                        // we would introduce duplicates.
 
832
                        // we would introduce duplicates
794
833
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
795
834
                                continue;
796
835
 
853
892
                }
854
893
        }
855
894
 
 
895
        private void importContactNotes( Long id, HashSet< String > datas )
 
896
                throws ContactCreationException
 
897
        {
 
898
                // add notes
 
899
                Iterator< String > i = datas.iterator();
 
900
                while( i.hasNext() ) {
 
901
                        String note = i.next();
 
902
 
 
903
                        // we don't want to add this note if it exists already or we would
 
904
                        // introduce duplicates
 
905
                        if( _contacts_cache.hasAssociatedNote( id, note ) )
 
906
                                continue;
 
907
 
 
908
                        // add note
 
909
                        _backend.addContactNote( id, note );
 
910
 
 
911
                        // and add this note to the cache to prevent a addition of duplicate
 
912
                        // date from another file
 
913
                        _contacts_cache.addAssociatedNote( id, note );
 
914
                }
 
915
        }
 
916
 
 
917
        private void importContactBirthday( Long id, String birthday )
 
918
                throws ContactCreationException
 
919
        {
 
920
                // we don't want to import this birthday if it already exists
 
921
                if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
 
922
                        return;
 
923
 
 
924
                // add birthday
 
925
                _backend.addContactBirthday( id, birthday );
 
926
 
 
927
                // and update the cache
 
928
                _contacts_cache.addAssociatedBirthday( id, birthday );
 
929
        }
 
930
 
856
931
        synchronized protected void checkAbort() throws AbortImportException
857
932
        {
858
933
                if( _abort ) {