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 */
17package com.android.browser.homepages;
18
19import com.android.browser.BrowserSettings;
20
21import android.content.ContentProvider;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.res.AssetFileDescriptor;
25import android.database.Cursor;
26import android.net.Uri;
27import android.os.ParcelFileDescriptor;
28import android.util.Log;
29import android.webkit.WebResourceResponse;
30
31import java.io.IOException;
32import java.io.InputStream;
33
34public class HomeProvider extends ContentProvider {
35
36    private static final String TAG = "HomeProvider";
37    public static final String AUTHORITY = "com.android.browser.home";
38    public static final String MOST_VISITED = "content://" + AUTHORITY + "/";
39
40    @Override
41    public int delete(Uri uri, String selection, String[] selectionArgs) {
42        return 0;
43    }
44
45    @Override
46    public String getType(Uri uri) {
47        return null;
48    }
49
50    @Override
51    public Uri insert(Uri uri, ContentValues values) {
52        return null;
53    }
54
55    @Override
56    public boolean onCreate() {
57        return false;
58    }
59
60    @Override
61    public Cursor query(Uri uri, String[] projection, String selection,
62            String[] selectionArgs, String sortOrder) {
63        return null;
64    }
65
66    @Override
67    public int update(Uri uri, ContentValues values, String selection,
68            String[] selectionArgs) {
69        return 0;
70    }
71
72    @Override
73    public ParcelFileDescriptor openFile(Uri uri, String mode) {
74        try {
75            ParcelFileDescriptor[] pipes = ParcelFileDescriptor.createPipe();
76            final ParcelFileDescriptor write = pipes[1];
77            AssetFileDescriptor afd = new AssetFileDescriptor(write, 0, -1);
78            new RequestHandler(getContext(), uri, afd.createOutputStream()).start();
79            return pipes[0];
80        } catch (IOException e) {
81            Log.e(TAG, "Failed to handle request: " + uri, e);
82            return null;
83        }
84    }
85
86    public static WebResourceResponse shouldInterceptRequest(Context context,
87            String url) {
88        try {
89            boolean useMostVisited = BrowserSettings.getInstance().useMostVisitedHomepage();
90            if (useMostVisited && url.startsWith("content://")) {
91                Uri uri = Uri.parse(url);
92                if (AUTHORITY.equals(uri.getAuthority())) {
93                    InputStream ins = context.getContentResolver()
94                            .openInputStream(uri);
95                    return new WebResourceResponse("text/html", "utf-8", ins);
96                }
97            }
98        } catch (Exception e) {}
99        return null;
100    }
101
102}
103