Problems rewriting Java to Python using Pyjnius

116 views
Skip to first unread message

Ed_S

unread,
Aug 17, 2015, 10:33:27 AM8/17/15
to Kivy users support
I have just learned that  new android contacts cannot be inserted explicitly. So intents won't work unless implicit( which work but need complex URIs).
To counter that i have tried to rewrite both recommended android methods but the application is crashing.
Please help me rewrite this into actionable python script. Regards!
######################################
###Here is my first futile.
##imports...
from jnius import cast
from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
Contacts = autoclass("android.provider.ContactsContract$Contacts")
Insert = autoclass('android.provider.ContactsContract$Intents$Insert')
Intents = autoclass('android.provider.ContactsContract$Intents')
RawContacts = autoclass("android.provider.ContactsContract$RawContacts")
System = autoclass("android.provider.Settings$System")
Email = autoclass("android.provider.ContactsContract$CommonDataKinds$Email")
###############################################################
ContentValues = autoclass("android.content.ContentValues")
Phone = autoclass("android.provider.ContactsContract$CommonDataKinds$Phone")
Data = autoclass("android.provider.ContactsContract$Data")
Context = autoclass("android.content.Context")
############
values = ContentValues()
values.put(Data.RAW_CONTACT_ID, "001")
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
values.put(Phone.NUMBER, "1-800-GOOG-411")
values.put(Phone.TYPE, Phone.TYPE_CUSTOM)
values.put(Phone.LABEL, "free directory assistance")
#dataUri = getContentResolver().insert(Data.CONTENT_URI, values)
currentActivity = cast("android.content.Context", PythonActivity.mActivity)
currentActivity.getContentResolver().insert(Data.CONTENT_URI, values)
###########################################################

#Here are the recomended methods 1
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);

##Here is the recommended method 2
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
 ...
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
  .withValue(RawContacts.ACCOUNT_TYPE, accountType)
  .withValue(RawContacts.ACCOUNT_NAME, accountName)
  .build());

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
  .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
  .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
  .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
  .build());

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Reply all
Reply to author
Forward
0 new messages