diff options
author | 2013-02-27 14:40:54 -0800 | |
---|---|---|
committer | 2013-03-14 06:37:56 -0700 | |
commit | f33f1cab2fd57fe39955593e68902554ed57a053 (patch) | |
tree | bf680c9e18492bcca9b26bae0d755a8b2dd540a7 /java/src/com/android/inputmethod/research/ResearchLogDirectory.java | |
parent | 7faa2caa80ea3b07ab317a5aa92bd048b574af27 (diff) | |
download | latinime-f33f1cab2fd57fe39955593e68902554ed57a053.tar.gz latinime-f33f1cab2fd57fe39955593e68902554ed57a053.tar.xz latinime-f33f1cab2fd57fe39955593e68902554ed57a053.zip |
[FileEncap9] Extract ResearchLogDirectory class
Previously used a monotonically increasing int. Now uses uuid and nanoseconds.
squashed in:
[FileEncap11] Read preference from ResearchSettings
Change-Id: Ic779e0a69db6b16e92c6f4b63dbe7b7add566ab6
[FileEncap12] Simplify directory cleanup invocation
Change-Id: I688047409c0343d32b11447fb625dfb726c731ec
[FileEncap14] Change log filename syntax
Change-Id: I9243b20b2eb392f81ab8c5c3d19315211240e0bc
Change-Id: I5c9d70e0cb7b0965158e17dd71dfab796bd9a440
Diffstat (limited to 'java/src/com/android/inputmethod/research/ResearchLogDirectory.java')
-rw-r--r-- | java/src/com/android/inputmethod/research/ResearchLogDirectory.java | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/research/ResearchLogDirectory.java b/java/src/com/android/inputmethod/research/ResearchLogDirectory.java new file mode 100644 index 000000000..291dea5d0 --- /dev/null +++ b/java/src/com/android/inputmethod/research/ResearchLogDirectory.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.research; + +import android.content.Context; +import android.util.Log; + +import java.io.File; +import java.io.FileFilter; + +/** + * Manages log files. + * + * This class handles all aspects where and how research log data is stored. This includes + * generating log filenames in the correct place with the correct names, and cleaning up log files + * under this directory. + */ +public class ResearchLogDirectory { + public static final String TAG = ResearchLogDirectory.class.getSimpleName(); + /* package */ static final String LOG_FILENAME_PREFIX = "researchLog"; + private static final String FILENAME_SUFFIX = ".txt"; + private static final String USER_RECORDING_FILENAME_PREFIX = "recording"; + + private static final ReadOnlyLogFileFilter sUploadableLogFileFilter = + new ReadOnlyLogFileFilter(); + + private final File mFilesDir; + + static class ReadOnlyLogFileFilter implements FileFilter { + @Override + public boolean accept(final File pathname) { + return pathname.getName().startsWith(ResearchLogDirectory.LOG_FILENAME_PREFIX) + && !pathname.canWrite(); + } + } + + /** + * Creates a new ResearchLogDirectory, creating the storage directory if it does not exist. + */ + public ResearchLogDirectory(final Context context) { + mFilesDir = getLoggingDirectory(context); + if (mFilesDir == null) { + throw new NullPointerException("No files directory specified"); + } + if (!mFilesDir.exists()) { + mFilesDir.mkdirs(); + } + } + + private File getLoggingDirectory(final Context context) { + // TODO: Switch to using a subdirectory of getFilesDir(). + return context.getFilesDir(); + } + + /** + * Get an array of log files that are ready for uploading. + * + * A file is ready for uploading if it is marked as read-only. + * + * @return the array of uploadable files + */ + public File[] getUploadableLogFiles() { + try { + return mFilesDir.listFiles(sUploadableLogFileFilter); + } catch (final SecurityException e) { + Log.e(TAG, "Could not cleanup log directory, permission denied", e); + return new File[0]; + } + } + + public void cleanupLogFilesOlderThan(final long time) { + try { + for (final File file : mFilesDir.listFiles()) { + final String filename = file.getName(); + if ((filename.startsWith(LOG_FILENAME_PREFIX) + || filename.startsWith(USER_RECORDING_FILENAME_PREFIX)) + && (file.lastModified() < time)) { + file.delete(); + } + } + } catch (final SecurityException e) { + Log.e(TAG, "Could not cleanup log directory, permission denied", e); + } + } + + public File getLogFilePath(final long time) { + return new File(mFilesDir, getUniqueFilename(LOG_FILENAME_PREFIX, time)); + } + + public File getUserRecordingFilePath(final long time) { + return new File(mFilesDir, getUniqueFilename(USER_RECORDING_FILENAME_PREFIX, time)); + } + + private static String getUniqueFilename(final String prefix, final long time) { + return prefix + "-" + time + FILENAME_SUFFIX; + } +} |