package com.hanju.video.app.ui.recommend;
|
|
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.util.Log;
|
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.hanju.video.app.entity.ad.AdPositionEnum;
|
import com.hanju.video.app.util.ad.AdUtil;
|
import com.hanju.video.app.util.common.AppConfigUtil;
|
import com.hanju.lib.library.util.common.StringUtils;
|
import com.qq.e.ads.cfg.VideoOption;
|
import com.qq.e.ads.nativ.ADSize;
|
import com.qq.e.ads.nativ.NativeExpressAD;
|
import com.qq.e.ads.nativ.NativeExpressADView;
|
import com.qq.e.comm.util.AdError;
|
import com.umeng.analytics.MobclickAgent;
|
import com.hanju.video.app.util.http.BasicTextHttpResponseHandler;
|
import com.hanju.video.app.util.http.HttpApiUtil;
|
import com.hanju.video.app.ui.BaseActivity;
|
import com.hanju.video.app.ui.video.SuggestKeysAdapter;
|
import com.hanju.video.app.util.HanJuConstant;
|
import com.hanju.video.app.R;
|
import com.hanju.video.app.util.ui.StatusBarUtil;
|
|
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 GridView gv_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);
|
gv_hot_search = findViewById(R.id.gv_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.search_actvity);
|
StatusBarUtil.init(this);
|
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_hot_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);
|
}
|
});
|
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();// 广点通广告
|
}
|
|
@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 GridSuggestionAdapter(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());
|
gv_hot_search.setAdapter(new GridSuggestionAdapter(
|
hotSearchs, true));
|
}
|
}
|
});
|
}
|
|
/**
|
* 广点通广告
|
*/
|
private FrameLayout fl_advertisement;
|
|
// 1.加载广告,先设置加载上下文环境和条件
|
private void initAdvertisement() {
|
String appId = AppConfigUtil.getGDTAppId(getApplicationContext());
|
if (com.hanju.video.app.util.downutils.StringUtils.isNullOrEmpty(appId)) {
|
return;
|
}
|
|
String pid = AdUtil.getAdPid(getApplicationContext(), AdPositionEnum.videoSearch);
|
if (com.hanju.video.app.util.downutils.StringUtils.isNullOrEmpty(pid)) {
|
return;
|
}
|
|
NativeExpressAD nativeExpressAD = new NativeExpressAD(SearchActivity.this, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
|
appId, pid, new NativeExpressAD.NativeExpressADListener() {
|
@Override
|
public void onADLoaded(final List<NativeExpressADView> list) {
|
runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
if (null != list && list.size() > 0) {
|
NativeExpressADView adView = list.get(0);
|
adView.render();
|
FrameLayout parent = (FrameLayout) adView.getParent();
|
if (parent != null) {
|
parent.removeAllViews();
|
}
|
fl_advertisement.addView(adView);
|
}
|
}
|
});
|
|
}
|
|
@Override
|
public void onRenderFail(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADExposure(NativeExpressADView nativeExpressADView) {
|
}
|
|
@Override
|
public void onADClicked(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADClosed(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onNoAD(AdError adError) {
|
Log.i(TAG, adError.toString());
|
}
|
}); // 传入Activity
|
// 注意:如果您在联盟平台上新建原生模板广告位时,选择了支持视频,那么可以进行个性化设置(可选)
|
nativeExpressAD.setVideoOption(new VideoOption.Builder()
|
.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
|
.setAutoPlayMuted(true) // 自动播放时为静音
|
.build()); //
|
nativeExpressAD.loadAD(2);
|
}
|
|
}
|