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 17 package com.android.shell; 18 19 import android.database.Cursor; 20 import android.database.MatrixCursor; 21 import android.database.MatrixCursor.RowBuilder; 22 import android.net.Uri; 23 import android.os.CancellationSignal; 24 import android.os.FileUtils; 25 import android.os.ParcelFileDescriptor; 26 import android.provider.DocumentsContract; 27 import android.provider.DocumentsContract.Document; 28 import android.provider.DocumentsContract.Root; 29 30 import com.android.internal.content.FileSystemProvider; 31 32 import java.io.File; 33 import java.io.FileNotFoundException; 34 35 public class BugreportStorageProvider extends FileSystemProvider { 36 private static final String AUTHORITY = "com.android.shell.documents"; 37 private static final String DOC_ID_ROOT = "bugreport"; 38 39 private static final String[] DEFAULT_ROOT_PROJECTION = new String[]{ 40 Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_ICON, Root.COLUMN_TITLE, 41 Root.COLUMN_DOCUMENT_ID, 42 }; 43 44 private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[]{ 45 Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME, 46 Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE, 47 }; 48 49 private File mRoot; 50 51 @Override 52 public boolean onCreate() { 53 super.onCreate(DEFAULT_DOCUMENT_PROJECTION); 54 mRoot = new File(getContext().getFilesDir(), "bugreports"); 55 return true; 56 } 57 58 @Override 59 public Cursor queryRoots(String[] projection) throws FileNotFoundException { 60 final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection)); 61 final RowBuilder row = result.newRow(); 62 row.add(Root.COLUMN_ROOT_ID, DOC_ID_ROOT); 63 row.add(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY); 64 row.add(Root.COLUMN_ICON, android.R.mipmap.sym_def_app_icon); 65 row.add(Root.COLUMN_TITLE, getContext().getString(R.string.bugreport_storage_title)); 66 row.add(Root.COLUMN_DOCUMENT_ID, DOC_ID_ROOT); 67 return result; 68 } 69 70 @Override 71 public Cursor queryDocument(String documentId, String[] projection) 72 throws FileNotFoundException { 73 if (DOC_ID_ROOT.equals(documentId)) { 74 final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection)); 75 includeDefaultDocument(result); 76 return result; 77 } else { 78 return super.queryDocument(documentId, projection); 79 } 80 } 81 82 @Override 83 public ParcelFileDescriptor openDocument( 84 String documentId, String mode, CancellationSignal signal) 85 throws FileNotFoundException { 86 if (ParcelFileDescriptor.parseMode(mode) != ParcelFileDescriptor.MODE_READ_ONLY) { 87 throw new FileNotFoundException("Failed to open: " + documentId + ", mode = " + mode); 88 } 89 return ParcelFileDescriptor.open(getFileForDocId(documentId), 90 ParcelFileDescriptor.MODE_READ_ONLY); 91 } 92 93 @Override 94 protected Uri buildNotificationUri(String docId) { 95 return DocumentsContract.buildChildDocumentsUri(AUTHORITY, docId); 96 } 97 98 private static String[] resolveRootProjection(String[] projection) { 99 return projection != null ? projection : DEFAULT_ROOT_PROJECTION; 100 } 101 102 private static String[] resolveDocumentProjection(String[] projection) { 103 return projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION; 104 } 105 106 @Override 107 protected String getDocIdForFile(File file) { 108 return DOC_ID_ROOT + ":" + file.getName(); 109 } 110 111 @Override 112 protected File getFileForDocId(String documentId, boolean visible) 113 throws FileNotFoundException { 114 if (DOC_ID_ROOT.equals(documentId)) { 115 return mRoot; 116 } else { 117 final int splitIndex = documentId.indexOf(':', 1); 118 final String name = documentId.substring(splitIndex + 1); 119 if (splitIndex == -1 || !DOC_ID_ROOT.equals(documentId.substring(0, splitIndex)) || 120 !FileUtils.isValidExtFilename(name)) { 121 throw new FileNotFoundException("Invalid document ID: " + documentId); 122 } 123 final File file = new File(mRoot, name); 124 if (!file.exists()) { 125 throw new FileNotFoundException("File not found: " + documentId); 126 } 127 return file; 128 } 129 } 130 131 @Override 132 protected RowBuilder includeFile(MatrixCursor result, String docId, File file) 133 throws FileNotFoundException { 134 RowBuilder row = super.includeFile(result, docId, file); 135 row.add(Document.COLUMN_FLAGS, Document.FLAG_SUPPORTS_DELETE); 136 return row; 137 } 138 139 private void includeDefaultDocument(MatrixCursor result) { 140 final RowBuilder row = result.newRow(); 141 row.add(Document.COLUMN_DOCUMENT_ID, DOC_ID_ROOT); 142 row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR); 143 row.add(Document.COLUMN_DISPLAY_NAME, mRoot.getName()); 144 row.add(Document.COLUMN_LAST_MODIFIED, mRoot.lastModified()); 145 row.add(Document.COLUMN_FLAGS, Document.FLAG_DIR_PREFERS_LAST_MODIFIED); 146 } 147 } 148