admin
2021-02-03 1981dee5aec45793d3c4ebdbc4e637528c71b3c5
BuWanVideo/src/com/weikou/beibeivideo/ui/main/HomeFragment.java
@@ -5,7 +5,10 @@
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
@@ -14,26 +17,32 @@
import android.view.View;
import android.widget.FrameLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.util.cache.DiskLruCache;
import com.lcjian.library.util.common.StorageUtils;
import com.lcjian.library.util.common.StringUtils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.MainTabPageIndicator;
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.BeibeiVideoApplication;
import com.weikou.beibeivideo.R;
import com.weikou.beibeivideo.entity.JumpDetail;
import com.weikou.beibeivideo.entity.VideoType;
import com.weikou.beibeivideo.entity.recommend.HomeClass;
import com.weikou.beibeivideo.ui.MyRetainViewFragment;
import com.weikou.beibeivideo.ui.dialog.FloatADDialog;
import com.weikou.beibeivideo.ui.dialog.UserProtocolDialog;
import com.weikou.beibeivideo.ui.recommend.NovelJNZFragment;
import com.weikou.beibeivideo.ui.recommend.RecommendFragment;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.weikou.beibeivideo.util.UserUtil;
import com.weikou.beibeivideo.util.cache.DiskLruCacheManager;
import org.apache.http.Header;
import org.json.JSONObject;
@@ -63,55 +72,78 @@
    FrameLayout fl_indicator;
    private DiskLruCache cache;
    public HomeFragment() {
        super(HomeFragment.class.getClass().getName());
        super();
        setName(HomeFragment.class.getClass().getName());
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            cache = DiskLruCache.open(
                    new File(StorageUtils.getCacheDirectory(getContext())
                            .toString(), "http"), getVersionNum(getContext()),
                    1, 1024 * 1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!BeibeiConstant.IS_TEST)
            showProtocol();
            showFloatImgAd();
    }
    private void showProtocol() {
        String data = "感谢您对" + getResources().getString(R.string.app_name) + "一直以来的信任!<br>";
        data += "我们依照新的监管要求条例更新了";
        data += String.format("<a href='%s'>《隐私政策》</a>和", BeibeiConstant.PRIVACY_POLICY);
        data += String.format("<a href='%s'>《用户协议》</a>", BeibeiConstant.USER_AGREEMENT);
        data += "为采取相应安全保护措施,尽力保护您的个人信息安全可控,特向你说明如下:<br>" +
                "1、您在使用" + getResources().getString(R.string.app_name) + "各项产品或服务时,将会提供与具体功能相关的个人信息(可能涉及帐号、位置、存储等信息)。<br>" +
                "2、您可以随时查询、更正、删除您的个人信息,我们也提供账户注销的渠道。<br>" +
                "3、未经您的再次同意,我们不会将上述信息用于您未授权的其他用途或目的。<br>" +
                "4、未经监护人同意,我们不会收集使用14周岁以下(含14周岁)未成年人个人信息,且不会利用其信息推送新闻、时政信息、广告等定向推送活动。";
    /**
     * 展示悬浮广告
     */
    private void showFloatImgAd() {
        BeibeiVideoAPI.getHomeConfig(getContext(), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optBoolean("IsPost")) {
                    JSONObject data = jsonObject.optJSONObject("Data");
                    if (data != null) {
        if (!UserUtil.isAgreeUserProtocol(getContext())) {
            new UserProtocolDialog.Builder(getActivity()).setNegativeButton("不同意", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    getActivity().finish();
                        JSONObject ad = data.optJSONObject("floatAD");
                        if (ad != null) {
                            String id = ad.optString("id");
                            if (FloatADDialog.Builder.canShow(getContext(), id)) {
                                String picture = ad.optString("picture");
                                Glide.with(getContext()).load(picture).into(new SimpleTarget<Drawable>() {
                                    @Override
                                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                        JSONObject jumpDetailJSON = ad.optJSONObject("jumpDetail");
                                        JSONObject params = ad.optJSONObject("params");
                                        JumpDetail jumpDetail = new Gson().fromJson(jumpDetailJSON.toString(), JumpDetail.class);
                                        final FloatADDialog.Builder builder = new FloatADDialog.Builder(getActivity())
                                                .setId(id).setImage(resource);
                                        if (params != null) {
                                            builder.setJump(jumpDetail, com.alibaba.fastjson.JSONObject.parseObject(params.toString()));
                                        }
                                        fl_indicator.postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    builder.create().show();
                                                } catch (Exception e) {
                                                }
                                            }
                                        }, 2000);
                                    }
                                    @Override
                                    public void onLoadCleared(@Nullable Drawable placeholder) {
                                    }
                                });
                            }
                        }
                    }
                }
            }).setPositiveButton("同意", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    UserUtil.agreeUserProtocol(getContext());
                }
            }).setData(data).create().show();
        }
            }
        });
    }
    public static int getVersionNum(Context context) {
        try {
@@ -136,26 +168,23 @@
        if (fl_indicator.getChildCount() > 0) {
            fl_indicator.removeAllViews();
        }
        DiskLruCache.Snapshot snapshot = null;
        try {
            if (cache != null) {
                snapshot = cache.get(getKey("getCategories"));
                if (snapshot != null) {
                    Gson gson = new GsonBuilder().setFieldNamingPolicy(
                            FieldNamingPolicy.UPPER_CAMEL_CASE).create();
                    List<VideoType> list = gson.fromJson(
                            snapshot.getString(0),
                            new TypeToken<List<VideoType>>() {
                            }.getType());
//                    categories.addAll(list);
        String cacheValue = DiskLruCacheManager.getInstance(BeibeiVideoApplication.application).getCache(getKey("getCategories"));
        if (!StringUtils.isBlank(cacheValue)) {
            Gson gson = new GsonBuilder().setFieldNamingPolicy(
                    FieldNamingPolicy.UPPER_CAMEL_CASE).create();
            List<HomeClass> list = gson.fromJson(
                    cacheValue,
                    new TypeToken<List<HomeClass>>() {
                    }.getType());
            if (list != null)
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getDataType().equalsIgnoreCase("novel")) {
                        list.remove(i--);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (snapshot != null) {
                snapshot.close();
            }
            categories.addAll(list);
        }
        adapter = new HomePageAdapter(
@@ -223,6 +252,13 @@
                                    }.getType());
                            categories.addAll(list);
                            if (categories != null)
                                for (int i = 0; i < categories.size(); i++) {
                                    if (categories.get(i).getDataType().equalsIgnoreCase("novel")) {
                                        categories.remove(i--);
                                    }
                                }
                            adapter.notifyDataSetChanged();
                            indicator.notifyDataSetChanged();
@@ -230,13 +266,7 @@
                            count++;
                            try {
                                DiskLruCache.Editor editor = cache
                                        .edit(getKey("getCategories"));
                                editor.set(0, gson.toJson(categories));
                                editor.commit();
                            } catch (IOException e) {
                            }
                            DiskLruCacheManager.getInstance(BeibeiVideoApplication.application).cache(getKey("getCategories"), gson.toJson(categories));
                        }
                    }
@@ -262,12 +292,10 @@
        @Override
        public Fragment getItem(int position) {
            if ("novel".equalsIgnoreCase(mlist.get(position).getDataType())) {
                return NovelJNZFragment.newInstance();
            } else {
                Fragment fragment = RecommendFragment.newInstance(mlist.get(position), position + "");
                return fragment;
            }
            Fragment fragment = RecommendFragment.newInstance(mlist.get(position), position + "");
            return fragment;
        }
        @Override