| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | |
| | | FrameLayout fl_indicator; |
| | | |
| | | private DiskLruCache cache; |
| | | |
| | | public HomeFragment() { |
| | | super(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(); |
| | | } |
| | |
| | | public void onClick(DialogInterface dialog, int which) { |
| | | dialog.dismiss(); |
| | | UserUtil.agreeUserProtocol(getContext()); |
| | | showFloatImgAd(); |
| | | } |
| | | }).setData(data).create().show(); |
| | | } else { |
| | | showFloatImgAd(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 展示悬浮广告 |
| | | */ |
| | | 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) { |
| | | |
| | | 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() { |
| | | builder.create().show(); |
| | | } |
| | | }, 2000); |
| | | |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onLoadCleared(@Nullable Drawable placeholder) { |
| | | |
| | | } |
| | | |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | public static int getVersionNum(Context context) { |
| | | try { |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (snapshot != null) { |
| | | snapshot.close(); |
| | | } |
| | | |
| | | 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()); |
| | | categories.addAll(list); |
| | | } |
| | | |
| | | adapter = new HomePageAdapter( |
| | |
| | | |
| | | |
| | | 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)); |
| | | } |
| | | } |
| | | |