Add ContentProvider to access log file from other applications
[strongswan.git] / src / frontends / android / src / org / strongswan / android / data / LogContentProvider.java
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.data;
17
18 import java.io.File;
19 import java.io.FileNotFoundException;
20
21 import org.strongswan.android.logic.CharonVpnService;
22
23 import android.content.ContentProvider;
24 import android.content.ContentValues;
25 import android.database.Cursor;
26 import android.database.MatrixCursor;
27 import android.net.Uri;
28 import android.os.ParcelFileDescriptor;
29 import android.provider.OpenableColumns;
30
31 public class LogContentProvider extends ContentProvider
32 {
33 private static final String AUTHORITY = "org.strongswan.android.content.log";
34 private File mLogFile;
35
36 public LogContentProvider()
37 {
38 }
39
40 @Override
41 public boolean onCreate()
42 {
43 mLogFile = new File(getContext().getFilesDir(), CharonVpnService.LOG_FILE);
44 return true;
45 }
46
47 /**
48 * The log file can only be accessed by Uris created with this method
49 * @return null if failed to create the Uri
50 */
51 public static Uri createContentUri()
52 {
53 Uri uri = Uri.parse("content://"+ AUTHORITY + "/" + CharonVpnService.LOG_FILE);
54 return uri;
55 }
56
57 @Override
58 public String getType(Uri uri)
59 {
60 /* MIME type for our log file */
61 return "text/plain";
62 }
63
64 @Override
65 public Cursor query(Uri uri, String[] projection, String selection,
66 String[] selectionArgs, String sortOrder)
67 {
68 /* this is called by apps to find out the name and size of the file.
69 * since we only provide a single file this is simple to implement */
70 if (projection == null || projection.length < 1)
71 {
72 return null;
73 }
74 MatrixCursor cursor = new MatrixCursor(projection, 1);
75 if (OpenableColumns.DISPLAY_NAME.equals(cursor.getColumnName(0)))
76 {
77 cursor.newRow().add(CharonVpnService.LOG_FILE);
78 }
79 else if (OpenableColumns.SIZE.equals(cursor.getColumnName(0)))
80 {
81 cursor.newRow().add(mLogFile.length());
82 }
83 else
84 {
85 return null;
86 }
87 return cursor;
88 }
89
90 @Override
91 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException
92 {
93 return ParcelFileDescriptor.open(mLogFile, ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_READ_ONLY);
94 }
95
96 @Override
97 public Uri insert(Uri uri, ContentValues values)
98 {
99 /* not supported */
100 return null;
101 }
102
103 @Override
104 public int delete(Uri uri, String selection, String[] selectionArgs)
105 {
106 /* not supported */
107 return 0;
108 }
109
110 @Override
111 public int update(Uri uri, ContentValues values, String selection,
112 String[] selectionArgs)
113 {
114 /* not supported */
115 return 0;
116 }
117 }