--- /dev/null
+/*
+ * Copyright (C) 2012 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+package org.strongswan.android.data;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+
+import org.strongswan.android.logic.CharonVpnService;
+
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.database.MatrixCursor;
+import android.net.Uri;
+import android.os.ParcelFileDescriptor;
+import android.provider.OpenableColumns;
+
+public class LogContentProvider extends ContentProvider
+{
+ private static final String AUTHORITY = "org.strongswan.android.content.log";
+ private File mLogFile;
+
+ public LogContentProvider()
+ {
+ }
+
+ @Override
+ public boolean onCreate()
+ {
+ mLogFile = new File(getContext().getFilesDir(), CharonVpnService.LOG_FILE);
+ return true;
+ }
+
+ /**
+ * The log file can only be accessed by Uris created with this method
+ * @return null if failed to create the Uri
+ */
+ public static Uri createContentUri()
+ {
+ Uri uri = Uri.parse("content://"+ AUTHORITY + "/" + CharonVpnService.LOG_FILE);
+ return uri;
+ }
+
+ @Override
+ public String getType(Uri uri)
+ {
+ /* MIME type for our log file */
+ return "text/plain";
+ }
+
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection,
+ String[] selectionArgs, String sortOrder)
+ {
+ /* this is called by apps to find out the name and size of the file.
+ * since we only provide a single file this is simple to implement */
+ if (projection == null || projection.length < 1)
+ {
+ return null;
+ }
+ MatrixCursor cursor = new MatrixCursor(projection, 1);
+ if (OpenableColumns.DISPLAY_NAME.equals(cursor.getColumnName(0)))
+ {
+ cursor.newRow().add(CharonVpnService.LOG_FILE);
+ }
+ else if (OpenableColumns.SIZE.equals(cursor.getColumnName(0)))
+ {
+ cursor.newRow().add(mLogFile.length());
+ }
+ else
+ {
+ return null;
+ }
+ return cursor;
+ }
+
+ @Override
+ public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException
+ {
+ return ParcelFileDescriptor.open(mLogFile, ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_READ_ONLY);
+ }
+
+ @Override
+ public Uri insert(Uri uri, ContentValues values)
+ {
+ /* not supported */
+ return null;
+ }
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs)
+ {
+ /* not supported */
+ return 0;
+ }
+
+ @Override
+ public int update(Uri uri, ContentValues values, String selection,
+ String[] selectionArgs)
+ {
+ /* not supported */
+ return 0;
+ }
+}