/**
* Inserts a row in the database
*/
@Override
public Uri insert(final Uri uri, final ContentValues values) {
checkInsertPermissions(values);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
// note we disallow inserting into ALL_DOWNLOADS
int match = sURIMatcher.match(uri);
if (match != MY_DOWNLOADS) {
Log.d(Constants.TAG, "calling insert on an unknown/invalid URI: "
+ uri);
throw new IllegalArgumentException("Unknown/Invalid URI " + uri);
}
ContentValues filteredValues = new ContentValues();
copyString(Downloads.COLUMN_URI, values, filteredValues);
copyString(Downloads.COLUMN_APP_DATA, values, filteredValues);
copyBoolean(Downloads.COLUMN_NO_INTEGRITY, values, filteredValues);
copyString(Downloads.COLUMN_FILE_NAME_HINT, values, filteredValues);
copyString(Downloads.COLUMN_MIME_TYPE, values, filteredValues);
copyBoolean(Downloads.COLUMN_IS_PUBLIC_API, values, filteredValues);
boolean isPublicApi = values
.getAsBoolean(Downloads.COLUMN_IS_PUBLIC_API) == Boolean.TRUE;
Integer dest = values.getAsInteger(Downloads.COLUMN_DESTINATION);
if (dest != null) {
if (getContext().checkCallingPermission(
Downloads.PERMISSION_ACCESS_ADVANCED) != PackageManager.PERMISSION_GRANTED
&& dest != Downloads.DESTINATION_EXTERNAL
&& dest != Downloads.DESTINATION_FILE_URI) {
throw new SecurityException("unauthorized destination code");
}
if (dest == Downloads.DESTINATION_FILE_URI) {
getContext()
.enforcePermission(
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Binder.getCallingPid(), Binder.getCallingUid(),
"need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI");
checkFileUriDestination(values);
}
filteredValues.put(Downloads.COLUMN_DESTINATION, dest);
}
Integer vis = values.getAsInteger(Downloads.COLUMN_VISIBILITY);
if (vis == null) {
if (dest == Downloads.DESTINATION_EXTERNAL) {
filteredValues.put(Downloads.COLUMN_VISIBILITY,
Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
} else {
filteredValues.put(Downloads.COLUMN_VISIBILITY,
Downloads.VISIBILITY_HIDDEN);
}
} else {
filteredValues.put(Downloads.COLUMN_VISIBILITY, vis);
}
copyInteger(Downloads.COLUMN_CONTROL, values, filteredValues);
filteredValues.put(Downloads.COLUMN_STATUS, Downloads.STATUS_PENDING);
filteredValues.put(Downloads.COLUMN_LAST_MODIFICATION,
mSystemFacade.currentTimeMillis());
String pckg = values.getAsString(Downloads.COLUMN_NOTIFICATION_PACKAGE);
String clazz = values.getAsString(Downloads.COLUMN_NOTIFICATION_CLASS);
if (pckg != null && (clazz != null || isPublicApi)) {
int uid = Binder.getCallingUid();
try {
if (uid == 0 || mSystemFacade.userOwnsPackage(uid, pckg)) {
filteredValues.put(Downloads.COLUMN_NOTIFICATION_PACKAGE,
pckg);
if (clazz != null) {
filteredValues.put(Downloads.COLUMN_NOTIFICATION_CLASS,
clazz);
}
}
} catch (PackageManager.NameNotFoundException ex) {
/* ignored for now */
}
}
copyString(Downloads.COLUMN_NOTIFICATION_EXTRAS, values, filteredValues);
copyString(Downloads.COLUMN_COOKIE_DATA, values, filteredValues);
copyString(Downloads.COLUMN_USER_AGENT, values, filteredValues);
copyString(Downloads.COLUMN_REFERER, values, filteredValues);
if (getContext().checkCallingPermission(
Downloads.PERMISSION_ACCESS_ADVANCED) == PackageManager.PERMISSION_GRANTED) {
copyInteger(Downloads.COLUMN_OTHER_UID, values, filteredValues);
}
filteredValues.put(Constants.UID, Binder.getCallingUid());
if (Binder.getCallingUid() == 0) {
copyInteger(Constants.UID, values, filteredValues);
}
copyStringWithDefault(Downloads.COLUMN_TITLE, values, filteredValues,
"");
copyStringWithDefault(Downloads.COLUMN_DESCRIPTION, values,
filteredValues, "");
filteredValues.put(Downloads.COLUMN_TOTAL_BYTES, -1);
filteredValues.put(Downloads.COLUMN_CURRENT_BYTES, 0);
if (values.containsKey(Downloads.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI)) {
copyBoolean(Downloads.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, values,
filteredValues);
} else {
// by default, make external downloads visible in the UI
boolean isExternal = (dest == null || dest == Downloads.DESTINATION_EXTERNAL);
filteredValues.put(Downloads.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI,
isExternal);
}
if (isPublicApi) {
copyInteger(Downloads.COLUMN_ALLOWED_NETWORK_TYPES, values,
filteredValues);
copyBoolean(Downloads.COLUMN_ALLOW_ROAMING, values, filteredValues);
}
if (Constants.LOGVV) {
Log.v(Constants.TAG, "initiating download with UID "
+ filteredValues.getAsInteger(Constants.UID));
if (filteredValues.containsKey(Downloads.COLUMN_OTHER_UID)) {
Log.v(Constants.TAG,
"other UID "
+ filteredValues
.getAsInteger(Downloads.COLUMN_OTHER_UID));
}
}
//开启服务
Context context = getContext();
context.startService(new Intent(context, DownloadService.class));
long rowID = db.insert(DB_TABLE, null, filteredValues);
if (rowID == -1) {
Log.d(Constants.TAG, "couldn't insert into downloads database");
return null;
}
insertRequestHeaders(db, rowID, values);
context.startService(new Intent(context, DownloadService.class));
notifyContentChanged(uri, match);
return ContentUris.withAppendedId(Downloads.CONTENT_URI, rowID);
}