package com.asimkhanal.alarmclock;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;

import java.util.Random;

public class InitialContactsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_initial_contacts);

        // In any activity just pass the context and use the singleton method
        DatabaseHelper db = DatabaseHelper.getInstance(this);

        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        while (cursor.moveToNext()) {
            String id = "";
            String name = "";
            String phoneNumber = "";

            int idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            if (idIndex != -1) {
                id = cursor.getString(idIndex);
            }

            int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            if (nameIndex != -1) {
                name = cursor.getString(nameIndex);
            }

            int hasPhoneNumberIndex = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);

            if (Integer.parseInt(cursor.getString(hasPhoneNumberIndex)) > 0) {
                Cursor phoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);

                while (phoneCur.moveToNext()) {
                    int phoneIndex = phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    if (phoneIndex != -1) {
                        phoneNumber = phoneCur.getString(phoneIndex);
                    }
                }

                phoneCur.close();
            }

            String tier;

            int tierNum = new Random().nextInt(3);

            switch (tierNum) {
                case 0:
                    tier = "low";
                    break;
                case 1:
                    tier = "medium";
                    break;
                case 2:
                default:
                    tier = "high";
                    break;
            }

            Contact contact = new Contact();
            contact.name = name;
            contact.phone_number = phoneNumber;
            contact.tier = tier.toUpperCase();

            db.addOrUpdateContact(contact);

            Log.d("contact", contact.toString());
        }
        cursor.close();

        finish();
    }
}

@@ -832,21 +832,16 @@
 tring id
- = %22%22
 ;%0A      
@@ -857,21 +857,16 @@
 ing name
- = %22%22
 ;%0A      
@@ -895,16 +895,41 @@
 er = %22%22;
+%0A            String tier;
 %0A%0A      
@@ -1006,45 +1006,8 @@
 D);%0A
-            if (idIndex != -1) %7B%0A    
     
@@ -1041,38 +1041,24 @@
 ng(idIndex);
-%0A            %7D
 %0A%0A          
@@ -1142,47 +1142,8 @@
 E);%0A
-            if (nameIndex != -1) %7B%0A    
     
@@ -1185,30 +1185,16 @@
 eIndex);
-%0A            %7D
 %0A%0A      
@@ -1647,32 +1647,151 @@
 moveToNext()) %7B%0A
+                    int type = phoneCur.getInt(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));%0A%0A
                 
@@ -1911,24 +1911,66 @@
 if (
-phoneIndex != -1
+type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
 ) %7B%0A
@@ -2133,34 +2133,8 @@
  %7D%0A%0A
-            String tier;%0A%0A
     
@@ -2180,17 +2180,16 @@
 Int(3);%0A
-%0A
         
@@ -2700,39 +2700,175 @@
     
-db.addOrUpdateContact(contact);
+if (contact.name.equals(%22%22) %7C%7C contact.phone_number.equals(%22%22)) %7B%0A                Log.d(%22contactNotAdded%22, contact.toString());%0A                continue;%0A            %7D
 %0A%0A  
@@ -2906,32 +2906,77 @@
 act.toString());
+%0A%0A            db.addOrUpdateContact(contact);
 %0A        %7D%0A     

check for mobile phone number type