77import android .content .res .Configuration ;
88import android .os .Build ;
99import android .os .Bundle ;
10- import android .os .Handler ;
11- import android .os .Message ;
1210import android .text .TextUtils ;
1311import android .util .Log ;
1412import android .view .KeyEvent ;
2624import android .widget .ProgressBar ;
2725import android .widget .RelativeLayout ;
2826
29- import com .brian .codeblog .Config ;
3027import com .brian .codeblog .Env ;
3128import com .brian .codeblog .datacenter .preference .CommonPreference ;
3229import com .brian .codeblog .datacenter .preference .SettingPreference ;
3330import com .brian .codeblog .manager .AdHelper ;
3431import com .brian .codeblog .manager .BlogManager ;
3532import com .brian .codeblog .manager .BlogerManager ;
36- import com .brian .codeblog .manager .DataFetcher ;
37- import com .brian .codeblog .manager .DataFetcher .OnFetchDataListener ;
38- import com .brian .codeblog .manager .DataFetcher .Result ;
3933import com .brian .codeblog .manager .DataManager ;
4034import com .brian .codeblog .manager .ShareManager ;
4135import com .brian .codeblog .manager .ThreadManager ;
4640import com .brian .codeblog .model .SearchResult ;
4741import com .brian .codeblog .parser .BlogHtmlParserFactory ;
4842import com .brian .codeblog .parser .IBlogHtmlParser ;
43+ import com .brian .codeblog .proctocol .HttpGetBlogContentRequest ;
44+ import com .brian .codeblog .proctocol .base .IResponseCallback ;
4945import com .brian .codeblog .util .FileUtil ;
5046import com .brian .codeblog .util .LogUtil ;
5147import com .brian .codeblog .util .NetStatusUtil ;
5248import com .brian .codeblog .util .ToastUtil ;
53- import com .brian .codeblog .util .WeakRefHandler ;
5449import com .brian .common .view .TitleBar ;
5550import com .brian .csdnblog .R ;
5651import com .tencent .connect .share .QQShare ;
6358import tj .zl .op .normal .spot .SpotListener ;
6459import tj .zl .op .normal .spot .SpotManager ;
6560
66- public class BlogContentActivity extends BaseActivity implements OnFetchDataListener < Result < String >> {
61+ public class BlogContentActivity extends BaseActivity {
6762
6863 private static final String TAG = BlogContentActivity .class .getSimpleName ();
6964
@@ -77,6 +72,8 @@ public class BlogContentActivity extends BaseActivity implements OnFetchDataList
7772
7873 private IBlogHtmlParser mBlogParser = null ;
7974
75+ private HttpGetBlogContentRequest mHttpClient ;
76+
8077 private boolean mHasFavoed ;
8178
8279 private long mStartTime ;
@@ -140,17 +137,69 @@ protected void onCreate(Bundle savedInstanceState) {
140137
141138 mProgressBar .setVisibility (View .VISIBLE );
142139
140+ mHttpClient = new HttpGetBlogContentRequest ();
143141 getUIHandler ().post (new Runnable () {
144142 @ Override
145143 public void run () {
146- // 开始请求数据
147- if (TypeManager .getWebType (mBlogInfo .type ) == TypeManager .TYPE_WEB_JCC ) {
148- BlogManager .getInstance ().fetchBlogContent (mCurrentUrl , "GB2312" , BlogContentActivity .this );
144+ loadData ();
145+ BlogManager .getInstance ().saveBlog (mBlogInfo );
146+ }
147+ });
148+ }
149+
150+ private void loadData () {
151+ HttpGetBlogContentRequest .RequestParam param = new HttpGetBlogContentRequest .RequestParam ();
152+ param .url = mCurrentUrl ;
153+ param .type = mBlogInfo .type ;
154+ if (TypeManager .getWebType (mBlogInfo .type ) == TypeManager .TYPE_WEB_JCC ) {
155+ param .charset = "GB2312" ;
156+ }
157+ mHttpClient .request (param , new IResponseCallback <HttpGetBlogContentRequest .ResultData >() {
158+ @ Override
159+ public void onSuccess (final HttpGetBlogContentRequest .ResultData resultData ) {
160+ LogUtil .d ("resultData=" + resultData .blogContent );
161+ if (TextUtils .isEmpty (resultData .blogContent )) {
162+ showErrorPage ();
163+ return ;
164+ }
165+ if (mCurrentUrl .equalsIgnoreCase (mBlogInfo .link )) {
166+ saveBlog (resultData .blogContent );
167+ }
168+
169+ toggleAdShow (false );// 隐藏广告
170+ mReLoadImageView .setVisibility (View .GONE );
171+ mProgressBar .setVisibility (View .GONE );
172+
173+ CommonPreference .getInstance ().addBlogReadCount ();// 阅读数+1
174+ mBlogStack .push (resultData .blogContent );
175+
176+ mHasFavoed = BlogManager .getInstance ().isFavo (mBlogInfo );
177+ if (mHasFavoed ) {
178+ mPopupMenu .getMenu ().getItem (1 ).setTitle ("取消收藏" );
149179 } else {
150- BlogManager . getInstance ().fetchBlogContent ( mCurrentUrl , BlogContentActivity . this );
180+ mPopupMenu . getMenu ().getItem ( 1 ). setTitle ( "收藏" );
151181 }
182+ mWebView .setVisibility (View .VISIBLE );
183+ mWebView .loadDataWithBaseURL (mBlogParser .getBlogBaseUrl (), resultData .blogContent ,
184+ "text/html" , "utf-8" , null );
152185
153- BlogManager .getInstance ().saveBlog (mBlogInfo );
186+ String title = mBlogParser .getBlogTitle (mBlogInfo .type , resultData .blogContent );
187+ if (!TextUtils .isEmpty (title )) {
188+ mCurrentTitle = title ;
189+ mTitleBar .setTitle (mCurrentTitle );
190+ }
191+ }
192+
193+ @ Override
194+ public void onError (int rtn , String msg ) {
195+ LogUtil .d ("resultData=" + msg );
196+ showErrorPage ();
197+ }
198+
199+ @ Override
200+ public void onFailure (int errorCode , String msg ) {
201+ LogUtil .d ("errorCode=" + errorCode );
202+ showErrorPage ();
154203 }
155204 });
156205 }
@@ -175,6 +224,18 @@ private void initBlogInfo() {
175224 mBlogInfo .link = mCurrentUrl ;
176225 }
177226
227+ private void saveBlog (final String blogContent ) {
228+ ThreadManager .getPoolProxy ().execute (new Runnable () {
229+ @ Override
230+ public void run () {
231+ String cachePath = DataManager .getBlogCachePath (mBlogInfo .blogId );
232+ mBlogInfo .localPath = cachePath ;
233+ FileUtil .writeFile (cachePath , blogContent );
234+ BlogManager .getInstance ().saveBlog (mBlogInfo );
235+ }
236+ });
237+ }
238+
178239 private void initAd () {
179240 SpotManager .getInstance (Env .getContext ()).setImageType (SpotManager .IMAGE_TYPE_VERTICAL );
180241 // 获取原生插屏控件
@@ -355,7 +416,7 @@ public void onClick(View v) {
355416 mProgressBar .setVisibility (View .VISIBLE );
356417 toggleAdShow (true );
357418
358- DataFetcher . getInstance (). fetchString ( mCurrentUrl , BlogContentActivity . this );
419+ loadData ( );
359420 }
360421 });
361422 }
@@ -369,18 +430,11 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
369430 return true ;
370431 }
371432
372- if (mReLoadImageView .getVisibility () == View .VISIBLE ) {
373- mReLoadImageView .setVisibility (View .GONE );
374- mWebView .setVisibility (View .VISIBLE );
375- return true ;
376- }
377433 if (mBlogStack .size () > 1 ) {
378434 mBlogStack .pop ();//把当前的博客移除
379435 String html = mBlogStack .peek ();
380- Message msg = mHandler .obtainMessage (MSG_UPDATE );
381- msg .obj = html ;
382- mHandler .removeMessages (MSG_UPDATE );
383- mHandler .sendMessage (msg );
436+ mWebView .loadDataWithBaseURL (mBlogParser .getBlogBaseUrl (), html ,
437+ "text/html" , "utf-8" , null );
384438 return true ;
385439 } else {
386440 this .finish ();
@@ -412,7 +466,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
412466 toggleAdShow (true );
413467
414468 mCurrentUrl = blogUrl ;
415- BlogManager . getInstance (). fetchBlogContent ( blogUrl , BlogContentActivity . this );
469+ loadData ( );
416470 return true ;
417471 }
418472 return false ;
@@ -477,85 +531,17 @@ protected void onDestroy() {
477531 mWebView .stopLoading ();
478532 mWebView .onPause ();
479533 mWebView .destroy ();
480-
481- mHandler .removeCallbacksAndMessages (null );
482534 }
483535
484536 private void showErrorPage () {
485537 UsageStatsManager .sendUsageData (UsageStatsManager .EXP_EMPTY_BLOG , TypeManager .getBlogName (mBlogInfo .type ));
486538
487- mWebView .setVisibility (View .INVISIBLE );
488- mProgressBar .setVisibility (View .INVISIBLE );
539+ mWebView .setVisibility (View .GONE );
540+ mProgressBar .setVisibility (View .GONE );
489541 mReLoadImageView .setVisibility (View .VISIBLE );
490542 }
491543
492- private static final int MSG_UPDATE = 1 ;
493- private Handler .Callback mCallback = new Handler .Callback () {
494-
495- @ Override
496- public boolean handleMessage (Message msg ) {
497- switch (msg .what ) {
498- case MSG_UPDATE :
499- toggleAdShow (false );// 隐藏广告
500- mReLoadImageView .setVisibility (View .GONE );
501- mProgressBar .setVisibility (View .GONE );
502-
503- mHasFavoed = BlogManager .getInstance ().isFavo (mBlogInfo );
504- if (mHasFavoed ) {
505- mPopupMenu .getMenu ().getItem (1 ).setTitle ("取消收藏" );
506- } else {
507- mPopupMenu .getMenu ().getItem (1 ).setTitle ("收藏" );
508- }
509- mWebView .setVisibility (View .VISIBLE );
510- String content = (String ) msg .obj ;
511- mWebView .loadDataWithBaseURL (mBlogParser .getBlogBaseUrl (), content ,
512- "text/html" , "utf-8" , null );
513-
514- mCurrentTitle = mBlogParser .getBlogTitle (mBlogInfo .type , content );
515- if (!TextUtils .isEmpty (mCurrentTitle )) {
516- mTitleBar .setTitle (mCurrentTitle );
517- }
518- break ;
519- }
520- return true ;
521- }
522- };
523- private Handler mHandler = new WeakRefHandler (mCallback );
524-
525- @ Override
526- public void onFetchFinished (final Result <String > response ) {
527- LogUtil .i ("response=" + response .data );
528- if (TextUtils .isEmpty (response .data )) {
529- showErrorPage ();
530- } else {
531- ThreadManager .getPoolProxy ().execute (new Runnable () {
532- @ Override
533- public void run () {
534- if (Config .isDebug ) {
535- FileUtil .writeFile ("/sdcard/blogBefore" , response .data );
536- }
537- String contentHtml = mBlogParser .getBlogContent (mBlogInfo .type , response .data );
538- if (TextUtils .isEmpty (contentHtml )) { // 解析失败则直接显示原网页
539- contentHtml = response .data ;
540- }
541- if (response .url .equalsIgnoreCase (mBlogInfo .link )) {
542- String cachePath = DataManager .getBlogCachePath (mBlogInfo .blogId );
543- mBlogInfo .localPath = cachePath ;
544- FileUtil .writeFile (cachePath , contentHtml );
545- BlogManager .getInstance ().saveBlog (mBlogInfo );
546- }
547-
548- CommonPreference .getInstance ().addBlogReadCount ();// 阅读数+1
549- mBlogStack .push (contentHtml );
550544
551- Message msg = mHandler .obtainMessage (MSG_UPDATE );
552- msg .obj = contentHtml ;
553- mHandler .sendMessage (msg );
554- } // end run
555- });
556- }
557- }
558-
559545 @ Override
560546 public void onConfigurationChanged (Configuration newConfig ) {
561547 super .onConfigurationChanged (newConfig );
0 commit comments