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

  • Committer: edam
  • Date: 2010-07-13 19:13:38 UTC
  • Revision ID: edam@waxworlds.org-20100713191338-e23wre83uellvtt4
Tags: 1.0
- updated NEWS, TODO and manifest

Show diffs side-by-side

added added

removed removed

1
 
package org.waxworlds.importcontacts;
 
1
/*
 
2
 * Importer.java
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
5
 *
 
6
 * This file is part of the Import Contacts program (hereafter referred
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
9
 *
 
10
 * This program is free software: you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
 
23
 
 
24
package org.waxworlds.edam.importcontacts;
2
25
 
3
26
import java.util.HashMap;
4
27
import java.util.HashSet;
17
40
 
18
41
public class Importer extends Thread
19
42
{
20
 
        public final static int ACTION_GOBACK = 0;
21
43
        public final static int ACTION_ABORT = 1;
22
44
        public final static int ACTION_ALLDONE = 2;
23
45
 
120
142
                }
121
143
        }
122
144
 
 
145
        @SuppressWarnings("serial")
123
146
        protected class AbortImportException extends Exception { };
124
147
 
125
148
        public Importer( Doit doit )
127
150
                _doit = doit;
128
151
 
129
152
                SharedPreferences prefs = getSharedPreferences();
130
 
                _mergeSetting = prefs.getInt( "merge_setting", 0 );
 
153
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
131
154
        }
132
155
 
133
156
        @Override
207
230
                        wait();
208
231
                }
209
232
                catch( InterruptedException e ) { }
 
233
 
210
234
                // no need to check if an abortion happened during the wait, we are
211
235
                // about to finish anyway!
212
236
                finish( ACTION_ABORT );
227
251
                        wait();
228
252
                }
229
253
                catch( InterruptedException e ) { }
 
254
 
230
255
                // no need to check if an abortion happened during the wait, we are
231
256
                // about to finish anyway!
232
257
                finish( ACTION_ABORT );
292
317
                int message;
293
318
                switch( action )
294
319
                {
295
 
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
296
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
 
320
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
297
321
                default:        // fall through
298
 
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
 
322
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
299
323
                }
300
324
                _doit._handler.sendEmptyMessage( message );
301
325
 
323
347
                // handle special cases
324
348
                switch( mergeSetting )
325
349
                {
326
 
                case R.id.merge_keep:
 
350
                case Doit.ACTION_KEEP:
327
351
                        // if we keep contacts on duplicate, we better check for one
328
352
                        return !_contacts.containsKey( name );
329
353
 
330
 
                case R.id.merge_prompt:
 
354
                case Doit.ACTION_PROMPT:
331
355
                        // if we are prompting on duplicate, we better check for one
332
356
                        if( !_contacts.containsKey( name ) )
333
357
                                return true;
379
403
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
380
404
                {
381
405
                        // should we skip this import altogether?
382
 
                        if( _lastMergeDecision == R.id.merge_keep ) return;
 
406
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
383
407
 
384
408
                        // get contact's URI
385
409
                        contactUri = ContentUris.withAppendedId(
386
410
                                        Contacts.People.CONTENT_URI, id );
387
411
 
388
412
                        // should we destroy the existing contact before importing?
389
 
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
 
413
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
390
414
                                _doit.getContentResolver().delete( contactUri, null, null );
391
415
                                contactUri = null;
392
416
 
410
434
                        id = ContentUris.parseId( contactUri );
411
435
                        if( id <= 0 ) return;   // shouldn't happen!
412
436
 
413
 
                        // add them to the "My Contacts" group
414
 
                        Contacts.People.addToGroup(
415
 
                                        _doit.getContentResolver(), id,
416
 
                                        Contacts.Groups.GROUP_MY_CONTACTS );
 
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 ) { }
417
443
 
418
444
                        // update cache
419
445
                        _contacts.put( contact._name, id );
442
468
                Long contactId = ContentUris.parseId( contactUri );
443
469
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
444
470
                                Contacts.People.Phones.CONTENT_DIRECTORY );
 
471
                Set< String > phonesKeys = phones.keySet();
445
472
 
446
473
                // add phone numbers
447
 
                Set phonesKeys = phones.keySet();
448
 
                Iterator i = phonesKeys.iterator();
 
474
                Iterator< String > i = phonesKeys.iterator();
449
475
                while( i.hasNext() ) {
450
476
                        ContactData.PhoneData phone = phones.get( i.next() );
451
477
 
468
494
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
469
495
                        _doit.getContentResolver().insert( contactPhonesUri, values );
470
496
                }
 
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
                }
471
514
        }
472
515
 
473
516
        private void importContactEmails( Uri contactUri,
476
519
                Long contactId = ContentUris.parseId( contactUri );
477
520
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
478
521
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
522
                Set< String > emailsKeys = emails.keySet();
479
523
 
480
 
                // add phone numbers
481
 
                Set emailsKeys = emails.keySet();
482
 
                Iterator i = emailsKeys.iterator();
 
524
                // add email addresses
 
525
                Iterator< String > i = emailsKeys.iterator();
483
526
                while( i.hasNext() ) {
484
527
                        ContactData.EmailData email = emails.get( i.next() );
485
528
 
500
543
                        _doit.getContentResolver().insert( contactContactMethodsUri,
501
544
                                        values );
502
545
                }
 
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
                }
503
563
        }
504
564
 
505
565
        synchronized protected void checkAbort() throws AbortImportException