Add image-okhttp module
This commit is contained in:
		
							parent
							
								
									30532f9e1d
								
							
						
					
					
						commit
						eabe1d9994
					
				
							
								
								
									
										25
									
								
								markwon-image-okhttp/build.gradle
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								markwon-image-okhttp/build.gradle
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | ||||
| apply plugin: 'com.android.library' | ||||
| 
 | ||||
| android { | ||||
| 
 | ||||
|     compileSdkVersion config['compile-sdk'] | ||||
|     buildToolsVersion config['build-tools'] | ||||
| 
 | ||||
|     defaultConfig { | ||||
|         minSdkVersion config['min-sdk'] | ||||
|         targetSdkVersion config['target-sdk'] | ||||
|         versionCode 1 | ||||
|         versionName version | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| dependencies { | ||||
| 
 | ||||
|     api project(':markwon') | ||||
| 
 | ||||
|     deps.with { | ||||
|         api it['okhttp'] | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| registerArtifact(this) | ||||
							
								
								
									
										1
									
								
								markwon-image-okhttp/src/main/AndroidManifest.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								markwon-image-okhttp/src/main/AndroidManifest.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| <manifest package="ru.noties.markwon.image.okhttp" /> | ||||
| @ -0,0 +1,45 @@ | ||||
| package ru.noties.markwon.image.okhttp; | ||||
| 
 | ||||
| import android.support.annotation.NonNull; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| 
 | ||||
| import okhttp3.OkHttpClient; | ||||
| import ru.noties.markwon.AbstractMarkwonPlugin; | ||||
| import ru.noties.markwon.image.AsyncDrawableLoader; | ||||
| import ru.noties.markwon.image.network.NetworkSchemeHandler; | ||||
| 
 | ||||
| /** | ||||
|  * Plugin to use OkHttpClient to obtain images from network (http and https schemes) | ||||
|  * | ||||
|  * @see #create() | ||||
|  * @see #create(OkHttpClient) | ||||
|  * @since 3.0.0 | ||||
|  */ | ||||
| @SuppressWarnings("WeakerAccess") | ||||
| public class MarkwonImageOkHttpPlugin extends AbstractMarkwonPlugin { | ||||
| 
 | ||||
|     @NonNull | ||||
|     public static MarkwonImageOkHttpPlugin create() { | ||||
|         return new MarkwonImageOkHttpPlugin(new OkHttpClient()); | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     public static MarkwonImageOkHttpPlugin create(@NonNull OkHttpClient okHttpClient) { | ||||
|         return new MarkwonImageOkHttpPlugin(okHttpClient); | ||||
|     } | ||||
| 
 | ||||
|     private final OkHttpClient client; | ||||
| 
 | ||||
|     MarkwonImageOkHttpPlugin(@NonNull OkHttpClient client) { | ||||
|         this.client = client; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void configureImages(@NonNull AsyncDrawableLoader.Builder builder) { | ||||
|         builder.addSchemeHandler( | ||||
|                 Arrays.asList(NetworkSchemeHandler.SCHEME_HTTP, NetworkSchemeHandler.SCHEME_HTTPS), | ||||
|                 new OkHttpSchemeHandler(client) | ||||
|         ); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,57 @@ | ||||
| package ru.noties.markwon.image.okhttp; | ||||
| 
 | ||||
| import android.net.Uri; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| 
 | ||||
| import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| import okhttp3.ResponseBody; | ||||
| import ru.noties.markwon.image.ImageItem; | ||||
| import ru.noties.markwon.image.SchemeHandler; | ||||
| 
 | ||||
| class OkHttpSchemeHandler extends SchemeHandler { | ||||
| 
 | ||||
|     private static final String HEADER_CONTENT_TYPE = "Content-Type"; | ||||
| 
 | ||||
|     private final OkHttpClient client; | ||||
| 
 | ||||
|     OkHttpSchemeHandler(@NonNull OkHttpClient client) { | ||||
|         this.client = client; | ||||
|     } | ||||
| 
 | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public ImageItem handle(@NonNull String raw, @NonNull Uri uri) { | ||||
|         ImageItem out = null; | ||||
| 
 | ||||
|         final Request request = new Request.Builder() | ||||
|                 .url(raw) | ||||
|                 .tag(raw) | ||||
|                 .build(); | ||||
| 
 | ||||
|         Response response = null; | ||||
|         try { | ||||
|             response = client.newCall(request).execute(); | ||||
|         } catch (IOException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
| 
 | ||||
|         if (response != null) { | ||||
|             final ResponseBody body = response.body(); | ||||
|             if (body != null) { | ||||
|                 final InputStream inputStream = body.byteStream(); | ||||
|                 if (inputStream != null) { | ||||
|                     final String contentType = response.header(HEADER_CONTENT_TYPE); | ||||
|                     out = new ImageItem(contentType, inputStream); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return out; | ||||
|     } | ||||
| } | ||||
| @ -13,6 +13,12 @@ import java.net.URL; | ||||
| import ru.noties.markwon.image.ImageItem; | ||||
| import ru.noties.markwon.image.SchemeHandler; | ||||
| 
 | ||||
| /** | ||||
|  * A simple network scheme handler that is not dependent on any external libraries. | ||||
|  * | ||||
|  * @see #create() | ||||
|  * @since 3.0.0 | ||||
|  */ | ||||
| public class NetworkSchemeHandler extends SchemeHandler { | ||||
| 
 | ||||
|     public static final String SCHEME_HTTP = "http"; | ||||
|  | ||||
| @ -5,6 +5,7 @@ include ':app', | ||||
|         ':markwon-ext-strikethrough', | ||||
|         ':markwon-ext-tables', | ||||
|         ':markwon-ext-tasklist', | ||||
|         ':markwon-image-okhttp', | ||||
|         ':markwon-image-svg', | ||||
|         ':markwon-image-gif', | ||||
|         ':markwon-syntax-highlight', | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Dimitry Ivanov
						Dimitry Ivanov