最近在做海外项目需要定制chrome浏览器的书签和主页,在8.1的项目上实现,当时手机还没有预制GMS,只能下载chrome 这个apk做实验,出现了很多问题,书签没有作用,安装了gms里面的chrome,主页又没生效,六神无主,经过网上的文档发现,GMS必须预制才能生效。
1.预制书签。
预制书签很简单 ,只需要改系统自带的原生代码的目录下
.\packages\providers\PartnerBookmarksProvider
代码 这些都是自带的
src\com\android\providers\partnerbookmark/** Copyright (C) 2012 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.android.providers.partnerbookmarks;import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.UriMatcher;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.MatrixCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;/*** Default partner bookmarks provider implementation of {@link PartnerBookmarksContract} API.* It reads the flat list of bookmarks and the name of the root partner* bookmarks folder using getResources() API.** Sample resources structure:* res/* values/* strings.xml* string name="bookmarks_folder_name"* string-array name="bookmarks"* item TITLE1* item URL1* item TITLE2* item URL2...* bookmarks_icons.xml* array name="bookmark_preloads"* item @raw/favicon1* item @raw/touchicon1* item @raw/favicon2* item @raw/touchicon2* ...*/
public class PartnerBookmarksProvider extends ContentProvider {private static final String TAG = "PartnerBookmarksProvider";// URI matcherprivate static final int URI_MATCH_BOOKMARKS = 1000;private static final int URI_MATCH_BOOKMARKS_ID = 1001;private static final int URI_MATCH_BOOKMARKS_FOLDER = 1002;private static final int URI_MATCH_BOOKMARKS_FOLDER_ID = 1003;private static final int URI_MATCH_BOOKMARKS_PARTNER_BOOKMARKS_FOLDER_ID = 1004;private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);private static final Map<String, String> BOOKMARKS_PROJECTION_MAP= new HashMap<String, String>();// Default sort order for unsync'd bookmarksprivate static final String DEFAULT_BOOKMARKS_SORT_ORDER =PartnerBookmarksContract.Bookmarks.ID + " DESC, "+ PartnerBookmarksContract.Bookmarks.ID + " ASC";// Initial bookmark id when for getResources() importing// Make sure to fix tests if you are changing thisprivate static final long FIXED_ID_PARTNER_BOOKMARKS_ROOT =PartnerBookmarksContract.Bookmarks.BOOKMARK_PARENT_ROOT_ID + 1;// DB table nameprivate static final String TABLE_BOOKMARKS = "bookmarks";static {final UriMatcher matcher = URI_MATCHER;final String authority = PartnerBookmarksContract.AUTHORITY;matcher.addURI(authority, "bookmarks", URI_MATCH_BOOKMARKS);matcher.addURI(authority, "bookmarks/#", URI_MATCH_BOOKMARKS_ID);matcher.addURI(authority, "bookmarks/folder", URI_MATCH_BOOKMARKS_FOLDER);matcher.addURI(authority, "bookmarks/folder/#", URI_MATCH_BOOKMARKS_FOLDER_ID);matcher.addURI(authority, "bookmarks/folder/id",URI_MATCH_BOOKMARKS_PARTNER_BOOKMARKS_FOLDER_ID);// Projection mapsMap<String, String> map = BOOKMARKS_PROJECTION_MAP;map.put(PartnerBookmarksContract.Bookmarks.ID,PartnerBookmarksContract.Bookmarks.ID);map.put(PartnerBookmarksContract.Bookmarks.TITLE,PartnerBookmarksContract.Bookmarks.TITLE);map.put(PartnerBookmarksContract.Bookmarks.URL,PartnerBookmarksContract.Bookmarks.URL);map.put(PartnerBookmarksContract.Bookmarks.TYPE,PartnerBookmarksContract.Bookmarks.TYPE);map.put(PartnerBookmarksContract.Bookmarks.PARENT,PartnerBookmarksContract.Bookmarks.PARENT);map.put(PartnerBookmarksContract.Bookmarks.FAVICON,PartnerBookmarksContract.Bookmarks.FAVICON);map.put(PartnerBookmarksContract.Bookmarks.TOUCHICON,PartnerBookmarksContract.Bookmarks.TOUCHICON);}...................
...................
...................@Overridepublic String getType(Uri uri) {final int match = URI_MATCHER.match(uri);if (match == UriMatcher.NO_MATCH) return null;return PartnerBookmarksContract.Bookmarks.CONTENT_ITEM_TYPE;}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {throw new UnsupportedOperationException();}
}
代码里面有注释 ,只需要修改一下 res/values的资源就可以实现书签的定制
strings.xml<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"><string name="bookmarks_folder_name" translatable="false">Partner Bookmarks</string><string-array name="bookmarks" ><item>Goolgle</item><item>http://www.google.com/</item> </string-array>
</resources>
bookmarks_icons.xml<resources><array name="bookmark_preloads" ><item>@raw/a</item><item>@raw/b</item></array>
</resources>
只需要配置好,书签可以配置成功
2.主页配置
从网上下载代码 放入到
同级目录下
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;/*** Created by linl@x-thinks.com 2018/3/12.*/public class PartnerHomepageProvider extends ContentProvider {// 设置默认的主页网址private static String HOMEPAGE_URI = "http://www.baidu.com";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);}@Overridepublic boolean onCreate() {return true;}@Overridepublic String getType(Uri uri) {switch (URI_MATCHER.match(uri)) {case URI_MATCH_HOMEPAGE:return "vnd.android.cursor.item/partnerhomepage";default:return null;}}@Overridepublic 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;}}@Overridepublic Uri insert(Uri uri, ContentValues values) {throw new UnsupportedOperationException();}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {throw new UnsupportedOperationException();}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {throw new UnsupportedOperationException();}}
设置百度为默认主页,然后配置AndroidManifest.xml
<application><provider android:name="PartnerBookmarksProvider"android:authorities="com.android.partnerbookmarks" /><provider android:name="PartnerHomepageProvider"android:authorities="com.android.partnerbrowsercustomizations" /></application>
最后再编译 就ok了