Compare commits
2 Commits
504d58d636
...
9ee5468010
Author | SHA1 | Date |
---|---|---|
lijie | 9ee5468010 | |
lijie | 3678ad5d0c |
|
@ -1,17 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="deploymentTargetDropDown">
|
||||
<runningDeviceTargetSelectedWithDropDown>
|
||||
<Target>
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="SERIAL_NUMBER" />
|
||||
<value value="R5CT20CJY5V" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2023-07-27T08:21:37.522141400Z" />
|
||||
</component>
|
||||
</project>
|
|
@ -53,7 +53,6 @@
|
|||
android:exported="false"
|
||||
android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />
|
||||
|
||||
|
||||
<activity
|
||||
android:name=".activity.LoginActivity"
|
||||
android:exported="true"
|
||||
|
|
|
@ -13,6 +13,7 @@ import com.lsxiao.apollo.core.contract.ApolloBinder
|
|||
open class BaseActivity : AppCompatActivity() {
|
||||
private var mBaseLoadingDialog: BaseLoadingDialog? = null
|
||||
private var mApolloBinder: ApolloBinder? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
mBaseLoadingDialog = BaseLoadingDialog(this)
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
package com.cmx.wanhui.activity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.cmx.wanhui.databinding.ActivityLoginBinding;
|
||||
import com.cmx.wanhui.model.LoginBean;
|
||||
import com.cmx.wanhui.model.LoginRes;
|
||||
import com.cmx.wanhui.retrofit.RetrofitAPIManager;
|
||||
import com.cmx.wanhui.utils.MyUtils;
|
||||
import com.cmx.wanhui.utils.SpUtils;
|
||||
|
||||
import es.dmoral.toasty.Toasty;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class LoginActivity extends BaseActivity {
|
||||
|
||||
private ActivityLoginBinding binding;
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
binding = ActivityLoginBinding.inflate(getLayoutInflater());
|
||||
setContentView(binding.getRoot());
|
||||
MyUtils.setFullscreen(this, true, true);
|
||||
MyUtils.setAndroidNativeLightStatusBar(this, true);
|
||||
binding.btnLogin.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (TextUtils.isEmpty(binding.edtUsername.getText().toString().trim())) {
|
||||
Toasty.info(LoginActivity.this, "请输入用户名").show();
|
||||
return;
|
||||
}
|
||||
if (TextUtils.isEmpty(binding.edtPassword.getText().toString().trim())) {
|
||||
Toasty.info(LoginActivity.this, "请输入密码").show();
|
||||
return;
|
||||
}
|
||||
toLogin();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
private void toLogin() {
|
||||
showDialog();
|
||||
LoginBean LoginBean = new LoginBean();
|
||||
LoginBean.setUsername(binding.edtUsername.getText().toString().trim());
|
||||
LoginBean.setPassword(binding.edtPassword.getText().toString().trim());
|
||||
Call<LoginRes> call = RetrofitAPIManager.provideClientApi().login(LoginBean);
|
||||
call.enqueue(new Callback<LoginRes>() {
|
||||
@Override
|
||||
public void onResponse(Call<LoginRes> call, Response<LoginRes> response) {
|
||||
disDialog();
|
||||
if (response.isSuccessful() && response.body().getCode() == 0) {
|
||||
SpUtils.putString(LoginActivity.this, "token", response.body().getAccess_token());
|
||||
SpUtils.putString(LoginActivity.this, "admin", response.body().is_admin());
|
||||
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
} else {
|
||||
Toasty.error(LoginActivity.this, response.body().getMessage()).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<LoginRes> call, Throwable t) {
|
||||
|
||||
disDialog();
|
||||
Toasty.info(LoginActivity.this, "登录失败").show();
|
||||
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
binding = null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
package com.cmx.wanhui.activity
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.text.TextUtils
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.core.app.ActivityCompat
|
||||
import com.cmx.wanhui.R
|
||||
import com.cmx.wanhui.model.LoginBean
|
||||
import com.cmx.wanhui.model.LoginRes
|
||||
import com.cmx.wanhui.retrofit.RetrofitAPIManager
|
||||
import com.cmx.wanhui.utils.MyUtils.setAndroidNativeLightStatusBar
|
||||
import com.cmx.wanhui.utils.MyUtils.setFullscreen
|
||||
import com.cmx.wanhui.utils.SpUtils.putString
|
||||
import es.dmoral.toasty.Toasty
|
||||
import kotlinx.android.synthetic.main.activity_login.btn_login
|
||||
import kotlinx.android.synthetic.main.activity_login.edt_password
|
||||
import kotlinx.android.synthetic.main.activity_login.edt_username
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
|
||||
class LoginActivity : BaseActivity() {
|
||||
private val REQUIRED_PERMISSION_LIST = arrayOf(
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA
|
||||
)
|
||||
private val REQUEST_PERMISSION_CODE = 99
|
||||
private var allPermission = true
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_login)
|
||||
setFullscreen(this, true, true)
|
||||
setAndroidNativeLightStatusBar(this, true)
|
||||
checkAllPermission()
|
||||
btn_login!!.setOnClickListener(View.OnClickListener {
|
||||
if (TextUtils.isEmpty(edt_username!!.text.toString().trim { it <= ' ' })) {
|
||||
Toasty.info(this, "请输入用户名").show()
|
||||
return@OnClickListener
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(edt_password.text.toString().trim { it <= ' ' })) {
|
||||
Toasty.info(this@LoginActivity, "请输入密码").show()
|
||||
return@OnClickListener
|
||||
}
|
||||
toLogin()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 权限检查
|
||||
*/
|
||||
private fun checkAllPermission() {
|
||||
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSION_LIST, REQUEST_PERMISSION_CODE)
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
if (requestCode == REQUEST_PERMISSION_CODE) {
|
||||
for (i in grantResults.indices.reversed()) {
|
||||
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
|
||||
allPermission = false
|
||||
}
|
||||
}
|
||||
if (!allPermission) {
|
||||
Toasty.info(this, "权限已拒绝,请手动开启!").show()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
private fun toLogin() {
|
||||
showDialog()
|
||||
val LoginBean = LoginBean()
|
||||
LoginBean.username = edt_username.text.toString().trim()
|
||||
LoginBean.password = edt_password.text.toString().trim()
|
||||
val call = RetrofitAPIManager.provideClientApi().login(LoginBean)
|
||||
call!!.enqueue(object : Callback<LoginRes?> {
|
||||
override fun onResponse(call: Call<LoginRes?>, response: Response<LoginRes?>) {
|
||||
disDialog()
|
||||
if (response.isSuccessful && response.body()!!.code == 0) {
|
||||
putString(this@LoginActivity, "token", response.body()!!.access_token)
|
||||
putString(this@LoginActivity, "admin", response.body()!!.is_admin)
|
||||
val intent = Intent(this@LoginActivity, MainActivity::class.java)
|
||||
startActivity(intent)
|
||||
finish()
|
||||
} else {
|
||||
Toasty.error(this@LoginActivity, response.body()!!.message!!).show()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<LoginRes?>, t: Throwable) {
|
||||
disDialog()
|
||||
Toasty.info(this@LoginActivity, "登录失败").show()
|
||||
val intent = Intent(this@LoginActivity, MainActivity::class.java)
|
||||
startActivity(intent)
|
||||
finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package com.cmx.wanhui.retrofit;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
|
||||
|
||||
public class ApiRequtest<T> {
|
||||
private static final String TAG = ApiRequtest.class.getSimpleName();
|
||||
|
||||
private TypeToken<T> mResultType;
|
||||
|
||||
public ApiRequtest(Call<ResponseBody> call, TypeToken<T> resultType, final ApiResponse.Listener<ApiResult<T>> listener, final ApiResponse.ErrorListener errorListener) {
|
||||
mResultType = resultType;
|
||||
call.enqueue(new Callback<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
|
||||
try {
|
||||
ResponseBody responseBody = response.body();
|
||||
if (responseBody != null) {
|
||||
String jsonString = response.body().string();
|
||||
final ApiResult<T> result = new ApiResult<>(new JSONObject(jsonString), mResultType);
|
||||
listener.onResponse(result);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
} catch (JSONException e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ResponseBody> call, Throwable t) {
|
||||
errorListener.onErrorResponse(t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.cmx.wanhui.retrofit
|
||||
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import okhttp3.ResponseBody
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import java.io.IOException
|
||||
|
||||
class ApiRequtest<T>(
|
||||
call: Call<ResponseBody?>,
|
||||
private val mResultType: TypeToken<T>,
|
||||
listener: ApiResponse.Listener<ApiResult<T>?>,
|
||||
errorListener: ApiResponse.ErrorListener
|
||||
) {
|
||||
init {
|
||||
call.enqueue(object : Callback<ResponseBody?> {
|
||||
override fun onResponse(call: Call<ResponseBody?>, response: Response<ResponseBody?>) {
|
||||
try {
|
||||
val responseBody = response.body()
|
||||
if (responseBody != null) {
|
||||
val jsonString = response.body()!!.string()
|
||||
val result = ApiResult(JSONObject(jsonString), mResultType)
|
||||
listener.onResponse(result)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
} catch (e: JSONException) {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {
|
||||
errorListener.onErrorResponse(t)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = ApiRequtest::class.java.simpleName
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.cmx.wanhui.retrofit;
|
||||
|
||||
public class ApiResponse<T> {
|
||||
|
||||
/**
|
||||
* network request error response
|
||||
*/
|
||||
public interface ErrorListener {
|
||||
void onErrorResponse(Throwable requestError);
|
||||
}
|
||||
|
||||
/**
|
||||
* network request success response result
|
||||
* @param <T> data result
|
||||
*/
|
||||
public interface Listener<T> {
|
||||
void onResponse(T result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.cmx.wanhui.retrofit
|
||||
|
||||
class ApiResponse<T> {
|
||||
/**
|
||||
* network request error response
|
||||
*/
|
||||
interface ErrorListener {
|
||||
fun onErrorResponse(requestError: Throwable?)
|
||||
}
|
||||
|
||||
/**
|
||||
* network request success response result
|
||||
* @param <T> data result
|
||||
</T> */
|
||||
interface Listener<T> {
|
||||
fun onResponse(result: T)
|
||||
}
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
package com.cmx.wanhui.retrofit;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class ApiResult<T> {
|
||||
|
||||
private final static String TAG = ApiResult.class.getSimpleName();
|
||||
|
||||
public static final int OK = 0;
|
||||
|
||||
public Boolean success = true;
|
||||
private Integer code = OK;
|
||||
private String msg="";
|
||||
private T result;
|
||||
private String msgJson;
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
|
||||
public ApiResult() {
|
||||
super();
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public ApiResult(JSONObject jsonObject, TypeToken<T> typeToken) {
|
||||
super();
|
||||
try{
|
||||
if (jsonObject.has("errcode")){
|
||||
code = jsonObject.getInt("errcode");
|
||||
if (code != OK) {
|
||||
if (jsonObject.has("errmsg")) {
|
||||
msg = jsonObject.getString("errmsg");
|
||||
}
|
||||
|
||||
success = false;
|
||||
|
||||
}
|
||||
}else{
|
||||
|
||||
}
|
||||
|
||||
if (code == OK){
|
||||
try {
|
||||
result = gson.fromJson(jsonObject.toString(), typeToken.getType());
|
||||
}catch (Exception ex){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
|
||||
}
|
||||
private T testDefault(String msg){
|
||||
Gson gson = new Gson();
|
||||
result = gson.fromJson(msg,new TypeToken<T>(){}.getType());
|
||||
return result;
|
||||
}
|
||||
private T test(String msg, TypeToken<T> typeToken){
|
||||
Gson gson = new Gson();
|
||||
result = gson.fromJson(msg,typeToken.getType());
|
||||
return result;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(T result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public Boolean getSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(Boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public String getMsgJson() {
|
||||
return msgJson;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.cmx.wanhui.retrofit
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import org.json.JSONObject
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ApiResult<T> {
|
||||
var success = true
|
||||
var code = OK
|
||||
var msg = ""
|
||||
var result: T? = null
|
||||
private set
|
||||
val msgJson: String? = null
|
||||
|
||||
constructor() : super() { // TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
constructor(jsonObject: JSONObject, typeToken: TypeToken<T>) : super() {
|
||||
try {
|
||||
if (jsonObject.has("errcode")) {
|
||||
code = jsonObject.getInt("errcode")
|
||||
if (code != OK) {
|
||||
if (jsonObject.has("errmsg")) {
|
||||
msg = jsonObject.getString("errmsg")
|
||||
}
|
||||
success = false
|
||||
}
|
||||
} else {
|
||||
}
|
||||
if (code == OK) {
|
||||
try {
|
||||
result = gson.fromJson(jsonObject.toString(), typeToken.type)
|
||||
} catch (ex: Exception) {
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
|
||||
private fun testDefault(msg: String): T? {
|
||||
val gson = Gson()
|
||||
result = gson.fromJson(msg, object : TypeToken<T>() {}.type)
|
||||
return result
|
||||
}
|
||||
|
||||
private fun test(msg: String, typeToken: TypeToken<T>): T? {
|
||||
val gson = Gson()
|
||||
result = gson.fromJson(msg, typeToken.type)
|
||||
return result
|
||||
}
|
||||
|
||||
fun setResult(result: T) {
|
||||
this.result = result
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = ApiResult::class.java.simpleName
|
||||
const val OK = 0
|
||||
private val gson = Gson()
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
package com.cmx.wanhui.retrofit;
|
||||
|
||||
import com.cmx.wanhui.model.LoginBean;
|
||||
import com.cmx.wanhui.model.LoginRes;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.POST;
|
||||
|
||||
/**
|
||||
* Created by
|
||||
*/
|
||||
public interface ApiService {
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param body
|
||||
* @return
|
||||
*/
|
||||
@POST("/login")
|
||||
Call<LoginRes> login(@Body LoginBean body);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.cmx.wanhui.retrofit
|
||||
|
||||
import com.cmx.wanhui.model.LoginBean
|
||||
import com.cmx.wanhui.model.LoginRes
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.POST
|
||||
|
||||
/**
|
||||
* Created by
|
||||
*/
|
||||
interface ApiService {
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param body
|
||||
* @return
|
||||
*/
|
||||
@POST("/login")
|
||||
fun login(@Body body: LoginBean?): Call<LoginRes?>?
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package com.cmx.wanhui.retrofit;
|
||||
|
||||
import static com.cmx.wanhui.constant.Events.SERVER_URL;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.cmx.wanhui.utils.SpUtils;
|
||||
import com.cmx.wanhui.activity.MyApplication;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
/**
|
||||
* Created by TTLock on 2018/9/5.
|
||||
*/
|
||||
|
||||
|
||||
public class RetrofitAPIManager {
|
||||
|
||||
|
||||
public static ApiService provideClientApi() {
|
||||
Retrofit retrofit = new Retrofit.Builder().client(genericClient()).baseUrl(SERVER_URL).addConverterFactory(GsonConverterFactory.create()).build();
|
||||
return retrofit.create(ApiService.class);
|
||||
}
|
||||
|
||||
public static OkHttpClient genericClient() {
|
||||
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
|
||||
@Override
|
||||
public void log(String message) {
|
||||
// try {
|
||||
// String text = URLDecoder.decode(message, "utf-8");
|
||||
// Log.e("OKHttp-----", text);
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// e.printStackTrace();
|
||||
Log.e("OKHttp-----", message);
|
||||
// }
|
||||
}
|
||||
});
|
||||
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(35, TimeUnit.SECONDS).readTimeout(35, TimeUnit.SECONDS).writeTimeout(35, TimeUnit.SECONDS).addInterceptor(interceptor).addInterceptor(new Interceptor() {
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request original = chain.request();
|
||||
Request.Builder requestBuilder = original.newBuilder().header("token", SpUtils.getString(MyApplication.getmInstance(), "token"));
|
||||
Request request = requestBuilder.build();
|
||||
return chain.proceed(request);
|
||||
}
|
||||
}).build();
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public static <T> ApiRequtest enqueue(Call<ResponseBody> call, TypeToken<T> resultType, ApiResponse.Listener<ApiResult<T>> listener, ApiResponse.ErrorListener errorListener) {
|
||||
ApiRequtest<T> request = new ApiRequtest<>(call, resultType, listener, errorListener);
|
||||
return request;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.cmx.wanhui.retrofit
|
||||
|
||||
import android.util.Log
|
||||
import com.cmx.wanhui.activity.MyApplication.Companion.getmInstance
|
||||
import com.cmx.wanhui.constant.Events
|
||||
import com.cmx.wanhui.utils.SpUtils.getString
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.ResponseBody
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Call
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* Created by TTLock on 2018/9/5.
|
||||
*/
|
||||
object RetrofitAPIManager {
|
||||
fun provideClientApi(): ApiService {
|
||||
val retrofit = Retrofit.Builder().client(genericClient()).baseUrl(Events.SERVER_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create()).build()
|
||||
return retrofit.create(ApiService::class.java)
|
||||
}
|
||||
|
||||
fun genericClient(): OkHttpClient {
|
||||
val interceptor = HttpLoggingInterceptor { message -> Log.e("OKHttp-----", message) }
|
||||
interceptor.level = HttpLoggingInterceptor.Level.BODY
|
||||
return OkHttpClient.Builder().connectTimeout(35, TimeUnit.SECONDS).readTimeout(35, TimeUnit.SECONDS)
|
||||
.writeTimeout(35, TimeUnit.SECONDS).addInterceptor(interceptor).addInterceptor { chain ->
|
||||
val original = chain.request()
|
||||
val requestBuilder =
|
||||
original.newBuilder().header("token", getString(getmInstance()!!, "token"))
|
||||
val request = requestBuilder.build()
|
||||
chain.proceed(request)
|
||||
}.build()
|
||||
}
|
||||
|
||||
fun <T> enqueue(
|
||||
call: Call<ResponseBody?>?,
|
||||
resultType: TypeToken<T>?,
|
||||
listener: ApiResponse.Listener<ApiResult<T>?>?,
|
||||
errorListener: ApiResponse.ErrorListener?
|
||||
): ApiRequtest<*> {
|
||||
return ApiRequtest(call!!, resultType!!, listener!!, errorListener!!)
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package com.cmx.wanhui.retrofit;
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2018/1/17 0017.
|
||||
*/
|
||||
|
||||
public interface onRequestResponse {
|
||||
void onResult(boolean success);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.cmx.wanhui.retrofit
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2018/1/17 0017.
|
||||
*/
|
||||
interface onRequestResponse {
|
||||
fun onResult(success: Boolean)
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package com.cmx.wanhui.retrofit;
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2018/1/17 0017.
|
||||
*/
|
||||
|
||||
public interface onServerRequestCallBack {
|
||||
void onResult(int resultCode);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.cmx.wanhui.retrofit
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2018/1/17 0017.
|
||||
*/
|
||||
interface onServerRequestCallBack {
|
||||
fun onResult(resultCode: Int)
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.cmx.wanhui.ui
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
|
@ -21,6 +22,7 @@ open class BaseFragment : Fragment() {
|
|||
var mBaseLoadingDialog: BaseLoadingDialog? = null
|
||||
var mApolloBinder: ApolloBinder? = null
|
||||
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(null, container, false)
|
||||
}
|
||||
|
@ -37,7 +39,7 @@ open class BaseFragment : Fragment() {
|
|||
/**
|
||||
* 相关属性设置
|
||||
*/
|
||||
fun setVebView(webView: WebView) {
|
||||
fun setWebView(webView: WebView) {
|
||||
val webSettings = webView!!.settings
|
||||
webSettings.javaScriptEnabled = true
|
||||
webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
|
||||
|
@ -46,10 +48,13 @@ open class BaseFragment : Fragment() {
|
|||
webSettings.allowFileAccessFromFileURLs = true
|
||||
webView!!.addJavascriptInterface(AndroidtoJs(), "AndroidtoJs")
|
||||
webView!!.webViewClient = object : WebViewClient() {
|
||||
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
|
||||
super.onPageStarted(view, url, favicon)
|
||||
showDialog()
|
||||
}
|
||||
|
||||
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
|
||||
//使用WebView加载显示url
|
||||
view.loadUrl(url)
|
||||
//返回true
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,203 @@
|
|||
package com.cmx.wanhui.ui;
|
||||
|
||||
import static com.cmx.wanhui.constant.Events.SERVER_URL;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ClipData;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.ValueCallback;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.webkit.WebResourceError;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.cmx.wanhui.activity.LoginActivity;
|
||||
import com.cmx.wanhui.constant.Events;
|
||||
import com.cmx.wanhui.databinding.FragmentHomeBinding;
|
||||
import com.cmx.wanhui.utils.AndroidtoJs;
|
||||
import com.cmx.wanhui.utils.L;
|
||||
import com.cmx.wanhui.utils.MyUtils;
|
||||
import com.cmx.wanhui.view.BaseLoadingDialog;
|
||||
import com.lsxiao.apollo.core.Apollo;
|
||||
import com.lsxiao.apollo.core.annotations.Receive;
|
||||
import com.lsxiao.apollo.core.contract.ApolloBinder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class HomeFragment extends BaseFragment {
|
||||
|
||||
private FragmentHomeBinding binding;
|
||||
private WebView mWebView;
|
||||
private ValueCallback<Uri[]> mUploadMessage;
|
||||
private String mCameraPhotoPath = null;
|
||||
private long size = 0;
|
||||
private static final int INPUT_FILE_REQUEST_CODE = 1;
|
||||
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
binding = FragmentHomeBinding.inflate(inflater, container, false);
|
||||
View root = binding.getRoot();
|
||||
mWebView = binding.wvHome;
|
||||
setWebView(mWebView);
|
||||
mWebView.loadUrl("https://www.baidu.com/");
|
||||
mWebView.setWebChromeClient(new PQChromeClient());
|
||||
binding.tvTe.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Apollo.emit(Events.JS_OPEN_SCANQR);
|
||||
}
|
||||
});
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
binding = null;
|
||||
}
|
||||
|
||||
|
||||
public class PQChromeClient extends WebChromeClient {
|
||||
// For Android 5.0+
|
||||
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
|
||||
// Double check that we don't have any existing callbacks
|
||||
if (mUploadMessage != null) {
|
||||
mUploadMessage.onReceiveValue(null);
|
||||
}
|
||||
mUploadMessage = filePath;
|
||||
Log.e("FileCooserParams => ", filePath.toString());
|
||||
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
|
||||
// Create the File where the photo should go
|
||||
File photoFile = null;
|
||||
try {
|
||||
photoFile = createImageFile();
|
||||
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
|
||||
} catch (IOException ex) {
|
||||
// Error occurred while creating the File
|
||||
Log.e("test", "Unable to create Image File", ex);
|
||||
}
|
||||
|
||||
// Continue only if the File was successfully created
|
||||
if (photoFile != null) {
|
||||
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
|
||||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
|
||||
} else {
|
||||
takePictureIntent = null;
|
||||
}
|
||||
}
|
||||
|
||||
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
|
||||
contentSelectionIntent.setType("image/*");
|
||||
Intent[] intentArray;
|
||||
if (takePictureIntent != null) {
|
||||
intentArray = new Intent[]{takePictureIntent};
|
||||
} else {
|
||||
intentArray = new Intent[2];
|
||||
}
|
||||
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
||||
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
|
||||
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
|
||||
startActivityForResult(Intent.createChooser(chooserIntent, "Select images"), 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private File createImageFile() throws IOException {
|
||||
// Create an image file name
|
||||
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
||||
String imageFileName = "JPEG_" + timeStamp + "_";
|
||||
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||
File imageFile = File.createTempFile(imageFileName, /* prefix */
|
||||
".jpg", /* suffix */
|
||||
storageDir /* directory */);
|
||||
return imageFile;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String file_path = mCameraPhotoPath.replace("file:", "");
|
||||
File file = new File(file_path);
|
||||
size = file.length();
|
||||
if (data != null || mCameraPhotoPath != null) {
|
||||
Integer count = 1;
|
||||
ClipData images = null;
|
||||
try {
|
||||
images = data.getClipData();
|
||||
} catch (Exception e) {
|
||||
Log.e("Error!", e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
if (images == null && data != null && data.getDataString() != null) {
|
||||
count = data.getDataString().length();
|
||||
} else if (images != null) {
|
||||
count = images.getItemCount();
|
||||
}
|
||||
Uri[] results = new Uri[count];
|
||||
// Check that the response is a good one
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
if (size != 0) {
|
||||
// If there is not data, then we may have taken a photo
|
||||
if (mCameraPhotoPath != null) {
|
||||
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
|
||||
}
|
||||
} else if (data.getClipData() == null) {
|
||||
results = new Uri[]{Uri.parse(data.getDataString())};
|
||||
} else {
|
||||
|
||||
for (int i = 0; i < images.getItemCount(); i++) {
|
||||
results[i] = images.getItemAt(i).getUri();
|
||||
}
|
||||
}
|
||||
}
|
||||
mUploadMessage.onReceiveValue(results);
|
||||
mUploadMessage = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("Error!", "Error while opening image file" + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维码扫描返回
|
||||
*/
|
||||
@Receive(Events.JS_OPEN_SCANQR_BACK)
|
||||
public void onQRBack(String msg) {
|
||||
String jsString = "javascript:onQRBack(" + msg + ")";
|
||||
L.e("JS二维码扫描返回>>>>", jsString);
|
||||
mWebView.evaluateJavascript(jsString, new ValueCallback<String>() {
|
||||
@Override
|
||||
public void onReceiveValue(String s) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,175 +0,0 @@
|
|||
package com.cmx.wanhui.ui
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ClipData
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.ValueCallback
|
||||
import android.webkit.WebChromeClient
|
||||
import android.webkit.WebResourceError
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebSettings
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.cmx.wanhui.R
|
||||
import com.cmx.wanhui.constant.Events
|
||||
import com.cmx.wanhui.utils.AndroidtoJs
|
||||
import com.cmx.wanhui.utils.L
|
||||
import com.cmx.wanhui.utils.MyUtils
|
||||
import com.cmx.wanhui.view.BaseLoadingDialog
|
||||
import com.lsxiao.apollo.core.Apollo
|
||||
import com.lsxiao.apollo.core.annotations.Receive
|
||||
import kotlinx.android.synthetic.main.fragment_home.tv_te
|
||||
import kotlinx.android.synthetic.main.fragment_home.wv_home
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
||||
class HomeFragment : BaseFragment() {
|
||||
|
||||
private var mUploadMessage: ValueCallback<Array<Uri>>? = null
|
||||
private var mCameraPhotoPath: String? = null
|
||||
private var size: Long = 0
|
||||
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_home, container, false)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setVebView(wv_home!!)
|
||||
wv_home!!.loadUrl("https://www.baidu.com/")//SERVER_URL
|
||||
showDialog()
|
||||
wv_home!!.webChromeClient = PQChromeClient()
|
||||
tv_te.setOnClickListener { Apollo.emit(Events.JS_OPEN_SCANQR) }
|
||||
}
|
||||
|
||||
|
||||
inner class PQChromeClient : WebChromeClient() {
|
||||
// For Android 5.0+
|
||||
override fun onShowFileChooser(
|
||||
view: WebView, filePath: ValueCallback<Array<Uri>>, fileChooserParams: FileChooserParams
|
||||
): Boolean {
|
||||
// Double check that we don't have any existing callbacks
|
||||
if (mUploadMessage != null) {
|
||||
mUploadMessage!!.onReceiveValue(null)
|
||||
}
|
||||
mUploadMessage = filePath
|
||||
Log.e("FileCooserParams => ", filePath.toString())
|
||||
var takePictureIntent: Intent? = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
|
||||
if (takePictureIntent!!.resolveActivity(activity!!.packageManager) != null) {
|
||||
// Create the File where the photo should go
|
||||
var photoFile: File? = null
|
||||
try {
|
||||
photoFile = createImageFile()
|
||||
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath)
|
||||
} catch (ex: IOException) {
|
||||
// Error occurred while creating the File
|
||||
Log.e("test", "Unable to create Image File", ex)
|
||||
}
|
||||
|
||||
// Continue only if the File was successfully created
|
||||
if (photoFile != null) {
|
||||
mCameraPhotoPath = "file:" + photoFile.absolutePath
|
||||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile))
|
||||
} else {
|
||||
takePictureIntent = null
|
||||
}
|
||||
}
|
||||
val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
|
||||
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
|
||||
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
|
||||
contentSelectionIntent.type = "image/*"
|
||||
val intentArray: Array<Intent?>
|
||||
intentArray = takePictureIntent?.let { arrayOf(it) } ?: arrayOfNulls(2)
|
||||
val chooserIntent = Intent(Intent.ACTION_CHOOSER)
|
||||
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
|
||||
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser")
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
|
||||
startActivityForResult(Intent.createChooser(chooserIntent, "Select images"), 1)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun createImageFile(): File {
|
||||
// Create an image file name
|
||||
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
|
||||
val imageFileName = "JPEG_" + timeStamp + "_"
|
||||
val storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
|
||||
return File.createTempFile(
|
||||
imageFileName, /* prefix */
|
||||
".jpg", /* suffix */
|
||||
storageDir /* directory */
|
||||
)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
return
|
||||
}
|
||||
try {
|
||||
val file_path = mCameraPhotoPath!!.replace("file:", "")
|
||||
val file = File(file_path)
|
||||
size = file.length()
|
||||
if (data != null || mCameraPhotoPath != null) {
|
||||
var count = 1
|
||||
var images: ClipData? = null
|
||||
try {
|
||||
images = data!!.clipData
|
||||
} catch (e: Exception) {
|
||||
Log.e("Error!", e.localizedMessage)
|
||||
}
|
||||
if (images == null && data != null && data.dataString != null) {
|
||||
count = data.dataString!!.length
|
||||
} else if (images != null) {
|
||||
count = images.itemCount
|
||||
}
|
||||
var results = arrayOfNulls<Uri>(count)
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
if (size != 0L) {
|
||||
// If there is not data, then we may have taken a photo
|
||||
if (mCameraPhotoPath != null) {
|
||||
results = arrayOf(Uri.parse(mCameraPhotoPath))
|
||||
}
|
||||
} else if (data!!.clipData == null) {
|
||||
results = arrayOf(Uri.parse(data.dataString))
|
||||
} else {
|
||||
for (i in 0 until images!!.itemCount) {
|
||||
results[i] = images.getItemAt(i).uri
|
||||
}
|
||||
}
|
||||
}
|
||||
//mUploadMessage!!.onReceiveValue(results)
|
||||
mUploadMessage = null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("Error!", "Error while opening image file" + e.localizedMessage)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维码扫描返回
|
||||
*/
|
||||
@Receive(Events.JS_OPEN_SCANQR_BACK)
|
||||
fun onQRBack(msg: String) {
|
||||
val jsString = "javascript:onQRBack($msg)"
|
||||
L.e("JS二维码扫描返回>>>>", jsString)
|
||||
wv_home!!.evaluateJavascript(jsString) { }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val INPUT_FILE_REQUEST_CODE = 1
|
||||
}
|
||||
}
|
|
@ -0,0 +1,187 @@
|
|||
package com.cmx.wanhui.ui;
|
||||
|
||||
import static com.cmx.wanhui.constant.Events.SERVER_URL;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ClipData;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.ValueCallback;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.webkit.WebResourceError;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.cmx.wanhui.constant.Events;
|
||||
import com.cmx.wanhui.databinding.FragmentDashboardBinding;
|
||||
import com.cmx.wanhui.utils.AndroidtoJs;
|
||||
import com.cmx.wanhui.utils.L;
|
||||
import com.cmx.wanhui.utils.MyUtils;
|
||||
import com.cmx.wanhui.view.BaseLoadingDialog;
|
||||
import com.lsxiao.apollo.core.Apollo;
|
||||
import com.lsxiao.apollo.core.annotations.Receive;
|
||||
import com.lsxiao.apollo.core.contract.ApolloBinder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class ModeFragment extends BaseFragment {
|
||||
private FragmentDashboardBinding binding;
|
||||
private WebView mWebView;
|
||||
private ValueCallback<Uri[]> mUploadMessage;
|
||||
private String mCameraPhotoPath = null;
|
||||
private long size = 0;
|
||||
private static final int INPUT_FILE_REQUEST_CODE = 1;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
binding = FragmentDashboardBinding.inflate(inflater, container, false);
|
||||
View root = binding.getRoot();
|
||||
mWebView = binding.wvLoan;
|
||||
setWebView(mWebView);
|
||||
mWebView.loadUrl("https://www.baidu.com/");
|
||||
mWebView.setWebChromeClient(new PQChromeClient());
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
public class PQChromeClient extends WebChromeClient {
|
||||
// For Android 5.0+
|
||||
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
|
||||
// Double check that we don't have any existing callbacks
|
||||
if (mUploadMessage != null) {
|
||||
mUploadMessage.onReceiveValue(null);
|
||||
}
|
||||
mUploadMessage = filePath;
|
||||
Log.e("FileCooserParams => ", filePath.toString());
|
||||
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
|
||||
// Create the File where the photo should go
|
||||
File photoFile = null;
|
||||
try {
|
||||
photoFile = createImageFile();
|
||||
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
|
||||
} catch (IOException ex) {
|
||||
// Error occurred while creating the File
|
||||
Log.e("test", "Unable to create Image File", ex);
|
||||
}
|
||||
// Continue only if the File was successfully created
|
||||
if (photoFile != null) {
|
||||
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
|
||||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
|
||||
} else {
|
||||
takePictureIntent = null;
|
||||
}
|
||||
}
|
||||
|
||||
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
|
||||
contentSelectionIntent.setType("image/*");
|
||||
|
||||
Intent[] intentArray;
|
||||
if (takePictureIntent != null) {
|
||||
intentArray = new Intent[]{takePictureIntent};
|
||||
} else {
|
||||
intentArray = new Intent[2];
|
||||
}
|
||||
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
||||
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
|
||||
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
|
||||
startActivityForResult(Intent.createChooser(chooserIntent, "Select images"), 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private File createImageFile() throws IOException {
|
||||
// Create an image file name
|
||||
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
||||
String imageFileName = "JPEG_" + timeStamp + "_";
|
||||
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||
File imageFile = File.createTempFile(imageFileName, /* prefix */
|
||||
".jpg", /* suffix */
|
||||
storageDir /* directory */);
|
||||
return imageFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String file_path = mCameraPhotoPath.replace("file:", "");
|
||||
File file = new File(file_path);
|
||||
size = file.length();
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e("Error!", "Error while opening image file" + e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
if (data != null || mCameraPhotoPath != null) {
|
||||
Integer count = 1;
|
||||
ClipData images = null;
|
||||
try {
|
||||
images = data.getClipData();
|
||||
} catch (Exception e) {
|
||||
Log.e("Error!", e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
if (images == null && data != null && data.getDataString() != null) {
|
||||
count = data.getDataString().length();
|
||||
} else if (images != null) {
|
||||
count = images.getItemCount();
|
||||
}
|
||||
Uri[] results = new Uri[count];
|
||||
// Check that the response is a good one
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
if (size != 0) {
|
||||
// If there is not data, then we may have taken a photo
|
||||
if (mCameraPhotoPath != null) {
|
||||
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
|
||||
}
|
||||
} else if (data.getClipData() == null) {
|
||||
results = new Uri[]{Uri.parse(data.getDataString())};
|
||||
} else {
|
||||
|
||||
for (int i = 0; i < images.getItemCount(); i++) {
|
||||
results[i] = images.getItemAt(i).getUri();
|
||||
}
|
||||
}
|
||||
}
|
||||
mUploadMessage.onReceiveValue(results);
|
||||
mUploadMessage = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维码扫描返回
|
||||
*/
|
||||
@Receive(Events.JS_OPEN_SCANQR_BACK)
|
||||
public void onQRBack(String msg) {
|
||||
String jsString = "javascript:onQRBack(" + msg + ")";
|
||||
L.e("JS二维码扫描返回>>>>", jsString);
|
||||
mWebView.evaluateJavascript(jsString, new ValueCallback<String>() {
|
||||
@Override
|
||||
public void onReceiveValue(String s) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,169 +0,0 @@
|
|||
package com.cmx.wanhui.ui
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ClipData
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.ValueCallback
|
||||
import android.webkit.WebChromeClient
|
||||
import android.webkit.WebView
|
||||
import com.cmx.wanhui.R
|
||||
import com.cmx.wanhui.constant.Events
|
||||
import com.cmx.wanhui.utils.L
|
||||
import com.lsxiao.apollo.core.Apollo
|
||||
import com.lsxiao.apollo.core.annotations.Receive
|
||||
import kotlinx.android.synthetic.main.fragment_dashboard.wv_loan
|
||||
import kotlinx.android.synthetic.main.fragment_home.tv_te
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
|
||||
class ModeFragment : BaseFragment() {
|
||||
private var mUploadMessage: ValueCallback<Array<Uri>>? = null
|
||||
private var mCameraPhotoPath: String? = null
|
||||
private var size: Long = 0
|
||||
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_dashboard, container, false)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setVebView(wv_loan!!)
|
||||
wv_loan!!.loadUrl("https://www.baidu.com/")//SERVER_URL
|
||||
showDialog()
|
||||
wv_loan!!.webChromeClient = PQChromeClient()
|
||||
}
|
||||
|
||||
|
||||
inner class PQChromeClient : WebChromeClient() {
|
||||
// For Android 5.0+
|
||||
override fun onShowFileChooser(
|
||||
view: WebView,
|
||||
filePath: ValueCallback<Array<Uri>>,
|
||||
fileChooserParams: FileChooserParams
|
||||
): Boolean {
|
||||
// Double check that we don't have any existing callbacks
|
||||
if (mUploadMessage != null) {
|
||||
mUploadMessage!!.onReceiveValue(null)
|
||||
}
|
||||
mUploadMessage = filePath
|
||||
Log.e("FileCooserParams => ", filePath.toString())
|
||||
var takePictureIntent: Intent? = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
|
||||
if (takePictureIntent!!.resolveActivity(activity!!.packageManager) != null) {
|
||||
// Create the File where the photo should go
|
||||
var photoFile: File? = null
|
||||
try {
|
||||
photoFile = createImageFile()
|
||||
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath)
|
||||
} catch (ex: IOException) {
|
||||
// Error occurred while creating the File
|
||||
Log.e("test", "Unable to create Image File", ex)
|
||||
}
|
||||
|
||||
// Continue only if the File was successfully created
|
||||
if (photoFile != null) {
|
||||
mCameraPhotoPath = "file:" + photoFile.absolutePath
|
||||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile))
|
||||
} else {
|
||||
takePictureIntent = null
|
||||
}
|
||||
}
|
||||
val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
|
||||
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
|
||||
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
|
||||
contentSelectionIntent.type = "image/*"
|
||||
val intentArray: Array<Intent?>
|
||||
intentArray = takePictureIntent?.let { arrayOf(it) } ?: arrayOfNulls(2)
|
||||
val chooserIntent = Intent(Intent.ACTION_CHOOSER)
|
||||
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
|
||||
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser")
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
|
||||
startActivityForResult(Intent.createChooser(chooserIntent, "Select images"), 1)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun createImageFile(): File {
|
||||
// Create an image file name
|
||||
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
|
||||
val imageFileName = "JPEG_" + timeStamp + "_"
|
||||
val storageDir =
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
|
||||
return File.createTempFile(
|
||||
imageFileName, /* prefix */
|
||||
".jpg", /* suffix */
|
||||
storageDir /* directory */
|
||||
)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
return
|
||||
}
|
||||
try {
|
||||
val file_path = mCameraPhotoPath!!.replace("file:", "")
|
||||
val file = File(file_path)
|
||||
size = file.length()
|
||||
} catch (e: Exception) {
|
||||
Log.e("Error!", "Error while opening image file" + e.localizedMessage)
|
||||
}
|
||||
if (data != null || mCameraPhotoPath != null) {
|
||||
var count = 1
|
||||
var images: ClipData? = null
|
||||
try {
|
||||
images = data!!.clipData
|
||||
} catch (e: Exception) {
|
||||
Log.e("Error!", e.localizedMessage)
|
||||
}
|
||||
if (images == null && data != null && data.dataString != null) {
|
||||
count = data.dataString!!.length
|
||||
} else if (images != null) {
|
||||
count = images.itemCount
|
||||
}
|
||||
var results = arrayOfNulls<Uri>(count)
|
||||
// Check that the response is a good one
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
if (size != 0L) {
|
||||
// If there is not data, then we may have taken a photo
|
||||
if (mCameraPhotoPath != null) {
|
||||
results = arrayOf(Uri.parse(mCameraPhotoPath))
|
||||
}
|
||||
} else if (data!!.clipData == null) {
|
||||
results = arrayOf(Uri.parse(data.dataString))
|
||||
} else {
|
||||
for (i in 0 until images!!.itemCount) {
|
||||
results[i] = images.getItemAt(i).uri
|
||||
}
|
||||
}
|
||||
}
|
||||
//mUploadMessage!!.onReceiveValue(results)
|
||||
mUploadMessage = null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维码扫描返回
|
||||
*/
|
||||
@Receive(Events.JS_OPEN_SCANQR_BACK)
|
||||
fun onQRBack(msg: String) {
|
||||
val jsString = "javascript:onQRBack($msg)"
|
||||
L.e("JS二维码扫描返回>>>>", jsString)
|
||||
wv_loan!!.evaluateJavascript(jsString) { }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val INPUT_FILE_REQUEST_CODE = 1
|
||||
}
|
||||
}
|
|
@ -21,9 +21,8 @@ class PersonalFragment : BaseFragment() {
|
|||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setVebView(wv_mine!!)
|
||||
setWebView(wv_mine!!)
|
||||
wv_mine!!.loadUrl("https://www.baidu.com/")//SERVER_URL
|
||||
showDialog()
|
||||
//wv_mine!!.webChromeClient = PQChromeClient()
|
||||
}
|
||||
|
||||
|
|
|
@ -10,9 +10,8 @@ import com.lsxiao.apollo.core.Apollo
|
|||
class AndroidtoJs {
|
||||
// 定义JS需要调用的方法
|
||||
// 被JS调用的方法必须加入@JavascriptInterface注解
|
||||
@get:JavascriptInterface
|
||||
val appToken: String?
|
||||
get() {
|
||||
@JavascriptInterface
|
||||
fun getAppToken(): String? {
|
||||
L.e("JS--", "getAppToken()")
|
||||
return SpUtils.getString(getmInstance()!!, "token")
|
||||
}
|
||||
|
@ -22,17 +21,15 @@ class AndroidtoJs {
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
@get:JavascriptInterface
|
||||
val user: String?
|
||||
get() {
|
||||
@JavascriptInterface
|
||||
fun getUser(): String? {
|
||||
L.e("JS--", "getUser()")
|
||||
return SpUtils.getString(getmInstance()!!, "admin")
|
||||
}
|
||||
|
||||
//版本号
|
||||
@get:JavascriptInterface
|
||||
val appVersion: String
|
||||
get() {
|
||||
@JavascriptInterface
|
||||
fun getAppVersion(): String {
|
||||
L.e("JS--", "getAppVersion()")
|
||||
val versionname: String //版本号
|
||||
versionname = try {
|
||||
|
@ -42,7 +39,6 @@ class AndroidtoJs {
|
|||
} catch (e: PackageManager.NameNotFoundException) {
|
||||
"未知"
|
||||
}
|
||||
Log.e(TAG, "getAppVersion=$versionname")
|
||||
return versionname
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ class L private constructor() {
|
|||
isDebug = debug
|
||||
}
|
||||
|
||||
fun i(msg: String?) {
|
||||
fun i(msg: String) {
|
||||
if (isDebug) Log.i(tAG, msg!!)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue