parent
ad9f40a665
commit
bc8519720d
@ -0,0 +1,8 @@ |
||||
package com.novel.read |
||||
|
||||
import android.content.Context |
||||
import android.widget.Toast |
||||
|
||||
fun Context.showToast(msg:String){ |
||||
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show() |
||||
} |
@ -1,40 +0,0 @@ |
||||
package com.novel.read.http; |
||||
import androidx.annotation.NonNull; |
||||
|
||||
import com.common_lib.base.utils.SecurityUtils; |
||||
import com.novel.read.constants.Constant; |
||||
import com.novel.read.utlis.SpUtil; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import okhttp3.Interceptor; |
||||
import okhttp3.Request; |
||||
import okhttp3.Response; |
||||
|
||||
/** |
||||
* Created by zlj on 2019/3/1. |
||||
*/ |
||||
public class CommonHeadersInterceptor implements Interceptor { |
||||
|
||||
@NotNull |
||||
@Override |
||||
public Response intercept(@NonNull Interceptor.Chain chain) throws IOException { |
||||
Request request = chain.request(); |
||||
Request.Builder builder = request.newBuilder(); |
||||
|
||||
String authKey = "Android"; |
||||
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000); |
||||
String uid = SpUtil.getStringValue(Constant.Uid, "1"); |
||||
builder.addHeader("Content-Type", "application/json"); |
||||
builder.addHeader("UID", uid); |
||||
builder.addHeader("AUTHKEY", authKey); |
||||
builder.addHeader("TIMESTAMP", timeStamp); |
||||
|
||||
builder.addHeader("SIGN", SecurityUtils.getInstance().MD5Decode(authKey + timeStamp).toUpperCase()); |
||||
|
||||
|
||||
return chain.proceed(builder.build()); |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
package com.novel.read.http |
||||
|
||||
import com.common_lib.base.utils.SecurityUtils |
||||
import com.novel.read.constants.Constant |
||||
import com.novel.read.utlis.SpUtil |
||||
|
||||
import java.io.IOException |
||||
|
||||
import okhttp3.Interceptor |
||||
import okhttp3.Request |
||||
import okhttp3.Response |
||||
|
||||
/** |
||||
* Created by zlj on 2019/3/1. |
||||
*/ |
||||
class CommonHeadersInterceptor : Interceptor { |
||||
|
||||
@Throws(IOException::class) |
||||
override fun intercept(chain: Interceptor.Chain): Response { |
||||
val request = chain.request() |
||||
val builder = request.newBuilder() |
||||
|
||||
val authKey = "Android" |
||||
val timeStamp = (System.currentTimeMillis() / 1000).toString() |
||||
val uid = SpUtil.getStringValue(Constant.Uid, "1") |
||||
builder.addHeader("Content-Type", "application/json") |
||||
builder.addHeader("UID", uid) |
||||
builder.addHeader("AUTHKEY", authKey) |
||||
builder.addHeader("TIMESTAMP", timeStamp) |
||||
|
||||
builder.addHeader("SIGN", SecurityUtils.getInstance().MD5Decode(authKey + timeStamp).toUpperCase()) |
||||
|
||||
return chain.proceed(builder.build()) |
||||
} |
||||
} |
@ -1,54 +0,0 @@ |
||||
package com.novel.read.http; |
||||
|
||||
import com.google.gson.Gson; |
||||
import com.google.gson.GsonBuilder; |
||||
import com.mango.mangolib.http.GsonUTCdateAdapter; |
||||
import com.mango.mangolib.http.MyRequestType; |
||||
import com.mango.mangolib.http.ResponseConverterFactory; |
||||
|
||||
import java.util.Date; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
import retrofit2.Retrofit; |
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; |
||||
import retrofit2.converter.gson.GsonConverterFactory; |
||||
|
||||
|
||||
public class ServiceGenerator { |
||||
private static final String API_BASE_URL_TEXT = "http://novel.duoduvip.com/"; |
||||
|
||||
private static Gson gson = new GsonBuilder() |
||||
.setPrettyPrinting() |
||||
.registerTypeAdapter(Date.class, new GsonUTCdateAdapter()).create(); |
||||
|
||||
|
||||
private static Retrofit.Builder builderTEXT = new Retrofit.Builder() |
||||
.baseUrl(API_BASE_URL_TEXT) |
||||
.client(getOkHttp()) |
||||
.addConverterFactory(ResponseConverterFactory.Companion.create()) |
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
||||
.addConverterFactory(GsonConverterFactory.create(gson)); |
||||
|
||||
private static OkHttpClient getOkHttp() { |
||||
return new OkHttpClient() |
||||
.newBuilder() |
||||
.addInterceptor(new CommonHeadersInterceptor()) |
||||
.connectTimeout(15, TimeUnit.SECONDS) |
||||
.writeTimeout(15, TimeUnit.SECONDS) |
||||
.readTimeout(15, TimeUnit.SECONDS) |
||||
.build(); |
||||
} |
||||
|
||||
private ServiceGenerator() { |
||||
} |
||||
|
||||
public static <S> S createService(Class<S> serviceClass, final MyRequestType type) { |
||||
Retrofit retrofit = builderTEXT.build(); |
||||
return retrofit.create(serviceClass); |
||||
} |
||||
|
||||
public static String formatResponse(Object obj) { |
||||
return gson.toJson(obj); |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
package com.novel.read.http |
||||
|
||||
import com.google.gson.GsonBuilder |
||||
import com.mango.mangolib.http.GsonUTCdateAdapter |
||||
import com.mango.mangolib.http.MyRequestType |
||||
import com.mango.mangolib.http.ResponseConverterFactory |
||||
|
||||
import java.util.Date |
||||
import java.util.concurrent.TimeUnit |
||||
|
||||
import okhttp3.OkHttpClient |
||||
import retrofit2.Retrofit |
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory |
||||
import retrofit2.converter.gson.GsonConverterFactory |
||||
|
||||
|
||||
object ServiceGenerator { |
||||
private const val API_BASE_URL_TEXT = "http://novel.duoduvip.com/" |
||||
|
||||
private val gson = GsonBuilder() |
||||
.setPrettyPrinting() |
||||
.registerTypeAdapter(Date::class.java, GsonUTCdateAdapter()).create() |
||||
|
||||
|
||||
private val builderTEXT = Retrofit.Builder() |
||||
.baseUrl(API_BASE_URL_TEXT) |
||||
.client(okHttp) |
||||
.addConverterFactory(ResponseConverterFactory.create()) |
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
||||
.addConverterFactory(GsonConverterFactory.create(gson)) |
||||
|
||||
private val okHttp: OkHttpClient |
||||
get() = OkHttpClient() |
||||
.newBuilder() |
||||
.addInterceptor(CommonHeadersInterceptor()) |
||||
.connectTimeout(15, TimeUnit.SECONDS) |
||||
.writeTimeout(15, TimeUnit.SECONDS) |
||||
.readTimeout(15, TimeUnit.SECONDS) |
||||
.build() |
||||
|
||||
fun <S> createService(serviceClass: Class<S>, type: MyRequestType): S { |
||||
val retrofit = builderTEXT.build() |
||||
return retrofit.create(serviceClass) |
||||
} |
||||
|
||||
fun formatResponse(obj: Any): String { |
||||
return gson.toJson(obj) |
||||
} |
||||
} |
@ -1,30 +0,0 @@ |
||||
|
||||
package com.novel.read.utlis; |
||||
|
||||
import android.graphics.Color; |
||||
|
||||
|
||||
import com.novel.read.constants.Constant; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
*/ |
||||
public class TagColor { |
||||
|
||||
public int borderColor = Color.parseColor("#49C120"); |
||||
public int backgroundColor = Color.parseColor("#49C120"); |
||||
public int textColor = Color.WHITE; |
||||
|
||||
public static List<TagColor> getRandomColors(int size) { |
||||
|
||||
List<TagColor> list = new ArrayList<>(); |
||||
for (int i = 0; i < size; i++) { |
||||
TagColor color = new TagColor(); |
||||
color.borderColor = color.backgroundColor = Constant.tagColors[i % Constant.tagColors.length]; |
||||
list.add(color); |
||||
} |
||||
return list; |
||||
} |
||||
} |
@ -1,102 +0,0 @@ |
||||
package com.novel.read.utlis; |
||||
|
||||
import android.content.Context; |
||||
import android.os.Looper; |
||||
import android.os.SystemClock; |
||||
import android.widget.Toast; |
||||
|
||||
public class ToastUtil { |
||||
|
||||
private static Toast toast; |
||||
/** |
||||
* 当前工具类的总开关 |
||||
*/ |
||||
private static boolean isAllowShow = true; |
||||
|
||||
/** |
||||
* 功能:开关(isAllowShow开启后,此方法功能可用) |
||||
* |
||||
* @param context |
||||
* @param msg |
||||
*/ |
||||
public static void show(Context context, String msg) { |
||||
if (!isAllowShow) { |
||||
return; |
||||
} |
||||
// 开关开启后才往下执行
|
||||
if (toast == null) { |
||||
if (context == null) { |
||||
return; |
||||
} |
||||
toast = Toast.makeText(context, msg, 0); |
||||
} else { |
||||
toast.setText(msg); |
||||
} |
||||
toast.show(); |
||||
} |
||||
|
||||
public static void show(Context context, int msg) { |
||||
if (!isAllowShow) { |
||||
return; |
||||
} |
||||
// 开关开启后才往下执行
|
||||
if (toast == null) { |
||||
if (context == null) { |
||||
return; |
||||
} |
||||
toast = Toast.makeText(context, msg, 0); |
||||
} else { |
||||
toast.setText(msg); |
||||
} |
||||
toast.show(); |
||||
} |
||||
|
||||
/** |
||||
* 功能:上面方法的增强版,当只需要显示部分Toast提示时,<br/> |
||||
* 总开关关闭,传入true,显示;<br/> |
||||
* 总开关开启,传入false,关闭 |
||||
* |
||||
* @param context |
||||
* @param msg |
||||
* @param flag 开关状态 |
||||
*/ |
||||
public static void show(Context context, String msg, boolean flag) { |
||||
if (!flag) { |
||||
return; |
||||
} |
||||
if (!isAllowShow && !flag) { |
||||
return; |
||||
} |
||||
// 开关开启后才往下执行
|
||||
if (toast == null) { |
||||
toast = Toast.makeText(context, msg, 0); |
||||
} else { |
||||
toast.setText(msg); |
||||
} |
||||
toast.show(); |
||||
} |
||||
|
||||
/** |
||||
* 功能:一直显示Toast |
||||
* |
||||
* @param context |
||||
* @param msg |
||||
* @param flag |
||||
*/ |
||||
public static void alwaysShow(final Context context, final String msg, final boolean flag) { |
||||
new Thread() { |
||||
public void run() { |
||||
while (true) { |
||||
Looper.prepare(); |
||||
SystemClock.sleep(3000); |
||||
// show(context, msg, flag);
|
||||
Toast.makeText(context, msg, 0).show(); |
||||
Looper.loop(); |
||||
} |
||||
} |
||||
|
||||
; |
||||
}.start(); |
||||
|
||||
} |
||||
} |
@ -1,51 +0,0 @@ |
||||
package com.novel.read.utlis; |
||||
|
||||
import android.content.Context; |
||||
import android.content.pm.PackageInfo; |
||||
import android.content.pm.PackageManager; |
||||
|
||||
public class VersionUtil { |
||||
|
||||
public static int getPackageCode(Context context) { |
||||
PackageManager manager = context.getPackageManager(); |
||||
int code = 0; |
||||
try { |
||||
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); |
||||
code = info.versionCode; |
||||
} catch (PackageManager.NameNotFoundException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return code; |
||||
} |
||||
public static String getPackageName(Context context) { |
||||
PackageManager manager = context.getPackageManager(); |
||||
String name = null; |
||||
try { |
||||
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); |
||||
name = info.versionName; |
||||
} catch (PackageManager.NameNotFoundException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
return name; |
||||
} |
||||
|
||||
/** |
||||
* [获取应用程序版本名称信息] |
||||
* @param context |
||||
* @return 当前应用的版本名称 |
||||
*/ |
||||
public static String getPackage(Context context) { |
||||
try { |
||||
PackageManager packageManager = context.getPackageManager(); |
||||
PackageInfo packageInfo = packageManager.getPackageInfo( |
||||
context.getPackageName(), 0); |
||||
return packageInfo.packageName; |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,55 @@ |
||||
package com.novel.read.utlis |
||||
|
||||
import android.content.Context |
||||
import android.content.pm.PackageInfo |
||||
import android.content.pm.PackageManager |
||||
|
||||
object VersionUtil { |
||||
|
||||
fun getPackageCode(context: Context): Int { |
||||
val manager = context.packageManager |
||||
var code = 0 |
||||
try { |
||||
val info = manager.getPackageInfo(context.packageName, 0) |
||||
code = info.versionCode |
||||
} catch (e: PackageManager.NameNotFoundException) { |
||||
e.printStackTrace() |
||||
} |
||||
|
||||
return code |
||||
} |
||||
|
||||
fun getPackageName(context: Context): String? { |
||||
val manager = context.packageManager |
||||
var name: String? = null |
||||
try { |
||||
val info = manager.getPackageInfo(context.packageName, 0) |
||||
name = info.versionName |
||||
} catch (e: PackageManager.NameNotFoundException) { |
||||
e.printStackTrace() |
||||
} |
||||
|
||||
return name |
||||
} |
||||
|
||||
/** |
||||
* [获取应用程序版本名称信息] |
||||
* @param context |
||||
* @return 当前应用的版本名称 |
||||
*/ |
||||
fun getPackage(context: Context): String? { |
||||
try { |
||||
val packageManager = context.packageManager |
||||
val packageInfo = packageManager.getPackageInfo( |
||||
context.packageName, 0 |
||||
) |
||||
return packageInfo.packageName |
||||
} catch (e: Exception) { |
||||
e.printStackTrace() |
||||
} |
||||
|
||||
return null |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue