1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
19import android.content.ContentProvider;
20import android.content.ContentValues;
21import android.database.Cursor;
22import android.net.Uri;
23import android.os.Environment;
24import android.os.ParcelFileDescriptor;
25
26import java.io.File;
27import java.io.FileNotFoundException;
28
29public class DumpHeapProvider extends ContentProvider {
30    static final Object sLock = new Object();
31    static File sHeapDumpJavaFile;
32
33    static public File getJavaFile() {
34        synchronized (sLock) {
35            return sHeapDumpJavaFile;
36        }
37    }
38
39    @Override
40    public boolean onCreate() {
41        synchronized (sLock) {
42            File dataDir = Environment.getDataDirectory();
43            File systemDir = new File(dataDir, "system");
44            File heapdumpDir = new File(systemDir, "heapdump");
45            heapdumpDir.mkdir();
46            sHeapDumpJavaFile = new File(heapdumpDir, "javaheap.bin");
47        }
48        return true;
49    }
50
51    @Override
52    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
53        return null;
54    }
55
56    @Override
57    public String getType(Uri uri) {
58        return "application/octet-stream";
59    }
60
61    @Override
62    public Uri insert(Uri uri, ContentValues values) {
63        return null;
64    }
65
66    @Override
67    public int delete(Uri uri, String selection, String[] selectionArgs) {
68        return 0;
69    }
70
71    @Override
72    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
73        return 0;
74    }
75
76    @Override
77    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
78        synchronized (sLock) {
79            String path = uri.getEncodedPath();
80            final String tag = Uri.decode(path);
81            if (tag.equals("/java")) {
82                return ParcelFileDescriptor.open(sHeapDumpJavaFile,
83                        ParcelFileDescriptor.MODE_READ_ONLY);
84            } else {
85                throw new FileNotFoundException("Invalid path for " + uri);
86            }
87        }
88    }
89}
90