Presenter of MVP.
22 | * Created by Zhenjie Yan on 2017/7/17. 23 | */ 24 | public interface BasePresenter extends LifecycleOwner, Bye {} -------------------------------------------------------------------------------- /sample/src/main/res/layout/item_form_button.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 |File interface. All the methods are called in thread.
20 | * Created in Oct 12, 2015 4:44:07 PM. 21 | */ 22 | public interface Binary extends Content { 23 | 24 | /** 25 | * Gets the name of Binary. 26 | */ 27 | String name(); 28 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/Canceller.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/27. 20 | */ 21 | public interface Canceller { 22 | /** 23 | * Cancel operation. 24 | */ 25 | void cancel(); 26 | 27 | /** 28 | * Operation is canceled. 29 | */ 30 | boolean isCancelled(); 31 | } 32 | -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/ssl/CompatSSLSocketFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.ssl; 17 | 18 | /** 19 | *Add CipherSuites to the lower version.
20 | * Created by Zhenjie Yan on 2017/6/13. 21 | * 22 | * @deprecated use {@link TLSSocketFactory} instead. 23 | */ 24 | @Deprecated 25 | public class CompatSSLSocketFactory extends TLSSocketFactory { 26 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/exception/HostError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.exception; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/22. 20 | */ 21 | public class HostError extends ConnectException { 22 | public HostError(String message) { 23 | super(message); 24 | } 25 | 26 | public HostError(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/exception/URLError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.exception; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/22. 20 | */ 21 | public class URLError extends ConnectException { 22 | public URLError(String message) { 23 | super(message); 24 | } 25 | 26 | public URLError(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/exception/ParseError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.exception; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/27. 20 | */ 21 | public class ParseError extends ReadException { 22 | public ParseError(String message) { 23 | super(message); 24 | } 25 | 26 | public ParseError(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/exception/NetworkError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.exception; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/22. 20 | */ 21 | public class NetworkError extends ConnectException { 22 | public NetworkError(String message) { 23 | super(message); 24 | } 25 | 26 | public NetworkError(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/exception/NoCacheError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.exception; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/26. 20 | */ 21 | public class NoCacheError extends ReadException { 22 | public NoCacheError(String message) { 23 | super(message); 24 | } 25 | 26 | public NoCacheError(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadTimeoutError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.exception; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/24. 20 | */ 21 | public class ReadTimeoutError extends ReadException { 22 | public ReadTimeoutError(String message) { 23 | super(message); 24 | } 25 | 26 | public ReadTimeoutError(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectTimeoutError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.exception; 17 | 18 | /** 19 | * Created by Zhenjie Yan on 2018/2/22. 20 | */ 21 | public class ConnectTimeoutError extends ConnectException { 22 | public ConnectTimeoutError(String message) { 23 | super(message); 24 | } 25 | 26 | public ConnectTimeoutError(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/util/LengthOutputStream.java: -------------------------------------------------------------------------------- 1 | package com.yanzhenjie.kalle.util; 2 | 3 | import java.io.OutputStream; 4 | import java.util.concurrent.atomic.AtomicLong; 5 | 6 | /** 7 | * Created by Zhenjie Yan on 2019-06-24. 8 | */ 9 | public class LengthOutputStream extends OutputStream { 10 | 11 | private final AtomicLong mLength = new AtomicLong(0L); 12 | 13 | public LengthOutputStream() { 14 | } 15 | 16 | public long getLength() { 17 | return mLength.get(); 18 | } 19 | 20 | public void write(long multiByte) { 21 | mLength.addAndGet(multiByte); 22 | } 23 | 24 | @Override 25 | public void write(int oneByte) { 26 | mLength.addAndGet(1); 27 | } 28 | 29 | @Override 30 | public void write(byte[] buffer) { 31 | mLength.addAndGet(buffer.length); 32 | } 33 | 34 | @Override 35 | public void write(byte[] buffer, int offset, int count) { 36 | mLength.addAndGet(count); 37 | } 38 | 39 | @Override 40 | public void close() { 41 | } 42 | 43 | @Override 44 | public void flush() { 45 | } 46 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/connect/ConnectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.connect; 17 | 18 | import com.yanzhenjie.kalle.Request; 19 | 20 | import java.io.IOException; 21 | 22 | /** 23 | * Created by Zhenjie Yan on 2018/2/20. 24 | */ 25 | public interface ConnectFactory { 26 | /** 27 | * According to the request attribute, 28 | * and the server to establish a connection. 29 | */ 30 | Connection connect(Request request) throws IOException; 31 | } -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 |25 | * Intercept before call request. 26 | *
27 | * Created by Zhenjie Yan on 2018/2/11. 28 | */ 29 | public interface Interceptor { 30 | 31 | /** 32 | * When intercepting the {@link Chain}, 33 | * here can do some signature and login operation, 34 | * it should send a response and return. 35 | * 36 | * @param chain request chain. 37 | * @return the server connection. 38 | * @throws IOException io exception may occur during the implementation, you can handle or throw. 39 | */ 40 | Response intercept(Chain chain) throws IOException; 41 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/download/UrlWorker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.download; 17 | 18 | import com.yanzhenjie.kalle.Response; 19 | import com.yanzhenjie.kalle.connect.http.Call; 20 | 21 | import java.io.IOException; 22 | 23 | /** 24 | * Created by Zhenjie Yan on 2018/3/18. 25 | */ 26 | public class UrlWorker extends BasicWorker24 | * CacheStore entity class. 25 | *
26 | * Created in Jan 10, 2016 12:43:10 AM. 27 | */ 28 | public class Cache implements Serializable { 29 | 30 | private String mKey; 31 | private int mCode; 32 | private Headers mHeaders; 33 | private byte[] mBody; 34 | private long mExpires; 35 | 36 | public Cache() { 37 | } 38 | 39 | public String getKey() { 40 | return mKey; 41 | } 42 | 43 | public void setKey(String key) { 44 | this.mKey = key; 45 | } 46 | 47 | public int getCode() { 48 | return mCode; 49 | } 50 | 51 | public void setCode(int code) { 52 | mCode = code; 53 | } 54 | 55 | public Headers getHeaders() { 56 | return mHeaders; 57 | } 58 | 59 | public void setHeaders(Headers headers) { 60 | mHeaders = headers; 61 | } 62 | 63 | public byte[] getBody() { 64 | return mBody; 65 | } 66 | 67 | public void setBody(byte[] body) { 68 | this.mBody = body; 69 | } 70 | 71 | public long getExpires() { 72 | return mExpires; 73 | } 74 | 75 | public void setExpires(long expires) { 76 | this.mExpires = expires; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/FileBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle; 17 | 18 | import android.text.TextUtils; 19 | import android.webkit.MimeTypeMap; 20 | 21 | import com.yanzhenjie.kalle.util.IOUtils; 22 | 23 | import java.io.File; 24 | import java.io.FileInputStream; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.io.OutputStream; 28 | 29 | /** 30 | * Created by Zhenjie Yan on 2018/2/13. 31 | */ 32 | public class FileBody extends BaseContent20 | * CacheStore interface. 21 | *
22 | * Created in Dec 14, 2015 5:52:41 PM. 23 | */ 24 | public interface CacheStore { 25 | 26 | CacheStore DEFAULT = new CacheStore() { 27 | @Override 28 | public Cache get(String key) { 29 | return null; 30 | } 31 | 32 | @Override 33 | public boolean replace(String key, Cache cache) { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean remove(String key) { 39 | return true; 40 | } 41 | 42 | @Override 43 | public boolean clear() { 44 | return true; 45 | } 46 | }; 47 | 48 | /** 49 | * Get the cache. 50 | * 51 | * @param key unique key. 52 | * @return cache. 53 | */ 54 | Cache get(String key); 55 | 56 | /** 57 | * Save or set the cache. 58 | * 59 | * @param key unique key. 60 | * @param cache cache. 61 | * @return cache. 62 | */ 63 | boolean replace(String key, Cache cache); 64 | 65 | /** 66 | * Remove cache. 67 | * 68 | * @param key unique. 69 | * @return cache. 70 | */ 71 | boolean remove(String key); 72 | 73 | /** 74 | * Clear all data. 75 | * 76 | * @return returns true if successful, false otherwise. 77 | */ 78 | boolean clear(); 79 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/util/WorkExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.util; 17 | 18 | import java.util.concurrent.Executor; 19 | import java.util.concurrent.LinkedBlockingQueue; 20 | import java.util.concurrent.ThreadFactory; 21 | import java.util.concurrent.ThreadPoolExecutor; 22 | import java.util.concurrent.TimeUnit; 23 | import java.util.concurrent.atomic.AtomicInteger; 24 | 25 | /** 26 | * Created by Zhenjie Yan on 2018/2/26. 27 | */ 28 | public class WorkExecutor implements Executor { 29 | 30 | private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); 31 | 32 | private static final ThreadFactory THREAD_FACTORY = new ThreadFactory() { 33 | private final AtomicInteger mCount = new AtomicInteger(1); 34 | 35 | public Thread newThread(Runnable r) { 36 | return new Thread(r, "Request #" + mCount.getAndIncrement()); 37 | } 38 | }; 39 | 40 | private ThreadPoolExecutor mPoolExecutor; 41 | 42 | public WorkExecutor() { 43 | mPoolExecutor = new ThreadPoolExecutor( 44 | Math.max(2, Math.min(CPU_COUNT - 1, 4)), 45 | CPU_COUNT * 2 + 1, 46 | 60L, 47 | TimeUnit.SECONDS, 48 | new LinkedBlockingQueue31 | * A default implementation of Binary. 32 | * All the methods are called in Son thread. 33 | *
34 | * Created in Oct 17, 2015 12:40:54 PM. 35 | */ 36 | public class FileBinary extends BaseContent22 | * HTTP handle method. 23 | *
24 | * Created in Oct 10, 2015 8:00:48 PM. 25 | */ 26 | public enum RequestMethod { 27 | 28 | GET("GET"), 29 | 30 | POST("POST"), 31 | 32 | PUT("PUT"), 33 | 34 | DELETE("DELETE"), 35 | 36 | HEAD("HEAD"), 37 | 38 | PATCH("PATCH"), 39 | 40 | OPTIONS("OPTIONS"), 41 | 42 | TRACE("TRACE"); 43 | 44 | private final String value; 45 | 46 | RequestMethod(String value) { 47 | this.value = value; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return value; 53 | } 54 | 55 | public boolean allowBody() { 56 | switch (this) { 57 | case POST: 58 | case PUT: 59 | case PATCH: 60 | case DELETE: { 61 | return true; 62 | } 63 | default: { 64 | return false; 65 | } 66 | } 67 | } 68 | 69 | public static RequestMethod reverse(String method) { 70 | method = method.toUpperCase(Locale.ENGLISH); 71 | switch (method) { 72 | case "GET": { 73 | return GET; 74 | } 75 | case "POST": { 76 | return POST; 77 | } 78 | case "PUT": { 79 | return PUT; 80 | } 81 | case "DELETE": { 82 | return DELETE; 83 | } 84 | case "HEAD": { 85 | return HEAD; 86 | } 87 | case "PATCH": { 88 | return PATCH; 89 | } 90 | case "OPTIONS": { 91 | return OPTIONS; 92 | } 93 | case "TRACE": { 94 | return TRACE; 95 | } 96 | default: { 97 | return GET; 98 | } 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/kalle/sample/http/DownloadCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.sample.http; 17 | 18 | import android.content.Context; 19 | 20 | import com.yanzhenjie.kalle.download.SimpleCallback; 21 | import com.yanzhenjie.kalle.exception.ConnectTimeoutError; 22 | import com.yanzhenjie.kalle.exception.HostError; 23 | import com.yanzhenjie.kalle.exception.NetworkError; 24 | import com.yanzhenjie.kalle.exception.ReadTimeoutError; 25 | import com.yanzhenjie.kalle.exception.URLError; 26 | import com.yanzhenjie.kalle.exception.WriteException; 27 | import com.yanzhenjie.kalle.sample.R; 28 | import com.yanzhenjie.kalle.sample.util.Logger; 29 | 30 | /** 31 | * Created by Zhenjie Yan on 2018/3/28. 32 | */ 33 | public abstract class DownloadCallback extends SimpleCallback { 34 | 35 | private Context mContext; 36 | 37 | public DownloadCallback(Context context) { 38 | mContext = context; 39 | } 40 | 41 | @Override 42 | public final void onException(Exception e) { 43 | String message; 44 | if (e instanceof NetworkError) { 45 | message = mContext.getString(R.string.http_exception_network); 46 | } else if (e instanceof URLError) { 47 | message = mContext.getString(R.string.http_exception_url); 48 | } else if (e instanceof HostError) { 49 | message = mContext.getString(R.string.http_exception_host); 50 | } else if (e instanceof ConnectTimeoutError) { 51 | message = mContext.getString(R.string.http_exception_connect_timeout); 52 | } else if (e instanceof WriteException) { 53 | message = mContext.getString(R.string.http_exception_write); 54 | } else if (e instanceof ReadTimeoutError) { 55 | message = mContext.getString(R.string.http_exception_read_timeout); 56 | } else { 57 | message = mContext.getString(R.string.http_exception_unknow_error); 58 | } 59 | Logger.e(e); 60 | onException(message); 61 | } 62 | 63 | /** 64 | * Error message. 65 | */ 66 | public abstract void onException(String message); 67 | 68 | } -------------------------------------------------------------------------------- /kalle/src/main/java/com/yanzhenjie/kalle/connect/BroadcastNetwork.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.connect; 17 | 18 | import android.content.BroadcastReceiver; 19 | import android.content.Context; 20 | import android.content.Intent; 21 | import android.content.IntentFilter; 22 | import android.net.ConnectivityManager; 23 | import android.net.wifi.WifiManager; 24 | 25 | /** 26 | * Created by Zhenjie Yan on 2018/2/20. 27 | */ 28 | public class BroadcastNetwork implements Network { 29 | 30 | private final Context mContext; 31 | private final NetworkReceiver mReceiver; 32 | 33 | public BroadcastNetwork(Context context) { 34 | this.mContext = context.getApplicationContext(); 35 | this.mReceiver = new NetworkReceiver(new NetworkChecker(mContext)); 36 | 37 | IntentFilter filter = new IntentFilter(); 38 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 39 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 40 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 41 | filter.addAction("android.net.ethernet.STATE_CHANGE"); 42 | filter.addAction("android.net.ethernet.ETHERNET_STATE_CHANGED"); 43 | filter.addAction(Intent.ACTION_SCREEN_OFF); 44 | filter.addAction(Intent.ACTION_SCREEN_ON); 45 | filter.addAction(Intent.ACTION_USER_PRESENT); 46 | mContext.registerReceiver(mReceiver, filter); 47 | } 48 | 49 | @Override 50 | public boolean isAvailable() { 51 | return mReceiver.mAvailable; 52 | } 53 | 54 | public void destroy() { 55 | mContext.unregisterReceiver(mReceiver); 56 | } 57 | 58 | private static class NetworkReceiver extends BroadcastReceiver { 59 | 60 | private NetworkChecker mChecker; 61 | private boolean mAvailable; 62 | 63 | public NetworkReceiver(NetworkChecker checker) { 64 | this.mChecker = checker; 65 | this.mAvailable = mChecker.isAvailable(); 66 | } 67 | 68 | @Override 69 | public void onReceive(Context context, Intent intent) { 70 | mAvailable = mChecker.isAvailable(); 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /config.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | plugins = [library: 'com.android.library', 3 | android: 'com.android.application', 4 | maven : 'com.github.dcendents.android-maven', 5 | bintray: 'com.jfrog.bintray'] 6 | 7 | android = [applicationId : "com.yanzhenjie.kalle.sample", 8 | compileSdkVersion : 28, 9 | buildToolsVersion : "28.0.3", 10 | 11 | librayMinSdkVersion: 9, 12 | sampleMinSdkVersion: 14, 13 | targetSdkVersion : 28, 14 | 15 | versionCode : 7, 16 | versionName : "0.1.7"] 17 | 18 | bintray = [version : "0.1.7", 19 | 20 | siteUrl : 'https://github.com/yanzhenjie/Kalle', 21 | gitUrl : 'https://github.com/yanzhenjie/Kalle.git', 22 | 23 | group : "com.yanzhenjie", 24 | 25 | packaging : 'aar', 26 | name : 'Kalle', 27 | description : 'Synchronous and asynchronous network request framework for Android', 28 | 29 | licenseName : 'The Apache Software License, Version 2.0', 30 | licenseUrl : 'http://www.apache.org/licenses/LICENSE-2.0.txt', 31 | 32 | developerId : 'yanzhenjie', 33 | developerName : 'yanzhenjie', 34 | developerEmail: 'im.yanzhenjie@foxmail.com', 35 | 36 | binrayLibrary : "", 37 | bintrayRepo : "maven", 38 | bintrayUser : 'yolanda', 39 | bintrayLicense: "Apache-2.0"] 40 | 41 | dependencies = [kalle : 'com.yanzhenjie:kalle:0.1.7', 42 | okalle : 'com.yanzhenjie:okalle:0.1.7', 43 | 44 | okhttpUrl : 'com.squareup.okhttp3:okhttp-urlconnection:3.12.3', 45 | 46 | material : 'com.android.support:design:28.0.0', 47 | appCompat : 'com.android.support:appcompat-v7:28.0.0', 48 | cardView : 'com.android.support:cardview-v7:28.0.0', 49 | recyclerView : 'com.android.support:recyclerview-v7:28.0.0', 50 | swipeRecyclerView : 'com.yanzhenjie.recyclerview:support:1.3.1', 51 | 52 | fastjson : 'com.alibaba:fastjson:1.1.70.android', 53 | permission : 'com.yanzhenjie.permission:support:2.0.1', 54 | loading : 'com.yanzhenjie:loading:1.0.2', 55 | album : 'com.yanzhenjie:album:2.1.3', 56 | glide : 'com.github.bumptech.glide:glide:3.8.0', 57 | 58 | butterKnife : 'com.jakewharton:butterknife:8.8.1', 59 | butterKnifeCompiler: 'com.jakewharton:butterknife-compiler:8.8.1'] 60 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/kalle/sample/App.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.sample; 17 | 18 | import android.app.Application; 19 | 20 | import com.squareup.leakcanary.LeakCanary; 21 | import com.yanzhenjie.album.Album; 22 | import com.yanzhenjie.album.AlbumConfig; 23 | import com.yanzhenjie.kalle.Kalle; 24 | import com.yanzhenjie.kalle.KalleConfig; 25 | import com.yanzhenjie.kalle.OkHttpConnectFactory; 26 | import com.yanzhenjie.kalle.connect.BroadcastNetwork; 27 | import com.yanzhenjie.kalle.connect.http.LoggerInterceptor; 28 | import com.yanzhenjie.kalle.cookie.DBCookieStore; 29 | import com.yanzhenjie.kalle.sample.config.AppConfig; 30 | import com.yanzhenjie.kalle.sample.http.JsonConverter; 31 | import com.yanzhenjie.kalle.sample.http.LoginInterceptor; 32 | import com.yanzhenjie.kalle.sample.util.Logger; 33 | import com.yanzhenjie.kalle.sample.util.MediaLoader; 34 | import com.yanzhenjie.kalle.simple.cache.DiskCacheStore; 35 | 36 | /** 37 | * Created by Zhenjie Yan on 2018/3/27. 38 | */ 39 | public class App extends Application { 40 | 41 | private static App _instance; 42 | 43 | @Override 44 | public void onCreate() { 45 | super.onCreate(); 46 | if (_instance == null) { 47 | LeakCanary.install(this); 48 | _instance = this; 49 | 50 | Logger.setEnable(BuildConfig.DEBUG); 51 | kalle(); 52 | album(); 53 | } 54 | } 55 | 56 | private void kalle() { 57 | Kalle.setConfig(KalleConfig.newBuilder() 58 | .connectFactory(OkHttpConnectFactory.newBuilder().build()) 59 | .cookieStore(DBCookieStore.newBuilder(this).build()) 60 | .cacheStore(DiskCacheStore.newBuilder(AppConfig.get().PATH_APP_CACHE).build()) 61 | .network(new BroadcastNetwork(this)) 62 | .addInterceptor(new LoginInterceptor()) 63 | .addInterceptor(new LoggerInterceptor("KalleSample", BuildConfig.DEBUG)) 64 | .converter(new JsonConverter(this)) 65 | .build()); 66 | } 67 | 68 | private void album() { 69 | Album.initialize(AlbumConfig.newBuilder(this).setAlbumLoader(new MediaLoader()).build()); 70 | } 71 | 72 | public static App get() { 73 | return _instance; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_body.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 |Cookie database operation class.
24 | * Created in Dec 18, 2015 6:30:59 PM. 25 | */ 26 | final class SQLHelper extends SQLiteOpenHelper implements Field { 27 | 28 | private static final String DB_COOKIE_NAME = "_kalle_cookies_db.db"; 29 | private static final int DB_COOKIE_VERSION = 3; 30 | 31 | private static final String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 32 | " " + URL + " TEXT, " + NAME + " TEXT, " + VALUE + " TEXT, " + COMMENT + " TEXT, " + COMMENT_URL + " TEXT, " + DISCARD + " TEXT," + 33 | " " + DOMAIN + " TEXT, " + EXPIRY + " INTEGER, " + PATH + " TEXT, " + PORT_LIST + " TEXT, " + SECURE + " TEXT, " + VERSION + " INTEGER)"; 34 | private static final String SQL_CREATE_UNIQUE_INDEX = "CREATE UNIQUE INDEX COOKIE_UNIQUE_INDEX ON COOKIES_TABLE(\"" + NAME + "\", \"" + DOMAIN + "\", \"" + PATH + "\")"; 35 | private static final String SQL_DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME; 36 | 37 | SQLHelper(Context context) { 38 | super(context.getApplicationContext(), DB_COOKIE_NAME, null, DB_COOKIE_VERSION); 39 | } 40 | 41 | @Override 42 | public void onCreate(SQLiteDatabase db) { 43 | db.beginTransaction(); 44 | try { 45 | db.execSQL(SQL_CREATE_TABLE); 46 | db.execSQL(SQL_CREATE_UNIQUE_INDEX); 47 | db.setTransactionSuccessful(); 48 | } finally { 49 | db.endTransaction(); 50 | } 51 | } 52 | 53 | @Override 54 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 55 | if (newVersion != oldVersion) { 56 | db.beginTransaction(); 57 | try { 58 | db.execSQL(SQL_DELETE_TABLE); 59 | db.execSQL(SQL_CREATE_TABLE); 60 | db.execSQL(SQL_CREATE_UNIQUE_INDEX); 61 | db.setTransactionSuccessful(); 62 | } finally { 63 | db.endTransaction(); 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/kalle/sample/app/download/DownloadView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.sample.app.download; 17 | 18 | import android.app.Activity; 19 | import android.view.View; 20 | import android.widget.Button; 21 | import android.widget.ProgressBar; 22 | import android.widget.TextView; 23 | 24 | import com.yanzhenjie.kalle.sample.R; 25 | 26 | import butterknife.BindView; 27 | import butterknife.OnClick; 28 | 29 | /** 30 | * Created by Zhenjie Yan on 2018/3/28. 31 | */ 32 | public class DownloadView extends Contract.DownloadView { 33 | 34 | @BindView(R.id.progress_bar) 35 | ProgressBar mProgressBar; 36 | @BindView(R.id.tv_speed) 37 | TextView mTvProgress; 38 | @BindView(R.id.tv_status) 39 | TextView mTvStatus; 40 | 41 | @BindView(R.id.btn_download) 42 | Button mBtnOperation; 43 | 44 | public DownloadView(Activity activity, Contract.DownloadPresenter presenter) { 45 | super(activity, presenter); 46 | } 47 | 48 | @Override 49 | public void onStart() { 50 | mBtnOperation.setText(R.string.download_stop); 51 | mTvStatus.setText(R.string.download_status_start); 52 | } 53 | 54 | @Override 55 | public void setProgress(int progress, String speed) { 56 | mProgressBar.setProgress(progress); 57 | mTvProgress.setText(speed); 58 | 59 | String progressText = getString(R.string.download_progress, progress); 60 | mTvStatus.setText(progressText); 61 | } 62 | 63 | @Override 64 | public void onFinish() { 65 | mBtnOperation.setText(R.string.download_start); 66 | mTvStatus.setText(R.string.download_status_finish); 67 | } 68 | 69 | @Override 70 | public void onError(String message) { 71 | mBtnOperation.setText(R.string.download_start); 72 | mTvStatus.setText(R.string.download_status_error); 73 | } 74 | 75 | @Override 76 | public void onCancel() { 77 | mBtnOperation.setText(R.string.download_start); 78 | mTvStatus.setText(R.string.download_status_cancel); 79 | } 80 | 81 | @OnClick(R.id.btn_download) 82 | void onViewClick(View v) { 83 | int id = v.getId(); 84 | switch (id) { 85 | case R.id.btn_download: { 86 | getPresenter().tryDownload(); 87 | break; 88 | } 89 | } 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/yanzhenjie/kalle/sample/app/form/FormView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Zhenjie Yan. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.yanzhenjie.kalle.sample.app.form; 17 | 18 | import android.app.Activity; 19 | import android.graphics.Color; 20 | import android.support.v7.widget.GridLayoutManager; 21 | import android.support.v7.widget.RecyclerView; 22 | import android.view.View; 23 | import android.widget.TextView; 24 | 25 | import com.yanzhenjie.album.widget.divider.Api21ItemDivider; 26 | import com.yanzhenjie.kalle.sample.R; 27 | import com.yanzhenjie.kalle.sample.app.form.entity.FileItem; 28 | import com.yanzhenjie.kalle.sample.util.DisplayUtils; 29 | 30 | import java.util.List; 31 | 32 | import butterknife.BindView; 33 | import butterknife.OnClick; 34 | 35 | /** 36 | * Created by Zhenjie Yan on 2018/3/29. 37 | */ 38 | public class FormView extends Contract.FormView { 39 | 40 | @BindView(R.id.tv_status) 41 | TextView mTvStatus; 42 | @BindView(R.id.recycler_view) 43 | RecyclerView mRecyclerView; 44 | 45 | ImageAdapter mAdapter; 46 | 47 | public FormView(Activity activity, Contract.FormPresenter presenter) { 48 | super(activity, presenter); 49 | 50 | mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3)); 51 | int size = DisplayUtils.dip2px(10); 52 | mRecyclerView.addItemDecoration(new Api21ItemDivider(Color.TRANSPARENT, size, size)); 53 | mAdapter = new ImageAdapter(getContext()); 54 | mAdapter.setAddClickListener(new View.OnClickListener() { 55 | @Override 56 | public void onClick(View v) { 57 | getPresenter().addFile(); 58 | } 59 | }); 60 | mRecyclerView.setAdapter(mAdapter); 61 | } 62 | 63 | @Override 64 | public void setFileList(List