码迷,mamicode.com
首页 > 其他好文 > 详细

Browser默认书签加载过程

时间:2015-05-15 15:22:29      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

Browser配置默认书签——string.xml中<string-array name="bookmarks" translatable="false">下添加书签item.

 <!-- Bookmarks -->
    <string-array name="bookmarks" translatable="false">
        <item>百度</item>
        <item>http://m.baidu.com/?from=1000235a</item>
        <item>新浪</item>
        <item>http://3g.sina.cn</item>
        <item>腾讯网</item>
        <item>http://3g.qq.com</item>
        <item>搜狐</item>
        <item>http://m.sohu.com/?_trans_=000011_tysj_sysq</item> 
        <item>网易</item>
        <item>http://3g.163.com/</item>         
    </string-array>

加载过程——BrowserProvider2创建数据库,初始化table数据时,插入书签数据。

<application
        android:name="Browser"
        android:backupAgent=".BrowserBackupAgent"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher_browser"
        android:label="@string/application_name"
        android:taskAffinity="android.task.browser"
        android:theme="@style/Theme.Setting.Light" >
        <provider
            android:name=".provider.BrowserProvider2"
            android:authorities="com.android.browser;browser"
            android:exported="true"
            android:multiprocess="false"
            android:readPermission="com.android.browser.permission.READ_HISTORY_BOOKMARKS"
            android:writePermission="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS" >
            <path-permission
                android:path="/bookmarks/search_suggest_query"
                android:readPermission="android.permission.GLOBAL_SEARCH" />
        </provider>
public class BrowserProvider2 extends SQLiteContentProvider

public abstract class SQLiteContentProvider extends ContentProvider

public class BrowserProvider2 extends SQLiteContentProvider {
  final class DatabaseHelper extends SQLiteOpenHelper {
     static final String DATABASE_NAME = "browser2.db";
        static final int DATABASE_VERSION = 32;
        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            setWriteAheadLoggingEnabled(true);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_BOOKMARKS + "(" +
                    Bookmarks._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    Bookmarks.TITLE + " TEXT," +
                    Bookmarks.URL + " TEXT," +
                    Bookmarks.IS_FOLDER + " INTEGER NOT NULL DEFAULT 0," +
                    Bookmarks.PARENT + " INTEGER," +
                    Bookmarks.POSITION + " INTEGER NOT NULL," +
                    Bookmarks.INSERT_AFTER + " INTEGER," +
                    Bookmarks.IS_DELETED + " INTEGER NOT NULL DEFAULT 0," +
                    Bookmarks.ACCOUNT_NAME + " TEXT," +
                    Bookmarks.ACCOUNT_TYPE + " TEXT," +
                    Bookmarks.SOURCE_ID + " TEXT," +
                    Bookmarks.VERSION + " INTEGER NOT NULL DEFAULT 1," +
                    Bookmarks.DATE_CREATED + " INTEGER," +
                    Bookmarks.DATE_MODIFIED + " INTEGER," +
                    Bookmarks.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
                    BrowserUtils.Bookmarks_VISITES + " INTEGER NOT NULL DEFAULT 0," +
                    BrowserUtils.Bookmarks_DATE_LAST_VISITED + " INTEGER," +
                    Bookmarks.SYNC1 + " TEXT," +
                    Bookmarks.SYNC2 + " TEXT," +
                    Bookmarks.SYNC3 + " TEXT," +
                    Bookmarks.SYNC4 + " TEXT," +
                    Bookmarks.SYNC5 + " TEXT" +
                    ");");

            // TODO indices

            db.execSQL("CREATE TABLE " + TABLE_HISTORY + "(" +
                    History._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    History.TITLE + " TEXT," +
                    History.URL + " TEXT NOT NULL," +
                    History.DATE_CREATED + " INTEGER," +
                    History.DATE_LAST_VISITED + " INTEGER," +
                    History.VISITS + " INTEGER NOT NULL DEFAULT 0," +
                    History.USER_ENTERED + " INTEGER" +
                    ");");

            db.execSQL("CREATE TABLE " + TABLE_IMAGES + " (" +
                    Images.URL + " TEXT UNIQUE NOT NULL," +
                    Images.FAVICON + " BLOB," +
                    Images.THUMBNAIL + " BLOB," +
                    Images.TOUCH_ICON + " BLOB" +
                    ");");
            db.execSQL("CREATE INDEX imagesUrlIndex ON " + TABLE_IMAGES +
                    "(" + Images.URL + ")");

            db.execSQL("CREATE TABLE " + TABLE_SEARCHES + " (" +
                    Searches._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    Searches.SEARCH + " TEXT," +
                    Searches.DATE + " LONG" +
                    ");");

            db.execSQL("CREATE TABLE " + TABLE_SETTINGS + " (" +
                    Settings.KEY + " TEXT PRIMARY KEY," +
                    Settings.VALUE + " TEXT NOT NULL" +
                    ");");

            createProperties(db);
            createAccountsView(db);
            createThumbnails(db);

            mSyncHelper.createDatabase(db);

            if (!importFromBrowserProvider(db)) {
                createDefaultBookmarks(db);//默认书签插入数据库
            }

            enableSync(db);
            createOmniboxSuggestions(db);
        }
技术分享
private void createDefaultBookmarks(SQLiteDatabase db) {
            ContentValues values = new ContentValues();
            // TODO figure out how to deal with localization for the defaults

            // Bookmarks folder
            values.put(Bookmarks._ID, FIXED_ID_ROOT);
            values.put(ChromeSyncColumns.SERVER_UNIQUE, ChromeSyncColumns.FOLDER_NAME_BOOKMARKS);
            values.put(Bookmarks.TITLE, "Bookmarks");
            values.putNull(Bookmarks.PARENT);
            values.put(Bookmarks.POSITION, 0);
            values.put(Bookmarks.IS_FOLDER, true);
            values.put(Bookmarks.DIRTY, true);
            db.insertOrThrow(TABLE_BOOKMARKS, null, values);

            addDefaultBookmarks(db, FIXED_ID_ROOT);
        }

        private void addDefaultBookmarks(SQLiteDatabase db, long parentId) {
            Resources res = getContext().getResources();
            final CharSequence[] bookmarks = res.getTextArray(
                    R.array.bookmarks);
            int size = bookmarks.length;
            Log.i("antoon", TAG+", addDefaultBookmarks, size = "+size);
            TypedArray preloads = res.obtainTypedArray(R.array.bookmark_preloads);
            try {
                String parent = Long.toString(parentId);
                String now = Long.toString(System.currentTimeMillis());
                for (int i = 0; i < size; i = i + 2) {
                    CharSequence bookmarkDestination = replaceSystemPropertyInString(getContext(),
                            bookmarks[i + 1]);
                    Log.d("antoon", TAG+", "+bookmarks[i]+"--"+bookmarkDestination);
                    db.execSQL("INSERT INTO bookmarks (" +
                            Bookmarks.TITLE + ", " +
                            Bookmarks.URL + ", " +
                            Bookmarks.IS_FOLDER + "," +
                            Bookmarks.PARENT + "," +
                            Bookmarks.POSITION + "," +
                            Bookmarks.DATE_CREATED + ","+
                            BrowserUtils.Bookmarks_VISITES + "," +
                            BrowserUtils.Bookmarks_DATE_LAST_VISITED +
                        ") VALUES (" +
                            "‘" + bookmarks[i] + "‘, " +
                            "‘" + bookmarkDestination + "‘, " +
                            "0," +
                            parent + "," +
                            Integer.toString(i) + "," +
                            now + "," +
                            "0," +
                            now +
                            ");");
                    /*插入res/raw/中的图片与书签匹配显示,注释掉避免图片不匹配,使用默认图片。
                    int faviconId = preloads.getResourceId(i, 0);
                    int thumbId = preloads.getResourceId(i + 1, 0);
                    byte[] thumb = null, favicon = null;
                    try {
                        thumb = readRaw(res, thumbId);
                    } catch (IOException e) {
                    }
                    try {
                        favicon = readRaw(res, faviconId);
                    } catch (IOException e) {
                    }
                    Log.i("antoon", TAG+", faviconId = "+faviconId+", thumbId = "+thumbId);
                    Log.i("antoon", TAG+", thumb = "+thumb+", favicon = "+favicon);
                    
                    if (thumb != null || favicon != null) {
                        ContentValues imageValues = new ContentValues();
                        imageValues.put(Images.URL, bookmarkDestination.toString());
                        if (favicon != null) {
                            imageValues.put(Images.FAVICON, favicon);
                        }
                        if (thumb != null) {
                            imageValues.put(Images.THUMBNAIL, thumb);
                        }
                        db.insert(TABLE_IMAGES, Images.FAVICON, imageValues);
                    }
                     */
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            } finally {
                preloads.recycle();
            }
        }
View Code


需要了解一下ContentProvider加载流程。参考 http://www.cnblogs.com/linjiqin/archive/2011/05/28/2061396.html

 

Browser默认书签加载过程

标签:

原文地址:http://www.cnblogs.com/antoon/p/4505213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!