Chromium内核浏览器编译记(三)116版本内核UI定制

转载请注明出处:https://blog.csdn.net/kong_gu_you_lan/article/details/132180843?spm=1001.2014.3001.5501

本文出自 容华谢后的博客

往期回顾:

Chromium内核浏览器编译记(一)踩坑实录

Chromium内核浏览器编译记(二)UI定制

0.写在前面

最近因为业务需求,需要编译最新版本的Chromium内核,目前最新的版本是116.0.5845.59,编译的过程中发现有一些代码逻辑修改的地方都变了,所以在此记录下,分享给大家。

定制需求是这样的:

  • 修改包名、版本号、应用图标、应用名称

  • 不显示应用第一次启动时的设置引导页

  • 应用全屏显示,移除首页的所有菜单栏

  • 增加点击两次退出应用的功能

  • 默认不加载新标签页

1.修改包名

源码路径:/chromium/src/chrome/android/chrome_public_apk_tmpl.gni

将下面两个属性的值替换成你要修改的包名就可以了:

chrome_public_manifest_package = "org.chromium.chrome"

chrome_public_test_manifest_package = "org.chromium.chrome.tests"

2.修改版本号

源码路径:/chromium/src/build/config/android/config.gni

将下面四个属性的值替换成你要修改的版本号就可以了:

android_default_version_code = "10000"
android_default_version_name = "1.00.00"

android_override_version_code = "10000"
android_override_version_name = "1.00.00"

3.修改应用图标

源码路径:/chromium/src/chrome/android/java/res_chromium_base

在这个目录下,有几个mipmap文件夹,把里面的图标替换下就可以,和AS项目是一样的。

4.修改应用名称

源码路径:/chromium/src/chrome/android/java/res_chromium_base/values/channel_constants.xml


    应用名称

5.屏蔽启动引导页

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/init/AsyncInitializationActivity.java

将requiresFirstRunToBeCompleted方法的返回值修改为false:

/**
 * Overriding this function is almost always wrong.
 * @return Whether or not the user needs to go through First Run before using this Activity.
 */
protected boolean requiresFirstRunToBeCompleted(Intent intent) {
    return false;
}

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java

删除或注释掉dispatch方法中下面的判断逻辑:

// Check if we should push the user through First Run.
if (FirstRunFlowSequencer.launch(mActivity, mIntent, false /* requiresBroadcast */,
            false /* preferLightweightFre */)) {
    return Action.FINISH_ACTIVITY;
}

6.应用全屏显示

源码路径:/chromium/src/chrome/android/java/res/values/styles.xml

在上面的源码中,找到Base.Theme.Chromium.WithWindowAnimation主题,增加下面的全屏设置属性:


7.移除首页的所有菜单栏

源码路径:/chromium/src/chrome/android/java/res_app/layout/main.xml

在CoordinatorLayoutForPointer控件中,增加layout_marginTop属性,其实是往上移动了UI,把菜单栏隐藏了:



	...

    

        

8.增加点击两次退出应用的功能

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/app/ChromeActivity.java

在ChromeActivity类中增加下面的方法:

public int getTabsCount() {
    TabModelSelector modelSelector = getTabModelSelector();
    if (modelSelector == null) return 0;
    return modelSelector.getTotalTabCount();
}    

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java

修改minimizeAppAndCloseTabOnBackPress方法:

private boolean mExitApp;

private boolean minimizeAppAndCloseTabOnBackPress(@Nullable Tab currentTab) {
    if (currentTab == null) {
        BackPressManager.record(BackPressHandler.Type.MINIMIZE_APP_AND_CLOSE_TAB);
        MinimizeAppAndCloseTabBackPressHandler.record(MinimizeAppAndCloseTabType.MINIMIZE_APP);
        assertOnLastBackPress();
        moveTaskToBack(true);
        return true;
    }
    // TAB history handler has a higher priority and should navigate page back before
    // minimizing app and closing tab.
    assert !currentTab.canGoBack()
        : "Tab should be navigated back before closing or exiting app";
    final boolean shouldCloseTab = backShouldCloseTab(currentTab);
    final WebContents webContents = currentTab.getWebContents();

    // Minimize the app if either:
    // - we decided not to close the tab
    // - we decided to close the tab, but it was opened by an external app, so we will go
    //   exit Chrome on top of closing the tab
    final boolean minimizeApp = false;

    BackPressManager.record(BackPressHandler.Type.MINIMIZE_APP_AND_CLOSE_TAB);
    assertOnLastBackPress();

    if (minimizeApp) {
        if (shouldCloseTab) {
            MinimizeAppAndCloseTabBackPressHandler.record(
                    MinimizeAppAndCloseTabType.MINIMIZE_APP_AND_CLOSE_TAB);
            sendToBackground(currentTab);
        } else {
            MinimizeAppAndCloseTabBackPressHandler.record(
                    MinimizeAppAndCloseTabType.MINIMIZE_APP);
            sendToBackground(null);
        }
        return true;
    } else if (shouldCloseTab) {
        if (getTabsCount() > 1) {
            MinimizeAppAndCloseTabBackPressHandler.record(MinimizeAppAndCloseTabType.CLOSE_TAB);
            if (webContents != null) webContents.dispatchBeforeUnload(false);
            return true;
        }
        if (mExitApp) {
            MinimizeAppAndCloseTabBackPressHandler.record(MinimizeAppAndCloseTabType.CLOSE_TAB);
            if (webContents != null) webContents.dispatchBeforeUnload(false);
            return false;
        }
        Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
        mExitApp = true;
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mExitApp = false;
            }
        }, 2000);
        return true;
    }
    return false;
}

9.默认不加载新标签页

源码路径:/chromium/src/components/embedder_support/android/java/src/org/chromium/components/embedder_support/util/UrlConstants.java

把下面两个参数,改成你想要显示的网页地址就可以了:

public static final String NTP_URL = "chrome-native://newtab/";

public static final String NTP_NON_NATIVE_URL = "chrome://newtab/";

10.导航栏背景

新版本的浏览器,底部导航栏背景是白色的,如果有修改背景颜色的需求,可以改这里:

源码路径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/tabbed_mode/TabbedNavigationBarColorController.java

把updateNavigationBarColor方法中,setNavigationBarIconColor参数改成false:

@SuppressLint("NewApi")
private void updateNavigationBarColor() {
    ...
    
    UiUtils.setNavigationBarIconColor(mRootView, false);
}

修改下面两个方法,冒号后面修改成你想要设置的导航栏背景颜色就可以了:

private @ColorInt int getNavigationBarColor(boolean forceDarkNavigationBar) {
    return forceDarkNavigationBar
            ? mContext.getColor(R.color.toolbar_background_primary_dark)
            : mContext.getColor(R.color.navigation_bubble_arrow);
}

private @ColorInt int getNavigationBarDividerColor(boolean forceDarkNavigationBar) {
    return forceDarkNavigationBar
            ? mContext.getColor(R.color.bottom_system_nav_divider_color_light)
            : mContext.getColor(R.color.navigation_bubble_arrow);
}

11.遇到的问题

更新源码的时候,遇到这个提示:

NOTICE: You have PROXY values set in your environment, but gsutilin depot_tools does not (yet) obey them.

Also, --no_auth prevents the normal BOTO_CONFIG environmentvariable from being used.

To use a proxy in this situation, please supply those settingsin a .boto file pointed to by the NO_AUTH_BOTO_CONFIG environmentvariable.

被墙了,已经设置了全局代理,还需要再加个配置,将下面的配置保存成文件**.boto**,然后配置下环境变量:

[boto]
proxy=127.0.0.1
proxy_port=端口号

环境变量:

export PATH="$PATH:$HOME/.boto"

注意:文章中是把depot_tool下载到了【home/用户名】目录下,所以使用【$HOME】代表,环境变量要根据实际下载的路径修改。

12.编译脚本

最后在附上args.gn编译脚本:

target_os = "android"
target_cpu = "arm"
# Release版本
is_debug = false
# 不允许远程调试
enable_remoting = false
# 使用Chrome官方的编译优化建议
is_official_build = true
# 分散链接编译
is_component_build = false
# 不使用官方API秘钥
use_official_google_api_keys = false
# 关闭支持NACL,这是一种Chrome插件,因为安全性,稳定性存在问题,已经很少使用了
enable_nacl = false

# 支持H264编码
proprietary_codecs = true
# 解码器
ffmpeg_branding = "Chrome"

# 解决不能播放MP4格式的视频问题
rtc_use_h264  = true

declare_args(){
  # 整体优化,对整个程序的多个编译单元进行全局优化,获取更好的性能和代码体积优化
  full_wpo_on_official = true
  # 删除内核层支持调试的符号文件
  remove_webcore_debug_symbols = false
  # 初始化FFmpeg
  rtc_initialize_ffmpeg = true
  # 支持OpenH264
  use_openh264 = true
}

13.写在最后

到这里,Chromium源码的UI定制就完成了,有问题可以给我留言评论,谢谢。

你可能感兴趣的:(开源项目,chrome,chromium,浏览器,内核,android)