SimProviderpublic class SimProvider extends android.content.ContentProvider
Fields Summary |
---|
private static final String | TAG | private static final boolean | DBG | private static final String[] | ADDRESS_BOOK_COLUMN_NAMES | private static final int | ADN | private static final int | FDN | private static final int | SDN | private static final String | STR_TAG | private static final String | STR_NUMBER | private static final String | STR_PIN2 | private static final android.content.UriMatcher | URL_MATCHER | private boolean | mSimulator |
Methods Summary |
---|
private boolean | addSimRecordToEf(int efType, java.lang.String name, java.lang.String number, java.lang.String pin2)
if (DBG) log("addSimRecordToEf: efType=" + efType + ", name=" + name +
", number=" + number);
boolean success = false;
// TODO: do we need to call getAdnRecordsInEf() before calling
// updateAdnRecordsInEfBySearch()? In any case, we will leave
// the UI level logic to fill that prereq if necessary. But
// hopefully, we can remove this requirement.
try {
ISimPhoneBook simIpb = ISimPhoneBook.Stub.asInterface(
ServiceManager.getService("simphonebook"));
if (simIpb != null) {
success = simIpb.updateAdnRecordsInEfBySearch(efType, "", "",
name, number, pin2);
}
} catch (RemoteException ex) {
// ignore it
} catch (SecurityException ex) {
if (DBG) log(ex.toString());
}
if (DBG) log("addSimRecordToEf: " + success);
return success;
| public int | delete(android.net.Uri url, java.lang.String where, java.lang.String[] whereArgs)
int efType;
if (DBG) log("delete");
int match = URL_MATCHER.match(url);
switch (match) {
case ADN:
efType = SimConstants.EF_ADN;
break;
case FDN:
efType = SimConstants.EF_FDN;
break;
default:
throw new UnsupportedOperationException(
"Cannot insert into URL: " + url);
}
// parse where clause
String tag = null;
String number = null;
String pin2 = null;
String[] tokens = where.split("AND");
int n = tokens.length;
while (--n >= 0) {
String param = tokens[n];
if (DBG) log("parsing '" + param + "'");
String[] pair = param.split("=");
if (pair.length != 2) {
Log.e(TAG, "resolve: bad whereClause parameter: " + param);
continue;
}
String key = pair[0].trim();
String val = pair[1].trim();
if (STR_TAG.equals(key)) {
tag = normalizeValue(val);
} else if (STR_NUMBER.equals(key)) {
number = normalizeValue(val);
} else if (STR_PIN2.equals(key)) {
pin2 = normalizeValue(val);
}
}
if (TextUtils.isEmpty(tag)) {
return 0;
}
if (efType == FDN && TextUtils.isEmpty(pin2)) {
return 0;
}
boolean success = deleteSimRecordFromEf(efType, tag, number, pin2);
if (!success) {
return 0;
}
return 1;
| private boolean | deleteSimRecordFromEf(int efType, java.lang.String name, java.lang.String number, java.lang.String pin2)
if (DBG) log("deleteSimRecordFromEf: efType=" + efType +
", name=" + name + ", number=" + number + ", pin2=" + pin2);
boolean success = false;
try {
ISimPhoneBook simIpb = ISimPhoneBook.Stub.asInterface(
ServiceManager.getService("simphonebook"));
if (simIpb != null) {
success = simIpb.updateAdnRecordsInEfBySearch(efType,
name, number, "", "", pin2);
}
} catch (RemoteException ex) {
// ignore it
} catch (SecurityException ex) {
if (DBG) log(ex.toString());
}
if (DBG) log("deleteSimRecordFromEf: " + success);
return success;
| public java.lang.String | getType(android.net.Uri url)
switch (URL_MATCHER.match(url)) {
case ADN:
case FDN:
case SDN:
return "vnd.android.cursor.dir/sim-contact";
default:
throw new IllegalArgumentException("Unknown URL " + url);
}
| public android.net.Uri | insert(android.net.Uri url, android.content.ContentValues initialValues)
Uri resultUri;
int efType;
String pin2 = null;
if (DBG) log("insert");
int match = URL_MATCHER.match(url);
switch (match) {
case ADN:
efType = SimConstants.EF_ADN;
break;
case FDN:
efType = SimConstants.EF_FDN;
pin2 = initialValues.getAsString("pin2");
break;
default:
throw new UnsupportedOperationException(
"Cannot insert into URL: " + url);
}
String tag = initialValues.getAsString("tag");
String number = initialValues.getAsString("number");
boolean success = addSimRecordToEf(efType, tag, number, pin2);
if (!success) {
return null;
}
StringBuilder buf = new StringBuilder("content://im/");
switch (match) {
case ADN:
buf.append("adn/");
break;
case FDN:
buf.append("fdn/");
break;
}
// TODO: we need to find out the rowId for the newly added record
buf.append(0);
resultUri = Uri.parse(buf.toString());
/*
// notify interested parties that an insertion happened
getContext().getContentResolver().notifyInsert(
resultUri, rowID, null);
*/
return resultUri;
| private java.util.ArrayList | loadFromEf(int efType)
ArrayList<ArrayList> results = new ArrayList<ArrayList>();
List<AdnRecord> adnRecords = null;
if (DBG) log("loadFromEf: efType=" + efType);
try {
ISimPhoneBook simIpb = ISimPhoneBook.Stub.asInterface(
ServiceManager.getService("simphonebook"));
if (simIpb != null) {
adnRecords = simIpb.getAdnRecordsInEf(efType);
}
} catch (RemoteException ex) {
// ignore it
} catch (SecurityException ex) {
if (DBG) log(ex.toString());
}
if (adnRecords != null) {
// Load the results
int N = adnRecords.size();
if (DBG) log("adnRecords.size=" + N);
for (int i = 0; i < N ; i++) {
loadRecord(adnRecords.get(i), results);
}
} else {
// No results to load
Log.w(TAG, "Cannot load ADN records");
results.clear();
}
if (DBG) log("loadFromEf: return results");
return results;
| private void | loadRecord(AdnRecord record, java.util.ArrayList results)Loads an AdnRecord into an ArrayList. Must be called with mLock held.
if (!record.isEmpty()) {
ArrayList<String> contact = new ArrayList<String>(2);
String alphaTag = record.getAlphaTag();
String number = record.getNumber();
if (DBG) log("loadRecord: " + alphaTag + ", " + number);
contact.add(alphaTag);
contact.add(number);
results.add(contact);
}
| private void | log(java.lang.String msg)
Log.d(TAG, "[SimProvider] " + msg);
| private java.lang.String | normalizeValue(java.lang.String inVal)
int len = inVal.length();
String retVal = inVal;
if (inVal.charAt(0) == '\'" && inVal.charAt(len-1) == '\'") {
retVal = inVal.substring(1, len-1);
}
return retVal;
| public boolean | onCreate()
String device = SystemProperties.get("ro.product.device");
if (!TextUtils.isEmpty(device)) {
mSimulator = false;
} else {
// simulator
mSimulator = true;
}
return true;
| public android.database.Cursor | query(android.net.Uri url, java.lang.String[] projection, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sort)
ArrayList<ArrayList> results;
if (!mSimulator) {
switch (URL_MATCHER.match(url)) {
case ADN:
results = loadFromEf(SimConstants.EF_ADN);
break;
case FDN:
results = loadFromEf(SimConstants.EF_FDN);
break;
case SDN:
results = loadFromEf(SimConstants.EF_SDN);
break;
default:
throw new IllegalArgumentException("Unknown URL " + url);
}
} else {
// Fake up some data for the simulator
results = new ArrayList<ArrayList>(4);
ArrayList<String> contact;
contact = new ArrayList<String>();
contact.add("Ron Stevens/H");
contact.add("512-555-5038");
results.add(contact);
contact = new ArrayList<String>();
contact.add("Ron Stevens/M");
contact.add("512-555-8305");
results.add(contact);
contact = new ArrayList<String>();
contact.add("Melissa Owens");
contact.add("512-555-8305");
results.add(contact);
contact = new ArrayList<String>();
contact.add("Directory Assistence");
contact.add("411");
results.add(contact);
}
return new ArrayListCursor(ADDRESS_BOOK_COLUMN_NAMES, results);
| public int | update(android.net.Uri url, android.content.ContentValues values, java.lang.String where, java.lang.String[] whereArgs)
int efType;
String pin2 = null;
if (DBG) log("update");
int match = URL_MATCHER.match(url);
switch (match) {
case ADN:
efType = SimConstants.EF_ADN;
break;
case FDN:
efType = SimConstants.EF_FDN;
pin2 = values.getAsString("pin2");
break;
default:
throw new UnsupportedOperationException(
"Cannot insert into URL: " + url);
}
String tag = values.getAsString("tag");
String number = values.getAsString("number");
String newTag = values.getAsString("newTag");
String newNumber = values.getAsString("newNumber");
boolean success = updateSimRecordInEf(efType, tag, number,
newTag, newNumber, pin2);
if (!success) {
return 0;
}
return 1;
| private boolean | updateSimRecordInEf(int efType, java.lang.String oldName, java.lang.String oldNumber, java.lang.String newName, java.lang.String newNumber, java.lang.String pin2)
if (DBG) log("updateSimRecordInEf: efType=" + efType +
", oldname=" + oldName + ", oldnumber=" + oldNumber +
", newname=" + newName + ", newnumber=" + newNumber);
boolean success = false;
try {
ISimPhoneBook simIpb = ISimPhoneBook.Stub.asInterface(
ServiceManager.getService("simphonebook"));
if (simIpb != null) {
success = simIpb.updateAdnRecordsInEfBySearch(efType,
oldName, oldNumber, newName, newNumber, pin2);
}
} catch (RemoteException ex) {
// ignore it
} catch (SecurityException ex) {
if (DBG) log(ex.toString());
}
if (DBG) log("updateSimRecordInEf: " + success);
return success;
|
|