]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/frontends/android/app/src/main/java/org/strongswan/android/data/LogContentProvider.java
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / data / LogContentProvider.java
CommitLineData
c3afe9d3
TB
1/*
2 * Copyright (C) 2012 Tobias Brunner
19ef2aec
TB
3 *
4 * Copyright (C) secunet Security Networks AG
c3afe9d3
TB
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17package org.strongswan.android.data;
18
19import java.io.File;
20import java.io.FileNotFoundException;
064f4f75
TB
21import java.security.NoSuchAlgorithmException;
22import java.security.SecureRandom;
23import java.util.concurrent.ConcurrentHashMap;
c3afe9d3
TB
24
25import org.strongswan.android.logic.CharonVpnService;
26
27import android.content.ContentProvider;
28import android.content.ContentValues;
29import android.database.Cursor;
30import android.database.MatrixCursor;
31import android.net.Uri;
32import android.os.ParcelFileDescriptor;
064f4f75 33import android.os.SystemClock;
c3afe9d3
TB
34import android.provider.OpenableColumns;
35
36public class LogContentProvider extends ContentProvider
37{
38 private static final String AUTHORITY = "org.strongswan.android.content.log";
064f4f75
TB
39 /* an Uri is valid for 30 minutes */
40 private static final long URI_VALIDITY = 30 * 60 * 1000;
41 private static ConcurrentHashMap<Uri, Long> mUris = new ConcurrentHashMap<Uri, Long>();
c3afe9d3
TB
42 private File mLogFile;
43
44 public LogContentProvider()
45 {
46 }
47
48 @Override
49 public boolean onCreate()
50 {
51 mLogFile = new File(getContext().getFilesDir(), CharonVpnService.LOG_FILE);
52 return true;
53 }
54
55 /**
56 * The log file can only be accessed by Uris created with this method
57 * @return null if failed to create the Uri
58 */
59 public static Uri createContentUri()
60 {
064f4f75
TB
61 SecureRandom random;
62 try
63 {
64 random = SecureRandom.getInstance("SHA1PRNG");
65 }
66 catch (NoSuchAlgorithmException e)
67 {
68 return null;
69 }
70 Uri uri = Uri.parse("content://" + AUTHORITY + "/" + random.nextLong());
71 mUris.put(uri, SystemClock.uptimeMillis());
c3afe9d3
TB
72 return uri;
73 }
74
75 @Override
76 public String getType(Uri uri)
77 {
78 /* MIME type for our log file */
79 return "text/plain";
80 }
81
82 @Override
83 public Cursor query(Uri uri, String[] projection, String selection,
84 String[] selectionArgs, String sortOrder)
85 {
86 /* this is called by apps to find out the name and size of the file.
87 * since we only provide a single file this is simple to implement */
88 if (projection == null || projection.length < 1)
89 {
90 return null;
91 }
064f4f75
TB
92 Long timestamp = mUris.get(uri);
93 if (timestamp == null)
94 { /* don't check the validity as this information is not really private */
95 return null;
96 }
c3afe9d3
TB
97 MatrixCursor cursor = new MatrixCursor(projection, 1);
98 if (OpenableColumns.DISPLAY_NAME.equals(cursor.getColumnName(0)))
99 {
100 cursor.newRow().add(CharonVpnService.LOG_FILE);
101 }
102 else if (OpenableColumns.SIZE.equals(cursor.getColumnName(0)))
103 {
104 cursor.newRow().add(mLogFile.length());
105 }
106 else
107 {
108 return null;
109 }
110 return cursor;
111 }
112
113 @Override
114 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException
115 {
064f4f75
TB
116 Long timestamp = mUris.get(uri);
117 if (timestamp != null)
118 {
119 long elapsed = SystemClock.uptimeMillis() - timestamp;
120 if (elapsed > 0 && elapsed < URI_VALIDITY)
121 { /* we fail if clock wrapped, should happen rarely though */
122 return ParcelFileDescriptor.open(mLogFile, ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_READ_ONLY);
123 }
124 mUris.remove(uri);
125 }
126 return super.openFile(uri, mode);
c3afe9d3
TB
127 }
128
129 @Override
130 public Uri insert(Uri uri, ContentValues values)
131 {
132 /* not supported */
133 return null;
134 }
135
136 @Override
137 public int delete(Uri uri, String selection, String[] selectionArgs)
138 {
139 /* not supported */
140 return 0;
141 }
142
143 @Override
144 public int update(Uri uri, ContentValues values, String selection,
145 String[] selectionArgs)
146 {
147 /* not supported */
148 return 0;
149 }
150}