| 14792 |
manas |
1 |
/**
|
|
|
2 |
* Copyright 2010-present Facebook.
|
|
|
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 |
package com.facebook;
|
|
|
18 |
|
|
|
19 |
import android.content.ContentProvider;
|
|
|
20 |
import android.content.ContentValues;
|
|
|
21 |
import android.database.Cursor;
|
|
|
22 |
import android.net.Uri;
|
|
|
23 |
import android.os.ParcelFileDescriptor;
|
|
|
24 |
import android.util.Log;
|
|
|
25 |
import android.util.Pair;
|
|
|
26 |
|
|
|
27 |
import java.io.File;
|
|
|
28 |
import java.io.FileNotFoundException;
|
|
|
29 |
import java.util.UUID;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* <p>Implements a <a href="http://developer.android.com/reference/android/content/ContentProvider.html">
|
|
|
33 |
* ContentProvider</a> that can be used to provide binary attachments (e.g., images) to calls made
|
|
|
34 |
* via @link FacebookDialog}. The {@link NativeAppCallAttachmentStore} class provides methods to attach
|
|
|
35 |
* and clean up the attachments.
|
|
|
36 |
*
|
|
|
37 |
* <p>Note that this ContentProvider is only necessary if an application wishes to attach images, etc., that are
|
|
|
38 |
* stored in memory and do not have another way to be referenced by a content URI. For images obtained from,
|
|
|
39 |
* e.g., the Camera or Gallery, that already have a content URI associated with them, use of this class is not
|
|
|
40 |
* necessary.</p>
|
|
|
41 |
*
|
|
|
42 |
* <p>If an application wishes to attach images that are stored in-memory within the application, this content
|
|
|
43 |
* provider must be listed in the application's AndroidManifest.xml, and it should be named according to the
|
|
|
44 |
* pattern <code>"com.facebook.app.NativeAppCallContentProvider{FACEBOOK_APP_ID}"</code>. See the
|
|
|
45 |
* {@link NativeAppCallContentProvider#getAttachmentUrl(String) getContentProviderName} method.</p>
|
|
|
46 |
*/
|
|
|
47 |
public class NativeAppCallContentProvider extends ContentProvider {
|
|
|
48 |
private static final String TAG = NativeAppCallContentProvider.class.getName();
|
|
|
49 |
private static final String ATTACHMENT_URL_BASE = "content://com.facebook.app.NativeAppCallContentProvider";
|
|
|
50 |
|
|
|
51 |
private final AttachmentDataSource dataSource;
|
|
|
52 |
|
|
|
53 |
public NativeAppCallContentProvider() {
|
|
|
54 |
this(new NativeAppCallAttachmentStore());
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
NativeAppCallContentProvider(AttachmentDataSource dataSource) {
|
|
|
58 |
this.dataSource = dataSource;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
interface AttachmentDataSource {
|
|
|
62 |
File openAttachment(UUID callId, String attachmentName) throws FileNotFoundException;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Returns the name of the content provider formatted correctly for constructing URLs.
|
|
|
67 |
* @param applicationId the Facebook application ID of the application
|
|
|
68 |
* @return the String to use as the authority portion of a content URI.
|
|
|
69 |
*/
|
|
|
70 |
public static String getAttachmentUrl(String applicationId, UUID callId, String attachmentName) {
|
|
|
71 |
return String.format("%s%s/%s/%s", ATTACHMENT_URL_BASE, applicationId, callId.toString(), attachmentName);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
@Override
|
|
|
75 |
public boolean onCreate() {
|
|
|
76 |
return true;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
@Override
|
|
|
80 |
public Cursor query(Uri uri, String[] strings, String s, String[] strings2, String s2) {
|
|
|
81 |
return null;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
@Override
|
|
|
85 |
public String getType(Uri uri) {
|
|
|
86 |
return null;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
@Override
|
|
|
90 |
public Uri insert(Uri uri, ContentValues contentValues) {
|
|
|
91 |
return null;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
@Override
|
|
|
95 |
public int delete(Uri uri, String s, String[] strings) {
|
|
|
96 |
return 0;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
@Override
|
|
|
100 |
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
|
|
|
101 |
return 0;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
@Override
|
|
|
105 |
public android.os.ParcelFileDescriptor openFile(android.net.Uri uri, java.lang.String mode)
|
|
|
106 |
throws java.io.FileNotFoundException {
|
|
|
107 |
|
|
|
108 |
Pair<UUID, String> callIdAndAttachmentName = parseCallIdAndAttachmentName(uri);
|
|
|
109 |
if (callIdAndAttachmentName == null) {
|
|
|
110 |
throw new FileNotFoundException();
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
try {
|
|
|
114 |
File file = dataSource.openAttachment(callIdAndAttachmentName.first, callIdAndAttachmentName.second);
|
|
|
115 |
|
|
|
116 |
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
|
117 |
} catch (FileNotFoundException exception) {
|
|
|
118 |
Log.e(TAG, "Got unexpected exception:" + exception);
|
|
|
119 |
throw exception;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
Pair<UUID, String> parseCallIdAndAttachmentName(Uri uri) {
|
|
|
124 |
try {
|
|
|
125 |
// We don't do explicit format checking here. Malformed URIs may generate NullPointerExceptions or
|
|
|
126 |
// array bounds exceptions, which we'll catch and return null. All of these will result in a
|
|
|
127 |
// FileNotFoundException being thrown in openFile.
|
|
|
128 |
String callIdAndAttachmentName = uri.getPath().substring(1);
|
|
|
129 |
String [] parts = callIdAndAttachmentName.split("/");
|
|
|
130 |
|
|
|
131 |
String callIdString = parts[0];
|
|
|
132 |
String attachmentName = parts[1];
|
|
|
133 |
UUID callId = UUID.fromString(callIdString);
|
|
|
134 |
|
|
|
135 |
return new Pair<UUID, String>(callId, attachmentName);
|
|
|
136 |
} catch (Exception exception) {
|
|
|
137 |
return null;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|