Skip to content

Commit

Permalink
timob-23429 create custom content provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fmerzadyan committed May 18, 2017
1 parent 6e85a80 commit 7100631
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
17 changes: 17 additions & 0 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,7 @@ AndroidBuilder.prototype.run = function run(logger, config, cli, finished) {
// overwritten by some module's strings.xml
'generateI18N',

'generateFileProviderPaths',
'generateTheme',
'generateAndroidManifest',
'packageApp',
Expand Down Expand Up @@ -3408,6 +3409,22 @@ AndroidBuilder.prototype.generateI18N = function generateI18N(next) {
next();
};

AndroidBuilder.prototype.generateFileProviderPaths = function generateFileProviderPaths(next) {
var xmlDir = path.join(this.buildResDir, 'xml');

if fppFile = path.join(xmlDir, 'file_provider_paths.xml');
if (!fs.existsSync(xmlDir)) {
fs.mkdirSync(xmlDir);
}

if (!fs.existsSync(fppFile)) {
this.logger.info(__('Generating %s', fppFile.cyan));
fs.writeFileSync(fppFile, fs.readFileSync(path.join(this.templatesDir, 'file_provider_paths.xml')));
}

next();
};

AndroidBuilder.prototype.generateTheme = function generateTheme(next) {
var themeFile = path.join(this.buildResDir, 'values', 'theme.xml');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiFileProxy;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFileProvider;
import org.appcelerator.titanium.io.TiFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.proxy.TiViewProxy;
Expand Down Expand Up @@ -131,6 +132,18 @@ public class MediaModule extends KrollModule
private static String mediaType = MEDIA_TYPE_PHOTO;
private static String extension = ".jpg";

private static class ApiLevel16
{
private ApiLevel16() {}

public static void setIntentClipData(Intent intent, ClipData data)
{
if (intent != null) {
intent.setClipData(data);
}
}
}

public MediaModule()
{
super();
Expand Down Expand Up @@ -244,8 +257,12 @@ private void launchNativeCamera(KrollDict cameraOptions)
}

//Create Intent
Uri fileUri = Uri.fromFile(imageFile); // create a file to save the image
Uri fileUri = TiFileProvider.getUriForFile(imageFile);
Intent intent = new Intent(intentType);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= 16) {
ApiLevel16.setIntentClipData(intent, android.content.ClipData.newRawUri("", fileUri));
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, videoQuality);
intent.putExtra("android.intent.extras.CAMERA_FACING", cameraType);
Expand Down
10 changes: 10 additions & 0 deletions android/templates/build/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
android:theme="@style/Theme.AppCompat.Translucent" />
<activity android:name="ti.modules.titanium.ui.android.TiPreferencesActivity" />

<provider
android:name="org.appcelerator.titanium.io.TiFileProvider"
android:authorities="<%- appid %>.tifileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>
</provider>

</application>

</manifest>
10 changes: 10 additions & 0 deletions android/templates/build/file_provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache" path="."/>
<files-path name="files" path="."/>
<root-path name="root" path="."/>
<external-cache-path name="external_cache" path="."/>
<external-files-path name="external_files" path="."/>
<external-path name="external" path="."/>
<external-path name="external_files" path="."/>
</paths>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.appcelerator.titanium.io;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.support.v4.content.FileProvider;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiMimeTypeHelper;

import java.io.File;

/**
* TiFileProvider is intended to expose filesystem resources to outside apps.
*/
public class TiFileProvider extends ContentProvider {
private static final String TAG = "TiFileProvider";

@Override
public boolean onCreate() {
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
File file = new File(getUriForFile(new File(uri.getPath())).getPath());

final String[] columns = { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE };
if (projection == null) {
projection = columns;
}

String[] cols = new String[projection.length];
Object[] values = new Object[projection.length];
int i = 0;
for (String col: projection) {
if (OpenableColumns.DISPLAY_NAME.equals(col)) {
cols[i] = OpenableColumns.DISPLAY_NAME;
values[i++] = file.getName();
} else if (OpenableColumns.SIZE.equals(col)) {
cols[i] = OpenableColumns.SIZE;
values[i++] = file.length();
}
}

cols = copyOf(cols, i);
values = copyOf(values, i);

final MatrixCursor cursor = new MatrixCursor(cols, 1);
cursor.addRow(values);
return cursor;
}

public static Uri getUriForFile(File file) {
final TiApplication tiApp = TiApplication.getInstance();
if (tiApp == null) return null;
final String id = tiApp.getAppInfo().getId();

return FileProvider.getUriForFile(tiApp, id + ".tifileprovider", file);
}

private static String[] copyOf(String[] original, int newLength) {
final String[] result = new String[newLength];
System.arraycopy(original, 0, result, 0, newLength);
return result;
}

private static Object[] copyOf(Object[] original, int newLength) {
final Object[] result = new Object[newLength];
System.arraycopy(original, 0, result, 0, newLength);
return result;
}

/**
* Returns the type of a file assuming that it has an extension otherwise 'application/octet-stream' is returned.
*/
@Override
public String getType(Uri uri) {
return TiMimeTypeHelper.getMimeType(uri.getPath(), "application/octet-stream");
}

@Override
public Uri insert(Uri uri, ContentValues values) {
throw new UnsupportedOperationException("No external inserts");
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("No external deletions");
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("No external updates");
}
}

0 comments on commit 7100631

Please sign in to comment.