Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add webp support for reading APK icon that caused "unknown format" before #90

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

turtletramp
Copy link

@turtletramp turtletramp commented Sep 27, 2024

I had the same problem as in #89 and in my case my APK used an adaptive icon that in the APK ends up as an image encoded using webp format. Adding webp in addition to png and jpg support fixed my problem with "unknown format" for the icon.

Summary by CodeRabbit

  • New Features
    • Enhanced handling of APK icons with support for adaptive icons.
  • Chores
    • Updated dependencies to include the new golang.org/x/image library and additional indirect dependencies.

Copy link

coderabbitai bot commented Sep 27, 2024

Walkthrough

The changes introduced in this pull request enhance the handling of APK icons in the apk/apk.go file. A new AdaptiveIcon struct has been added to define the structure for adaptive icons, and the Icon method of the Apk struct has been updated to process XML files representing these icons. Additionally, the go.mod file has been modified to include new dependencies, expanding the set of external libraries utilized by the module.

Changes

File Change Summary
apk/apk.go Added AdaptiveIcon struct; updated Icon method to handle adaptive icons and XML parsing; added error handling.
go.mod Added new dependency: require golang.org/x/image v0.20.0; added indirect dependencies for apkparser and compress.

Poem

In the garden where icons play,
Adaptive shapes come out to stay.
With backgrounds bright and foregrouds bold,
New stories in APKs to unfold.
Let's hop with joy, for change is here,
A vibrant world, let’s give a cheer! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
go.mod (1)

3-3: Consider updating the Go version.

The current Go version is set to 1.17, which is not the latest version. While this may be intentional for compatibility reasons, it's worth considering an update to a more recent version to take advantage of performance improvements and new features.

apk/apk.go (1)

12-12: LGTM! Consider grouping image format imports.

The addition of WebP support through this import statement is correct and aligns with the PR objectives. This change should enable the application to handle WebP format APK icons as intended.

For better organization, consider grouping all image format imports together. You could move the WebP import next to the JPEG and PNG imports:

 import (
 	"archive/zip"
 	"bytes"
 	"fmt"
 	"image"
 	"io"
 	"os"
 
 	"github.com/shogo82148/androidbinary"
-
-	_ "golang.org/x/image/webp"
 
 	_ "image/jpeg" // handle jpeg format
 	_ "image/png"  // handle png format
+	_ "golang.org/x/image/webp" // handle webp format
 )

This grouping makes it clearer that these imports are all related to image format support.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 565dc46 and 8fe0f87.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • apk/apk.go (1 hunks)
  • go.mod (1 hunks)
🔇 Additional comments (1)
go.mod (1)

5-5: LGTM: New dependency for WebP support.

The addition of golang.org/x/image v0.20.0 is appropriate for implementing WebP support as mentioned in the PR objectives. This package provides the necessary functionality for handling WebP images.

@@ -9,6 +9,7 @@ import (
"os"

"github.com/shogo82148/androidbinary"
_ "golang.org/x/image/webp"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

WebP support is not properly integrated

While the golang.org/x/image/webp package is imported in apk.go, there is no evidence of explicit handling for WebP in image decoding functions. This likely causes the "unknown format" error when processing APKs with WebP icons.

🔗 Analysis chain

Verify WebP support and check for explicit format handling

While this change should theoretically add WebP support, it's important to ensure it fully addresses the issue mentioned in #89.

  1. Please verify that the application can now successfully read and display WebP icons from APKs.
  2. Check if there are any other places in the codebase where image formats are explicitly checked or handled. If so, they might need to be updated to include WebP.

Run the following script to search for potential places where image formats are explicitly handled:

If the script returns any relevant results, please review those locations to ensure WebP is properly supported throughout the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for potential explicit image format handling

# Test: Search for image format related keywords
rg --type go -i '\b(image format|png|jpeg|jpg|webp)\b'

Length of output: 256


Script:

#!/bin/bash
# Description: Check if WebP is handled in image decoding functions

# Search for functions that decode images and verify WebP handling
rg --type go 'decode|parse' | rg -i 'webp'

Length of output: 42

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
apk/apk.go (2)

104-104: Remove or address the TODO comment

The TODO comment indicates uncertainty about the implementation's robustness. Either validate the implementation with more test cases or document the specific concerns that need to be addressed.


102-155: Consider handling the background drawable

The current implementation only processes the foreground drawable of the adaptive icon. For complete icon rendering, you might want to consider handling the background drawable as well, as adaptive icons typically consist of both layers.

Would you like me to provide an example implementation that handles both layers?

🧰 Tools
🪛 golangci-lint

135-135: ineffectual assignment to innerForegroundImagePath

(ineffassign)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8fe0f87 and d2dd3da.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • apk/apk.go (3 hunks)
  • go.mod (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • go.mod
🧰 Additional context used
🪛 golangci-lint
apk/apk.go

135-135: ineffectual assignment to innerForegroundImagePath

(ineffassign)

🔇 Additional comments (3)
apk/apk.go (3)

6-6: LGTM: WebP support correctly imported

The WebP format support is properly added using the blank identifier import, which is the correct way to register the WebP format decoder.

Also applies to: 14-16


79-86: LGTM: Well-structured AdaptiveIcon definition

The AdaptiveIcon struct is properly defined with correct XML namespace and attributes for Android's adaptive icon format.


16-16: Verify WebP support implementation

The WebP support looks correctly implemented through:

  1. Registration of the WebP decoder via import
  2. Usage of image.Decode which automatically handles format detection

Let's verify this implementation:

Also applies to: 156-157

✅ Verification successful

WebP support is properly implemented

The verification confirms correct WebP implementation:

  • WebP decoder is registered via blank import in apk/apk.go
  • Generic image.Decode is used for decoding, which automatically handles format detection
  • No explicit format checks that could bypass WebP support
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify WebP support implementation
# Test 1: Check if WebP decoder is properly imported
rg --type go "_ \"golang.org/x/image/webp\""

# Test 2: Verify no explicit format checks that might bypass WebP
rg --type go "image\.(PNG|JPEG|GIF)"

# Test 3: Confirm image.Decode is used for generic decoding
rg --type go "image\.Decode"

Length of output: 218

Comment on lines +132 to +143
innerForegroundImagePathTmp, err := k.table.GetResource(resID, nil)
switch v := innerForegroundImagePathTmp.(type) {
case string:
innerForegroundImagePath = v
default:
return nil, errorf("failed to get resource %q: %w", adaptiveIcon.Foreground.Drawable, err)
}

innerForegroundImagePath = innerForegroundImagePathTmp.(string)
if err != nil {
return nil, errorf("failed to get resource %q: %w", adaptiveIcon.Foreground.Drawable, err)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix redundant error handling and ineffectual assignment

There are several issues in this code segment:

  1. The assignment at line 140 is ineffectual as the value was already assigned at line 135
  2. The error check at lines 141-143 is redundant with the type switch error handling above

Apply this diff to fix the issues:

       innerForegroundImagePathTmp, err := k.table.GetResource(resID, nil)
       switch v := innerForegroundImagePathTmp.(type) {
       case string:
           innerForegroundImagePath = v
       default:
           return nil, errorf("failed to get resource %q: %w", adaptiveIcon.Foreground.Drawable, err)
       }
-
-      innerForegroundImagePath = innerForegroundImagePathTmp.(string)
-      if err != nil {
-          return nil, errorf("failed to get resource %q: %w", adaptiveIcon.Foreground.Drawable, err)
-      }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
innerForegroundImagePathTmp, err := k.table.GetResource(resID, nil)
switch v := innerForegroundImagePathTmp.(type) {
case string:
innerForegroundImagePath = v
default:
return nil, errorf("failed to get resource %q: %w", adaptiveIcon.Foreground.Drawable, err)
}
innerForegroundImagePath = innerForegroundImagePathTmp.(string)
if err != nil {
return nil, errorf("failed to get resource %q: %w", adaptiveIcon.Foreground.Drawable, err)
}
innerForegroundImagePathTmp, err := k.table.GetResource(resID, nil)
switch v := innerForegroundImagePathTmp.(type) {
case string:
innerForegroundImagePath = v
default:
return nil, errorf("failed to get resource %q: %w", adaptiveIcon.Foreground.Drawable, err)
}
🧰 Tools
🪛 golangci-lint

135-135: ineffectual assignment to innerForegroundImagePath

(ineffassign)

// read from the inner foreground image location
imgData, err = k.readZipFile(innerForegroundImagePath)
if err != nil {
return nil, errorf("failed to read %q: %w", adaptiveIcon.Foreground, err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix error message to use the correct path variable

The error message uses adaptiveIcon.Foreground directly instead of innerForegroundImagePath.

Apply this diff to fix the error message:

-            return nil, errorf("failed to read %q: %w", adaptiveIcon.Foreground, err)
+            return nil, errorf("failed to read %q: %w", innerForegroundImagePath, err)

Committable suggestion skipped: line range outside the PR's diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant