美文网首页
[原创]PermissionController原生系统设置默认

[原创]PermissionController原生系统设置默认

作者: seekting | 来源:发表于2020-07-20 15:03 被阅读0次

PermissionController原生系统设置默认浏览器

项目路径

android-10.0.0_r17/packages/apps/PermissionController

DefaultAppActivity

创建fragment

if (savedInstanceState == null) {
            Fragment fragment;
            if (DeviceUtils.isAuto(this)) {
                fragment = AutoDefaultAppFragment.newInstance(roleName, user);
            } else {
                fragment = HandheldDefaultAppFragment.newInstance(roleName, user);
            }
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, fragment)
                    .commit();
        }

HandheldDefaultAppFragment

创建子fragment

if (savedInstanceState == null) {
            DefaultAppChildFragment fragment = DefaultAppChildFragment.newInstance(mRoleName,
                    mUser);
            getChildFragmentManager().beginTransaction()
                    .add(fragment, null)
                    .commit();
        }
@Override
    public boolean onPreferenceClick(@NonNull Preference preference) {
        String key = preference.getKey();
        if (Objects.equals(key, PREFERENCE_KEY_NONE)) {
            mViewModel.setNoneDefaultApp();
        } else {
            String packageName = key;
            CharSequence confirmationMessage = mRole.getConfirmationMessage(packageName,
                    requireContext());
            if (confirmationMessage != null) {
                DefaultAppConfirmationDialogFragment.show(packageName, confirmationMessage, this);
            } else {
                setDefaultApp(packageName);
            }
        }
        return true;
    }
    "连原生应用也开始用mvvm"
      @Override
    public void setDefaultApp(@NonNull String packageName) {
        mViewModel.setDefaultApp(packageName);
    }

DefaultAppViewModel

vm设置默认

  public void setDefaultApp(@NonNull String packageName) {
        if (mManageRoleHolderStateLiveData.getValue() != ManageRoleHolderStateLiveData.STATE_IDLE) {
            Log.i(LOG_TAG, "Trying to set default app while another request is on-going");
            return;
        }
        mManageRoleHolderStateLiveData.setRoleHolderAsUser(mRole.getName(), packageName, true, 0,
                mUser, getApplication());
    }

ManageRoleHolderStateLiveData

livedata设置默认

public void setRoleHolderAsUser(@NonNull String roleName, @NonNull String packageName,
            boolean add, int flags, @NonNull UserHandle user, @NonNull Context context) {
       

        RoleManager roleManager = context.getSystemService(RoleManager.class);
        Executor executor = context.getMainExecutor();
        Consumer<Boolean> callback = successful -> {
            if (successful) {
                setValue(STATE_SUCCESS);
            } else {
               
                setValue(STATE_FAILURE);
            }
        };
        "调用rolemanager去设置默认浏览器"
        if (add) {
            roleManager.addRoleHolderAsUser(roleName, packageName, flags, user, executor, callback);
        } else {
            roleManager.removeRoleHolderAsUser(roleName, packageName, flags, user, executor,
                    callback);
        }
    }

相关文章

网友评论

      本文标题:[原创]PermissionController原生系统设置默认

      本文链接:https://www.haomeiwen.com/subject/ttqykktx.html