blob: 0f9d0bbd06fd470e7d0dbea24ed89c25577821ec [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 <android_runtime/ActivityManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070019#include <binder/IBinder.h>
20#include <binder/IServiceManager.h>
21#include <binder/Parcel.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <utils/String8.h>
23
24namespace android {
25
26const uint32_t OPEN_CONTENT_URI_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION + 4;
27
28// Perform ContentProvider.openFile() on the given URI, returning
29// the resulting native file descriptor. Returns < 0 on error.
30int openContentProviderFile(const String16& uri)
31{
32 int fd = -1;
33
34 sp<IServiceManager> sm = defaultServiceManager();
35 sp<IBinder> am = sm->getService(String16("activity"));
36 if (am != NULL) {
37 Parcel data, reply;
38 data.writeInterfaceToken(String16("android.app.IActivityManager"));
39 data.writeString16(uri);
40 status_t ret = am->transact(OPEN_CONTENT_URI_TRANSACTION, data, &reply);
41 if (ret == NO_ERROR) {
Brad Fitzpatrickc0a7e692010-07-13 15:33:35 -070042 int32_t exceptionCode = reply.readExceptionCode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 if (!exceptionCode) {
44 // Success is indicated here by a nonzero int followed by the fd;
45 // failure by a zero int with no data following.
46 if (reply.readInt32() != 0) {
47 fd = dup(reply.readFileDescriptor());
48 }
49 } else {
50 // An exception was thrown back; fall through to return failure
Steve Block5baa3a62011-12-20 16:23:08 +000051 ALOGD("openContentUri(%s) caught exception %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 String8(uri).string(), exceptionCode);
53 }
54 }
55 }
56
57 return fd;
58}
59
60} /* namespace android */