Wednesday, 17 December 2014

Open Source Chat Application

1) Open source whatsapp alternative chat application
http://www.johannesdeml.com/blog/whatsapp-open-source-alternatives/


2) https://github.com/QuickBlox/q-municate-android

Q-municate is an open source code of a chat application with a wide range of communication features included (such as messaging, file transfer, push notifications, audio/video calls). http://q-municate.com/

Material Design

1) Recycle View
Implements recycle view with CardView

https://github.com/rdrobinson3/WelcomeToL

2) Navigation Drawer
https://github.com/lewisjdeane/L-Navigation-Drawer

3) Api Demo Source Code
https://android.googlesource.com/platform/development/+/master/samples/

https://github.com/kesenhoo/AndroidApiDemo

4)
Shared Element Animation Transition example adapted for Fragments.
https://github.com/yrizk/FragmentAnimations



Wednesday, 19 November 2014

A vCard parser library written in Java

https://code.google.com/p/ez-vcard/wiki/TypeList

https://code.google.com/p/ez-vcard/wiki/WhatIsAVCard

try
  {

   ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
   int rawContactInsertIndex = ops.size();

   ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
     .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

   VCard vcard = Ezvcard.parse(savedVCard).first();
   String name = null;
   try
   {
    if (vcard.getFormattedName() != null)
    {
     name = vcard.getFormattedName().getValue();
    }
   }
   catch (Exception e3)
   {
    e3.printStackTrace();
   }

   // Names
   if (name != null)
   {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
      .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
      .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name).build());
   }

   // Telephone number
   List<Telephone> telePhonevcard = vcard.getTelephoneNumbers();
   if (telePhonevcard != null)
   {
    for (Telephone telephone : telePhonevcard)
    {
     // ------------------------------------------------------ Home Numbers
     if (telephone.getTypes().toArray()[0].equals(TelephoneType.CELL))
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, telephone.getText())
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
     }
     else if (telephone.getTypes().toArray()[0].equals(TelephoneType.WORK))
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, telephone.getText())
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK).build());
     }
     else if (telephone.getTypes().toArray()[0].equals(TelephoneType.HOME))
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, telephone.getText())
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME).build());
     }
     else if (telephone.getTypes().toArray()[0].equals(TelephoneType.PREF))
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, telephone.getText())
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MAIN).build());
     }
     else if (telephone.getTypes().toArray()[0].equals(TelephoneType.VOICE))
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, telephone.getText())
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_OTHER).build());
     }
     else
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, telephone.getText())
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_OTHER).build());
     }

    }
   }
   // Photo
   try
   {
    byte[] data = null;
    List<Photo> photos = vcard.getPhotos();
    for (Photo photo : photos)
    {
     ImageType contentType = photo.getContentType(); // e.g. "image/jpeg"

     String url = photo.getUrl();
     if (url != null)
     {
      // property value is a URL
      continue;
     }

     data = photo.getData();
     if (data != null)
     {
      // INSERT imAGE
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, data).build());
      continue;
     }
    }
   }
   catch (Exception e2)
   {
    e2.printStackTrace();
   }

   String street = null;
   String city = null;
   String state = null;
   String zipcode = null;
   String country = null;

   // Get Address
   for (Address adr : vcard.getAddresses())
   {
    street = adr.getStreetAddress();
    city = adr.getLocality();
    state = adr.getRegion();
    zipcode = adr.getPostalCode();
    country = adr.getCountry();
    if (adr.getTypes().size() > 0)
    {
     if (adr.getTypes().toArray()[0].equals(AddressType.HOME))
     {
      ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, null)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
          ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
        // .withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
        .build());
     }
     else if (adr.getTypes().toArray()[0].equals(AddressType.WORK))
     {
      ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, null)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
          ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
        // .withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
        .build());
     }
     else
     {
      ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, null)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
          ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
        // .withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
        .build());
     }

    }
    else
    {
     ops.add(ContentProviderOperation
       .newInsert(ContactsContract.Data.CONTENT_URI)
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, null)
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
         ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER)
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
       // .withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
       .build());
    }

   }

   // Email address
   List<Email> emails = vcard.getEmails();
   for (Email email : emails)
   {
    if (email.getTypes().size() > 0)
    {
     if (email.getTypes().toArray()[0].equals(EmailType.HOME))
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, email.getValue())
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME).build());
     }
     else if (email.getTypes().toArray()[0].equals(EmailType.WORK))
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, email.getValue())
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK).build());
     }
     else
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, email.getValue())
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM).build());
     }
    }
    else
    {
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
       .withValue(ContactsContract.CommonDataKinds.Email.DATA, email.getValue())
       .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_OTHER).build());
    }
   }
   // Address
   Organization organization = null;
   try
   {
    organization = vcard.getOrganization();
    // ------------------------------------------------------ Organization
    if (organization != null && organization.getPids() != null)
    {
     ops.add(ContentProviderOperation
       .newInsert(ContactsContract.Data.CONTENT_URI)
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
       .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,
         organization.getPids().get(0) == null ? "" : organization.getPids().get(0))
       .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
       .withValue(ContactsContract.CommonDataKinds.Organization.TITLE,
         organization.getPids().get(1) == null ? "" : organization.getPids().get(1))
       .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
       .build());

    }
   }
   catch (Exception e1)
   {
    e1.printStackTrace();
   }
   // Website
   try
   {

    if (vcard.getUrls() != null)
    {
     List<Url> urls = vcard.getUrls();
     for (Url url : urls)
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Website.URL, url.getValue())
        .withValue(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_WORK).build());

     }

    }
   }
   catch (Exception e1)
   {
    e1.printStackTrace();
   }
   // Note
   try
   {

    if (vcard.getNotes() != null)
    {
     List<Note> notes = vcard.getNotes();
     for (Note note : notes)
     {
      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note.getValue()).build());
     }

    }
   }
   catch (Exception e1)
   {
    e1.printStackTrace();
   }
   // Asking the Contact provider to create a new contact
   try
   {
    context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    Toast.makeText(context.getApplicationContext(), "Contact saved successfully", Toast.LENGTH_SHORT).show();
   }
   catch (Exception e)
   {
    e.printStackTrace();
    // Toast.makeText(getApplicationContext(),"Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
   }

   // Instance Messanger RawProperty
   List<Impp> impps = vcard.getImpps();
   for (Impp impp : impps)
   {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
      .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
      .withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, impp.toString())
      .withValue(ContactsContract.CommonDataKinds.Im.TYPE, ContactsContract.CommonDataKinds.Im.TYPE_WORK).build());
   }

   // Address of the contact
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    cur.moveToLast();
    String contactID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
    intent.setData(uri);
    context.startActivity(intent);

   // Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
   // i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
   // i.putExtra(ContactsContract.Intents.Insert.NAME, name);
   // context.startActivity(i);

   //context.getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, new MyContentObserver());
  }
  catch (Exception e1)
  {
   e1.printStackTrace();
  }