2013-04-28 16:46:31 -04:00
|
|
|
using System;
|
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
|
|
|
using Android.Util;
|
|
|
|
using Java.IO;
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
2013-06-15 16:02:48 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Makes attachments of PwEntries accessible when they are stored in the app cache
|
|
|
|
/// </summary>
|
2013-06-15 06:40:01 -04:00
|
|
|
[ContentProvider(new[]{"keepass2android."+AppNames.PackagePart+".provider"})]
|
2013-04-28 16:46:31 -04:00
|
|
|
public class AttachmentContentProvider : ContentProvider {
|
2013-07-30 14:42:16 -04:00
|
|
|
public const string AttachmentCacheSubDir = "AttachmentCache";
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private const String ClassName = "AttachmentContentProvider";
|
2013-04-28 16:46:31 -04:00
|
|
|
|
|
|
|
// The authority is the symbolic name for the provider class
|
2013-06-15 06:40:01 -04:00
|
|
|
public const String Authority = "keepass2android."+AppNames.PackagePart+".provider";
|
2013-04-28 16:46:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
public override bool OnCreate() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ParcelFileDescriptor OpenFile(Android.Net.Uri uri, String mode)
|
|
|
|
{
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
const string logTag = ClassName + " - openFile";
|
2013-04-28 16:46:31 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
Log.Verbose(logTag,
|
2013-04-28 16:46:31 -04:00
|
|
|
"Called with uri: '" + uri + "'." + uri.LastPathSegment);
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (uri.ToString().StartsWith("content://" + Authority))
|
2013-04-28 16:46:31 -04:00
|
|
|
{
|
|
|
|
// The desired file name is specified by the last segment of the
|
|
|
|
// path
|
|
|
|
// E.g.
|
|
|
|
// 'content://keepass2android.provider/Test.txt'
|
|
|
|
// Take this and build the path to the file
|
2013-07-30 14:42:16 -04:00
|
|
|
|
|
|
|
String fileLocation = Context.CacheDir + File.Separator + AttachmentCacheSubDir + File.Separator
|
2013-04-28 16:46:31 -04:00
|
|
|
+ uri.LastPathSegment;
|
|
|
|
|
|
|
|
// Create & return a ParcelFileDescriptor pointing to the file
|
|
|
|
// Note: I don't care what mode they ask for - they're only getting
|
|
|
|
// read only
|
|
|
|
ParcelFileDescriptor pfd = ParcelFileDescriptor.Open(new File(
|
|
|
|
fileLocation), ParcelFileMode.ReadOnly);
|
|
|
|
return pfd;
|
|
|
|
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
Log.Verbose(logTag, "Unsupported uri: '" + uri + "'.");
|
|
|
|
throw new FileNotFoundException("Unsupported uri: "
|
|
|
|
+ uri.ToString());
|
2013-04-28 16:46:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////////
|
|
|
|
// Not supported / used / required for this example
|
|
|
|
// //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
public override int Update(Android.Net.Uri uri, ContentValues contentvalues, String s,
|
|
|
|
String[] strings) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int Delete(Android.Net.Uri uri, String s, String[] strings) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Android.Net.Uri Insert(Android.Net.Uri uri, ContentValues contentvalues) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override String GetType(Android.Net.Uri uri) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Android.Database.ICursor Query(Android.Net.Uri uri, string[] projection, string selection, string[] selectionArgs, string sortOrder)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|