-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added LottieConfig with NetworkFetcher and NetworkCache customization (…
- Loading branch information
Showing
11 changed files
with
403 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.airbnb.lottie; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
/** | ||
* Class for initializing the library with custom config | ||
*/ | ||
public class Lottie { | ||
|
||
private Lottie() { | ||
} | ||
|
||
public static void initialize(@NonNull final LottieConfig lottieConfig) { | ||
L.setFetcher(lottieConfig.networkFetcher); | ||
L.setCacheProvider(lottieConfig.cacheProvider); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.airbnb.lottie; | ||
|
||
import com.airbnb.lottie.network.LottieNetworkFetcher; | ||
import com.airbnb.lottie.network.LottieNetworkCacheProvider; | ||
|
||
import java.io.File; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* Class for custom library configuration | ||
*/ | ||
public class LottieConfig { | ||
|
||
@Nullable final LottieNetworkFetcher networkFetcher; | ||
@Nullable final LottieNetworkCacheProvider cacheProvider; | ||
|
||
private LottieConfig(@Nullable LottieNetworkFetcher networkFetcher, @Nullable LottieNetworkCacheProvider cacheProvider) { | ||
this.networkFetcher = networkFetcher; | ||
this.cacheProvider = cacheProvider; | ||
} | ||
|
||
public static final class Builder { | ||
|
||
@Nullable | ||
private LottieNetworkFetcher networkFetcher; | ||
@Nullable | ||
private LottieNetworkCacheProvider cacheProvider; | ||
|
||
@NonNull | ||
public Builder setNetworkFetcher(@NonNull LottieNetworkFetcher fetcher) { | ||
this.networkFetcher = fetcher; | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public Builder setCacheDir(@NonNull final File file) { | ||
cacheProvider = new LottieNetworkCacheProvider() { | ||
@Override @NonNull public File getCacheDir() { | ||
if (!file.isDirectory()) { | ||
throw new IllegalArgumentException("cache file must be a directory"); | ||
} | ||
return file; | ||
} | ||
}; | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public Builder setCacheProvider(@NonNull final LottieNetworkCacheProvider fileCacheProvider) { | ||
cacheProvider = new LottieNetworkCacheProvider() { | ||
@NonNull @Override public File getCacheDir() { | ||
File file = fileCacheProvider.getCacheDir(); | ||
if (!file.isDirectory()) { | ||
throw new IllegalArgumentException("cache file must be a directory"); | ||
} | ||
return file; | ||
} | ||
}; | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public LottieConfig build() { | ||
return new LottieConfig(networkFetcher, cacheProvider); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
lottie/src/main/java/com/airbnb/lottie/network/DefaultLottieFetchResult.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,73 @@ | ||
package com.airbnb.lottie.network; | ||
|
||
import com.airbnb.lottie.utils.Logger; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
public class DefaultLottieFetchResult implements LottieFetchResult { | ||
|
||
@NonNull | ||
private final HttpURLConnection connection; | ||
|
||
public DefaultLottieFetchResult(@NonNull HttpURLConnection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override public boolean isSuccessful() { | ||
try { | ||
return connection.getResponseCode() / 100 == 2; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@NonNull @Override public InputStream bodyByteStream() throws IOException { | ||
return connection.getInputStream(); | ||
} | ||
|
||
@Nullable @Override public String contentType() { | ||
return connection.getContentType(); | ||
} | ||
|
||
@Nullable @Override public String error() { | ||
try { | ||
return isSuccessful() ? null : | ||
"Unable to fetch " + connection.getURL() + ". Failed with " + connection.getResponseCode() + "\n" + getErrorFromConnection(connection); | ||
} catch (IOException e) { | ||
Logger.warning("get error failed ", e); | ||
return e.getMessage(); | ||
} | ||
} | ||
|
||
@Override public void close() { | ||
connection.disconnect(); | ||
} | ||
|
||
private String getErrorFromConnection(HttpURLConnection connection) throws IOException { | ||
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getErrorStream())); | ||
StringBuilder error = new StringBuilder(); | ||
String line; | ||
|
||
try { | ||
while ((line = r.readLine()) != null) { | ||
error.append(line).append('\n'); | ||
} | ||
} catch (Exception e) { | ||
throw e; | ||
} finally { | ||
try { | ||
r.close(); | ||
} catch (Exception e) { | ||
// Do nothing. | ||
} | ||
} | ||
return error.toString(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
lottie/src/main/java/com/airbnb/lottie/network/DefaultLottieNetworkFetcher.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,19 @@ | ||
package com.airbnb.lottie.network; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public class DefaultLottieNetworkFetcher implements LottieNetworkFetcher { | ||
|
||
@Override | ||
@NonNull | ||
public LottieFetchResult fetchSync(@NonNull String url) throws IOException { | ||
final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); | ||
connection.setRequestMethod("GET"); | ||
connection.connect(); | ||
return new DefaultLottieFetchResult(connection); | ||
} | ||
} |
Oops, something went wrong.