package com.alexstyl.specialdates.contact;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.View;

import com.alexstyl.specialdates.DisplayName;
import com.alexstyl.specialdates.Optional;
import com.alexstyl.specialdates.contact.actions.ContactActionFactory;
import com.alexstyl.specialdates.contact.actions.LabeledAction;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.util.ContactUtils;

import java.util.ArrayList;
import java.util.List;

public class DeviceContact extends Contact {

    private final String lookupKey;

    DeviceContact(long contactId, DisplayName displayName, String lookupKey, Optional<Date> dateOfBirth) {
        super(contactId, displayName, dateOfBirth);
        this.lookupKey = lookupKey;
    }

    @Override
    public Uri getLookupUri() {
        return Contacts.getLookupUri(getContactID(), lookupKey);
    }

    @Override
    public String getImagePath() {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, getContactID());
        return contactUri.toString();
    }

    /**
     * Displays the information window for this contact.
     * </p>
     *
     * @param context
     * @param view    The view that was clicked in order to display the winod
     */
    @Override
    public void displayQuickInfo(Context context, View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
        intent.setData(uri);
        context.startActivity(intent);
    }

    @Override
    protected List<LabeledAction> onBuildActions(Context context) {
        final ContentResolver resolver = context.getContentResolver();
        boolean hasPhoneNumber = ContactUtils.hasPhoneNumber(resolver, getContactID());
        List<LabeledAction> actions = new ArrayList<>(2);
        if (hasPhoneNumber) {
            actions.add(ContactActionFactory.get().createCallAction(getContactID()));
            actions.add(ContactActionFactory.get().createSMSAction(getContactID()));
        }

        boolean hasEmails = ContactUtils.hasEmail(resolver, getContactID());
        if (hasEmails) {
            actions.add(ContactActionFactory.get().createEmailAction(getContactID()));
        }

        return actions;
    }

}

@@ -37,16 +37,66 @@
 ntact;%0A%0A
+import android.content.ActivityNotFoundException;%0A
 import a
@@ -368,16 +368,45 @@
 ew.View;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -488,16 +488,52 @@
 tional;%0A
+import com.alexstyl.specialdates.R;%0A
 import c
@@ -1484,30 +1484,8 @@
   *%0A
-     * @param context%0A
     
@@ -1499,19 +1499,16 @@
 am view 
-   
 The view
@@ -1636,24 +1636,42 @@
 iew view) %7B%0A
+        try %7B%0A    
         Inte
@@ -1714,16 +1714,20 @@
 _VIEW);%0A
+    
         
@@ -1820,24 +1820,28 @@
 ontactID));%0A
+    
         inte
@@ -1865,16 +1865,20 @@
         
+    
 context.
@@ -1900,16 +1900,162 @@
 ntent);%0A
+        %7D catch (ActivityNotFoundException ex) %7B%0A            Toast.makeText(context, R.string.no_app_found, Toast.LENGTH_SHORT).show();%0A        %7D%0A
     %7D%0A%0A 

Handle ActivityNotFoundException when launching contact info