]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.kt
Switch to the new Day/Night theme. https://redmine.stoutner.com/issues/522
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateHomeScreenShortcutDialog.kt
index 092d28c576daf66ad8fdcbd5a9fd5c4442511b10..69be19d27344fe231a9c3944e75c7423b6ce353c 100644 (file)
@@ -92,7 +92,7 @@ class CreateHomeScreenShortcutDialog: DialogFragment() {
     @SuppressLint("InflateParams")
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
         // Get the arguments.
-        val arguments = arguments!!
+        val arguments = requireArguments()
 
         // Get the strings from the arguments.
         val initialShortcutName = arguments.getString("shortcut_name")
@@ -104,19 +104,8 @@ class CreateHomeScreenShortcutDialog: DialogFragment() {
         // Convert the favorite icon byte array to a bitmap.
         val favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.size)
 
-        // Get a handle for the shared preferences.
-        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
-
-        // Get the theme and screenshot preferences.
-        val allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false)
-        val darkTheme = sharedPreferences.getBoolean("dark_theme", false)
-
-        // Use an alert dialog builder to create the dialog and set the style according to the theme.
-        val dialogBuilder = if (darkTheme) {
-            AlertDialog.Builder(activity!!, R.style.PrivacyBrowserAlertDialogDark)
-        } else {
-            AlertDialog.Builder(activity!!, R.style.PrivacyBrowserAlertDialogLight)
-        }
+        // Use an alert dialog builder to create the dialog.
+        val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
 
         // Create a drawable version of the favorite icon.
         val favoriteIconDrawable: Drawable = BitmapDrawable(resources, favoriteIconBitmap)
@@ -126,7 +115,7 @@ class CreateHomeScreenShortcutDialog: DialogFragment() {
         dialogBuilder.setIcon(favoriteIconDrawable)
 
         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
-        dialogBuilder.setView(activity!!.layoutInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null))
+        dialogBuilder.setView(requireActivity().layoutInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null))
 
         // Set a listener on the close button.  Using null closes the dialog without doing anything else.
         dialogBuilder.setNegativeButton(R.string.cancel, null)
@@ -140,6 +129,12 @@ class CreateHomeScreenShortcutDialog: DialogFragment() {
         // Create an alert dialog from the alert dialog builder.
         val alertDialog = dialogBuilder.create()
 
+        // Get a handle for the shared preferences.
+        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
+
+        // Get the screenshot preference.
+        val allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false)
+
         // Disable screenshots if not allowed.
         if (!allowScreenshots) {
             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
@@ -240,9 +235,6 @@ class CreateHomeScreenShortcutDialog: DialogFragment() {
     }
 
     private fun createHomeScreenShortcut(favoriteIconBitmap: Bitmap) {
-        // Get a handle for the context.
-        val context = context!!
-
         // Get the strings from the edit texts.
         val shortcutName = shortcutNameEditText.text.toString()
         val urlString = urlEditText.text.toString()
@@ -263,7 +255,7 @@ class CreateHomeScreenShortcutDialog: DialogFragment() {
         shortcutIntent.data = Uri.parse(urlString)
 
         // Create a shortcut info builder.  The shortcut name becomes the shortcut ID.
-        val shortcutInfoBuilder = ShortcutInfoCompat.Builder(context, shortcutName)
+        val shortcutInfoBuilder = ShortcutInfoCompat.Builder(requireContext(), shortcutName)
 
         // Add the required fields to the shortcut info builder.
         shortcutInfoBuilder.setIcon(favoriteIcon)
@@ -271,6 +263,6 @@ class CreateHomeScreenShortcutDialog: DialogFragment() {
         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)
+        ShortcutManagerCompat.requestPinShortcut(requireContext(), shortcutInfoBuilder.build(), null)
     }
 }
\ No newline at end of file