package com.tejia.lijin.app.ui.trends;
|
|
import android.Manifest;
|
import android.annotation.SuppressLint;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.drawable.AnimationDrawable;
|
import android.media.MediaScannerConnection;
|
import android.os.Bundle;
|
import android.os.Environment;
|
import android.os.Handler;
|
import android.os.Message;
|
import android.provider.Settings;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
|
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.wpc.library.RetainViewFragment;
|
import com.wpc.library.content.ConnectivityChangeHelper;
|
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
|
import com.wpc.library.util.NetUtils;
|
import com.wpc.library.util.RefreshLayout;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.wpc.library.util.common.StringUtils;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.callBack.PermissionInterface;
|
import com.tejia.lijin.app.entity.HomeBanner;
|
import com.tejia.lijin.app.entity.TrendsCategory;
|
import com.tejia.lijin.app.entity.common.ImageShowEntity;
|
import com.tejia.lijin.app.entity.trends.SendCircleCommment;
|
import com.tejia.lijin.app.entity.trends.SendCircleContent;
|
import com.tejia.lijin.app.entity.trends.SendCircleImage;
|
import com.tejia.lijin.app.ui.dialog.MultiImageDownloadDialog;
|
import com.tejia.lijin.app.ui.dialog.SendCircleShareDialog;
|
import com.tejia.lijin.app.ui.dialog.ShapeLoadingDialog;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
import com.tejia.lijin.app.util.PermissionHelper;
|
import com.tejia.lijin.app.util.baichuan.AlibcTradeUtil;
|
import com.tejia.lijin.app.util.clipboard.ClipboardUtil;
|
import com.tejia.lijin.app.util.user.UserUtil;
|
import com.ysh.wpc.appupdate.download.DownLoadFile;
|
import com.ysh.wpc.appupdate.download.FileUtils;
|
|
import org.apache.http.Header;
|
import org.json.JSONArray;
|
import org.json.JSONObject;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 动态--活动
|
*/
|
|
public class SendCircleFragmet extends RetainViewFragment implements SendCircleAdapter.AdapterEventActionListener, PermissionInterface {
|
|
|
private RefreshLayout srl_send_circle;
|
private RecyclerView rv_send_circle;
|
private List<HomeBanner> mBannerList = new ArrayList<>();
|
private List<SendCircleContent> mContentList = new ArrayList<>();
|
private int page = 1;
|
private ImageView iv_slid_top;
|
private View v_divider;
|
private int count = 0;
|
private SendCircleAdapter adapter;
|
private boolean isLoad = false;
|
private View bottom;
|
private TextView tv_loading;
|
private ImageView iv_loading;
|
private ConnectivityChangeHelper mChangeHelper;
|
private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
|
private boolean haveCache = false;
|
private String TYPE = "";
|
private TextView tv_notice_content, tv_no_data_hint;
|
private LinearLayout ll_notice;
|
private ImageView iv_close, iv_no_data;
|
private TrendsCategory mCategory;
|
private String cId;
|
private String cate;
|
private PermissionHelper mPermissionsChecker; // 权限检测器
|
|
//上次复制的类型
|
private Map<String, Integer> copyTypeMap = new HashMap<>();
|
|
public static SendCircleFragmet newInstance(String cate, String cId) {
|
SendCircleFragmet fragment = new SendCircleFragmet();
|
Bundle bundle = new Bundle();
|
bundle.putString("cid", cId);
|
bundle.putString("cate", cate);
|
fragment.setArguments(bundle);
|
return fragment;
|
}
|
|
@Override
|
public int getContentResource() {
|
return R.layout.fragment_send_circle;
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
|
mCategory = (TrendsCategory) getArguments().getSerializable("category");
|
cId = getArguments().getString("cid", "0");
|
cate = getArguments().getString("cate");
|
|
ll_no_net = contentView.findViewById(R.id.ll_no_net);
|
ll_no_data = contentView.findViewById(R.id.ll_no_data);
|
ll_request_failture = contentView.findViewById(R.id.ll_request_failture);
|
v_divider = contentView.findViewById(R.id.v_divider);
|
|
srl_send_circle = contentView.findViewById(R.id.srl_send_circle);
|
rv_send_circle = contentView.findViewById(R.id.lv_send_circle);
|
|
tv_notice_content = contentView.findViewById(R.id.tv_content);
|
ll_notice = contentView.findViewById(R.id.ll_notice);
|
ll_notice.setVisibility(View.GONE);
|
iv_close = contentView.findViewById(R.id.iv_left);
|
tv_no_data_hint = contentView.findViewById(R.id.tv_no_data_hint);
|
/**小黄条*/
|
getAppPageNotification();
|
|
iv_slid_top = contentView.findViewById(R.id.iv_slid_top);
|
float hRatio = 260 / 720f;
|
float wRatio = 23 / 720f;
|
int bottomPo = (int) (DimenUtils.getScreenWidth(getActivity()) * hRatio);
|
int rightPo = (int) (DimenUtils.getScreenWidth(getActivity()) * wRatio);
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(DimenUtils.dip2px(getActivity(), 40), DimenUtils.dip2px(getActivity(), 40));
|
params.setMargins(0, 0, rightPo, bottomPo);
|
params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
|
iv_slid_top.setLayoutParams(params);
|
iv_slid_top.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
rv_send_circle.getLayoutManager().scrollToPosition(0);
|
iv_slid_top.setVisibility(View.GONE);
|
}
|
});
|
|
rv_send_circle.setLayoutManager(new LinearLayoutManager(srl_send_circle.getContext()));
|
DividerItemDecoration decoration = new DividerItemDecoration();
|
decoration.setSize(DimenUtils.dip2px(rv_send_circle.getContext(), 6));
|
rv_send_circle.addItemDecoration(decoration);
|
|
|
v_divider = contentView.findViewById(R.id.v_divider);
|
v_divider.setVisibility(StringUtils.isEmpty(cate) ? View.GONE : View.VISIBLE);
|
bottom = LayoutInflater.from(srl_send_circle.getContext()).inflate(R.layout.item_recyclerview_bottom, null);
|
pd = new ShapeLoadingDialog.Builder(getContext()).build();
|
//TODO 添加加载动画
|
// adapter.addFooterView(bottom);
|
bottom.setVisibility(View.GONE);
|
tv_loading = bottom.findViewById(R.id.tv_loading);
|
iv_loading = bottom.findViewById(R.id.iv_loading);
|
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
|
R.drawable.anim_list_loading);
|
iv_loading.setImageDrawable(animationDrawable);
|
animationDrawable.start();
|
// Glide.with(this).load(R.drawable.gif_loading).into(iv_loading);
|
bottom.setVisibility(View.GONE);
|
srl_send_circle.setColorSchemeColors(getResources().getColor(R.color.theme));
|
contentView.findViewById(R.id.tv_net_setting).setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
srl_send_circle.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
}
|
});
|
contentView.findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
rv_send_circle.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
}
|
});
|
mChangeHelper = new ConnectivityChangeHelper(srl_send_circle.getContext(),
|
new ConnectivityChangeHelper.OnConnectivityChangeListener() {
|
|
@Override
|
public void onNetworkUnAvailable() {
|
if (mContentList.size() == 0 && mBannerList.size() == 0)
|
requestState(3);
|
}
|
|
@Override
|
public void onNetworkAvailable() {
|
if ((mContentList.size() == 0 && mBannerList.size() == 0) || haveCache) {
|
getContentList();
|
}
|
}
|
});
|
rv_send_circle.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int first = manager.findFirstVisibleItemPosition();
|
int last = manager.findLastVisibleItemPosition();
|
int total = manager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if (first == 0) {
|
iv_slid_top.setVisibility(View.GONE);
|
}
|
if (last == total - 1 && mContentList.size() < count && isLoad) {
|
page++;
|
isLoad = false;
|
iv_loading.setVisibility(View.VISIBLE);
|
tv_loading.setText(getResources().getString(R.string.loading_more_text));
|
getContentList();
|
}
|
}
|
// else {
|
// iv_slid_top.setVisibility(View.GONE);
|
// }
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
if (Math.abs(dy) > 10) {
|
if (dy > 0) {
|
iv_slid_top.setVisibility(View.GONE);
|
} else {
|
iv_slid_top.setVisibility(View.VISIBLE);
|
}
|
}
|
}
|
});
|
srl_send_circle.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
getContentList();
|
}
|
});
|
rv_send_circle.setAdapter(adapter);
|
srl_send_circle.setRefreshing(true);
|
mPermissionsChecker = new PermissionHelper(getActivity(), this);
|
// handler_timeCurrent.sendEmptyMessage(1001);
|
}
|
|
private boolean isCancel = false;//来标识是fragment否退出
|
//这里很重要,使用Handler的延时效果,每隔一秒刷新一下适配器,以此产生倒计时效果
|
@SuppressLint("HandlerLeak")
|
private Handler handler_timeCurrent = new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
//isCancel,防止界面销毁时,再一次改变UI
|
if (isCancel || handler_timeCurrent == null) {
|
return;
|
}
|
if (msg.what == 1001) {
|
handler_timeCurrent.sendEmptyMessageDelayed(1001, 1000);
|
}
|
}
|
};
|
|
|
private void resumeAction() {
|
if (actionParams != null) {
|
switch (actionParams.type) {
|
case ActionParams.TYPE_COPY_COMMENT:
|
copyComment(actionParams.content, actionParams.comment);
|
break;
|
case ActionParams.TYPE_CREATE_ERCODE:
|
createERCode(actionParams.content, actionParams.imgList, actionParams.img, actionParams.position);
|
break;
|
case ActionParams.TYPE_SAVE_IMAGES:
|
saveImgs(actionParams.content);
|
break;
|
case ActionParams.TYPE_SHARE:
|
share(actionParams.content);
|
break;
|
}
|
|
}
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
mChangeHelper.registerReceiver();
|
if (actionCode > 0) {
|
switch (actionCode) {
|
case ACTION_LOGIN://登录
|
actionCode = ACTION_NONE;
|
Long uid = UserUtil.getUid(getContext());
|
if (uid != null && uid > 0)
|
resumeAction();//重新走预备流程
|
break;
|
case ACTION_TBAUTH:
|
actionCode = ACTION_NONE;
|
break;
|
case ACTION_TBH5AUTH://授权
|
actionCode = ACTION_NONE;
|
getTaoBaoH5AuthInfo(new IPrePareSuccessListener() {
|
@Override
|
public void onSuccess() {
|
resumeAction();//重新走预备流程
|
}
|
});
|
break;
|
}
|
}
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
mChangeHelper.unregisterReceiver();
|
}
|
|
private void getAppPageNotification() {
|
ShoppingApi.getAppPageNotification(rv_send_circle.getContext(), TYPE, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optInt("code") == 0) {
|
boolean isClose = jsonObject.optJSONObject("data").optBoolean("canClose");
|
String content = jsonObject.optJSONObject("data").optString("content");
|
final String contentUrl = jsonObject.optJSONObject("data").optString("contentUrl");
|
final String md5 = jsonObject.optJSONObject("data").optString("md5");
|
|
tv_notice_content.setVisibility(!StringUtils.isEmpty(content) ? View.VISIBLE : View.GONE);
|
tv_notice_content.setText(content);
|
final SharedPreferences sp = rv_send_circle.getContext().getSharedPreferences("notice", Context.MODE_PRIVATE);
|
if (!sp.getBoolean(md5, false)) {//false 展示,true不展示
|
ll_notice.setVisibility(View.VISIBLE);
|
} else {
|
ll_notice.setVisibility(View.GONE);
|
}
|
|
if (isClose) {
|
iv_close.setImageDrawable(getResources().getDrawable(R.drawable.ic_notice_close_red));
|
iv_close.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
ll_notice.setVisibility(View.GONE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean(md5, true);
|
editor.commit();
|
}
|
});
|
} else {
|
iv_close.setImageDrawable(getResources().getDrawable(R.drawable.icon_array_right_red));
|
}
|
ll_notice.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (StringUtils.isEmpty(contentUrl))
|
return;
|
Intent intent = new Intent(rv_send_circle.getContext(), ShareBrowserActivity.class);
|
intent.putExtra("url", contentUrl);
|
rv_send_circle.getContext().startActivity(intent);
|
}
|
});
|
} else {
|
ll_notice.setVisibility(View.GONE);
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
});
|
|
|
}
|
|
/**
|
* 发圈内容请求
|
*/
|
private void getContentList() {
|
ShoppingApi.sendCircleContentList(srl_send_circle.getContext(), page, cate, cId + "", new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (page > 1) {
|
bottom.setVisibility(View.VISIBLE);
|
} else {
|
bottom.setVisibility(View.GONE);
|
}
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
ll_request_failture.setVisibility(View.GONE);
|
bottom.setVisibility(View.GONE);
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
JSONObject data = jsonObject.optJSONObject("data");
|
haveCache = false;
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
if (page == 1) {
|
mContentList.clear();
|
|
if (data.optJSONArray("banners") != null) {
|
List<HomeBanner> list = gson.fromJson(
|
data.optJSONArray("banners").toString(),
|
new TypeToken<List<HomeBanner>>() {
|
}.getType());
|
mBannerList.clear();
|
mBannerList.addAll(list);
|
}
|
}
|
if (data.optJSONArray("list") != null) {
|
List<SendCircleContent> circleContentList = gson.fromJson(
|
data.optJSONArray("list").toString(),
|
new TypeToken<List<SendCircleContent>>() {
|
}.getType());
|
if (circleContentList != null) {
|
for (SendCircleContent content : circleContentList) {
|
if (content.getImgList() != null) {
|
List<SendCircleImage> largeImageList = new ArrayList<>();
|
List<SendCircleImage> smallImageList = new ArrayList<>();
|
for (SendCircleImage image : content.getImgList()) {
|
if (image.isLarge())
|
largeImageList.add(image);
|
else
|
smallImageList.add(image);
|
}
|
//兼容每行2个图片的情况
|
if (smallImageList.size() > 0 && content.getLineNum() == 2) {
|
int smallCount = smallImageList.size();
|
int oldSmallCount = smallCount;
|
int line = smallImageList.size() % 2 == 0 ? smallImageList.size() / 2 : smallImageList.size() / 2 + 1;
|
for (int i = 0; i < line; i++) {
|
if (2 + 3 * i < smallCount) {
|
smallImageList.add(2 + 3 * i, null);
|
smallCount++;
|
}
|
}
|
if (oldSmallCount % 2 != 0) {
|
smallImageList.add(null);
|
smallImageList.add(null);
|
}
|
|
}
|
largeImageList.addAll(smallImageList);
|
content.setImgList(largeImageList);
|
}
|
}
|
mContentList.addAll(circleContentList);
|
}
|
}
|
if (adapter == null) {
|
adapter = new SendCircleAdapter(getActivity(), mBannerList, mContentList, SendCircleFragmet.this);
|
rv_send_circle.setRecycledViewPool(new RecyclerView.RecycledViewPool());
|
rv_send_circle.setAdapter(adapter);
|
initLoadingView();
|
} else {
|
adapter.notifyDataSetChanged();
|
}
|
count = data.optInt("count");
|
|
if (mContentList.size() == count) {
|
bottom.setVisibility(View.VISIBLE);
|
iv_loading.setVisibility(View.GONE);
|
tv_loading.setText(getResources().getString(R.string.list_no_more));
|
}
|
requestState((mContentList.size() == 0 && mBannerList.size() == 0) ? 1 : 0);
|
} else {
|
if (mContentList.size() == 0 && mBannerList.size() == 0)
|
requestState(2);
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
srl_send_circle.setRefreshing(false);
|
isLoad = true;
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
bottom.setVisibility(View.GONE);
|
if (NetUtils.getNetworkState(srl_send_circle.getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
Toast.makeText(srl_send_circle.getContext(), "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
|
} else {
|
requestState(2);
|
Toast.makeText(srl_send_circle.getContext(), "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
|
}
|
}
|
});
|
}
|
|
/**
|
* 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
|
*
|
* @param state
|
*/
|
private void requestState(int state) {
|
srl_send_circle.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
|
rv_send_circle.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
|
ll_no_data.setVisibility(state == 1 ? View.VISIBLE : View.GONE);
|
ll_request_failture.setVisibility(state == 2 ? View.VISIBLE : View.GONE);
|
ll_no_net.setVisibility(state == 3 ? View.VISIBLE : View.GONE);
|
}
|
|
//防止当前Activity结束以后, handler依然继续循环浪费资源
|
@Override
|
public void onDestroy() {
|
isCancel = true;
|
handler_timeCurrent.removeCallbacksAndMessages(null);
|
|
//清除分享临时目录
|
String path = Environment.getExternalStorageDirectory()
|
+ "/rebateshare";
|
File storageDir = new File(path);
|
if (storageDir.exists()) {
|
FileUtils.deleteDirectory(storageDir);
|
}
|
super.onDestroy();
|
}
|
|
|
private void initLoadingView() {
|
bottom = LayoutInflater.from(getContext()).inflate(R.layout.item_recyclerview_bottom, null);
|
tv_loading = bottom.findViewById(R.id.tv_loading);
|
iv_loading = bottom.findViewById(R.id.iv_loading);
|
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
|
R.drawable.anim_list_loading);
|
iv_loading.setImageDrawable(animationDrawable);
|
animationDrawable.start();
|
// Glide.with(this).load(R.drawable.gif_loading).into(iv_loading);
|
bottom.setVisibility(View.GONE);
|
adapter.addFooterView(bottom);
|
}
|
|
|
@Override
|
public void copyComment(final SendCircleContent content, SendCircleCommment comment) {
|
actionParams = new ActionParams(content, comment);
|
actionParams.type = ActionParams.TYPE_COPY_COMMENT;
|
prepareAction(new IPrePareSuccessListener() {
|
@Override
|
public void onSuccess() {
|
Long uid = UserUtil.getUid(getContext());
|
ShoppingApi.evaluateCopyComment(getContext(), uid, actionParams.content.getId(), actionParams.comment.getId(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (!pd.isShowing())
|
pd.show();
|
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (!filterResponse(jsonObject))
|
return;
|
if (jsonObject.optInt("code") == 0) {
|
copyTypeMap.put(content.getId(), 2);
|
ClipboardUtil.copy(getContext(), jsonObject.optJSONObject("data").optString("text"));
|
Toast.makeText(getContext(), "复制成功", Toast.LENGTH_SHORT).show();
|
} else {
|
Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
}
|
});
|
}
|
|
@Override
|
public void createERCode(SendCircleContent content, List<SendCircleImage> imgList, SendCircleImage img, final int position) {
|
actionParams = new ActionParams(content, imgList, img, position);
|
actionParams.type = ActionParams.TYPE_CREATE_ERCODE;
|
prepareAction(new IPrePareSuccessListener() {
|
@Override
|
public void onSuccess() {
|
Long uid = UserUtil.getUid(getContext());
|
ShoppingApi.evaluateShare(getContext(), uid, 1, actionParams.content.getId(), actionParams.img.getGoods().getGoodsId(), actionParams.img.getGoods().getGoodsType(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (!pd.isShowing())
|
pd.show();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (!filterResponse(jsonObject))
|
return;
|
if (jsonObject.optInt("code") == 0) {
|
JSONArray list = jsonObject.optJSONObject("data").optJSONArray("list");
|
if (list != null && list.length() > 0) {
|
List<ImageShowEntity> imageEntityList = new ArrayList<>();
|
for (int i = 0; i < list.length(); i++) {
|
imageEntityList.add(new ImageShowEntity(null, list.optString(i)));
|
}
|
getContext().startActivity(new Intent(getContext(), BigImageActivity.class).putExtra("imgList", new Gson().toJson(imageEntityList)).putExtra("position", actionParams.position));
|
}
|
} else {
|
Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
}
|
});
|
}
|
|
private void share(Long uid, final String copyText) {
|
|
String id = actionParams.content.getId();
|
ShoppingApi.evaluateShare(getContext(), uid, 3, id, null, null, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (!pd.isShowing())
|
pd.show();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (!filterResponse(jsonObject))
|
return;
|
if (jsonObject.optInt("code") == 0) {
|
JSONObject data = jsonObject.optJSONObject("data");
|
JSONArray array = data.optJSONArray("list");
|
if (array != null && array.length() > 0) {
|
List<String> imgList = new ArrayList<>();
|
for (int i = 0; i < array.length(); i++) {
|
imgList.add(array.optString(i));
|
}
|
new SendCircleShareDialog.Builder(getActivity()).setCopyText(copyText).setShareImages(imgList).create().show();
|
}
|
} else {
|
Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
|
}
|
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
|
|
}
|
|
@Override
|
public void share(final SendCircleContent content) {
|
actionParams = new ActionParams(content);
|
actionParams.type = ActionParams.TYPE_SHARE;
|
if (mPermissionsChecker.lacksPermissions(getPermissions())) {
|
actionCode = ACTION_AUTH;
|
mPermissionsChecker.showNormalDialog();
|
return;
|
}
|
prepareAction(new IPrePareSuccessListener() {
|
@Override
|
public void onSuccess() {
|
final Long uid = UserUtil.getUid(getContext());
|
//没有点击复制按钮
|
// if (copyType == null) {
|
//复制推荐语
|
share(uid, content.getTitle());
|
// } else {
|
// copyTypeMap.remove(content.getId());
|
// share(uid, null);
|
// }
|
|
|
}
|
});
|
}
|
|
private MultiImageDownloadDialog.Builder downloadProgressDialogBuilder;
|
|
@Override
|
public void saveImgs(SendCircleContent content) {
|
actionParams = new ActionParams(content);
|
actionParams.type = ActionParams.TYPE_SAVE_IMAGES;
|
if (mPermissionsChecker.lacksPermissions(getPermissions())) {
|
actionCode = ACTION_AUTH;
|
mPermissionsChecker.showNormalDialog();
|
return;
|
}
|
prepareAction(new IPrePareSuccessListener() {
|
@Override
|
public void onSuccess() {
|
Long uid = UserUtil.getUid(getContext());
|
ShoppingApi.evaluateShare(getContext(), uid, 2, actionParams.content.getId(), null, null, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (!pd.isShowing())
|
pd.show();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (!filterResponse(jsonObject))
|
return;
|
if (jsonObject.optInt("code") == 0) {
|
JSONArray list = jsonObject.optJSONObject("data").optJSONArray("list");
|
if (list.length() > 0) {
|
//保存图片
|
String rootPath = android.os.Environment.getExternalStorageDirectory()
|
+ "/tejiaapp";
|
File rootFile = new File(rootPath);
|
if (!rootFile.exists()) {
|
rootFile.mkdirs();
|
}
|
List<String> urlList = new ArrayList<>();
|
final List<String> filePathList = new ArrayList<>();
|
for (int i = 0; i < list.length(); i++) {
|
urlList.add(list.optString(i));
|
String fileName = rootPath + "/" + System.currentTimeMillis() + "_" + (long) (Math.random() * 10000000000L) + ".jpg";
|
filePathList.add(fileName);
|
}
|
|
new DownLoadFile().downloadMultiImages(urlList, filePathList, getContext(), new DownLoadFile.FileDownLoadListener() {
|
@Override
|
public void finish(List<File> fileList) {
|
rv_send_circle.post(new Runnable() {
|
@Override
|
public void run() {
|
if (pd.isShowing())
|
pd.dismiss();
|
String[] strings = new String[filePathList.size()];
|
filePathList.toArray(strings);
|
MediaScannerConnection.scanFile(getContext(), strings, null, null);
|
Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show();
|
}
|
});
|
}
|
|
@Override
|
public void process(int count) {
|
final int fcount = count;
|
rv_send_circle.post(new Runnable() {
|
@Override
|
public void run() {
|
}
|
});
|
}
|
});
|
} else {
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
} else {
|
if (pd.isShowing())
|
pd.dismiss();
|
Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
|
}
|
});
|
}
|
});
|
|
}
|
|
@Override
|
public void copyRecommendText(SendCircleContent content) {
|
ClipboardUtil.copy(getContext(), content.getTitle());
|
Toast.makeText(getContext(), "复制成功", Toast.LENGTH_SHORT).show();
|
copyTypeMap.put(content.getId(), 1);
|
}
|
|
|
@Override
|
public void sendOrder(SendCircleContent content) {
|
final String id = content.getId();
|
prepareAction(new IPrePareSuccessListener() {
|
@Override
|
public void onSuccess() {
|
//一键发单
|
ShoppingApi.sendCircle(getContext(), UserUtil.getUid(getContext()), id, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (!pd.isShowing())
|
pd.show();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (!filterResponse(jsonObject))
|
return;
|
if (jsonObject.optInt("code") == 0) {
|
Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();
|
} else {
|
Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
}
|
});
|
}
|
|
int actionCode = ACTION_NONE;
|
|
private final static int ACTION_NONE = 0;//无事件
|
private final static int ACTION_LOGIN = 1;//登录
|
private final static int ACTION_TBAUTH = 2;//淘宝授权
|
private final static int ACTION_TBH5AUTH = 3;//淘宝H5授权
|
private final static int ACTION_AUTH = 4;//存储权限
|
|
private ActionParams actionParams = null;
|
|
private void prepareAction(final IPrePareSuccessListener listener) {
|
Long uid = UserUtil.getUid(getContext());
|
if (uid == null || uid == 0L) {
|
UserUtil.jumpLogin(getContext());
|
actionCode = ACTION_LOGIN;
|
return;
|
}
|
listener.onSuccess();
|
}
|
|
|
private boolean filterResponse(JSONObject root) {
|
if (root.optInt("code") == 3) {
|
Toast.makeText(getContext(), "请授权淘宝", Toast.LENGTH_SHORT).show();
|
getTaoBaoH5AuthInfo(null);
|
return false;
|
}
|
return true;
|
}
|
|
|
private ShapeLoadingDialog pd = null;
|
|
private void getTaoBaoH5AuthInfo(final IPrePareSuccessListener listener) {
|
if (!pd.isShowing())
|
pd.show();
|
Long uid = UserUtil.getUid(getContext());
|
ShoppingApi.getTaoBaoAuthInfo(getContext(), uid , null, "share", true, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optInt("code") == 0) {
|
final String authLink = jsonObject.optJSONObject("data").getString("authLink");
|
if (!StringUtils.isEmpty(authLink)) {
|
if (AlibcLogin.getInstance().isLogin()) {
|
bindTBInfo(authLink);
|
} else {
|
tbLogin(new IPrePareSuccessListener() {
|
@Override
|
public void onSuccess() {
|
bindTBInfo(authLink);
|
}
|
});
|
}
|
}
|
} else if (jsonObject.optInt("code") == 1) {
|
if (listener != null)
|
listener.onSuccess();
|
} else {
|
pd.dismiss();
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
Toast.makeText(getContext(), "请求失败", Toast.LENGTH_SHORT).show();
|
}
|
|
});
|
}
|
|
|
private void bindTBInfo(String authLink) {
|
if (!StringUtils.isEmpty(authLink)) {
|
actionCode = ACTION_TBH5AUTH;
|
AlibcTradeUtil.openAuthLink(getActivity(), authLink);
|
}
|
}
|
|
private void tbLogin(final IPrePareSuccessListener listener) {
|
actionCode = ACTION_TBAUTH;
|
final AlibcLogin alibcLogin = AlibcLogin.getInstance();
|
alibcLogin.showLogin(new AlibcLoginCallback() {
|
@Override
|
public void onSuccess(int i, String s, String s1) {
|
listener.onSuccess();
|
}
|
|
@Override
|
public void onFailure(int code, String msg) {
|
Toast.makeText(getContext(), "淘宝登录失败,请稍候再试",
|
Toast.LENGTH_LONG).show();
|
}
|
});
|
}
|
|
|
class ActionParams {
|
public final static int TYPE_COPY_COMMENT = 1;//复制评论
|
public final static int TYPE_CREATE_ERCODE = 2;//生成二维码
|
public final static int TYPE_SHARE = 3;//分享
|
public final static int TYPE_SAVE_IMAGES = 4;//保存图片
|
int type;
|
SendCircleContent content;
|
SendCircleImage img;
|
SendCircleCommment comment;
|
List<SendCircleImage> imgList;
|
int position;
|
|
public ActionParams(SendCircleContent content, SendCircleCommment comment) {
|
this.content = content;
|
this.comment = comment;
|
}
|
|
public ActionParams(SendCircleContent content, List<SendCircleImage> imgList, SendCircleImage img, int position) {
|
this.content = content;
|
this.img = img;
|
this.imgList = imgList;
|
this.position = position;
|
}
|
|
public ActionParams(SendCircleContent content) {
|
this.content = content;
|
}
|
|
}
|
|
/**
|
* 权限请求码
|
*
|
* @return
|
*/
|
@Override
|
public int getPermissionsRequestCode() {
|
return 1001;
|
}
|
|
/**
|
* 请求权限
|
*
|
* @return
|
*/
|
@Override
|
public String[] getPermissions() {
|
return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
|
Manifest.permission.WRITE_EXTERNAL_STORAGE};
|
}
|
|
/**
|
* 权限请求成功
|
*/
|
@Override
|
public void requestPermissionsSuccess() {
|
resumeAction();
|
}
|
|
/**
|
* 权限请求失败
|
*/
|
@Override
|
public void requestPermissionsFail() {
|
// Toast.makeText(this, "你以拒绝权限", Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
|
|
interface IPrePareSuccessListener {
|
public void onSuccess();
|
}
|