Commit 14a60b11 authored by sheteng's avatar sheteng

登录

parent 7819d3c3
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
<entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/fragment_login_quick.xml" value="0.2006340579710145" /> <entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/fragment_login_quick.xml" value="0.2006340579710145" />
<entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/fragment_mine.xml" value="0.3328125" /> <entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/fragment_mine.xml" value="0.3328125" />
<entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/fragment_quick.xml" value="0.21829710144927536" /> <entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/fragment_quick.xml" value="0.21829710144927536" />
<entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/fragment_webview.xml" value="0.2708333333333333" />
<entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/main_activity.xml" value="0.23505434782608695" /> <entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/main_activity.xml" value="0.23505434782608695" />
<entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/main_fragment.xml" value="0.23505434782608695" /> <entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/layout/main_fragment.xml" value="0.23505434782608695" />
<entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/menu/bottom_nav_menu.xml" value="0.3328125" /> <entry key="..\:/ccwangluo/cc_quickly/app/src/main/res/menu/bottom_nav_menu.xml" value="0.3328125" />
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
android:name=".App" android:name=".App"
android:allowBackup="true" android:allowBackup="true"
android:usesCleartextTraffic="true" android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/tt_logo"
android:label="@string/app_name" android:label="@string/app_name"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
......
package com.ccwangluo.accelerator.model package com.ccwangluo.accelerator.model
data class BaseModel<T>( data class BaseModel<out M>(
val code:Int, val code:Int,
val error : String?, val error : String?,
val data:T? val data:M?
) )
...@@ -3,6 +3,7 @@ package com.ccwangluo.cc_quickly.ui ...@@ -3,6 +3,7 @@ package com.ccwangluo.cc_quickly.ui
import android.Manifest import android.Manifest
import android.os.Bundle import android.os.Bundle
import com.ccwangluo.accelerator.ui.BottomNavigationFragment import com.ccwangluo.accelerator.ui.BottomNavigationFragment
import com.ccwangluo.accelerator.utils.LoginUtils
import com.ccwangluo.accelerator.utils.PhoneUtils import com.ccwangluo.accelerator.utils.PhoneUtils
import com.karumi.dexter.Dexter import com.karumi.dexter.Dexter
import com.karumi.dexter.PermissionToken import com.karumi.dexter.PermissionToken
...@@ -48,6 +49,11 @@ class MainActivity() : XPageActivity() { ...@@ -48,6 +49,11 @@ class MainActivity() : XPageActivity() {
PhoneUtils.getPhoneInfo() PhoneUtils.getPhoneInfo()
} }
override fun onDestroy() {
super.onDestroy()
LoginUtils.clearToken()
}
} }
\ No newline at end of file
...@@ -14,7 +14,6 @@ import com.xuexiang.xpage.base.XPageFragment ...@@ -14,7 +14,6 @@ import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xpage.utils.TitleBar import com.xuexiang.xpage.utils.TitleBar
class MineFragment : XPageFragment() { class MineFragment : XPageFragment() {
private var _binding: FragmentMineBinding? = null private var _binding: FragmentMineBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
...@@ -23,6 +22,7 @@ class MineFragment : XPageFragment() { ...@@ -23,6 +22,7 @@ class MineFragment : XPageFragment() {
//不使用@Page标注的一定要注意覆盖这个方法 //不使用@Page标注的一定要注意覆盖这个方法
return null return null
} }
override fun inflateView(inflater: LayoutInflater?, container: ViewGroup?): View { override fun inflateView(inflater: LayoutInflater?, container: ViewGroup?): View {
_binding = FragmentMineBinding.inflate(inflater!!, container, false) _binding = FragmentMineBinding.inflate(inflater!!, container, false)
return binding.root return binding.root
...@@ -30,20 +30,21 @@ class MineFragment : XPageFragment() { ...@@ -30,20 +30,21 @@ class MineFragment : XPageFragment() {
override fun initViews() { override fun initViews() {
val agentWeb = val agentWeb =
createAgentWeb(this, binding.commonWebview, HttpConfig.UI_MAIN_URL) createAgentWeb(this, binding.mineWebview, HttpConfig.UI_MAIN_URL)
agentWeb.getJsInterfaceHolder().addJavaObject("android", AndroidInterface(this)) agentWeb.getJsInterfaceHolder().addJavaObject("android", AndroidInterface(this, agentWeb))
} }
override fun initListeners() { override fun initListeners() {
} }
companion object{ companion object {
fun newInstance(): MineFragment { fun newInstance(): MineFragment {
val fragment = MineFragment() val fragment = MineFragment()
return fragment return fragment
} }
} }
fun createAgentWeb(fragment: Fragment?, viewGroup: ViewGroup, url: String): AgentWeb { fun createAgentWeb(fragment: Fragment?, viewGroup: ViewGroup, url: String): AgentWeb {
return AgentWeb.with(fragment!!) return AgentWeb.with(fragment!!)
.setAgentWebParent( .setAgentWebParent(
......
...@@ -38,7 +38,9 @@ class QuickFragment : XPageFragment() { ...@@ -38,7 +38,9 @@ class QuickFragment : XPageFragment() {
} }
binding.btnQuick.setOnClickListener { binding.btnQuick.setOnClickListener {
if (!LoginUtils.hasToken()) { if (!LoginUtils.hasToken()) {
LoginUtils.login(this) LoginUtils.login(this,{
})
} else { } else {
} }
......
...@@ -3,11 +3,9 @@ package com.ccwangluo.accelerator.ui.login ...@@ -3,11 +3,9 @@ package com.ccwangluo.accelerator.ui.login
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.ccwangluo.accelerator.model.BaseModel
import com.ccwangluo.accelerator.model.LoginRes import com.ccwangluo.accelerator.model.LoginRes
import com.ccwangluo.accelerator.utils.LoginUtils import com.ccwangluo.accelerator.utils.LoginUtils
import com.ccwangluo.accelerator.utils.http.HttpGo import com.ccwangluo.accelerator.utils.http.HttpGo
import com.google.gson.Gson
import com.hjq.toast.ToastUtils import com.hjq.toast.ToastUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
...@@ -22,7 +20,7 @@ class LoginViewModel : ViewModel() { ...@@ -22,7 +20,7 @@ class LoginViewModel : ViewModel() {
map.put("phone", phone) map.put("phone", phone)
map.put("verifyCode",authCdoe) map.put("verifyCode",authCdoe)
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val res = HttpGo.postSync<BaseModel<LoginRes>>("/user/verifyCode/login", map) val res = HttpGo.postSync<LoginRes>("/user/verifyCode/login", map)
res?.let { res?.let {
if (it.code == 200) { if (it.code == 200) {
LoginUtils.token = it.data?.token LoginUtils.token = it.data?.token
...@@ -35,13 +33,11 @@ class LoginViewModel : ViewModel() { ...@@ -35,13 +33,11 @@ class LoginViewModel : ViewModel() {
} }
} }
inline fun <reified T:Any> T.toJson():String = Gson().toJson(this,T::class.java)
fun getAuthCode(phone: String) { fun getAuthCode(phone: String) {
val map = mutableMapOf<String, String>() val map = mutableMapOf<String, String>()
map.put("phone", phone) map.put("phone", phone)
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val res = HttpGo.postSync<BaseModel<Boolean>>("/user/sendSms", map) val res = HttpGo.postSync<Boolean>("/user/sendSms", map)
res?.let { res?.let {
if (it.code == 200) { if (it.code == 200) {
sendCodeState.postValue(true) sendCodeState.postValue(true)
......
...@@ -69,6 +69,7 @@ class PhoneLoginFragment : XPageFragment() { ...@@ -69,6 +69,7 @@ class PhoneLoginFragment : XPageFragment() {
binding.login.isEnabled = false binding.login.isEnabled = false
} }
} }
loginViewModel.sendCodeState.observe(this, { loginViewModel.sendCodeState.observe(this, {
if (it) { if (it) {
sendVerifyCode() sendVerifyCode()
......
package com.ccwangluo.accelerator.ui.web package com.ccwangluo.accelerator.ui.web
import android.webkit.JavascriptInterface import android.webkit.JavascriptInterface
import androidx.core.content.ContextCompat.checkSelfPermission
import com.ccwangluo.accelerator.utils.LoginUtils
import com.ccwangluo.accelerator.utils.SysUtils
import com.ccwangluo.cc_quickly.utils.XToastUtils import com.ccwangluo.cc_quickly.utils.XToastUtils
import com.just.agentweb.core.AgentWeb
import com.xuexiang.xpage.base.XPageFragment import com.xuexiang.xpage.base.XPageFragment
/** /**
* js注入的方法 * js注入的方法
*/ */
class AndroidInterface(val xPageFragment: XPageFragment) { class AndroidInterface(val xPageFragment: XPageFragment, val agentWeb: AgentWeb) {
@JavascriptInterface @JavascriptInterface
fun callAndroid(msg: String) { fun callAndroid(msg: String) {
...@@ -19,7 +24,44 @@ class AndroidInterface(val xPageFragment: XPageFragment) { ...@@ -19,7 +24,44 @@ class AndroidInterface(val xPageFragment: XPageFragment) {
xPageFragment.popToBack() xPageFragment.popToBack()
} }
fun getPermission(key: String) { @JavascriptInterface
fun open(pageName: String) {
xPageFragment.openPage(pageName)
}
@JavascriptInterface
fun login(callback: String) {
LoginUtils.login(xPageFragment, {
if (it) {
agentWeb.getJsAccessEntrace()
.quickCallJs(callback, LoginUtils.token)
}
})
}
/**
* 检查权限
*/
@JavascriptInterface
fun getPermissionState(key: String, callback: String) {
xPageFragment.context?.let {
val hasPermission: Boolean = if (checkSelfPermission(it, key) > 0) true else false
agentWeb.getJsAccessEntrace()
.quickCallJs(callback,key, hasPermission.toString())
}
}
/**
* 检查安装包
*/
@JavascriptInterface
fun getApkState(packageName: String, callback: String) {
xPageFragment.context?.let {
val packageInstall = SysUtils.isPackageInstall(it, packageName)
agentWeb.getJsAccessEntrace()
.quickCallJs(callback, packageName, packageInstall.toString())
}
} }
} }
\ No newline at end of file
package com.ccwangluo.accelerator.ui.web
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.ccwangluo.accelerator.utils.http.HttpConfig
import com.ccwangluo.cc_quickly.databinding.FragmentWebviewBinding
import com.just.agentweb.core.AgentWeb
import com.just.agentweb.core.client.DefaultWebClient
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.base.XPageFragment
@Page(name = "webview")
class CommonWebViewFragment : XPageFragment() {
lateinit var binding: FragmentWebviewBinding
override fun inflateView(inflater: LayoutInflater?, container: ViewGroup?): View {
binding = FragmentWebviewBinding.inflate(inflater!!, container, false)
return binding.root
}
override fun initViews() {
val agentWeb =
createAgentWeb(this, binding.commonWebview, HttpConfig.UI_MAIN_URL)
agentWeb.getJsInterfaceHolder().addJavaObject("android", AndroidInterface(this, agentWeb))
}
fun createAgentWeb(fragment: Fragment?, viewGroup: ViewGroup, url: String): AgentWeb {
return AgentWeb.with(fragment!!)
.setAgentWebParent(
viewGroup!!,
-1,
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
)
.useDefaultIndicator(-1, 3)
.setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
.createAgentWeb()
.ready()
.go(url)
}
override fun initListeners() {
}
}
\ No newline at end of file
...@@ -2,27 +2,43 @@ package com.ccwangluo.accelerator.utils ...@@ -2,27 +2,43 @@ package com.ccwangluo.accelerator.utils
import android.content.Context import android.content.Context
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.ccwangluo.accelerator.model.BaseModel
import com.ccwangluo.accelerator.model.LoginRes import com.ccwangluo.accelerator.model.LoginRes
import com.ccwangluo.accelerator.utils.http.HttpGo import com.ccwangluo.accelerator.utils.http.HttpGo
import com.ccwangluo.cc_quickly.ui.login.PhoneLoginFragment import com.ccwangluo.cc_quickly.ui.login.PhoneLoginFragment
import com.chuanglan.shanyan_sdk.OneKeyLoginManager
import com.hjq.toast.ToastUtils
import com.tencent.mmkv.MMKV import com.tencent.mmkv.MMKV
import com.xuexiang.xpage.base.XPageFragment import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xpage.core.PageOption import com.xuexiang.xpage.core.PageOption
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
object LoginUtils { object LoginUtils {
fun login(xPageFragment: XPageFragment){ fun login(xPageFragment: XPageFragment, callBack: (Boolean) -> Unit) {
PhoneUtils.openLoginAuth(xPageFragment) { b, s -> PhoneUtils.openLoginAuth(xPageFragment) { b, s ->
if (b) { if (b) {
val map = mutableMapOf<String, String>() val map = mutableMapOf<String, String>()
map.put("token", s) map.put("token", s)
xPageFragment.lifecycleScope.launch(Dispatchers.IO) { xPageFragment.lifecycleScope.launch(Dispatchers.IO) {
val res = HttpGo.postSync<BaseModel<LoginRes>>("/user/flash/login", map) val res = HttpGo.postSync<LoginRes>("/user/flash/login", map)
res?.let { res?.let {
println(it) if (it.code == 200) {
token = it.data?.token
ToastUtils.show("登录成功")
OneKeyLoginManager.getInstance().finishAuthActivity()
withContext(Dispatchers.Main){
callBack(true)
}
} else {
it.error?.let {
ToastUtils.show(it)
}
withContext(Dispatchers.Main){
callBack(false)
}
}
} }
} }
} else { } else {
......
package com.ccwangluo.accelerator.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Created by wangguifa on 2017/5/22
* 获取网络状态工具类
*/
public class NetUtils {
public static final int NETWORK_NONE = 0; // 没有网络连接
public static final int NETWORK_WIFI = 1; // wifi连接
public static final int NETWORK_2G = 2; // 2G
public static final int NETWORK_3G = 3; // 3G
public static final int NETWORK_4G = 4; // 4G
public static final int NETWORK_MOBILE = 5; // 手机流量
/**
* 获取运营商名字
*
* @param context context
* @return int
*/
public static String getOperatorName(Context context) {
/*
* getSimOperatorName()就可以直接获取到运营商的名字
* 也可以使用IMSI获取,getSimOperator(),然后根据返回值判断,例如"46000"为移动
* IMSI相关链接:http://baike.baidu.com/item/imsi
*/
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// getSimOperatorName就可以直接获取到运营商的名字
return telephonyManager.getSimOperatorName();
}
/**
* 获取当前网络连接的类型
*
* @param context context
* @return int
*/
public static int getNetworkState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // 获取网络服务
if (null == connManager) { // 为空则认为无网络
return NETWORK_NONE;
}
// 获取网络类型,如果为空,返回无网络
NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
return NETWORK_NONE;
}
// 判断是否为WIFI
NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (null != wifiInfo) {
NetworkInfo.State state = wifiInfo.getState();
if (null != state) {
if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
return NETWORK_WIFI;
}
}
}
// 若不是WIFI,则去判断是2G、3G、4G网
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = telephonyManager.getNetworkType();
switch (networkType) {
/*
GPRS : 2G(2.5) General Packet Radia Service 114kbps
EDGE : 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
UMTS : 3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
CDMA : 2G 电信 Code Division Multiple Access 码分多址
EVDO_0 : 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
EVDO_A : 3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
1xRTT : 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
HSDPA : 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
HSUPA : 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
HSPA : 3G (分HSDPA,HSUPA) High Speed Packet Access
IDEN : 2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
EVDO_B : 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
LTE : 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G
EHRPD : 3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
HSPAP : 3G HSPAP 比 HSDPA 快些
*/
// 2G网络
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return NETWORK_2G;
// 3G网络
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return NETWORK_3G;
// 4G网络
case TelephonyManager.NETWORK_TYPE_LTE:
return NETWORK_4G;
default:
return NETWORK_MOBILE;
}
}
/**
* 判断网络是否连接
*
* @param context context
* @return true/false
*/
public static boolean isNetConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
/**
* 判断是否wifi连接
*
* @param context context
* @return true/false
*/
public static synchronized boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
int networkInfoType = networkInfo.getType();
if (networkInfoType == ConnectivityManager.TYPE_WIFI || networkInfoType == ConnectivityManager.TYPE_ETHERNET) {
return networkInfo.isConnected();
}
}
}
return false;
}
}
\ No newline at end of file
...@@ -128,9 +128,9 @@ object PhoneUtils { ...@@ -128,9 +128,9 @@ object PhoneUtils {
// .addCustomView( // .addCustomView(
// mOtherTv2, true, false, null // mOtherTv2, true, false, null
// ) // )
.setNumFieldOffsetBottomY(378) .setNumFieldOffsetBottomY(330)
.setNumberSize(25) .setNumberSize(25)
.setLogBtnOffsetBottomY(270) .setLogBtnOffsetBottomY(230)
.setLogBtnWidth(AbScreenUtils.getScreenWidth(context, true) - 60) .setLogBtnWidth(AbScreenUtils.getScreenWidth(context, true) - 60)
.setLogBtnText("登录") .setLogBtnText("登录")
.setLogBtnTextSize(16) .setLogBtnTextSize(16)
...@@ -144,7 +144,7 @@ object PhoneUtils { ...@@ -144,7 +144,7 @@ object PhoneUtils {
.setCheckBoxWH(20, 20) .setCheckBoxWH(20, 20)
.setPrivacyOffsetX(20) .setPrivacyOffsetX(20)
.setcheckBoxOffsetXY(0, 5) .setcheckBoxOffsetXY(0, 5)
.setPrivacyOffsetBottomY(194) .setPrivacyOffsetBottomY(174)
.setPrivacyOffsetGravityLeft(true) .setPrivacyOffsetGravityLeft(true)
.setPrivacyTextSize(12) .setPrivacyTextSize(12)
.setOperatorPrivacyAtLast(true) .setOperatorPrivacyAtLast(true)
......
package com.ccwangluo.accelerator.utils
import android.content.Context
import android.content.pm.PackageManager
import android.net.ConnectivityManager
object SysUtils {
fun getConnectivityStatus(context: Context): Boolean {
val cm = context
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = cm.activeNetworkInfo
if (null != activeNetwork) {
if (activeNetwork.type == ConnectivityManager.TYPE_WIFI) return true
if (activeNetwork.type == ConnectivityManager.TYPE_MOBILE) return true
}
return false
}
/**
* 正在使用的网络
*/
fun getActiveNetWork(context: Context): String {
val networkState = NetUtils.getNetworkState(context)
return ""
}
/**
* 判断包是否安装
*/
fun isPackageInstall(context: Context, packageName: String): Boolean {
val packageManager: PackageManager = context.getPackageManager()
val packageInfos = packageManager.getInstalledPackages(0)
val packageNames: MutableList<String> = ArrayList()
if (packageInfos != null) {
for (i in packageInfos.indices) {
val packName = packageInfos[i].packageName
packageNames.add(packName)
}
}
return packageNames.contains(packageName)
}
}
\ No newline at end of file
package com.ccwangluo.accelerator.utils.http package com.ccwangluo.accelerator.utils.http
import com.ccwangluo.accelerator.model.BaseModel
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hjq.toast.ToastUtils import com.hjq.toast.ToastUtils
import okhttp3.* import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
...@@ -81,23 +83,26 @@ object HttpGo { ...@@ -81,23 +83,26 @@ object HttpGo {
/** /**
* 同步请求 * 同步请求
*/ */
inline fun <reified T> postSync(url: String, param: MutableMap<String, String>): T? { inline fun <reified T> postSync(url: String, param: MutableMap<String, String>): BaseModel<T>? {
val body: RequestBody = gson.toJson(param).toString().toRequestBody(jsonType) try {
val request: Request = Request.Builder() val body: RequestBody = gson.toJson(param).toString().toRequestBody(jsonType)
.url(HttpConfig.baseUrl + url) val request: Request = Request.Builder()
.post(body) .url(HttpConfig.baseUrl + url)
.build() .post(body)
val call = okHttpClient.newCall(request) .build()
val response: Response = call.execute() val call = okHttpClient.newCall(request)
println(response) val response: Response = call.execute()
if (response.isSuccessful) { println(response)
val string = response.body?.string() if (response.isSuccessful) {
println(string) val string = response.body?.string()
return gson.fromJson(string, T::class.java) println(string)
} else { val type = object : TypeToken<BaseModel<T>>() {}.type
return gson.fromJson(string, type)
}
} catch (e : java.lang.Exception){
ToastUtils.show("网络错误") ToastUtils.show("网络错误")
return null
} }
return null
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<FrameLayout <FrameLayout
android:id="@+id/common_webview" android:id="@+id/mine_webview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
</LinearLayout> </LinearLayout>
\ No newline at end of file
...@@ -24,6 +24,16 @@ ...@@ -24,6 +24,16 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="36dp"> android:layout_marginTop="36dp">
<TextView
android:id="@+id/quick_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加速中 00:00:21"
android:layout_centerInParent="true"
android:textColor="#FFFFFFFF"
android:textSize="16sp"
/>
<ImageView <ImageView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/common_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment