/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/org/waxworlds/importcontacts/Importer.java

  • Committer: edam
  • Date: 2009-01-11 16:47:01 UTC
  • Revision ID: edam@waxworlds.org-20090111164701-tvnnv5axo6jd7ck0
- added GPL header comments to all files
- added GPLv3 to project as COPYING

Show diffs side-by-side

added added

removed removed

21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package org.waxworlds.importcontacts;
25
25
 
26
26
import java.util.HashMap;
27
27
import java.util.HashSet;
40
40
 
41
41
public class Importer extends Thread
42
42
{
 
43
        public final static int ACTION_GOBACK = 0;
43
44
        public final static int ACTION_ABORT = 1;
44
45
        public final static int ACTION_ALLDONE = 2;
45
46
 
142
143
                }
143
144
        }
144
145
 
145
 
        @SuppressWarnings("serial")
146
146
        protected class AbortImportException extends Exception { };
147
147
 
148
148
        public Importer( Doit doit )
150
150
                _doit = doit;
151
151
 
152
152
                SharedPreferences prefs = getSharedPreferences();
153
 
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
 
153
                _mergeSetting = prefs.getInt( "merge_setting", 0 );
154
154
        }
155
155
 
156
156
        @Override
230
230
                        wait();
231
231
                }
232
232
                catch( InterruptedException e ) { }
233
 
 
234
233
                // no need to check if an abortion happened during the wait, we are
235
234
                // about to finish anyway!
236
235
                finish( ACTION_ABORT );
251
250
                        wait();
252
251
                }
253
252
                catch( InterruptedException e ) { }
254
 
 
255
253
                // no need to check if an abortion happened during the wait, we are
256
254
                // about to finish anyway!
257
255
                finish( ACTION_ABORT );
317
315
                int message;
318
316
                switch( action )
319
317
                {
320
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
 
318
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
 
319
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
321
320
                default:        // fall through
322
 
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
 
321
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
323
322
                }
324
323
                _doit._handler.sendEmptyMessage( message );
325
324
 
347
346
                // handle special cases
348
347
                switch( mergeSetting )
349
348
                {
350
 
                case Doit.ACTION_KEEP:
 
349
                case R.id.merge_keep:
351
350
                        // if we keep contacts on duplicate, we better check for one
352
351
                        return !_contacts.containsKey( name );
353
352
 
354
 
                case Doit.ACTION_PROMPT:
 
353
                case R.id.merge_prompt:
355
354
                        // if we are prompting on duplicate, we better check for one
356
355
                        if( !_contacts.containsKey( name ) )
357
356
                                return true;
403
402
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
404
403
                {
405
404
                        // should we skip this import altogether?
406
 
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
 
405
                        if( _lastMergeDecision == R.id.merge_keep ) return;
407
406
 
408
407
                        // get contact's URI
409
408
                        contactUri = ContentUris.withAppendedId(
410
409
                                        Contacts.People.CONTENT_URI, id );
411
410
 
412
411
                        // should we destroy the existing contact before importing?
413
 
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
 
412
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
414
413
                                _doit.getContentResolver().delete( contactUri, null, null );
415
414
                                contactUri = null;
416
415
 
434
433
                        id = ContentUris.parseId( contactUri );
435
434
                        if( id <= 0 ) return;   // shouldn't happen!
436
435
 
437
 
                        // try to add them to the "My Contacts" group
438
 
                        try {
439
 
                                Contacts.People.addToMyContactsGroup(
440
 
                                        _doit.getContentResolver(), id );
441
 
                        }
442
 
                        catch( IllegalStateException e ) { }
 
436
                        // add them to the "My Contacts" group
 
437
                        Contacts.People.addToGroup(
 
438
                                        _doit.getContentResolver(), id,
 
439
                                        Contacts.Groups.GROUP_MY_CONTACTS );
443
440
 
444
441
                        // update cache
445
442
                        _contacts.put( contact._name, id );
468
465
                Long contactId = ContentUris.parseId( contactUri );
469
466
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
470
467
                                Contacts.People.Phones.CONTENT_DIRECTORY );
471
 
                Set< String > phonesKeys = phones.keySet();
472
468
 
473
469
                // add phone numbers
474
 
                Iterator< String > i = phonesKeys.iterator();
 
470
                Set phonesKeys = phones.keySet();
 
471
                Iterator i = phonesKeys.iterator();
475
472
                while( i.hasNext() ) {
476
473
                        ContactData.PhoneData phone = phones.get( i.next() );
477
474
 
494
491
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
495
492
                        _doit.getContentResolver().insert( contactPhonesUri, values );
496
493
                }
497
 
 
498
 
                // now add those phone numbers to the cache to prevent the addition of
499
 
                // duplicate data from another file
500
 
                i = phonesKeys.iterator();
501
 
                while( i.hasNext() ) {
502
 
                        ContactData.PhoneData phone = phones.get( i.next() );
503
 
 
504
 
                        String number = sanitisePhoneNumber( phone._number );
505
 
                        if( number != null ) {
506
 
                                HashSet< String > numbers = _contactNumbers.get( contactId );
507
 
                                if( numbers == null ) {
508
 
                                        _contactNumbers.put( contactId, new HashSet< String >() );
509
 
                                        numbers = _contactNumbers.get( contactId );
510
 
                                }
511
 
                                numbers.add( number );
512
 
                        }
513
 
                }
514
494
        }
515
495
 
516
496
        private void importContactEmails( Uri contactUri,
519
499
                Long contactId = ContentUris.parseId( contactUri );
520
500
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
521
501
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
522
 
                Set< String > emailsKeys = emails.keySet();
523
502
 
524
 
                // add email addresses
525
 
                Iterator< String > i = emailsKeys.iterator();
 
503
                // add phone numbers
 
504
                Set emailsKeys = emails.keySet();
 
505
                Iterator i = emailsKeys.iterator();
526
506
                while( i.hasNext() ) {
527
507
                        ContactData.EmailData email = emails.get( i.next() );
528
508
 
543
523
                        _doit.getContentResolver().insert( contactContactMethodsUri,
544
524
                                        values );
545
525
                }
546
 
 
547
 
                // now add those email addresses to the cache to prevent the addition of
548
 
                // duplicate data from another file
549
 
                i = emailsKeys.iterator();
550
 
                while( i.hasNext() ) {
551
 
                        ContactData.EmailData email = emails.get( i.next() );
552
 
 
553
 
                        String address = sanitiseEmailAddress( email.getAddress() );
554
 
                        if( address != null ) {
555
 
                                HashSet< String > addresses = _contactEmails.get( contactId );
556
 
                                if( addresses == null ) {
557
 
                                        _contactEmails.put( contactId, new HashSet< String >() );
558
 
                                        addresses = _contactEmails.get( contactId );
559
 
                                }
560
 
                                addresses.add( address );
561
 
                        }
562
 
                }
563
526
        }
564
527
 
565
528
        synchronized protected void checkAbort() throws AbortImportException