当前位置:   article > 正文

android仅安装客户指定应用(高通KK_8x12平台)_install non market_apps

install non market_apps

前言:客户需要预置他们的应用为系统应用,并且除了他们的应用更新外,无法安装其他应用。

对于预置应用到/system/priv-app/下,可以在makefile中添加:
LOCAL_PRIVILEGED_MODULE := true

正文:
在安装应用时,通过白名单判断是否是用户指定需要安装的应用,如果是,则继续安装;如果不是,则返回未知来源,安装失败。
白名单由一个xml文件构成,放到/system/etc/下:

  1. pkgname_filterlist.xml
  2. <gapps-packages>
  3. <app package="com.customer.packagename"/>
  4. </gapps-packages>
  • 1

安装的具体流程就不在这里赘述了,最后在PackageManagerService.java中判断:

  1. PackageManagerService.java(/frameworks/base/services/java/com/android/server/pm/)
  2. private void installPackageLI(InstallArgs args,
  3. boolean newInstall, PackageInstalledInfo res) {
  4. ...
  5. if (GET_CERTIFICATES && !pp.collectCertificates(pkg, parseFlags)) {
  6. res.returnCode = pp.getParseError();
  7. return;
  8. }
  9. // add {
  10. mFilterPkgNameList.clear();
  11. fillFilterPkgNameList();
  12. for (int i = 0; i < mFilterPkgNameList.size(); i++){
  13. if (pkg.packageName.contains(mFilterPkgNameList.get(i))){
  14. break;
  15. }
  16. if (i == mFilterPkgNameList.size() - 1){
  17. Slog.w(TAG, "Not allowed to install unknown apk!");
  18. res.returnCode = PackageManager.INSTALL_FAILED_UNKNOWN_SOURCES;
  19. return;
  20. }
  21. }
  22. // add }
  23. /* If the installer passed in a manifest digest, compare it now. */
  24. ...
  25. }
  • 1

添加方法fillFilterPkgNameList()

  1. private ArrayList<String> mFilterPkgNameList = new ArrayList<String>();
  2. private void fillFilterPkgNameList() {
  3. File configureDir = Environment.getRootDirectory();
  4. File gappsInfoDir = new File(configureDir, "etc/pkgname_filterlist.xml");
  5. FileInputStream stream = null;
  6. if (gappsInfoDir.exists()) {
  7. try {
  8. stream = new FileInputStream(gappsInfoDir);
  9. XmlPullParser parser = Xml.newPullParser();
  10. parser.setInput(stream, null);
  11. int type;
  12. do {
  13. type = parser.next();
  14. if (type == XmlPullParser.START_TAG) {
  15. String tag = parser.getName();
  16. if ("app".equals(tag)) {
  17. String pkgName = parser.getAttributeValue(null, "package");
  18. mFilterPkgNameList.add(pkgName);
  19. }
  20. }
  21. } while (type != XmlPullParser.END_DOCUMENT);
  22. } catch (FileNotFoundException e) {
  23. Log.w(TAG, "FileNotFoundException " + gappsInfoDir, e);
  24. } catch (NullPointerException e) {
  25. Log.w(TAG, "NullPointerException " + gappsInfoDir, e);
  26. } catch (NumberFormatException e) {
  27. Log.w(TAG, "NumberFormatException " + gappsInfoDir, e);
  28. } catch (XmlPullParserException e) {
  29. Log.w(TAG, "XmlPullParserException " + gappsInfoDir, e);
  30. } catch (IOException e) {
  31. Log.w(TAG, "IOException " + gappsInfoDir, e);
  32. } catch (IndexOutOfBoundsException e) {
  33. Log.w(TAG, "IndexOutOfBoundsException " + gappsInfoDir, e);
  34. } finally {
  35. if (stream != null) {
  36. try {
  37. stream.close();
  38. } catch (IOException e) {
  39. Log.w(TAG, "IOException " + gappsInfoDir, e);
  40. }
  41. }
  42. }
  43. }
  44. }
  • 1

另外,为了方便客户,将未知来源默认勾选:

  1. SecuritySettings.java(/packages/apps/Settings/src/com/android/settings/)
  2. private PreferenceScreen createPreferenceHierarchy() {
  3. ...
  4. mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
  5. ...
  6. }
  7. private boolean isNonMarketAppsAllowed() {
  8. return Settings.Global.getInt(getContentResolver(),
  9. Settings.Global.INSTALL_NON_MARKET_APPS, 0) > 0;
  10. }
  • 1

加载global变量:

  1. DatabaseHelper.java(/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/)
  2. loadBooleanSetting(stmt, Settings.Global.INSTALL_NON_MARKET_APPS,
  3. R.bool.def_install_non_market_apps);
  • 1

def_install_non_market_apps定义在defaults.xml中(注意项目的overlay

  1. defaults.xml(/frameworks/base/packages/SettingsProvider/res/values/)
  2. <bool name="def_install_non_market_apps">true</bool>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/153056
推荐阅读
相关标签
  

闽ICP备14008679号