Skip to content

Privacy Notice

The privacy notice can be used to explain to app users why the app needs specific permissions. It allows the application developer to describe why a specific feature that the user wants to access requires a particular permission.

There are two variations of the privacy notice that the application developer can use: PrivacyNoticeActivity and PrivacyNoticeDialogFragment. Both require a PrivacyNoticeSettings object that is used to set the type of permission and also provide a way to customize the privacy notices. Currently there are five predefined types of permissions supported: LOCATION, STORAGE, CALENDAR, CAMERA and CUSTOM. The first four permission types provide the privacy notice text, icons and, in the case of PrivacyNoticeActivity, launching the Android system permission dialog out-of-the-box. For the CUSTOM permission type, the application developer needs to set the appropriate fields.

Privacy Notice Activity

The PrivacyNoticeActivity is an activity that can be shown while the user is onboarding. It allows the user to be made aware of all the permissions, and their purpose, that an app may require before being used.

Privacy Notice Activity

Using the Privacy Notice Activity

In order to use the PrivacyNoticeActivity, initialize a PrivacyNoticeSettings object first, and set the permission type by calling the setType(PrivacyNoticeSettings.Type type) method. Then create an intent for the PrivacyNoticeActivity activity and save this intent in the PrivacyNoticeSettings object by calling the saveToIntent(Intent intent) method. The PrivacyNoticeActivity can then be launched using a ActivityResultLauncher instance and the result of the permission request can be captured in the ActivityResultCallback<ActivityResult> callback.

// ActivityResultLauncher instance holds reference to registerForActivityResult(),
// which needs to be called before your fragment or activity is created
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent data = result.getData();
                    if (data != null) {
                        // The type of permission that was requested
                        PrivacyNoticeSettings.Type type = (PrivacyNoticeSettings.Type) data.getSerializableExtra(PRIVACY_PERMISSION_TYPE);
                        // Map containing the name of the permission that was requested
                        // and user's response to the permission
                        HashMap<String, Boolean> permissionResultsMap = (HashMap<String, Boolean>) data.getSerializableExtra(PRIVACY_RESULTS_KEY);
                        for (Map.Entry<String, Boolean> entry : permissionResultsMap.entrySet()) {
                            String permission = entry.getKey();
                            Boolean permissionGranted = entry.getValue();
                        }
                    }
                }
            }
        });


// Launch the Intent using the previously created ActivityResultLauncher instance after your fragment or activity is created
Intent intent = new Intent(this, PrivacyNoticeActivity.class);
PrivacyNoticeSettings privacyNoticeSettings = new PrivacyNoticeSettings();
privacyNoticeSettings.setType(PrivacyNoticeSettings.Type.LOCATION);
privacyNoticeSettings.saveToIntent(intent);
activityResultLauncher.launch(intent);

Customizing the Privacy Notice Activity

The text and the icon of the PrivacyNoticeActivity can be customized using the PrivacyNoticeSettings object. The following options are available:

Return Type Java API Description
int getActivityIconResource() Retrieves the icon resource used in the privacy notice activity.
void setActivityIconResource(int iconResource) Sets the icon resource to be used in the privacy notice activity.
CharSequence getActivityTitle() Retrieves the title of the privacy notice activity.
void setActivityTitle(CharSequence title) Sets the title of the privacy notice activity.
CharSequence getActivityDescription() Retrieves the description of the privacy notice activity.
void setActivityDescription(CharSequence description) Sets the description of the privacy notice activity.
CharSequence getActivityButtonText() Retrieves the text displayed on the privacy notice activity button.
void setActivityButtonText(CharSequence text) Sets the text to be displayed on the privacy notice activity button.
String[] getActivityCustomPermissions() Retrieves the custom permissions used in the privacy notice activity for the CUSTOM type.
void setActivityCustomPermissions(String[] customPermissions) Sets the custom permissions to be used in the privacy notice activity for the CUSTOM type.

Privacy Notice Dialog Fragment

The PrivacyNoticeDialogFragment is a DialogFragment that can be shown before the app is about to request a specific permission. This allows the app developer to only show the Privacy Notice if and when the user accesses a feature of the app that requires a particular permission. The PrivacyNoticeDialogFragment provides a button that takes the user to the app's info screen, where the user can grant the corresponding permission to the app.

Privacy Notice Dialog Fragment

Using the Privacy Notice Dialog Fragment

To display the PrivacyNoticeDialogFragment, first create a PrivacyNoticeSettings object and set the permission type by calling the setType(PrivacyNoticeSettings.Type type) method. An instance of the PrivacyNoticeDialogFragment class then needs to be created by calling the newInstance(PrivacyNoticeSettings privacyNoticeSettings) or the newInstance(PrivacyNoticeSettings privacyNoticeSettings, int style, int theme) method. Finally, similar to displaying a DialogFragment, call show() on the instance passing the FragmentManager and a tag name for the dialog fragment.

String type = PrivacyNoticeSettings.Type.LOCATION;
String tag = type.name();
PrivacyNoticeSettings privacyNoticeSettings = new PrivacyNoticeSettings();
privacyNoticeSettings.setType(type);
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.findFragmentByTag(tag) == null) {
    PrivacyNoticeDialogFragment privacyNoticeDialogFragment = PrivacyNoticeDialogFragment.newInstance(privacyNoticeSettings);
    privacyNoticeDialogFragment.show(fragmentManager, tag);
}

The PrivacyNoticeDialogFragment can also be displayed by adding it to the FragmentManager using the add() method of the FragmentTransaction class.

String type = PrivacyNoticeSettings.Type.LOCATION;
String tag = type.name();
PrivacyNoticeSettings privacyNoticeSettings = new PrivacyNoticeSettings();
privacyNoticeSettings.setType(type);
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.findFragmentByTag(tag) == null) {
    PrivacyNoticeDialogFragment privacyNoticeDialogFragment = PrivacyNoticeDialogFragment.newInstance(privacyNoticeSettings);
     FragmentTransaction ft = fragmentManager.beginTransaction();
     ft.add(privacyNoticeDialogFragment, tag);
     ft.commitAllowingStateLoss();
}

Customizing the Privacy Notice Dialog Fragment

The application developer can customize the Privacy Notice Dialog Fragment by passing in their own style and theme using the newInstance(PrivacyNoticeSettings privacyNoticeSettings, int style, int theme) method. Additionally, the text is customizable using the PrivacyNoticeSettings object. The following options are available:

Return Type Java API Description
CharSequence getDialogTitle() Retrieves the title of the privacy notice dialog.
void setDialogTitle(CharSequence title) Sets the title to be displayed on the privacy notice dialog.
CharSequence getDialogMessage() Retrieves the message displayed on the privacy notice dialog.
void setDialogMessage(CharSequence message) Sets the message to be displayed on the privacy notice dialog.
CharSequence getDialogPositiveButtonText() Retrieves the text displayed on the privacy notice dialog positive button.
void setDialogPositiveButtonText(CharSequence text) Sets the text to be displayed on the privacy notice dialog positive button.
CharSequence getDialogNegativeButtonText() Retrieves the text displayed on the privacy notice dialog negative button.
void setDialogNegativeButtonText(CharSequence text) Sets the text to be displayed on the privacy notice dialog negative button.

Last update: March 1, 2022