|
package com.electric.chargingpile.activity;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.electric.chargingpile.R;
import com.electric.chargingpile.adapter.FeedbackMessageAdapter;
import com.electric.chargingpile.application.MainApplication;
import com.electric.chargingpile.data.FeedbackMessageData;
import com.electric.chargingpile.util.BarColorUtil;
import com.electric.chargingpile.util.JsonUtils;
import com.electric.chargingpile.util.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import java.util.ArrayList;
import okhttp3.Call;
public class FeedbackMessageActivity extends Activity implements View.OnClickListener, TextWatcher {
private static final String TAG = "FeedbackMessageActivity";
private ImageView iv_back;
private EditText et_input_message;
private ListView lv_message;
private TextView tv_submit;
private FeedbackMessageAdapter adapter;
private String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback_message);
BarColorUtil.initStatusBarColor(FeedbackMessageActivity.this);
getIntentData();
initView();
getFeedbackMessage();
setEvent();
}
private void getIntentData() {
id = getIntent().getStringExtra("id");
Log.e(TAG, "getIntentData: id=" + id);
}
private void initView() {
iv_back = (ImageView) findViewById(R.id.iv_back);
et_input_message = (EditText) findViewById(R.id.et_input_message);
lv_message = (ListView) findViewById(R.id.lv_message);
tv_submit = (TextView) findViewById(R.id.tv_submit);
}
private void setEvent() {
iv_back.setOnClickListener(this);
et_input_message.setOnClickListener(this);
tv_submit.setOnClickListener(this);
et_input_message.addTextChangedListener(this);
}
private void getFeedbackMessage() {
String url = MainApplication.url + "/zhannew/basic/web/index.php/advice/detail?advice_id=" + id + "&user_id=" + MainApplication.userId;
OkHttpUtils.get().url(url).build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
ToastUtil.showToast(getApplicationContext(), "请检查当前网络", Toast.LENGTH_SHORT);
}
@Override
public void onResponse(String response) {
// Log.e(TAG, "onResponse: "+response );
String rtnCode = JsonUtils.getKeyResult(response, "rtnCode");
if (rtnCode.equals("01")) {
String data = JsonUtils.getKeyResult(response, "data");
ArrayList<FeedbackMessageData> list = (ArrayList<FeedbackMessageData>) JsonUtils.parseToObjectList(data, FeedbackMessageData.class);
Log.e(TAG, "onResponse: list=" + list.size());
adapter = new FeedbackMessageAdapter(list);
lv_message.setAdapter(adapter);
lv_message.setSelection(lv_message.getBottom());
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.et_input_message:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
lv_message.setSelection(lv_message.getBottom());
}
}, 300);
break;
case R.id.tv_submit:
String message = et_input_message.getText().toString();
if (message.trim().isEmpty() || message.length() == 0) {
ToastUtil.showToast(getApplicationContext(), "输入不能为空", Toast.LENGTH_SHORT);
break;
}
if (message.length() > 500) {
ToastUtil.showToast(getApplicationContext(), "输入字数不能超过500", Toast.LENGTH_SHORT);
break;
}
if (containsEmoji(message)) {
ToastUtil.showToast(getApplicationContext(), "不支持输入表情", Toast.LENGTH_SHORT);
break;
}
uploadFeedbackInfo();
break;
}
}
private void uploadFeedbackInfo() {
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
final String imem = telephonyManager.getDeviceId();
String url = MainApplication.url+"/zhannew/basic/web/index.php/advice/add?user_id="+MainApplication.userId+
"&msg="+et_input_message.getText().toString()+"&telephone="+MainApplication.userPhone+"&imem="+imem+
"&main_id="+id+"&url=";
Log.e(TAG, "uploadFeedbackInfo: url="+url );
OkHttpUtils.get().url(url).build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
ToastUtil.showToast(getApplicationContext(),"请检查当前网络",Toast.LENGTH_SHORT);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_input_message.getWindowToken(), 0);
}
@Override
public void onResponse(String response) {
Log.e(TAG, "onResponse: response="+response );
String rtnCode = JsonUtils.getKeyResult(response,"rtnCode");
if (rtnCode.equals("01")){
et_input_message.setText("");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_input_message.getWindowToken(), 0);
getFeedbackMessage();
}else if (rtnCode.equals("02")){
ToastUtil.showToast(getApplicationContext(),"提交失败", Toast.LENGTH_SHORT);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_input_message.getWindowToken(), 0);
}else if (rtnCode.equals("03")){
String rtnMsg = JsonUtils.getKeyResult(response,"rtnMsg");
ToastUtil.showToast(getApplicationContext(),rtnMsg,Toast.LENGTH_SHORT);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_input_message.getWindowToken(), 0);
}
}
});
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
lv_message.setSelection(lv_message.getBottom());
}
},300);
}
/**
* 检测是否有emoji表情
*
* @param source
* @return
*/
public static boolean containsEmoji(String source) {
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
return true;
}
}
return false;
}
/**
* 判断是否是Emoji
*
* @param codePoint 比较的单个字符
* @return
*/
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
(codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
&& (codePoint <= 0x10FFFF));
}
}
|