/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: 2016-03-28 18:13:50 UTC
  • Revision ID: tim@ed.am-20160328181350-ytxvox60fxj1tevm
fixed typo in strings

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
142
142
                protected HashMap< String, PreferredDetail > _emails = null;
143
143
                protected HashMap< String, TypeDetail > _addresses = null;
144
144
                protected HashSet< String > _notes = null;
 
145
                protected String _birthday = null;
145
146
 
146
147
                private ContactsCache.CacheIdentifier _cache_identifier = null;
147
148
 
185
186
 
186
187
                        // if this is the first organisation added, or it's a preferred
187
188
                        // organisation and the current primary organisation isn't, then
188
 
                        // record this as the primary organisation.
 
189
                        // record this as the primary organisation
189
190
                        if( _primary_organisation == null ||
190
191
                                ( is_preferred && !_primary_organisation_is_preferred ) )
191
192
                        {
239
240
                        // and the current primary number isn't, or this number is on equal
240
241
                        // standing with the primary number in terms of preference and it is
241
242
                        // a voice number and the primary number isn't, then record this as
242
 
                        // the primary number.
 
243
                        // the primary number
243
244
                        if( _primary_number == null ||
244
245
                                ( is_preferred && !_primary_number_is_preferred ) ||
245
246
                                ( is_preferred == _primary_number_is_preferred &&
291
292
 
292
293
                        // if this is the first email added, or it's a preferred email and
293
294
                        // the current primary organisation isn't, then record this as the
294
 
                        // primary email.
 
295
                        // primary email
295
296
                        if( _primary_email == null ||
296
297
                                ( is_preferred && !_primary_email_is_preferred ) )
297
298
                        {
362
363
                        return _notes;
363
364
                }
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
 
365
381
                protected void finalise()
366
382
                        throws ContactNotIdentifiableException
367
383
                {
368
 
                        // ensure that if there is a primary number, it is preferred so
369
 
                        // 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
370
386
                        // preference to one anyway so we might as well decide one sensibly.
371
387
                        if( _primary_number != null ) {
372
388
                                PreferredDetail data = _numbers.get( _primary_number );
417
433
                        Matcher m = p.matcher( email );
418
434
                        if( m.matches() ) {
419
435
                                String[] bits = email.split( "@" );
420
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
 
436
                                return bits[ 0 ] + "@" +
 
437
                                        bits[ 1 ].toLowerCase( Locale.ENGLISH );
421
438
                        }
422
439
                        return null;
423
440
                }
527
544
                finish( ACTION_ABORT );
528
545
        }
529
546
 
530
 
        protected void showFatalError( int res ) throws AbortImportException
531
 
        {
532
 
                showFatalError( _doit.getText( res ).toString() );
533
 
        }
534
 
 
535
 
        synchronized protected void showFatalError( String message )
536
 
                        throws AbortImportException
537
 
        {
538
 
                checkAbort();
539
 
                _doit._handler.sendMessage( Message.obtain(
540
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
541
 
                try {
542
 
                        wait();
543
 
                }
544
 
                catch( InterruptedException e ) { }
545
 
 
546
 
                // no need to check if an abortion happened during the wait, we are
547
 
                // about to finish anyway!
548
 
                finish( ACTION_ABORT );
549
 
        }
550
 
 
551
 
        protected boolean showContinue( int res ) throws AbortImportException
552
 
        {
553
 
                return showContinue( _doit.getText( res ).toString() );
554
 
        }
555
 
 
556
 
        synchronized protected boolean showContinue( String message )
 
547
        protected void showContinueOrAbort( int res ) throws AbortImportException
 
548
        {
 
549
                showContinueOrAbort( _doit.getText( res ).toString() );
 
550
        }
 
551
 
 
552
        synchronized protected void showContinueOrAbort( String message )
557
553
                        throws AbortImportException
558
554
        {
559
555
                checkAbort();
564
560
                }
565
561
                catch( InterruptedException e ) { }
566
562
 
567
 
                // check if an abortion happened during the wait
568
 
                checkAbort();
569
 
 
570
 
                return _response == RESPONSE_POSITIVE;
 
563
                // if we're aborting, there's no need to check if an abortion happened
 
564
                // during the wait
 
565
                if( _response == RESPONSE_NEGATIVE )
 
566
                        finish( ACTION_ABORT );
 
567
                else
 
568
                        checkAbort();
571
569
        }
572
570
 
573
571
        protected void setProgressMessage( int res ) throws AbortImportException
627
625
        /**
628
626
         * Should we skip a contact, given whether it exists or not and the current
629
627
         * merge setting?  This routine handles throwing up a prompt, if required.
 
628
         *
630
629
         * @param contact_detail the display name of the contact
631
630
         * @param exists true if this contact matches one in the cache
632
631
         * @param merge_setting the merge setting to use
686
685
        {
687
686
                checkAbort();
688
687
 
689
 
                // It is expected that we use contact.getCacheIdentifier() here. The
 
688
                // It is expected that we use contact.getCacheIdentifier() here.  The
690
689
                // contact we are passed should have been successfully finalise()d,
691
690
                // which includes generating a valid cache identifier.
692
691
                ContactsCache.CacheIdentifier cache_identifier =
761
760
                                importContactOrganisations( id, contact.getOrganisations() );
762
761
                        if( contact.hasNotes() )
763
762
                                importContactNotes( id, contact.getNotes() );
 
763
                        if( contact.hasBirthday() )
 
764
                                importContactBirthday( id, contact.getBirthday() );
764
765
                }
765
766
                catch( Backend.ContactCreationException e )
766
767
                {
779
780
                        String number = i.next();
780
781
                        ContactData.PreferredDetail data = datas.get( number );
781
782
 
782
 
                        // we don't want to add this number if it's crap, or it already
783
 
                        // exists (which would cause a duplicate to be created). We don't
784
 
                        // take in to account the type when checking for duplicates. This is
785
 
                        // intentional: types aren't really very reliable. We assume that
786
 
                        // if the number exists at all, it doesn't need importing. Because
787
 
                        // of this, we also can't update the cache (which we don't need to
788
 
                        // 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).
789
790
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
790
791
                                continue;
791
792
 
810
811
                        ContactData.PreferredDetail data = datas.get( email );
811
812
 
812
813
                        // we don't want to add this email address if it exists already or
813
 
                        // we would introduce duplicates.
 
814
                        // we would introduce duplicates
814
815
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
815
816
                                continue;
816
817
 
873
874
                }
874
875
        }
875
876
 
876
 
        private void importContactNotes( Long id,
877
 
                HashSet< String > datas )
 
877
        private void importContactNotes( Long id, HashSet< String > datas )
878
878
                throws ContactCreationException
879
879
        {
880
880
                // add notes
894
894
                        // date from another file
895
895
                        _contacts_cache.addAssociatedNote( id, note );
896
896
                }
897
 
 
 
897
        }
 
898
 
 
899
        private void importContactBirthday( Long id, String birthday )
 
900
                throws ContactCreationException
 
901
        {
 
902
                // we don't want to import this birthday if it already exists
 
903
                if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
 
904
                        return;
 
905
 
 
906
                // add birthday
 
907
                _backend.addContactBirthday( id, birthday );
 
908
 
 
909
                // and update the cache
 
910
                _contacts_cache.addAssociatedBirthday( id, birthday );
898
911
        }
899
912
 
900
913
        synchronized protected void checkAbort() throws AbortImportException