1559bf2836f5da25b75bfb229fec0d20d540ee426James Dong/* 2559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * Copyright (C) 2006 The Android Open Source Project 3559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * 4559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * Licensed under the Apache License, Version 2.0 (the "License"); 5559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * you may not use this file except in compliance with the License. 6559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * You may obtain a copy of the License at 7559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * 8559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * http://www.apache.org/licenses/LICENSE-2.0 9559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * 10559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * Unless required by applicable law or agreed to in writing, software 11559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * distributed under the License is distributed on an "AS IS" BASIS, 12559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * See the License for the specific language governing permissions and 14559bf2836f5da25b75bfb229fec0d20d540ee426James Dong * limitations under the License. 15559bf2836f5da25b75bfb229fec0d20d540ee426James Dong */ 16559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 17559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include <unistd.h> 18559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include <binder/IBinder.h> 19559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include <binder/IServiceManager.h> 20559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include <binder/Parcel.h> 21559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include <utils/String8.h> 22559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 23559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include "ActivityManager.h" 24559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 25559bf2836f5da25b75bfb229fec0d20d540ee426James Dongnamespace android { 26559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 27559bf2836f5da25b75bfb229fec0d20d540ee426James Dongconst uint32_t OPEN_CONTENT_URI_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION + 4; 28559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 29559bf2836f5da25b75bfb229fec0d20d540ee426James Dong// Perform ContentProvider.openFile() on the given URI, returning 30559bf2836f5da25b75bfb229fec0d20d540ee426James Dong// the resulting native file descriptor. Returns < 0 on error. 31559bf2836f5da25b75bfb229fec0d20d540ee426James Dongint openContentProviderFile(const String16& uri) 32559bf2836f5da25b75bfb229fec0d20d540ee426James Dong{ 33559bf2836f5da25b75bfb229fec0d20d540ee426James Dong int fd = -1; 34559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 35559bf2836f5da25b75bfb229fec0d20d540ee426James Dong sp<IServiceManager> sm = defaultServiceManager(); 36559bf2836f5da25b75bfb229fec0d20d540ee426James Dong sp<IBinder> am = sm->getService(String16("activity")); 37559bf2836f5da25b75bfb229fec0d20d540ee426James Dong if (am != NULL) { 38559bf2836f5da25b75bfb229fec0d20d540ee426James Dong Parcel data, reply; 39559bf2836f5da25b75bfb229fec0d20d540ee426James Dong data.writeInterfaceToken(String16("android.app.IActivityManager")); 40559bf2836f5da25b75bfb229fec0d20d540ee426James Dong data.writeString16(uri); 41559bf2836f5da25b75bfb229fec0d20d540ee426James Dong status_t ret = am->transact(OPEN_CONTENT_URI_TRANSACTION, data, &reply); 42559bf2836f5da25b75bfb229fec0d20d540ee426James Dong if (ret == NO_ERROR) { 43559bf2836f5da25b75bfb229fec0d20d540ee426James Dong int32_t exceptionCode = reply.readExceptionCode(); 44559bf2836f5da25b75bfb229fec0d20d540ee426James Dong if (!exceptionCode) { 45559bf2836f5da25b75bfb229fec0d20d540ee426James Dong // Success is indicated here by a nonzero int followed by the fd; 46559bf2836f5da25b75bfb229fec0d20d540ee426James Dong // failure by a zero int with no data following. 47559bf2836f5da25b75bfb229fec0d20d540ee426James Dong if (reply.readInt32() != 0) { 48559bf2836f5da25b75bfb229fec0d20d540ee426James Dong fd = dup(reply.readFileDescriptor()); 49559bf2836f5da25b75bfb229fec0d20d540ee426James Dong } 50559bf2836f5da25b75bfb229fec0d20d540ee426James Dong } else { 51559bf2836f5da25b75bfb229fec0d20d540ee426James Dong // An exception was thrown back; fall through to return failure 52559bf2836f5da25b75bfb229fec0d20d540ee426James Dong ALOGD("openContentUri(%s) caught exception %d\n", 53559bf2836f5da25b75bfb229fec0d20d540ee426James Dong String8(uri).string(), exceptionCode); 54559bf2836f5da25b75bfb229fec0d20d540ee426James Dong } 55559bf2836f5da25b75bfb229fec0d20d540ee426James Dong } 56559bf2836f5da25b75bfb229fec0d20d540ee426James Dong } 57559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 58559bf2836f5da25b75bfb229fec0d20d540ee426James Dong return fd; 59559bf2836f5da25b75bfb229fec0d20d540ee426James Dong} 60559bf2836f5da25b75bfb229fec0d20d540ee426James Dong 61559bf2836f5da25b75bfb229fec0d20d540ee426James Dong} /* namespace android */ 62