All Products
Search
Document Center

Extend API permissions

Last Updated: Mar 24, 2021

In Mini Program, some special APIs, such as Location, Camera and Album, usually prompt users for authorization, and the APIs can be executed only when the users grant permission.

The Mini Program container allows the following extensions for API call:

  • Customize text prompt. The access party can control the text and presentation style.
  • Allow the access party to read and write permission configuration.
Note: The extended configuration only works when the Mini Program permission control is enabled.

Permission configuration

For the APIs that need users’ authorization, it is required to configure a permission key. It is allowed that multiple APIs share the same key. For example, photo selection and code scanning may correspond to the permission key of Camera.

The table below lists Mini Program’s existing default keys and the corresponding APIs.

Permissions key API
Camera camera scan, chooseImage, chooseVideo
Album album saveImage, saveVideosToPhotosAlbum, shareTokenImageSilent
Location location getLocation, getCurrentLocation
Microphone audioRecord startAudioRecord, stopAudioRecord, cancelAudioRecord

The container reads the permission configuration classes passed by the access party to process the API call permission.

  1. public static class PermissionConfig {
  2. public String action; // API name
  3. public String key; // Permission key
  4. public String desc; // Text to be presented, it is allowed to add the placeholder "%s", and the container will automatically fill the mini program name
  5. public PermissionConfig() {
  6. }
  7. }
Note: When action is chooseImage or chooseVideo, the key configured by the access party doesn’t work. The container has two special logics processing the above two APIs. However, text is configurable.

Permission loading configuration

To configure the permission loading, you need to use the API TinyAppPermissionExternProvider provided by the container. The interface classes are as follows:

  1. package com.alipay.mobile.nebula.provider;
  2. import android.content.Context;
  3. import java.util.List;
  4. public abstract class TinyAppPermissionExternProvider {
  5. public interface PermissionCheckCallback {
  6. void accept();
  7. void deny();
  8. }
  9. public abstract List<PermissionConfig> loadPermissionCheckConfig();
  10. public abstract void showPermissionDialog(Context context, String appId, PermissionConfig config, PermissionCheckCallback callback);
  11. public abstract boolean shouldHandlePermissionDialog();
  12. }

The method loadPermissionCheckConfig loads the permission configuration to the container.

Custom presentation

Present the authorization information in a customized way, and allow users to make confirmation.

  1. Use the method shouldHandlePermissionDialog to return true.
  2. The container calls the method showPermissionDialog, and the access party presents its custom style there.
  3. When the users accept or reject the invocation, call the corresponding method of PermissionCheckCallback.

See the sample code below:

  1. package com.mpaas.demo.nebula;
  2. import android.content.Context;
  3. import com.alipay.mobile.antui.dialog.AUNoticeDialog;
  4. import com.alipay.mobile.nebula.provider.TinyAppPermissionExternProvider;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class TinyExternalPermissionCheckProvider extends TinyAppPermissionExternProvider {
  8. private PermissionConfig create(String action, String key, String desc) {
  9. PermissionConfig config = new PermissionConfig();
  10. config.action = action;
  11. config.key = key;
  12. config.desc = desc;
  13. return config;
  14. }
  15. private List<PermissionConfig> permissionConfigs = new ArrayList<>();
  16. public TinyExternalPermissionCheckProvider() {
  17. permissionConfigs.add(create("saveFile", "file", "%s want to use your file storage"));
  18. permissionConfigs.add(create("getFileInfo", "file", "%s want to use your file storage"));
  19. }
  20. @Override
  21. public List<PermissionConfig> loadPermissionCheckConfig() {
  22. return permissionConfigs;
  23. }
  24. @Override
  25. public void showPermissionDialog(Context context, String action, PermissionConfig permissionConfig, final PermissionCheckCallback permissionCheckCallback) {
  26. AUNoticeDialog dialog = new AUNoticeDialog(context, "Authorization reminder", permissionConfig.desc, "Accept", "Reject");
  27. dialog.setPositiveListener(new AUNoticeDialog.OnClickPositiveListener() {
  28. @Override
  29. public void onClick() {
  30. permissionCheckCallback.accept();
  31. }
  32. });
  33. dialog.setNegativeListener(new AUNoticeDialog.OnClickNegativeListener() {
  34. @Override
  35. public void onClick() {
  36. permissionCheckCallback.deny();
  37. }
  38. });
  39. dialog.show();
  40. }
  41. @Override
  42. public boolean shouldHandlePermissionDialog() {
  43. return true;
  44. }
  45. }

Read/write configuration

The following methods can be called to read the configuration:

  1. MPTinyHelper.getInstance().getMiniProgramSetting(appId)
Note:
  • The Mini Program configuration is stored in two dimensions: application and user, so make sure that the application has called the methodMPLogger.setUserId.
  • If an API has never been called, the authorization status of the key value corresponding to the API cannot be obtained.

The following methods can be called to write configuration:

  1. MPTinyHelper.getInstance().updateMiniProgramSetting(appId, key, isAllowed);

See the sample code below:

  1. package com.mpaas.demo.nebula;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.CompoundButton;
  6. import com.alipay.mobile.antui.basic.AUSearchBar;
  7. import com.alipay.mobile.antui.tablelist.AUSwitchListItem;
  8. import com.alipay.mobile.framework.app.ui.BaseFragmentActivity;
  9. import com.alipay.mobile.nebula.util.H5Utils;
  10. import com.mpaas.nebula.adapter.api.MPTinyHelper;
  11. import java.util.Map;
  12. public class PermissionDisplayActivity extends BaseFragmentActivity {
  13. private ViewGroup mScrollView;
  14. private AUSearchBar mSearchInputBox;
  15. private Map<String, Boolean> permissions;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_permission);
  20. mScrollView = (ViewGroup) findViewById(R.id.scrollview);
  21. mSearchInputBox = (AUSearchBar) findViewById(R.id.search);
  22. mSearchInputBox.getSearchButton().setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. mScrollView.removeAllViews();
  26. final String appId = mSearchInputBox.getSearchEditView().getText().toString();
  27. permissions = MPTinyHelper.getInstance().getMiniProgramSetting(appId);
  28. for (Map.Entry<String, Boolean> entry : permissions.entrySet()) {
  29. AUSwitchListItem item = new AUSwitchListItem(PermissionDisplayActivity.this);
  30. final String key = entry.getKey();
  31. item.setLeftText(key);
  32. item.getCompoundSwitch().setChecked(entry.getValue());
  33. item.getCompoundSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  34. @Override
  35. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  36. MPTinyHelper.getInstance().updateMiniProgramSetting(appId, key, isChecked);
  37. }
  38. });
  39. mScrollView.addView(item, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, H5Utils.dip2px(PermissionDisplayActivity.this, 48)));
  40. }
  41. }
  42. });
  43. }
  44. }