aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/research/ResearchLogDirectory.java
blob: d156068d6bb2e8ae0e6922dedba344589201554f (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * 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, final long nanoTime) {
        return new File(mFilesDir, getUniqueFilename(LOG_FILENAME_PREFIX, time, nanoTime));
    }

    public File getUserRecordingFilePath(final long time, final long nanoTime) {
        return new File(mFilesDir, getUniqueFilename(USER_RECORDING_FILENAME_PREFIX, time,
                nanoTime));
    }

    private static String getUniqueFilename(final String prefix, final long time,
            final long nanoTime) {
        return prefix + "-" + time + "-" + nanoTime + FILENAME_SUFFIX;
    }
}