【开发方案】KaiOS APN 设置界面菜单选项定制

一、模块需求介绍

参考:KaiOS APN Settings模块代码-CSDN博客

APN 列表页对每一条APN提供编辑、删除等功能 ,对APN整体提供reset重置功能。

当前需要隐藏reset,不允许用户重置APN。

二、代码实现方案

APN列表页在gaia/apps/settings/应用中,在panel.js设置菜单选项,接口registerSoftkey

  • gaia/apps/settings/elements/apn_lists.html 定义了 APN Settings界面的结构和布局
  • gaia/apps/settings/js/panels/apn_list/panel.js 功能逻辑
  • gaia/apps/settings/js/panels/apn_list/apn_template_factory.js 界面模板

apn_list/panel.js

javascript 复制代码
/**
 * The apn list panel
 */
'use strict';
define(function(require) { //eslint-disable-line
  const ListView = require('modules/mvvm/list_view');
  const SettingsPanel = require('modules/settings_panel');
  const ApnSettingsManager = require('modules/apn/apn_settings_manager');
  const ApnTemplateFactory = require('panels/apn_list/apn_template_factory');
  return function createApnSettingsPanel() {

    //设置案件
    function registerSoftkey() {
      //1.左边:添加APN
      const softkeyAdd = {
        name: 'Add Apn',
        l10nId: 'add-apn',
        priority: 1,
        method: () => {
          onApnAddAction(serviceIdNum, apnType, rootElement);
        }
      };
      //2.中间:选中APN
      const softkeySelect = {
        name: 'Select',
        l10nId: 'select',
        priority: 2,
        method: () => {
          if (!rootElement.querySelector('.focus label')) {
            return;
          }
          const dataItemId = rootElement.querySelector('.focus label').dataset
            .id;
          const radioElement = rootElement.querySelector('.focus input');
          onApnItemDefaultSet(
            serviceIdNum,
            apnType,
            radioElement,
            itemsOBJ[dataItemId]
          );
          SettingsSoftkey.hide();
          returnTop();
        }
      };
      //3.右边:编辑+删除(+重置)
      const softkeyOption = [
        {
          name: 'Edit',
          l10nId: 'edit',
          priority: 7,
          method: () => {
            focusId = getFocusPos(rootElement);
            const dataItemId = rootElement.querySelector('.focus label').dataset
              .id;
            onApnItemEdit(itemsOBJ[dataItemId]);
          }
        },
        {
          name: 'Delete',
          l10nId: 'delete-apn',
          priority: 8,
          method: () => {
            const dataItemId = rootElement.querySelector('.focus label').dataset
              .id;
            const focusItem = itemsOBJ[dataItemId];
            const dialogConfig = {
              title: { id: 'confirmation', args: {} },
              body: {
                id: 'delete-apn-confirm',
                args: {
                  apnName: focusItem.apn.carrier
                }
              },
              cancel: {
                l10nId: 'cancel',
                priority: 1
              },
              confirm: {
                l10nId: 'delete-apn',
                priority: 3,
                callback: () => {
                  if (focusItem.apn.deletedCpApn === false) {
                    focusItem.apn.deletedCpApn = true;
                    ApnSettingsManager.updateApn(
                      serviceIdNum,
                      focusItem.id,
                      focusItem.apn
                    )
                      .then(() => {
                        onApnDelete(rootElement, focusItem.apn.carrier);
                      })
                      .then(() => {
                        updateUI();
                      });
                  } else {
                    ApnSettingsManager.removeApn(serviceIdNum, focusItem.id)
                      .then(() => {
                        onApnDelete(rootElement, focusItem.apn.carrier);
                      })
                      .then(() => {
                        updateUI();
                      });
                  }
                }
              }
            };
            DialogHelper.show(dialogConfig);
          }
        }, //如果没有定制开发添加下面reset,可以去掉此逗号 

        //【开发方案】添加 reset 功能,重置APN
        {
            name: 'Reset to Default',
            l10nId: 'reset-apn',
            priority: 9,
            method() {
                resetApn();
            }
        }
      ]; //softkeyOption 

      const softkeyParams = {
        header: { l10nId: 'options' },
        items: []
      };
      let focusLabel = rootElement.querySelector('.focus label');
      if (!focusLabel) {
        focusLabel = rootElement.querySelector('li label');
      }
      softkeyParams.items.push(softkeyAdd);

      //最终判断菜单用的常量
      if (focusLabel) {
        softkeyParams.items.push(softkeySelect);
        const dataItemId = focusLabel.dataset.id;
        const focusItem = itemsOBJ[dataItemId];
        if (
          focusItem.itemCategory !== 'preset' ||
          focusItem.itemApn.carrier !== 'Jio 4G'
        ) {
          // eslint-disable-next-line
          softkeyParams.items.push.apply(softkeyParams.items, softkeyOption);
        }
      }
      SettingsSoftkey.init(softkeyParams);

    } //菜单功能函数接口 registerSoftkey

    //更新菜单配置
    function updateSoftkey() {
      registerSoftkey();
      SettingsSoftkey.show();
    }


    return SettingsPanel({
    }); //SettingsPanel

  }; //createApnSettingsPanel.
});

方案介绍

在功能函数 registerSoftkey 中自定义菜单选项常量 softkeyOption_custom,然后最后通过判断你选择不同的菜单内容项。

javascript 复制代码
      const softkeyOption_custom = [
          {
              name: 'More Info',
              l10nId: 'more-info',
              priority: 7,
              method: function() {
                  focusId = getFocusPos(rootElement);
                  const dataItemId=rootElement.querySelector('.focus label').dataset.id;
                  onApnItemMoreInfo(itemsOBJ[dataItemId]);
              }
          }
      ];

      if (focusLabel) {
        softkeyParams.items.push(softkeySelect);
        const dataItemId = focusLabel.dataset.id;
        const focusItem = itemsOBJ[dataItemId];
        if(ApnUtils.isOpSim()) { //自定义接口判断 , ApnUtils工具类自定义在js/module中
			softkeyParams.items.push.apply(softkeyParams.items, softkeyOption_custom );
			dump('apn_list/panel, OP require to remove Reset menu.');
        } else {
            softkeyParams.items.push.apply(softkeyParams.items, softkeyOption);
        }
      }

apn_list/panel.js 导入个工具类可用如下方式:

javascript 复制代码
const ApnUtils = require('modules/apn/apn_utils');

apn_utils.js

  • gaia/apps/settings/js/module/apn/apn_utils.js

Note:新增接口的时候一定要同步加到return中,否则报错找不到函数:

04-05 16:50:43.277 2048 2048 E Web Content: [JavaScript Error: "TypeError: ApnUtils.isOpSim is not a function" {file: "http://settings.localhost/js/panels/apn_list/panel.js" line: 291}]

javascript 复制代码
/* global ApnHelper, SimCardHelper */
/**
 * The apn utilities
 */
'use strict';
define(function(require) { //eslint-disable-line
  const ApnConst = require('modules/apn/apn_const');
  const ApnItem = require('modules/apn/apn_item');



  //新增函数判断运营商
  function isOpSim() {
    let connection = navigator.b2g.mobileConnections;
    if (connection[0]) {
      let iccId = connection[0].iccId;
      if (iccId) {
        let iccInfo = navigator.b2g.iccManager.getIccById(iccId).iccInfo;
        if (iccInfo) {
	  let mcc = iccInfo.mcc;
          let mnc = iccInfo.mnc;
          let gid1 = iccInfo.gid1 + '';
          let gid1Upper = gid1.toUpperCase() + '';
          let mccmnc = mcc.concat(mnc);
          dump('apn_utils: check isOpSim, mccmnc = ' + mccmnc + ', gid1 = ' + gid1);
          if (gid1Upper.indexOf('BA01490000000000') == 0 && mccmnc === '311480') {
              return true;
          } else if (mccmnc === '31420') {
              return true;
          }
        }
      }
    }
	return false;
  }
                    
  return {
    getOperatorCode,
    apnTypeFilter,
    getDefaultApns,
    getCpApns,
    getEuApns,
    generateId,
    separateApnsByType,
    isMatchedApn,
    sortByCategory,
    clone,
    isOpSim
  };
});

三、调试Debug

push替换settings应用文件application.zip到设备快速调试。

如果是kaios全编项目,则settings产物在gaia/profile/webapps目录下。

相关推荐
hello world smile17 小时前
Flutter常用命令整理
android·flutter·移动开发·android studio·安卓
袁震6 天前
鸿蒙Harmony-多边形绘制组件Polygon使用详解
华为·移动开发·harmonyos·鸿蒙·harmony
袁震6 天前
鸿蒙Harmony-圆形绘制组件Circle使用详解
华为·移动开发·harmonyos·鸿蒙·harmony
第三女神程忆难11 天前
Android Kotlin 高阶函数详解及其在协程中的应用
android·开发语言·kotlin·移动开发·安卓·高阶函数·1024程序员节
少恭写代码12 天前
通过duxapp提供的基础方法、UI组件、全局样式,快速编写项目
react native·移动开发·taro·duxapp
氦客17 天前
浅谈华为 HarmonyOS Next
android·华为·移动开发·harmonyos·next·概念·万物互联
_waylau1 个月前
HarmonyOS NEXT Release版本今日发布
华为·移动开发·harmonyos·arkts·鸿蒙
少恭写代码1 个月前
duxui:基于Taro,兼容React Native、小程序、H5的多端UI库
react native·小程序·移动开发·taro·duxapp
Android技术栈1 个月前
鸿蒙开发(NEXT/API 12)【穿戴设备信息查询】手机侧应用开发
嵌入式硬件·硬件架构·移动开发·harmonyos·鸿蒙·鸿蒙系统
Android技术栈1 个月前
鸿蒙开发(NEXT/API 12)【硬件(取消注册监听系统导航信息和指令)】车载系统
车载系统·移动开发·harmonyos·鸿蒙·鸿蒙系统·openharmony·硬件