package com.ysvideo.zhibo.app.ui.video;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.content.SharedPreferences.Editor;
|
import android.graphics.drawable.Drawable;
|
import android.os.Bundle;
|
import android.text.Editable;
|
import android.text.TextUtils;
|
import android.text.TextWatcher;
|
import android.view.KeyEvent;
|
import android.view.MotionEvent;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.View.OnTouchListener;
|
import android.view.inputmethod.EditorInfo;
|
import android.widget.AdapterView;
|
import android.widget.AdapterView.OnItemClickListener;
|
import android.widget.AutoCompleteTextView;
|
import android.widget.FrameLayout;
|
import android.widget.GridView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.qq.e.ads.nativ.ADSize;
|
import com.ysvideo.zhibo.app.R;
|
import com.ysvideo.zhibo.app.entity.ad.AdPositionEnum;
|
import com.ysvideo.zhibo.app.ui.video.adapter.GridSearchRecordAdapter;
|
import com.ysvideo.zhibo.app.ui.video.adapter.SuggestKeysAdapter;
|
import com.ysvideo.zhibo.app.util.ad.CSJConstant;
|
import com.ysvideo.zhibo.app.util.ad.ExpressAdManager;
|
import com.ysvideo.zhibo.app.util.ad.GDTConstant;
|
import com.ysvideo.zhibo.app.util.api.BasicTextHttpResponseHandler;
|
import com.ysvideo.zhibo.app.util.api.HttpApiUtil;
|
import com.ysvideo.zhibo.lib.common.activity.BaseActivity;
|
import com.ysvideo.zhibo.lib.common.util.common.DimenUtils;
|
import com.ysvideo.zhibo.lib.common.util.common.StringUtils;
|
import com.ysvideo.zhibo.lib.common.widget.FlowLayout;
|
import com.ysvideo.zhibo.library_ad.AdUtil;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class SearchActivity extends BaseActivity implements OnClickListener {
|
|
private final String TAG = SearchActivity.class.getSimpleName();
|
|
private AutoCompleteTextView et_search_key;
|
|
private TextView tv_clear;
|
|
private TextView tv_search_cancel;
|
|
private FlowLayout fl_hot_search;
|
|
private GridView gv_history_search;
|
|
private LinearLayout ll_search_history;
|
|
private String value = "";
|
|
private void initView() {
|
fl_advertisement = findViewById(R.id.fl_advertisement);
|
et_search_key = findViewById(R.id.et_search_key);
|
fl_advertisement.setOnClickListener(this);
|
tv_search_cancel = findViewById(R.id.tv_search_cancel);
|
fl_hot_search = findViewById(R.id.fl_hot_search);
|
gv_history_search = findViewById(R.id.gv_history_search);
|
tv_clear = findViewById(R.id.tv_search_clear_his);
|
ll_search_history = findViewById(R.id.ll_search_history);
|
}
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_search);
|
initView();
|
|
|
et_search_key.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
@Override
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
|
if (StringUtils.isEmpty(et_search_key.getEditableText()
|
.toString())) {
|
Toast.makeText(SearchActivity.this, "请输入搜索内容!", Toast.LENGTH_LONG).show();
|
return false;
|
} else {
|
intent.putExtra("key", et_search_key.getEditableText()
|
.toString());
|
}
|
startActivity(intent);
|
|
return true;
|
}
|
return false;
|
}
|
});
|
|
tv_search_cancel.setOnClickListener(this);
|
tv_clear.setOnClickListener(this);
|
|
value = getIntent().getStringExtra("value");
|
if (!StringUtils.isEmpty(value)) {
|
et_search_key.setHint(value);
|
// et_search_key.setSelection(value.length());
|
}
|
|
et_search_key.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
public void onTextChanged(CharSequence s, int start, int before,
|
int count) {
|
|
}
|
|
@Override
|
public void beforeTextChanged(CharSequence s, int start, int count,
|
int after) {
|
|
}
|
|
@Override
|
public void afterTextChanged(Editable s) {
|
if (!TextUtils.isEmpty(s.toString())
|
|| !TextUtils.isEmpty(et_search_key.getHint())) {
|
suggestSearch(s.toString());
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
R.drawable.ic_clear, 0);
|
// tv_search_cancel.setText(R.string.search);
|
} else {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
0, 0);
|
// tv_search_cancel.setText(R.string.cancel);
|
}
|
}
|
});
|
|
if (!TextUtils.isEmpty(value)) {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
R.drawable.ic_clear, 0);
|
// tv_search_cancel.setText(R.string.search);
|
} else {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
0, 0);
|
// tv_search_cancel.setText(R.string.cancel);
|
}
|
|
et_search_key.setOnItemClickListener(new OnItemClickListener() {
|
|
@Override
|
public void onItemClick(AdapterView<?> parent, View view,
|
int position, long id) {
|
if (parent.getItemAtPosition(position) != null) {
|
Intent intent = new Intent(SearchActivity.this,
|
SearchResultActivity.class);
|
intent.putExtra("key", parent.getItemAtPosition(position)
|
.toString());
|
startActivity(intent);
|
}
|
}
|
});
|
|
et_search_key.setOnTouchListener(new OnTouchListener() {
|
|
@Override
|
public boolean onTouch(View v, MotionEvent event) {
|
// getCompoundDrawables() 可以获取一个长度为4的数组,
|
// 存放drawableLeft,Right,Top,Bottom四个图片资源对象
|
// index=2 表示的是 drawableRight 图片资源对象
|
Drawable drawable = et_search_key.getCompoundDrawables()[2];
|
if (drawable == null)
|
return false;
|
|
if (event.getAction() != MotionEvent.ACTION_UP)
|
return false;
|
|
// drawable.getIntrinsicWidth() 获取drawable资源图片呈现的宽度
|
if (event.getX() > et_search_key.getWidth()
|
- et_search_key.getPaddingRight()
|
- drawable.getIntrinsicWidth()) {
|
et_search_key.setText("");
|
}
|
return false;
|
}
|
});
|
|
gv_history_search.setOnItemClickListener(new OnItemClickListener() {
|
|
@Override
|
public void onItemClick(AdapterView<?> parent, View view,
|
int position, long id) {
|
Intent intent = new Intent(SearchActivity.this,
|
SearchResultActivity.class);
|
intent.putExtra("key",
|
(String) parent.getItemAtPosition(position));
|
startActivity(intent);
|
}
|
});
|
|
et_search_key.setHint(getIntent().getStringExtra("key"));
|
|
getHotSearch();
|
getHistorySearch();
|
initAdvertisement();// 广点通广告
|
}
|
|
private void fillHotSearch(List<String> hotSearchs) {
|
fl_hot_search.removeAllViews();
|
int p = 0;
|
for (String hotSearch : hotSearchs) {
|
final TextView tv = new TextView(this);
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
|
params.setMargins(0, DimenUtils.dip2px(this, 8), DimenUtils.dip2px(this, 12), 0);
|
tv.setLayoutParams(params);
|
tv.setLines(1);
|
tv.setTextColor(getResources().getColor(R.color.theme));
|
tv.setEllipsize(TextUtils.TruncateAt.END);
|
tv.setBackgroundResource(R.drawable.shape_hot_search_bg);
|
tv.setPadding(DimenUtils.dip2px(this, 12), DimenUtils.dip2px(this, 10), DimenUtils.dip2px(this, 12), DimenUtils.dip2px(this, 10));
|
tv.setText(hotSearch);
|
tv.setTextSize(13);
|
|
|
tv.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
Intent intent = new Intent(SearchActivity.this,
|
SearchResultActivity.class);
|
intent.putExtra("key",
|
hotSearch);
|
startActivity(intent);
|
}
|
});
|
|
int drawableRight = -1;
|
if (p == 0 || p == 2 || p == 4) {
|
drawableRight = R.drawable.ic_hot_search_type_hot;
|
} else if (p == 3) {
|
drawableRight = R.drawable.ic_hot_search_type_new;
|
} else if (p == 5) {
|
drawableRight = R.drawable.ic_hot_search_type_recommend;
|
}
|
|
if (drawableRight != -1) {
|
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRight, 0);
|
tv.setCompoundDrawablePadding(DimenUtils.dip2px(this,8));
|
}
|
|
|
fl_hot_search.addView(tv);
|
|
|
p++;
|
}
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
getHistorySearch();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
@Override
|
public void onClick(final View v) {
|
switch (v.getId()) {
|
case R.id.tv_search_cancel:
|
finish();
|
break;
|
case R.id.tv_search_clear_his:
|
clearHistorySearch();
|
break;
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
default:
|
break;
|
}
|
}
|
|
private void suggestSearch(String key) {
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.suggestSearch(this, uid, key,
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
List<String> results = gson.fromJson(jsonObject
|
.getJSONObject("Data").getJSONArray("data")
|
.toString(), new TypeToken<List<String>>() {
|
}.getType());
|
|
SuggestKeysAdapter adapter = new SuggestKeysAdapter(getApplicationContext(), results);
|
et_search_key.setAdapter(adapter);
|
adapter.notifyDataSetChanged();
|
}
|
}
|
});
|
}
|
|
private void getHistorySearch() {
|
SharedPreferences historySp = getSharedPreferences("search_history",
|
Context.MODE_PRIVATE);
|
String historyStr = historySp.getString("history", "");
|
if (!StringUtils.isBlank(historyStr)) {
|
String[] historyArray = historyStr.split(",");
|
ll_search_history.setVisibility(View.VISIBLE);
|
List<String> history = new ArrayList<String>();
|
for (int i = 0; i < 10 && i < historyArray.length; i++) {
|
if (!TextUtils.isEmpty(historyArray[historyArray.length - 1 - i])) {
|
history.add(historyArray[historyArray.length - 1 - i]);
|
}
|
}
|
gv_history_search.setAdapter(new GridSearchRecordAdapter(history, false));
|
} else {
|
ll_search_history.setVisibility(View.GONE);
|
}
|
}
|
|
private void clearHistorySearch() {
|
SharedPreferences historySp = getSharedPreferences("search_history",
|
Context.MODE_PRIVATE);
|
Editor ed = historySp.edit();
|
ed.putString("history", "");
|
ed.commit();
|
getHistorySearch();
|
}
|
|
private void getHotSearch() {
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
HttpApiUtil.getHotSearch(this, uid,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
List<String> hotSearchs = gson.fromJson(jsonObject
|
.getJSONObject("Data").getJSONArray("data")
|
.toString(), new TypeToken<List<String>>() {
|
}.getType());
|
if (hotSearchs != null && hotSearchs.size() > 0) {
|
String key = hotSearchs.get((int) (Math.random() * hotSearchs.size()));
|
et_search_key.setText(key);
|
}
|
fillHotSearch(hotSearchs);
|
}
|
}
|
});
|
}
|
|
/**
|
* 广点通广告
|
*/
|
private FrameLayout fl_advertisement;
|
|
// 1.加载广告,先设置加载上下文环境和条件
|
private void initAdvertisement() {
|
ExpressAdManager.ExpressAdInfo adInfo = new ExpressAdManager.ExpressAdInfo.Builder().setWidth(ADSize.FULL_WIDTH).setHeight(ADSize.AUTO_HEIGHT).setAdType(AdUtil.getAdType(getApplicationContext(), AdPositionEnum.videoSearch.name())).build();
|
if (adInfo.getAdType() == AdUtil.AD_TYPE.csj) {
|
adInfo.setPid(CSJConstant.PID_VIDEO_SEARCH);
|
} else {
|
adInfo.setPid(GDTConstant.PID_VIDEO_SEARCH);
|
}
|
ExpressAdManager.getInstance(getApplicationContext()).loadAndRender(this, adInfo, fl_advertisement, null);
|
}
|
|
}
|