summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/provider/MyNavigationProvider.java
blob: 3dc2954bf6c0036bc9287ab0938af91249368371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *       * Redistributions in binary form must reproduce the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer in the documentation and/or other materials provided
 *         with the distribution.
 *       * Neither the name of The Linux Foundation nor the names of its
 *         contributors may be used to endorse or promote products derived
 *         from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.browser.provider;

import android.content.Context;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebResourceResponse;

import com.android.browser.BrowserSettings;
import com.android.browser.homepages.RequestHandler;
import com.android.browser.mynavigation.MyNavigationUtil;
import com.android.browser.mynavigation.MyNavigationRequestHandler;
import com.android.browser.provider.BrowserProvider2;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import com.android.browser.R;

public class MyNavigationProvider extends ContentProvider {

    private static final String LOGTAG = "MyNavigationProvider";
    private static final String TABLE_WEB_SITES = "websites";
    private static final int WEB_SITES_ALL = 0;
    private static final int WEB_SITES_ID = 1;
    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        URI_MATCHER.addURI(MyNavigationUtil.AUTHORITY, "websites", WEB_SITES_ALL);
        URI_MATCHER.addURI(MyNavigationUtil.AUTHORITY, "websites/#", WEB_SITES_ID);
    }
    private static final Uri NOTIFICATION_URI = MyNavigationUtil.MY_NAVIGATION_URI;

    private SiteNavigationDatabaseHelper mOpenHelper;

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Current not used, just return 0
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        // Current not used, just return null
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // Current not used, just return null
        return null;
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new SiteNavigationDatabaseHelper(this.getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(TABLE_WEB_SITES);
        switch (URI_MATCHER.match(uri)) {
            case WEB_SITES_ALL:
                break;
            case WEB_SITES_ID:
                qb.appendWhere(MyNavigationUtil.ID + "=" + uri.getPathSegments().get(0));
                break;
            default:
                Log.e(LOGTAG, "query Unknown URI: " + uri);
                return null;
        }

        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = null;
        } else {
            orderBy = sortOrder;
        }
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
        if (c != null) {
            c.setNotificationUri(getContext().getContentResolver(), NOTIFICATION_URI);
        }
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count = 0;

        switch (URI_MATCHER.match(uri)) {
            case WEB_SITES_ALL:
                count = db.update(TABLE_WEB_SITES, values, selection, selectionArgs);
                break;
            case WEB_SITES_ID:
                String newIdSelection = MyNavigationUtil.ID + "=" + uri.getLastPathSegment()
                        + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
                count = db.update(TABLE_WEB_SITES, values, newIdSelection, selectionArgs);
                break;
            default:
                Log.e(LOGTAG, "update Unknown URI: " + uri);
                return count;
        }

        if (count > 0) {
            ContentResolver cr = getContext().getContentResolver();
            cr.notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) {
        try {
            ParcelFileDescriptor[] pipes = ParcelFileDescriptor.createPipe();
            final ParcelFileDescriptor write = pipes[1];
            AssetFileDescriptor afd = new AssetFileDescriptor(write, 0, -1);
            new MyNavigationRequestHandler(getContext(), uri, afd.createOutputStream())
                    .start();
            return pipes[0];
        } catch (IOException e) {
            Log.e(LOGTAG, "Failed to handle request: " + uri, e);
            return null;
        }
    }

    public static WebResourceResponse shouldInterceptRequest(Context context,
            String url) {
        try {
            if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
                Uri uri = Uri.parse(url);
                if (MyNavigationUtil.AUTHORITY.equals(uri.getAuthority())) {
                    InputStream ins = context.getContentResolver()
                            .openInputStream(uri);
                    return new WebResourceResponse("text/html", "utf-8", ins);
                }
            }
            boolean listFiles = BrowserSettings.getInstance().isDebugEnabled();
            if (listFiles && interceptFile(url)) {
                PipedInputStream ins = new PipedInputStream();
                PipedOutputStream outs = new PipedOutputStream(ins);
                new RequestHandler(context, Uri.parse(url), outs).start();
                return new WebResourceResponse("text/html", "utf-8", ins);
            }
        } catch (Exception e) {}
        return null;
    }

    private static boolean interceptFile(String url) {
        if (!url.startsWith("file:///")) {
            return false;
        }
        String fpath = url.substring(7);
        File f = new File(fpath);
        if (!f.isDirectory()) {
            return false;
        }
        return true;
    }

    private class SiteNavigationDatabaseHelper extends SQLiteOpenHelper {
        private Context mContext;
        static final String DATABASE_NAME = "mynavigation.db";

        public SiteNavigationDatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, 1); // "1" is the db version here
            // TODO Auto-generated constructor stub
            mContext = context;
        }

        public SiteNavigationDatabaseHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
            mContext = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            createWebsitesTable(db);
            initWebsitesTable(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
        }

        private void createWebsitesTable(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE websites (" +
                    MyNavigationUtil.ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    MyNavigationUtil.URL + " TEXT," +
                    MyNavigationUtil.TITLE + " TEXT," +
                    MyNavigationUtil.DATE_CREATED + " LONG," +
                    MyNavigationUtil.WEBSITE + " INTEGER," +
                    MyNavigationUtil.THUMBNAIL + " BLOB DEFAULT NULL," +
                    MyNavigationUtil.FAVICON + " BLOB DEFAULT NULL," +
                    MyNavigationUtil.DEFAULT_THUMB + " TEXT" +
                    ");");
        }

        // initial table , insert websites to table websites
        private void initWebsitesTable(SQLiteDatabase db) {
            int WebsiteNumber = MyNavigationUtil.WEBSITE_NUMBER;
            for (int i = 0; i < WebsiteNumber; i++) {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                        R.raw.my_navigation_add);
                bm.compress(Bitmap.CompressFormat.PNG, 100, os);
                ContentValues values = new ContentValues();
                values.put(MyNavigationUtil.URL, "ae://" + (i + 1) + "add-fav");
                values.put(MyNavigationUtil.TITLE, mContext.getString(R.string.my_navigation_add));
                values.put(MyNavigationUtil.DATE_CREATED, 0 + "");
                values.put(MyNavigationUtil.WEBSITE, 1 + "");
                values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
                db.insertOrThrow(TABLE_WEB_SITES, MyNavigationUtil.URL, values);
            }
        }
    }
}