/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-04 14:44:48 UTC
  • Revision ID: edam@waxworlds.org-20100704144448-1m30v811opup20fs
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
- massively simplified the WizzardActivity class so it works propperly
- moved all code to org.waxworlds.edam
- added an "aborted" message when the importion is aborted
- simplified the 3 actions the worker thread can take when stopping (only 2 were actualy used) to "aborted" or "alldone"
- changed intro message to match website
- bugfix: don't blow up when the My Contacts group is missing

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.importcontacts;
 
24
package org.waxworlds.edam.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;
44
43
        public final static int ACTION_ABORT = 1;
45
44
        public final static int ACTION_ALLDONE = 2;
46
45
 
143
142
                }
144
143
        }
145
144
 
 
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", 0 );
 
153
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
154
154
        }
155
155
 
156
156
        @Override
230
230
                        wait();
231
231
                }
232
232
                catch( InterruptedException e ) { }
 
233
 
233
234
                // no need to check if an abortion happened during the wait, we are
234
235
                // about to finish anyway!
235
236
                finish( ACTION_ABORT );
250
251
                        wait();
251
252
                }
252
253
                catch( InterruptedException e ) { }
 
254
 
253
255
                // no need to check if an abortion happened during the wait, we are
254
256
                // about to finish anyway!
255
257
                finish( ACTION_ABORT );
315
317
                int message;
316
318
                switch( action )
317
319
                {
318
 
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
319
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
 
320
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
320
321
                default:        // fall through
321
 
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
 
322
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
322
323
                }
323
324
                _doit._handler.sendEmptyMessage( message );
324
325
 
346
347
                // handle special cases
347
348
                switch( mergeSetting )
348
349
                {
349
 
                case R.id.merge_keep:
 
350
                case Doit.ACTION_KEEP:
350
351
                        // if we keep contacts on duplicate, we better check for one
351
352
                        return !_contacts.containsKey( name );
352
353
 
353
 
                case R.id.merge_prompt:
 
354
                case Doit.ACTION_PROMPT:
354
355
                        // if we are prompting on duplicate, we better check for one
355
356
                        if( !_contacts.containsKey( name ) )
356
357
                                return true;
402
403
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
403
404
                {
404
405
                        // should we skip this import altogether?
405
 
                        if( _lastMergeDecision == R.id.merge_keep ) return;
 
406
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
406
407
 
407
408
                        // get contact's URI
408
409
                        contactUri = ContentUris.withAppendedId(
409
410
                                        Contacts.People.CONTENT_URI, id );
410
411
 
411
412
                        // should we destroy the existing contact before importing?
412
 
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
 
413
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
413
414
                                _doit.getContentResolver().delete( contactUri, null, null );
414
415
                                contactUri = null;
415
416
 
433
434
                        id = ContentUris.parseId( contactUri );
434
435
                        if( id <= 0 ) return;   // shouldn't happen!
435
436
 
436
 
                        // add them to the "My Contacts" group
437
 
                        Contacts.People.addToGroup(
438
 
                                        _doit.getContentResolver(), id,
439
 
                                        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 ) { }
440
443
 
441
444
                        // update cache
442
445
                        _contacts.put( contact._name, id );
465
468
                Long contactId = ContentUris.parseId( contactUri );
466
469
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
467
470
                                Contacts.People.Phones.CONTENT_DIRECTORY );
 
471
                Set< String > phonesKeys = phones.keySet();
468
472
 
469
473
                // add phone numbers
470
 
                Set phonesKeys = phones.keySet();
471
 
                Iterator i = phonesKeys.iterator();
 
474
                Iterator< String > i = phonesKeys.iterator();
472
475
                while( i.hasNext() ) {
473
476
                        ContactData.PhoneData phone = phones.get( i.next() );
474
477
 
491
494
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
492
495
                        _doit.getContentResolver().insert( contactPhonesUri, values );
493
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
                }
494
514
        }
495
515
 
496
516
        private void importContactEmails( Uri contactUri,
499
519
                Long contactId = ContentUris.parseId( contactUri );
500
520
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
501
521
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
522
                Set< String > emailsKeys = emails.keySet();
502
523
 
503
 
                // add phone numbers
504
 
                Set emailsKeys = emails.keySet();
505
 
                Iterator i = emailsKeys.iterator();
 
524
                // add email addresses
 
525
                Iterator< String > i = emailsKeys.iterator();
506
526
                while( i.hasNext() ) {
507
527
                        ContactData.EmailData email = emails.get( i.next() );
508
528
 
523
543
                        _doit.getContentResolver().insert( contactContactMethodsUri,
524
544
                                        values );
525
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
                }
526
563
        }
527
564
 
528
565
        synchronized protected void checkAbort() throws AbortImportException