+
+ private void updateCreateButton(Button createButton) {
+ // Get the contents of the edit texts.
+ String shortcutName = shortcutNameEditText.getText().toString();
+ String urlString = urlEditText.getText().toString();
+
+ // Enable the create button if both the shortcut name and the URL are not empty.
+ createButton.setEnabled(!shortcutName.isEmpty() && !urlString.isEmpty());
+ }
+
+ private void createHomeScreenShortcut(Bitmap favoriteIconBitmap) {
+ // Get a handle for the context.
+ Context context = getContext();
+
+ // Remove the incorrect lint warning below that the context might be null.
+ assert context != null;
+
+ // Get the strings from the edit texts.
+ String shortcutName = shortcutNameEditText.getText().toString();
+ String urlString = urlEditText.getText().toString();
+
+ // Convert the favorite icon bitmap to an icon. `IconCompat` must be used until the minimum API >= 26.
+ IconCompat favoriteIcon = IconCompat.createWithBitmap(favoriteIconBitmap);
+
+ // Create a shortcut intent.
+ Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);
+
+ // Check to see if the shortcut should open up Privacy Browser explicitly.
+ if (openWithPrivacyBrowserRadioButton.isChecked()) {
+ // Set the current application ID as the target package.
+ shortcutIntent.setPackage(BuildConfig.APPLICATION_ID);
+ }
+
+ // Add the URL to the intent.
+ shortcutIntent.setData(Uri.parse(urlString));
+
+ // Create a shortcut info builder. The shortcut name becomes the shortcut ID.
+ ShortcutInfoCompat.Builder shortcutInfoBuilder = new ShortcutInfoCompat.Builder(context, shortcutName);
+
+ // Add the required fields to the shortcut info builder.
+ shortcutInfoBuilder.setIcon(favoriteIcon);
+ shortcutInfoBuilder.setIntent(shortcutIntent);
+ shortcutInfoBuilder.setShortLabel(shortcutName);
+
+ // Add the shortcut to the home screen. `ShortcutManagerCompat` can be switched to `ShortcutManager` once the minimum API >= 26.
+ ShortcutManagerCompat.requestPinShortcut(context, shortcutInfoBuilder.build(), null);
+ }