/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-13 06:35:26 UTC
  • Revision ID: edam@waxworlds.org-20090113063526-l9t1s9git4bav60a
- new contact's phone numebrs and email addresses are added to the caches after those contacts are updated to account for the situation where the same contact is imported again from another file (or the contact exists twice in the same file!?)

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();
 
468
                Set phonesKeys = phones.keySet();
472
469
 
473
470
                // add phone numbers
474
 
                Iterator< String > i = phonesKeys.iterator();
 
471
                Iterator i = phonesKeys.iterator();
475
472
                while( i.hasNext() ) {
476
473
                        ContactData.PhoneData phone = phones.get( i.next() );
477
474
 
519
516
                Long contactId = ContentUris.parseId( contactUri );
520
517
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
521
518
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
522
 
                Set< String > emailsKeys = emails.keySet();
 
519
                Set emailsKeys = emails.keySet();
523
520
 
524
521
                // add email addresses
525
 
                Iterator< String > i = emailsKeys.iterator();
 
522
                Iterator i = emailsKeys.iterator();
526
523
                while( i.hasNext() ) {
527
524
                        ContactData.EmailData email = emails.get( i.next() );
528
525