1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <unistd.h> 18 #include <binder/IBinder.h> 19 #include <binder/IServiceManager.h> 20 #include <binder/Parcel.h> 21 #include <utils/String8.h> 22 23 #include "ActivityManager.h" 24 25 namespace android { 26 27 const uint32_t OPEN_CONTENT_URI_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION + 4; 28 29 // Perform ContentProvider.openFile() on the given URI, returning 30 // the resulting native file descriptor. Returns < 0 on error. 31 int openContentProviderFile(const String16& uri) 32 { 33 int fd = -1; 34 35 sp<IServiceManager> sm = defaultServiceManager(); 36 sp<IBinder> am = sm->getService(String16("activity")); 37 if (am != NULL) { 38 Parcel data, reply; 39 data.writeInterfaceToken(String16("android.app.IActivityManager")); 40 data.writeString16(uri); 41 status_t ret = am->transact(OPEN_CONTENT_URI_TRANSACTION, data, &reply); 42 if (ret == NO_ERROR) { 43 int32_t exceptionCode = reply.readExceptionCode(); 44 if (!exceptionCode) { 45 // Success is indicated here by a nonzero int followed by the fd; 46 // failure by a zero int with no data following. 47 if (reply.readInt32() != 0) { 48 fd = dup(reply.readFileDescriptor()); 49 } 50 } else { 51 // An exception was thrown back; fall through to return failure 52 ALOGD("openContentUri(%s) caught exception %d\n", 53 String8(uri).string(), exceptionCode); 54 } 55 } 56 } 57 58 return fd; 59 } 60 61 } /* namespace android */ 62