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 <android_runtime/ActivityManager.h>
19 #include <binder/IBinder.h>
20 #include <binder/IServiceManager.h>
21 #include <binder/Parcel.h>
22 #include <utils/String8.h>
23
24 namespace android {
25
26 const 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.
openContentProviderFile(const String16 & uri)30 int 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) {
42 int32_t exceptionCode = reply.readInt32();
43 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
51 LOGD("openContentUri(%s) caught exception %d\n",
52 String8(uri).string(), exceptionCode);
53 }
54 }
55 }
56
57 return fd;
58 }
59
60 } /* namespace android */
61