-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timob-23429 create custom content provider
- Loading branch information
fmerzadyan
committed
May 18, 2017
1 parent
6e85a80
commit 7100631
Showing
5 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
98 changes: 98 additions & 0 deletions
98
android/titanium/src/java/org/appcelerator/titanium/io/TiFileProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |