因为google为了扩大chrome浏览器的范围还有影响,所以在很早以前就开始充许客户订制主页和书签,这个其中主要使用了ContentProvider的特性进行数据的分享。
在google提供的源码中有定制主页和书签的apk,但是在高通和mtk分发的时候这两个apk已经做了一定的处理,不在进行编译。其中两个项目都位置在:packages/providers下
一,定制主页
定制主页google给的项目名称是ChromeCustomizations,这个项目只是一个ContentProvider,然后提供一个数据给chrome,其实两个项目都是如此
具体代码:
// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Package path can be changed, but should match <manifest package="..."> in AndroidManifest.xml. package com.android.providers.partnerbookmarks; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.os.Build; // Class name can be changed, but should match <provider android:name="..."> in AndroidManifest.xml. public class PartnerHomepageProviderExample extends ContentProvider { // "http://www.android.com/" is just an example. Please replace this to actual homepage. // Other strings in this class must remain as it is. private static String HOMEPAGE_URI="http://o"; private static final int URI_MATCH_HOMEPAGE = 0; private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); static { URI_MATCHER.addURI("com.android.partnerbrowsercustomizations", "homepage", URI_MATCH_HOMEPAGE); } @Override public boolean onCreate() { return true; } @Override public String getType(Uri uri) { // In fact, Chrome does not call this. // Just a recommaned ContentProvider practice in general. switch (URI_MATCHER.match(uri)) { case URI_MATCH_HOMEPAGE: return "vnd.android.cursor.item/partnerhomepage"; default: return null; } } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { switch (URI_MATCHER.match(uri)) { case URI_MATCH_HOMEPAGE: MatrixCursor cursor = new MatrixCursor(new String[] { "homepage" }, 1); cursor.addRow(new Object[] { HOMEPAGE_URI }); return cursor; default: return null; } } @Override public Uri insert(Uri uri, ContentValues values) { throw new UnsupportedOperationException(); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { throw new UnsupportedOperationException(); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { throw new UnsupportedOperationException(); } }
除了代码外我们还需要在AndroidManifest.xml 中进行注册,这样我们就能给chrome数据了,其实这个不用单独的使用一个apk,加到默认的apk中都能实现此功能
<provider android:name="PartnerHomepageProviderExample" android:authorities="com.android.partnerbrowsercustomizations" />
二,定制书签:PartnerBookmarksProvider/主要是重写string中的bookmarks数组和bookmarks_icon.xml中的bookmark_preloads数组
- <string-array name="bookmarks">
- <item>Baidu</item>
- <item>http://www.baidu.com/</item>
- </string-array>
- <array name="bookmark_preloads">
- <item>@raw/favicon_baidu</item>
- <item>@raw/thumb_baidu</item>
- </array>
最后需要说明的是在添加以上数据后,记得在mk文件中把项目添加上,然后在编译系统的时候我们修改才会起到作用