use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class ContactCreator method createOrUpdateContact.
public void createOrUpdateContact(String first, String last, String formattedaddress, String streetaddress, String neighbourhood, String town, String postcode, String state, String country) {
String CONTACT_SELECTION = RawContactsEntity.MIMETYPE + " IS '" + CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "' AND " + CommonDataKinds.StructuredName.DISPLAY_NAME + " IS '" + first + " " + last + "'";
Cursor c;
c = me.getContentResolver().query(RawContactsEntity.CONTENT_URI, CONTACT_PROJECTION, CONTACT_SELECTION, null, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
if (c.getCount() > 0) {
// the contact already exists, update it
c.moveToFirst();
long contactId = c.getLong(0);
ContentProviderOperation.Builder op;
op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(ContactsContract.Data.CONTACT_ID + " = " + String.valueOf(contactId) + " AND " + ContactsContract.Data.MIMETYPE + " = '" + CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'", null).withValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, formattedaddress).withValue(CommonDataKinds.StructuredPostal.TYPE, CommonDataKinds.StructuredPostal.TYPE_HOME).withValue(CommonDataKinds.StructuredPostal.STREET, streetaddress).withValue(CommonDataKinds.StructuredPostal.NEIGHBORHOOD, neighbourhood).withValue(CommonDataKinds.StructuredPostal.CITY, town).withValue(CommonDataKinds.StructuredPostal.POSTCODE, postcode).withValue(CommonDataKinds.StructuredPostal.REGION, state).withValue(CommonDataKinds.StructuredPostal.COUNTRY, country);
ops.add(op.build());
} else {
// the contact doesn't exist, create it
ContentProviderOperation.Builder op;
// null seems to be a valid account, it puts it in phone local
op = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null);
ops.add(op.build());
// We don't handle middle names here, because it's a constructed
// contact name which we know doesn't have one.
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, first + " " + last).withValue(CommonDataKinds.StructuredName.GIVEN_NAME, first).withValue(CommonDataKinds.StructuredName.FAMILY_NAME, last);
ops.add(op.build());
// Phone number records seem to contain a duplicate of the number
// in DATA4, even though the Android class description doesn't
// mention this: we put it in to be on the safe side.
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(CommonDataKinds.Phone.NUMBER, "0").withValue(CommonDataKinds.Phone.TYPE, CommonDataKinds.Phone.TYPE_HOME).withValue(ContactsContract.Data.DATA4, "0");
ops.add(op.build());
op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE).withValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, formattedaddress).withValue(CommonDataKinds.StructuredPostal.TYPE, CommonDataKinds.StructuredPostal.TYPE_HOME).withValue(CommonDataKinds.StructuredPostal.STREET, streetaddress).withValue(CommonDataKinds.StructuredPostal.NEIGHBORHOOD, neighbourhood).withValue(CommonDataKinds.StructuredPostal.CITY, town).withValue(CommonDataKinds.StructuredPostal.POSTCODE, postcode).withValue(CommonDataKinds.StructuredPostal.REGION, state).withValue(CommonDataKinds.StructuredPostal.COUNTRY, country);
ops.add(op.build());
}
try {
me.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
new MyLog(me, (c.getCount() > 0 ? "Updating" : "Creating") + " contact " + first + " " + last + " threw exception " + e.getCause().toString() + " with message " + e.getMessage());
}
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class ContactCreator method logAllContacts.
// dump the entire contacts list to the log
// only used for debugging
public void logAllContacts() {
if (BuildConfig.DEBUG) {
new MyLog(me, "Dump of contacts:");
Cursor c;
c = me.getContentResolver().query(RawContactsEntity.CONTENT_URI, new String[] { RawContactsEntity._ID, RawContactsEntity.CONTACT_ID, RawContactsEntity.MIMETYPE, RawContactsEntity.DELETED, RawContactsEntity.DATA1, RawContactsEntity.DATA2, RawContactsEntity.DATA3, RawContactsEntity.DATA4, RawContactsEntity.DATA5, RawContactsEntity.DATA6, RawContactsEntity.DATA7, RawContactsEntity.DATA8, RawContactsEntity.DATA9, RawContactsEntity.DATA10, RawContactsEntity.DATA11, RawContactsEntity.DATA12, RawContactsEntity.DATA13, RawContactsEntity.DATA14, RawContactsEntity.DATA15 }, null, null, null);
while (c.moveToNext()) {
Cursor c1;
c1 = me.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE }, ContactsContract.RawContacts._ID + " IS " + String.valueOf(c.getLong(0)), null, null);
if (c1.moveToFirst()) {
dumpSingle(null, c, c1);
}
}
}
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method checkOrientationWait.
// return true if still waiting for correct orientation
private boolean checkOrientationWait(int classNum, boolean before) {
int wanted;
if (before) {
wanted = PrefsManager.getBeforeOrientation(this, classNum);
} else {
wanted = PrefsManager.getAfterOrientation(this, classNum);
}
if ((wanted == 0) || (wanted == PrefsManager.BEFORE_ANY_POSITION)) {
return false;
}
switch(PrefsManager.getOrientationState(this)) {
case // sensor currently not active
PrefsManager.ORIENTATION_IDLE:
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor ams = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (ams == null) {
return false;
}
lock();
PrefsManager.setOrientationState(this, PrefsManager.ORIENTATION_WAITING);
sm.registerListener(this, ams, SensorManager.SENSOR_DELAY_FASTEST);
new MyLog(this, "Requested accelerometer value for class ".concat(PrefsManager.getClassName(this, classNum)));
// FALLTHRU
case // waiting for value
PrefsManager.ORIENTATION_WAITING:
return true;
case // just got a value
PrefsManager.ORIENTATION_DONE:
nextAccelTime = System.currentTimeMillis() + FIVE_MINUTES;
new MyLog(this, "accelerometerX = " + String.valueOf(accelerometerX) + ", accelerometerY = " + String.valueOf(accelerometerY) + ", accelerometerZ = " + String.valueOf(accelerometerZ));
if ((accelerometerX >= -0.3) && (accelerometerX <= 0.3) && (accelerometerY >= -0.3) && (accelerometerY <= 0.3) && (accelerometerZ >= 9.6) && (accelerometerZ <= 10.0)) {
if ((wanted & PrefsManager.BEFORE_FACE_UP) != 0) {
return false;
}
} else if ((accelerometerX >= -0.3) && (accelerometerX <= 0.3) && (accelerometerY >= -0.3) && (accelerometerY <= 0.3) && (accelerometerZ >= -10.0) && (accelerometerZ <= -9.6)) {
if ((wanted & PrefsManager.BEFORE_FACE_DOWN) != 0) {
return false;
}
} else if ((wanted & PrefsManager.BEFORE_OTHER_POSITION) != 0) {
return false;
}
nextAccelTime = System.currentTimeMillis() + FIVE_MINUTES;
PrefsManager.setOrientationState(this, PrefsManager.ORIENTATION_IDLE);
new MyLog(this, "Still waiting for orientation for class ".concat(PrefsManager.getClassName(this, classNum)));
return true;
default:
return false;
}
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method LocationUpdates.
public void LocationUpdates(int classNum, double which) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String s = "CalendarTrigger.Location";
PendingIntent pi = PendingIntent.getBroadcast(this, 0, /*requestCode*/
new Intent(s, Uri.EMPTY, this, StartServiceReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
if (which == PrefsManager.LATITUDE_IDLE) {
lm.removeUpdates(pi);
PrefsManager.setLocationState(this, false);
new MyLog(this, "Removing location updates");
} else {
List<String> ls = lm.getProviders(true);
if (which == PrefsManager.LATITUDE_FIRST) {
if (ls.contains("gps")) {
// The first update may take a long time if we are inside a
// building, but this is OK because we won't want to restore
// the state until we've left the building. If we don't
// force the use of GPS here, we may get a cellular network
// fix which can be some distance from our real position and
// if we then get a GPS fix while we are still in the
// building we can think that we have moved when in fact we
// haven't.
lm.requestSingleUpdate("gps", pi);
new MyLog(this, "Requesting first gps location for class ".concat(PrefsManager.getClassName(this, classNum)));
} else {
// If we don't have GPS, we use whatever the device can give
// us.
Criteria cr = new Criteria();
cr.setAccuracy(Criteria.ACCURACY_FINE);
lm.requestSingleUpdate(cr, pi);
new MyLog(this, "Requesting first fine location for class ".concat(PrefsManager.getClassName(this, classNum)));
}
PrefsManager.setLocationState(this, true);
PrefsManager.setLatitude(this, classNum, PrefsManager.LATITUDE_FIRST);
} else {
float meters = (float) PrefsManager.getAfterMetres(this, classNum);
if (ls.contains("gps")) {
lm.requestLocationUpdates("gps", 5 * 60 * 1000, meters, pi);
} else {
Criteria cr = new Criteria();
cr.setAccuracy(Criteria.ACCURACY_FINE);
lm.requestLocationUpdates(5 * 60 * 1000, meters, cr, pi);
}
}
}
}
use of uk.co.yahoo.p1rpp.calendartrigger.MyLog in project CalendarTrigger by rparkins999.
the class MuteService method PermissionFail.
private void PermissionFail(int mode) {
Notification.Builder builder = new Notification.Builder(this).setSmallIcon(R.drawable.notif_icon).setContentText(getString(R.string.permissionfail)).setStyle(new Notification.BigTextStyle().bigText(getString(R.string.permissionfailbig)));
// Show notification
NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(notifyId++, builder.build());
new MyLog(this, "Cannot set mode " + PrefsManager.getRingerStateName(this, mode) + " because CalendarTrigger no longer has permission " + "ACCESS_NOTIFICATION_POLICY.");
}
Aggregations