Tag: code

  • Android Studio Flatpak light/dark theme automatic change bug fix

    It is nice to have the desktop automatically change its theme from light to dark at sunset. Most applications work by default without further action in Fedora Kinoite. However Android Studio despite supporting light & dark themes for ages did not offer the option to automatically change the theme in Linux until very recently.

    After enabling the “Sync with OS” setting unfortunately nothing happened. Not even a restart of the application changed the theme. Guess Linux is still Linux where everything needs a bit of dedication to properly work.

    So digging into the topic it is (what else!) a Flatpak permissions issue. The dbus communication is not going through between the system and the application. After enabling the permissions I managed to get Android Studio to read the current theme on launch. But this was just a dead end as the automatic switching was still not working, I had to restart Android Studio to pick the switched theme which was not great for the workflow.

    After some time scouting sources of both Android Studio (really IntelliJ IDEA by JetBrains) and the application’s Flathub repo, the culprit was found. Android Studio itself is working fine as on a non-atomic system not using the Flatpak version there is no problem. The issue was in the Flatpak’s environment that missed a specific component, namely dbus-tools. Inside this module the command dbus-monitor is found and this is missing from the current Flathub build.

    The dbus-monitor is used to do exactly as its name states, it monitors the dbus for changes (signals) and this is what Android Studio uses to get the “color-scheme” parameter from the system. If this command cannot be executed it just dies silently and automatic theme change will not work. In a non-atomic system dbus-tools is part of the core system but Flatpaks run in a bare-bones environment and in this case the module is not included.

    To confirm this hypothesis I created my own Flatpak including dbus-tools:

    {
      "name": "dbus-tools",
      "buildsystem": "meson",
      "config-opts": [
        "-Dtools=true",
        "-Dmessage_bus=false",
        "-Dsystemd=disabled",
        "-Dx11_autolaunch=disabled"
      ],
      "sources": [
        {
          "type": "archive",
          "url": "https://dbus.freedesktop.org/releases/dbus/dbus-1.16.2.tar.xz",
          "sha256": "0ba2a1a4b16afe7bceb2c07e9ce99a8c2c3508e5dec290dbb643384bd6beb7e2"
        }
      ]
    },
    ...

    There are a few more tweaks that are not relevant for the case in question but are just to get the Flatpak built with less overhead:

    $ cat com.google.AndroidStudio.Test.json 
    {
      "id": "com.google.AndroidStudio.Test",
      "runtime": "org.freedesktop.Sdk",
      "runtime-version": "25.08",
      "sdk": "org.freedesktop.Sdk",
      "command": "android-studio-wrapper",
      "finish-args": [
        "--socket=x11",
        "--socket=pulseaudio",
        "--socket=ssh-auth",
        "--socket=gpg-agent",
        "--share=ipc",
        "--share=network",
        "--device=all",
        "--filesystem=home",
        "--allow=multiarch",
        "--env=JAVA_HOME=/app/extra/jbr",
        "--talk-name=org.freedesktop.Notifications",
        "--talk-name=org.freedesktop.secrets"
      ],
      "modules": [
        {
          "name": "dbus-tools",
          "buildsystem": "meson",
          "config-opts": [
            "-Dtools=true",
            "-Dmessage_bus=false",
    	"-Dsystemd=disabled",
            "-Dx11_autolaunch=disabled"
          ],
          "sources": [
            {
              "type": "archive",
              "url": "https://dbus.freedesktop.org/releases/dbus/dbus-1.16.2.tar.xz",
              "sha256": "0ba2a1a4b16afe7bceb2c07e9ce99a8c2c3508e5dec290dbb643384bd6beb7e2"
            }
          ]
        },
        {
          "name": "android-studio",
          "buildsystem": "simple",
          "build-commands": [
            "install -D -t ${FLATPAK_DEST}/bin/ apply_extra android-studio-wrapper",
            "echo '[Desktop Entry]' > test.desktop",
            "echo 'Type=Application' >> test.desktop",
            "echo 'Name=Android Studio Test' >> test.desktop",
            "echo 'Exec=android-studio-wrapper' >> test.desktop",
            "echo 'Icon=com.google.AndroidStudio.Test' >> test.desktop",
            "echo 'Categories=Development;IDE;' >> test.desktop",
            "install -Dm644 test.desktop ${FLATPAK_DEST}/share/applications/${FLATPAK_ID}.desktop",
            "echo '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' > test.metainfo.xml",
            "echo '<component type=\"desktop-application\">' >> test.metainfo.xml",
            "echo '  <id>com.google.AndroidStudio.Test</id>' >> test.metainfo.xml",
            "echo '  <metadata_license>CC0-1.0</metadata_license>' >> test.metainfo.xml",
            "echo '  <project_license>Apache-2.0</project_license>' >> test.metainfo.xml",
            "echo '  <name>Android Studio Test</name>' >> test.metainfo.xml",
            "echo '  <summary>Test Build</summary>' >> test.metainfo.xml",
            "echo '  <description><p>Testing theme switching configuration.</p></description>' >> test.metainfo.xml",
            "echo '  <launchable type=\"desktop-id\">com.google.AndroidStudio.Test.desktop</launchable>' >> test.metainfo.xml",
            "echo '</component>' >> test.metainfo.xml",
            "install -Dm644 test.metainfo.xml ${FLATPAK_DEST}/share/metainfo/${FLATPAK_ID}.metainfo.xml",
            "echo \"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'><rect width='10' height='10' fill='purple'/></svg>\" > test.svg",
            "install -Dm644 test.svg ${FLATPAK_DEST}/share/icons/hicolor/scalable/apps/${FLATPAK_ID}.svg"
          ],
          "sources": [
            {
              "type": "extra-data",
              "filename": "android-studio.tar.gz",
              "size": 1541218005,
              "only-arches": [
                "x86_64"
              ],
              "url": "https://dl.google.com/dl/android/studio/ide-zips/2026.1.2.10/android-studio-quail2-linux.tar.gz",
              "sha256": "64445a54092e7056c6eb7f1a89ad116d0feec2ef5f965b8e594d62abdb58590f"
            },
            {
              "type": "script",
              "dest-filename": "apply_extra",
              "commands": [
                "tar xzf android-studio.tar.gz --strip-components=1",
                "rm -f android-studio.tar.gz"
              ]
            },
            {
              "type": "script",
              "dest-filename": "android-studio-wrapper",
              "commands": [
                "/app/extra/bin/studio $@"
              ]
            }
          ]
        }
      ]
    }
    

    Then you install flatpak-builder, build your custom Flatpak and when finalised you can test it out:

    $ sudo dnf -y install flatpak-builder git
    $ flatpak-builder --user --install --force-clean --disable-rofiles-fuse build-dir com.google.AndroidStudio.Test.json
    $ flatpak run --command=dbus-monitor com.google.AndroidStudio.Test
    

    If the last command displays content it means dbus-monitor is now part of your Flatpak’s environment and you can safely launch the full appplication:

    $ flatpak run com.google.AndroidStudio.Test

    I ran all inside a toolbx so that I can get rid of it again easily. Also it should not affect your existing Flathub Android Studio installation, it is a completely parallel Flatpak (com.google.AndroidStudio.Test).

    Now the best moment comes, going into Settings>Appearance and enabling “Sync with OS“. Immediately Android Studio should change its theme to the system one. An easy way to change your theme if you have sunrise & sunset as triggers, is to simply change your location in the KDE Plasma settings e.g. to the other side of the planet, then you can quickly revert back and forth.

    As this is only a personal solution I have also created an issue at the Flathub repo so hopefully we will soon see this Android Studio Flatpak correctly switching light to dark themes and viceversa automatically for everyone.

  • Human Avatar for Robohash WordPress plugin

    I was tired of the similar looking avatars offered for comments. When changing them in Settings>Discussion>Default Avatar I noticed there was a new one, Robohash.

    Robohash has further customisation options but unfortunately they were not available to select in the avatar configuration options of WordPress.

    Specifically I liked their “human” set which I think Robohash keep pretty low key as it was only referenced briefly in a single paragraph at the very end.

    No point it customising the WordPress core directly because of updates, so instead I created a plugin for others to enjoy:

    Human Avatar for Robohash

  • Lightbox 2 and XHTML 1.1 validation

    I recently started using the Lightbox 2 WordPress plugin on my Marbella news site to automatically add the zooming overlay effect to certain images. It is really plug & play and works out of the box.

    I did encounter however that my pages stopped validating as XHTML 1.1 which is the standard doctype on all sites I develop.

    The error in question is due to the square brackets (“[” and “]”) being used to link individual images in one single post together as a gallery under the “rel” attribute. The square brackets are not permitted characters and therefore the W3 Validator spits out the following error message:

    character "[" is not allowed in the value of attribute "rel"
    It is possible that you violated the naming convention for this attribute. For example, id and name attributes must begin with a letter, not a digit.

    To avoid this XHTML 1.1 validation problem simply edit the following line in the Lightbox 2 plugin file named lightbox2.php under the “autoexpand_rel_wlightbox” function:

    from:

    $replacement    = '$1 rel="lightbox['.$post->ID.']">';

    to:

    $replacement    = '$1 rel="lightbox_'.$post->ID.'">';
    

    You should now be able to validate that plugin’s output as XHTML 1.1 with no issues.

  • WordPress + CDATA nightmare

    Do you know what is worse than not knowing the solution or even the reason to a problem?

    Well, that must be, knowing that you have experienced the same problem in the past and resolved it, but no matter how much you try, you can not remember at all what the problem was due and what you did to fix it.

    So this evening several hours went by due to a silly and stupid WordPress issue I already fixed once in the past but lost during an upgrade. This time I wanted to make sure I got the solution written down as I would probably forget it again.

    On my Marbella news site I was working on embedding an external flash video inside an iframe, which itself was invoked via Javascript to avoid the W3 Validator complaining about XHTML 1.1 and iframes being taboo.

    Basically it was WordPress’ over-zealous filters that caught a greater-than sign (>) and converted it to the equivalent HTML entity (&gt;). The most annoying thing is that I already have the Raw HTML plugin installed which disables WordPress’ text filtering. And it also worked fine for the rest of the Javascript and HTML tags in the page text. It was just on one single character, namely in the CDATA closing tag with // ]]> converted to // ]]&gt;.

    After searching many different combinations I finally hit it on the nail. I was not alone either.

    The annoying culprit is this little line in /wp-includes/post-template.php, which I once more commented out:

    $content = str_replace(']]>', ']]&gt;', $content);

    For some reason the WordPress developers have not yet fixed this issue after 3 years of it being reported.

    As a big fan of open source I appreciate the time and money spent by volunteers in many similar projects, however this is not the first time I see quick to fix issues hanging around for ages, simply due to people preferring to have an academic discussion on who can provide the most elegant solution, instead of simply fixing the issue first and talk later.

    Sometimes I wonder how projects ever become real projects.

    Anyhow there was a happy ending as you can see for yourself.