Add ContentProvider to access log file from other applications
authorTobias Brunner <tobias@strongswan.org>
Fri, 10 Aug 2012 14:33:05 +0000 (16:33 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 13 Aug 2012 09:22:21 +0000 (11:22 +0200)
src/frontends/android/AndroidManifest.xml
src/frontends/android/src/org/strongswan/android/data/LogContentProvider.java [new file with mode: 0644]

index 62fc6a1..1b1aaba 100644 (file)
                 <action android:name="org.strongswan.android.logic.CharonVpnService" />
             </intent-filter>
         </service>
+
+        <provider
+            android:name=".data.LogContentProvider"
+            android:authorities="org.strongswan.android.content.log" >
+        </provider>
     </application>
 
 </manifest>
diff --git a/src/frontends/android/src/org/strongswan/android/data/LogContentProvider.java b/src/frontends/android/src/org/strongswan/android/data/LogContentProvider.java
new file mode 100644 (file)
index 0000000..7225ca4
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * 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;
+       }
+}