| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package com.android.webview.chromium; | 5 package com.android.webview.chromium; |
| 6 | 6 |
| 7 import android.content.ContentProvider; | 7 import android.content.ContentProvider; |
| 8 import android.content.ContentValues; | 8 import android.content.ContentValues; |
| 9 import android.content.res.AssetFileDescriptor; | 9 import android.content.res.AssetFileDescriptor; |
| 10 import android.database.Cursor; | 10 import android.database.Cursor; |
| 11 import android.net.Uri; | 11 import android.net.Uri; |
| 12 import android.os.ParcelFileDescriptor; | 12 import android.os.ParcelFileDescriptor; |
| 13 import android.util.Log; | 13 import android.util.Log; |
| 14 | 14 |
| 15 import java.io.BufferedOutputStream; | 15 import org.chromium.base.FileUtils; |
| 16 |
| 16 import java.io.File; | 17 import java.io.File; |
| 17 import java.io.FileOutputStream; | |
| 18 import java.io.IOException; | 18 import java.io.IOException; |
| 19 import java.io.InputStream; | |
| 20 import java.io.OutputStream; | |
| 21 | 19 |
| 22 /** | 20 /** |
| 23 * Content provider for the OSS licenses file. | 21 * Content provider for the OSS licenses file. |
| 24 */ | 22 */ |
| 25 public class LicenseContentProvider extends ContentProvider { | 23 public class LicenseContentProvider extends ContentProvider { |
| 26 public static final String LICENSES_URI_SUFFIX = "LicenseContentProvider/web
view_licenses"; | 24 public static final String LICENSES_URI_SUFFIX = "LicenseContentProvider/web
view_licenses"; |
| 27 public static final String LICENSES_CONTENT_TYPE = "text/html"; | 25 public static final String LICENSES_CONTENT_TYPE = "text/html"; |
| 28 | 26 |
| 29 @Override | 27 @Override |
| 30 public boolean onCreate() { | 28 public boolean onCreate() { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 41 } | 39 } |
| 42 } | 40 } |
| 43 return null; | 41 return null; |
| 44 } | 42 } |
| 45 | 43 |
| 46 // This is to work around the known limitation of AssetManager.openFd to ref
use | 44 // This is to work around the known limitation of AssetManager.openFd to ref
use |
| 47 // opening files that are compressed in the apk file. | 45 // opening files that are compressed in the apk file. |
| 48 private AssetFileDescriptor extractAsset(String name) throws IOException { | 46 private AssetFileDescriptor extractAsset(String name) throws IOException { |
| 49 File extractedFile = new File(getContext().getCacheDir(), name); | 47 File extractedFile = new File(getContext().getCacheDir(), name); |
| 50 if (!extractedFile.exists()) { | 48 if (!extractedFile.exists()) { |
| 51 InputStream inputStream = null; | 49 FileUtils.extractAsset(getContext(), name, extractedFile); |
| 52 OutputStream outputStream = null; | |
| 53 try { | |
| 54 inputStream = getContext().getAssets().open(name); | |
| 55 outputStream = new BufferedOutputStream( | |
| 56 new FileOutputStream(extractedFile.getAbsolutePath())); | |
| 57 copyStreams(inputStream, outputStream); | |
| 58 } finally { | |
| 59 if (inputStream != null) inputStream.close(); | |
| 60 if (outputStream != null) outputStream.close(); | |
| 61 } | |
| 62 } | 50 } |
| 63 ParcelFileDescriptor parcelFd = | 51 ParcelFileDescriptor parcelFd = |
| 64 ParcelFileDescriptor.open(extractedFile, ParcelFileDescriptor.MO
DE_READ_ONLY); | 52 ParcelFileDescriptor.open(extractedFile, ParcelFileDescriptor.MO
DE_READ_ONLY); |
| 65 if (parcelFd != null) { | 53 if (parcelFd != null) { |
| 66 return new AssetFileDescriptor(parcelFd, 0, parcelFd.getStatSize()); | 54 return new AssetFileDescriptor(parcelFd, 0, parcelFd.getStatSize()); |
| 67 } | 55 } |
| 68 return null; | 56 return null; |
| 69 } | 57 } |
| 70 | 58 |
| 71 private static void copyStreams(InputStream in, OutputStream out) throws IOE
xception { | |
| 72 byte[] buffer = new byte[8192]; | |
| 73 int c; | |
| 74 while ((c = in.read(buffer)) != -1) { | |
| 75 out.write(buffer, 0, c); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 @Override | 59 @Override |
| 80 public String getType(Uri uri) { | 60 public String getType(Uri uri) { |
| 81 if (uri != null && uri.toString().endsWith(LICENSES_URI_SUFFIX)) { | 61 if (uri != null && uri.toString().endsWith(LICENSES_URI_SUFFIX)) { |
| 82 return LICENSES_CONTENT_TYPE; | 62 return LICENSES_CONTENT_TYPE; |
| 83 } | 63 } |
| 84 return null; | 64 return null; |
| 85 } | 65 } |
| 86 | 66 |
| 87 @Override | 67 @Override |
| 88 public int update(Uri uri, ContentValues values, String where, | 68 public int update(Uri uri, ContentValues values, String where, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 99 public Uri insert(Uri uri, ContentValues values) { | 79 public Uri insert(Uri uri, ContentValues values) { |
| 100 throw new UnsupportedOperationException(); | 80 throw new UnsupportedOperationException(); |
| 101 } | 81 } |
| 102 | 82 |
| 103 @Override | 83 @Override |
| 104 public Cursor query(Uri uri, String[] projection, String selection, | 84 public Cursor query(Uri uri, String[] projection, String selection, |
| 105 String[] selectionArgs, String sortOrder) { | 85 String[] selectionArgs, String sortOrder) { |
| 106 throw new UnsupportedOperationException(); | 86 throw new UnsupportedOperationException(); |
| 107 } | 87 } |
| 108 } | 88 } |
| OLD | NEW |