• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2011 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.android.browser.homepages;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.res.AssetFileDescriptor;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.os.ParcelFileDescriptor;
26 import android.util.Log;
27 import android.webkit.WebResourceResponse;
28 
29 import com.android.browser.BrowserSettings;
30 
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.PipedInputStream;
35 import java.io.PipedOutputStream;
36 
37 public class HomeProvider extends ContentProvider {
38 
39     private static final String TAG = "HomeProvider";
40     public static final String AUTHORITY = "com.android.browser.home";
41     public static final String MOST_VISITED = "content://" + AUTHORITY + "/";
42 
43     @Override
delete(Uri uri, String selection, String[] selectionArgs)44     public int delete(Uri uri, String selection, String[] selectionArgs) {
45         return 0;
46     }
47 
48     @Override
getType(Uri uri)49     public String getType(Uri uri) {
50         return null;
51     }
52 
53     @Override
insert(Uri uri, ContentValues values)54     public Uri insert(Uri uri, ContentValues values) {
55         return null;
56     }
57 
58     @Override
onCreate()59     public boolean onCreate() {
60         return false;
61     }
62 
63     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)64     public Cursor query(Uri uri, String[] projection, String selection,
65             String[] selectionArgs, String sortOrder) {
66         return null;
67     }
68 
69     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)70     public int update(Uri uri, ContentValues values, String selection,
71             String[] selectionArgs) {
72         return 0;
73     }
74 
75     @Override
openFile(Uri uri, String mode)76     public ParcelFileDescriptor openFile(Uri uri, String mode) {
77         try {
78             ParcelFileDescriptor[] pipes = ParcelFileDescriptor.createPipe();
79             final ParcelFileDescriptor write = pipes[1];
80             AssetFileDescriptor afd = new AssetFileDescriptor(write, 0, -1);
81             new RequestHandler(getContext(), uri, afd.createOutputStream()).start();
82             return pipes[0];
83         } catch (IOException e) {
84             Log.e(TAG, "Failed to handle request: " + uri, e);
85             return null;
86         }
87     }
88 
shouldInterceptRequest(Context context, String url)89     public static WebResourceResponse shouldInterceptRequest(Context context,
90             String url) {
91         try {
92             boolean useMostVisited = BrowserSettings.getInstance().useMostVisitedHomepage();
93             if (useMostVisited && url.startsWith("content://")) {
94                 Uri uri = Uri.parse(url);
95                 if (AUTHORITY.equals(uri.getAuthority())) {
96                     InputStream ins = context.getContentResolver()
97                             .openInputStream(uri);
98                     return new WebResourceResponse("text/html", "utf-8", ins);
99                 }
100             }
101             boolean listFiles = BrowserSettings.getInstance().isDebugEnabled();
102             if (listFiles && interceptFile(url)) {
103                 PipedInputStream ins = new PipedInputStream();
104                 PipedOutputStream outs = new PipedOutputStream(ins);
105                 new RequestHandler(context, Uri.parse(url), outs).start();
106                 return new WebResourceResponse("text/html", "utf-8", ins);
107             }
108         } catch (Exception e) {}
109         return null;
110     }
111 
interceptFile(String url)112     private static boolean interceptFile(String url) {
113         if (!url.startsWith("file:///")) {
114             return false;
115         }
116         String fpath = url.substring(7);
117         File f = new File(fpath);
118         if (!f.isDirectory()) {
119             return false;
120         }
121         return true;
122     }
123 
124 }
125