/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 12:58:54 UTC
  • Revision ID: edam@waxworlds.org-20090111125854-u8ofzso4jatk12me
- added "all done" message
- rewrote Importer.finish() to make the exit process more consistent
- moved Doit's message definitions to Doit
- ensure the importer is destroyed in Doit.onPause()
- only show the toaster popup if a) there is an importer to abort, and b) the abort actually did something (i.e., it's not already aborted)
- bugfix: added some checks for abortion to the Importer after wait()ing after a dialog. Importer.wake() now alsy does a notify() to break out of dialog waits.
- also, made Importer.checkAbort() protected, so it can be checked from specific importers as desired

Show diffs side-by-side

added added

removed removed

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;
 
1
package org.waxworlds.importcontacts;
25
2
 
26
3
import java.util.HashMap;
27
4
import java.util.HashSet;
40
17
 
41
18
public class Importer extends Thread
42
19
{
 
20
        public final static int ACTION_GOBACK = 0;
43
21
        public final static int ACTION_ABORT = 1;
44
22
        public final static int ACTION_ALLDONE = 2;
45
23
 
142
120
                }
143
121
        }
144
122
 
145
 
        @SuppressWarnings("serial")
146
123
        protected class AbortImportException extends Exception { };
147
124
 
148
125
        public Importer( Doit doit )
150
127
                _doit = doit;
151
128
 
152
129
                SharedPreferences prefs = getSharedPreferences();
153
 
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
 
130
                _mergeSetting = prefs.getInt( "merge_setting", 0 );
154
131
        }
155
132
 
156
133
        @Override
230
207
                        wait();
231
208
                }
232
209
                catch( InterruptedException e ) { }
233
 
 
234
210
                // no need to check if an abortion happened during the wait, we are
235
211
                // about to finish anyway!
236
212
                finish( ACTION_ABORT );
251
227
                        wait();
252
228
                }
253
229
                catch( InterruptedException e ) { }
254
 
 
255
230
                // no need to check if an abortion happened during the wait, we are
256
231
                // about to finish anyway!
257
232
                finish( ACTION_ABORT );
317
292
                int message;
318
293
                switch( action )
319
294
                {
320
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
 
295
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
 
296
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
321
297
                default:        // fall through
322
 
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
 
298
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
323
299
                }
324
300
                _doit._handler.sendEmptyMessage( message );
325
301
 
347
323
                // handle special cases
348
324
                switch( mergeSetting )
349
325
                {
350
 
                case Doit.ACTION_KEEP:
 
326
                case R.id.merge_keep:
351
327
                        // if we keep contacts on duplicate, we better check for one
352
328
                        return !_contacts.containsKey( name );
353
329
 
354
 
                case Doit.ACTION_PROMPT:
 
330
                case R.id.merge_prompt:
355
331
                        // if we are prompting on duplicate, we better check for one
356
332
                        if( !_contacts.containsKey( name ) )
357
333
                                return true;
403
379
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
404
380
                {
405
381
                        // should we skip this import altogether?
406
 
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
 
382
                        if( _lastMergeDecision == R.id.merge_keep ) return;
407
383
 
408
384
                        // get contact's URI
409
385
                        contactUri = ContentUris.withAppendedId(
410
386
                                        Contacts.People.CONTENT_URI, id );
411
387
 
412
388
                        // should we destroy the existing contact before importing?
413
 
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
 
389
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
414
390
                                _doit.getContentResolver().delete( contactUri, null, null );
415
391
                                contactUri = null;
416
392
 
434
410
                        id = ContentUris.parseId( contactUri );
435
411
                        if( id <= 0 ) return;   // shouldn't happen!
436
412
 
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 ) { }
 
413
                        // add them to the "My Contacts" group
 
414
                        Contacts.People.addToGroup(
 
415
                                        _doit.getContentResolver(), id,
 
416
                                        Contacts.Groups.GROUP_MY_CONTACTS );
443
417
 
444
418
                        // update cache
445
419
                        _contacts.put( contact._name, id );
468
442
                Long contactId = ContentUris.parseId( contactUri );
469
443
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
470
444
                                Contacts.People.Phones.CONTENT_DIRECTORY );
471
 
                Set< String > phonesKeys = phones.keySet();
472
445
 
473
446
                // add phone numbers
474
 
                Iterator< String > i = phonesKeys.iterator();
 
447
                Set phonesKeys = phones.keySet();
 
448
                Iterator i = phonesKeys.iterator();
475
449
                while( i.hasNext() ) {
476
450
                        ContactData.PhoneData phone = phones.get( i.next() );
477
451
 
494
468
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
495
469
                        _doit.getContentResolver().insert( contactPhonesUri, values );
496
470
                }
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
471
        }
515
472
 
516
473
        private void importContactEmails( Uri contactUri,
519
476
                Long contactId = ContentUris.parseId( contactUri );
520
477
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
521
478
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
522
 
                Set< String > emailsKeys = emails.keySet();
523
479
 
524
 
                // add email addresses
525
 
                Iterator< String > i = emailsKeys.iterator();
 
480
                // add phone numbers
 
481
                Set emailsKeys = emails.keySet();
 
482
                Iterator i = emailsKeys.iterator();
526
483
                while( i.hasNext() ) {
527
484
                        ContactData.EmailData email = emails.get( i.next() );
528
485
 
543
500
                        _doit.getContentResolver().insert( contactContactMethodsUri,
544
501
                                        values );
545
502
                }
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
503
        }
564
504
 
565
505
        synchronized protected void checkAbort() throws AbortImportException