-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refer to the original PR: #3707 Extract and refactor the part of the code in ArtworkLoader to a new interface ArtworkDownloader and its default implementation ArtworkDownloaderDefault, so that we could swap the implementation in Kimono to avoid GMS Extract the artwork downloading functionality from ArtworkLoader into a separate interface (ArtworkDownloader) and a default implementation (ArtworkDownloaderDefault). This will allow the Kimono project to provide its own implementation without relying on GMS dependencies. b/347963541 Change-Id: I7edd475991c7dd755aa24f313aaa890db4d5798d Co-authored-by: Colin Liang <[email protected]>
- Loading branch information
1 parent
a726465
commit 95125ce
Showing
6 changed files
with
126 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
starboard/android/apk/app/src/main/java/dev/cobalt/media/ArtworkDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dev.cobalt.media; | ||
|
||
/** | ||
* Interface to download artwork (Bitmap) from a URL, intended for use in media session metadata. | ||
*/ | ||
public interface ArtworkDownloader { | ||
public void downloadArtwork(String url, ArtworkLoader artworkLoader); | ||
} |
64 changes: 64 additions & 0 deletions
64
starboard/android/apk/app/src/main/java/dev/cobalt/media/ArtworkDownloaderDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dev.cobalt.media; | ||
|
||
import static dev.cobalt.media.Log.TAG; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.util.Pair; | ||
import dev.cobalt.util.Log; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
/** | ||
* Default implementation of ArtworkDownloader using java.net.HttpURLConnection to fetch artwork. | ||
*/ | ||
public class ArtworkDownloaderDefault implements ArtworkDownloader { | ||
|
||
public ArtworkDownloaderDefault() {} | ||
|
||
@Override | ||
public void downloadArtwork(String url, ArtworkLoader artworkLoader) { | ||
Bitmap bitmap = null; | ||
HttpURLConnection conn = null; | ||
InputStream is = null; | ||
try { | ||
conn = (HttpURLConnection) new URL(url).openConnection(); | ||
is = conn.getInputStream(); | ||
bitmap = BitmapFactory.decodeStream(is); | ||
} catch (IOException e) { | ||
Log.e(TAG, "Could not download artwork", e); | ||
} finally { | ||
try { | ||
if (conn != null) { | ||
conn.disconnect(); | ||
conn = null; | ||
} | ||
if (is != null) { | ||
is.close(); | ||
is = null; | ||
} | ||
} catch (Exception e) { | ||
Log.e(TAG, "Error closing connection for artwork", e); | ||
} | ||
} | ||
|
||
bitmap = artworkLoader.cropTo16x9(bitmap); | ||
artworkLoader.onDownloadFinished(Pair.create(url, bitmap)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters