Android Open Source - android.app.niuz.io News Reader Activity






From Project

Back to project page android.app.niuz.io.

License

The source code is released under:

GNU General Public License

If you think the Android project android.app.niuz.io listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.niuz;
//  w w w  .j  a  v a2 s  . c o  m
import io.niuz.model.APIRetrievalStatus;
import io.niuz.model.News;
import io.niuz.services.NewsService;
import io.niuz.services.PhoneService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;

public class NewsReaderActivity extends Activity {
  private static News newsToRead;
  private static Long timeStartedReading;

  private Button backToNewsHeader;
  private ProgressBar anrProgresssBar;
  private WebView webView;
  
  private Context context;
  private Long timeReadInSeconds;

  private AfterNewsReadTask afterNewsReadTask;
  
  private void toast(String message) {
    PhoneService.toast(message, this);
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_reader);
    this.context = this;
    
    this.backToNewsHeader = (Button)findViewById(R.id.backToNewsHeader);
    this.anrProgresssBar = (ProgressBar)findViewById(R.id.anrProgresssBar);
    this.webView = (WebView)findViewById(R.id.webView);
  }

  @Override
  protected void onResume() {
    super.onResume();
    
    if (newsToRead != null) {
      new OnNewsOpenTask().execute();
      
      this.webView.getSettings().setJavaScriptEnabled(true);
      this.webView.loadUrl(newsToRead.getLink());

      this.webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return true;
        }
        
        @Override
        public void onPageStarted(WebView view, String url,
            Bitmap favicon) {
          super.onPageStarted(view, url, favicon);
          anrProgresssBar.setVisibility(View.VISIBLE);
          
          NewsReaderActivity.timeStartedReading = -1L;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
          super.onPageFinished(view, url);
          anrProgresssBar.setVisibility(View.GONE);
          
          NewsReaderActivity.timeStartedReading = System.currentTimeMillis();
        }        
      });
      
      this.backToNewsHeader.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          backToReading();
        }
      });
      
    } else {
      toast("Fatal error occurred. Restarting.");
      startActivity(new Intent(context, PreMainActivity.class));
    }
  }  
  
  @Override
  protected void onStop() {
    if (afterNewsReadTask == null) {
      backToReading();
    } 
    
    super.onStop();
  }

  public static void setNewsToRead(News newsToRead) {
    NewsReaderActivity.newsToRead = newsToRead;
  }
  
  private void backToReading() {
    if (newsToRead != null && !NewsService.isNewsTooOld(newsToRead) && afterNewsReadTask == null) {
      this.timeReadInSeconds = ( timeStartedReading != -1L 
        ? (System.currentTimeMillis() - timeStartedReading) / 1000
        : 0);
      afterNewsReadTask = new AfterNewsReadTask();
      afterNewsReadTask.execute();

    } else {
      startActivity(new Intent(context, NewsHeadersActivity.class));
    }
  }
  
  private class AfterNewsReadTask extends AsyncTask<Object, Object, APIRetrievalStatus> {
    @Override
    protected APIRetrievalStatus doInBackground(Object... params) {
      try {
        NewsService.syncInitAfterUserReadNews(newsToRead, timeReadInSeconds, context);
        return APIRetrievalStatus.SUCCESS;
        
      } catch (Exception e) {
        return APIRetrievalStatus.ERROR;
      }
    }
    
    @Override
    protected void onPostExecute(APIRetrievalStatus result) {
      switch (result) {
      case SUCCESS:
        startActivity(new Intent(context, NewsHeadersActivity.class));
        break;

      case ERROR:
        toast("Failed to reach API after reading news! Restarting");
        startActivity(new Intent(context, PreMainActivity.class));
      }
    }
  }
  
  private class OnNewsOpenTask extends AsyncTask<Object, Object, APIRetrievalStatus> {
    @Override
    protected APIRetrievalStatus doInBackground(Object... params) {
      try {
        NewsService.syncOnUserOpenNews(newsToRead, context);
        return APIRetrievalStatus.SUCCESS;
        
      } catch (Exception e) {
        return APIRetrievalStatus.ERROR;
      }
    }
    
    @Override
    protected void onPostExecute(APIRetrievalStatus result) {
      if (result == APIRetrievalStatus.ERROR) {
        toast("Failed to reach API upon opening news! Restarting");
        startActivity(new Intent(context, PreMainActivity.class));
      }
    }
  }
  
  @Override
  public void onBackPressed() {
    backToReading();
  }
}




Java Source Code List

io.niuz.CategoriesSelectionActivity.java
io.niuz.CompanyInformationLoadingActivity.java
io.niuz.MainActivity.java
io.niuz.NewsCompaniesSelectionActivity.java
io.niuz.NewsHeadersActivity.java
io.niuz.NewsReaderActivity.java
io.niuz.PreMainActivity.java
io.niuz.model.APIRetrievalStatus.java
io.niuz.model.CategoryInformation.java
io.niuz.model.CompanyInformation.java
io.niuz.model.InitUserStatus.java
io.niuz.model.News.java
io.niuz.services.BaseService.java
io.niuz.services.CategoriesSubcategoriesService.java
io.niuz.services.CompaniesService.java
io.niuz.services.NewsService.java
io.niuz.services.PhoneService.java
io.niuz.services.UserService.java