1 /* 2 * Copyright (C) 2012 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.providers.contacts.debug; 18 19 import android.content.ContentProvider; 20 import android.content.ContentValues; 21 import android.database.Cursor; 22 import android.database.MatrixCursor; 23 import android.net.Uri; 24 import android.os.ParcelFileDescriptor; 25 import android.provider.OpenableColumns; 26 27 import java.io.File; 28 import java.io.FileNotFoundException; 29 30 /** 31 * Provider used to read dump files created by {@link DataExporter}. 32 * 33 * We send content: URI to sender apps (such as gmail). This provider implement the URI. 34 */ 35 public class DumpFileProvider extends ContentProvider { 36 public static final String AUTHORITY = "com.android.contacts.dumpfile"; 37 public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY); 38 39 @Override onCreate()40 public boolean onCreate() { 41 return true; 42 } 43 44 @Override insert(Uri uri, ContentValues values)45 public Uri insert(Uri uri, ContentValues values) { 46 // Not needed. 47 throw new UnsupportedOperationException(); 48 } 49 50 @Override delete(Uri uri, String selection, String[] selectionArgs)51 public int delete(Uri uri, String selection, String[] selectionArgs) { 52 // Not needed. 53 throw new UnsupportedOperationException(); 54 } 55 56 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)57 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 58 // Not needed. 59 throw new UnsupportedOperationException(); 60 } 61 62 @Override getType(Uri uri)63 public String getType(Uri uri) { 64 return DataExporter.ZIP_MIME_TYPE; 65 } 66 67 /** @return the path part of a URI, without the beginning "/". */ extractFileName(Uri uri)68 private static String extractFileName(Uri uri) { 69 final String path = uri.getPath(); 70 return path.startsWith("/") ? path.substring(1) : path; 71 } 72 73 /** @return file content */ 74 @Override openFile(Uri uri, String mode)75 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 76 if (!"r".equals(mode)) { 77 throw new UnsupportedOperationException(); 78 } 79 80 final String fileName = extractFileName(uri); 81 DataExporter.ensureValidFileName(fileName); 82 final File file = DataExporter.getOutputFile(getContext(), fileName); 83 return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); 84 } 85 86 /** 87 * Used to provide {@link OpenableColumns#DISPLAY_NAME} and {@link OpenableColumns#SIZE} 88 * for a URI. 89 */ 90 @Override query(Uri uri, String[] inProjection, String selection, String[] selectionArgs, String sortOrder)91 public Cursor query(Uri uri, String[] inProjection, String selection, String[] selectionArgs, 92 String sortOrder) { 93 final String fileName = extractFileName(uri); 94 DataExporter.ensureValidFileName(fileName); 95 96 final String[] projection = (inProjection != null) ? inProjection 97 : new String[] {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}; 98 99 final MatrixCursor c = new MatrixCursor(projection); 100 101 // Result will always have one row. 102 final MatrixCursor.RowBuilder b = c.newRow(); 103 104 for (int i = 0; i < c.getColumnCount(); i++) { 105 final String column = projection[i]; 106 if (OpenableColumns.DISPLAY_NAME.equals(column)) { 107 // Just return the requested path as the display name. We don't care if the file 108 // really exists. 109 b.add(fileName); 110 } else if (OpenableColumns.SIZE.equals(column)) { 111 final File file = DataExporter.getOutputFile(getContext(), fileName); 112 113 if (file.exists()) { 114 b.add(file.length()); 115 } else { 116 // File doesn't exist -- return null for "unknown". 117 b.add(null); 118 } 119 } else { 120 throw new IllegalArgumentException("Unknown column " + column); 121 } 122 } 123 124 return c; 125 } 126 127 } 128