Android Open Source - foxail_reader Main Activity






From Project

Back to project page foxail_reader.

License

The source code is released under:

GNU General Public License

If you think the Android project foxail_reader 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 org.foxail.android.reader.activity;
/*from   w w  w .j av a2 s .  com*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.foxail.android.common.CommonUtil;
import org.foxail.android.common.volley.HtmlRequest;
import org.foxail.android.reader.R;
import org.foxail.android.reader.client.Client;
import org.foxail.android.reader.model.News;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

@SuppressLint("NewApi")
public class MainActivity extends BaseActivity {
  
  private final static String TAG = "MainActivity";
  protected final static int DIALOG_ALERT_EXIT = 1;
  
  private int curPage = 0;
  private ListView mainList;
  private long returnKeyPressed = 0;
  private RequestQueue mQueue;
  private Client client;
  private Map<String, News> newsCache;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    mQueue = Volley.newRequestQueue(this);
    client = clientFactory.getClient("cnbeta");
    newsCache = new HashMap<String, News>();
    
    //??ActionBar????????
    getActionBar().setDisplayHomeAsUpEnabled(true);
    
    mainList = (ListView) findViewById(R.id.main_list);
    mainList.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        TextView newsIdView =(TextView) view.findViewById(R.id.main_list_id);
        String newsId = (String) newsIdView.getText();
        News news = newsCache.get(newsId);
        
        Intent intent = new Intent(view.getContext(), NewsActivity.class);
        intent.putExtra("news", news);
        startActivity(intent);
      }
        });
    
    showHome();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
  }
    
    //??????????????  
    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
    case android.R.id.home: {
        if(curPage == 1) {
          //??????
          confirmExit();
        } else {
          //???????????
            showHome();
        }
        break;
    }
      case R.id.main_menu_home: {
        showHome();
        break;
      }
      case R.id.main_menu_next: {
        showNext();
        break;
      }
      case R.id.main_menu_previous: {
        showPrevious();
        break;
      }
      case R.id.main_menu_refresh: {
        getMobileNews();
        break;
      }
      case R.id.main_menu_settings: {
        
        break;
      }
      case R.id.main_menu_about: {
        String msg = getString(R.string.button_about);
        msg += "\n" + getString(R.string.app_name) + " v" + CommonUtil.getVerName(this);
        
        Bundle bundle = new Bundle();
        bundle.putString("title", getString(R.string.button_about));
        bundle.putString("msg", msg);
      showDialog(DIALOG_ALERT_COMMON, bundle);
        break;
      }
      default:
        break;
      }
        return true;
    }
    
    /**
     * ???????????
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // ???????????????????
    if(keyCode == KeyEvent.KEYCODE_BACK){
      //??
      if(returnKeyPressed <= 0 || 
          event.getEventTime() - returnKeyPressed > 3000) {
        showToast(getString(R.string.msg_againExit));
        returnKeyPressed = event.getEventTime();
        return true;
      } else {
        finish();
        System.exit(0);
      }
    }
    return super.onKeyDown(keyCode, event);
  }

    @Override
    protected Dialog onCreateDialog(int id, Bundle bundle) {
      if(id == DIALOG_ALERT_EXIT){
        return new AlertDialog.Builder(MainActivity.this)
            //.setIconAttribute(android.R.attr.alertDialogIcon)
            .setTitle(getString(R.string.title_exit))
            .setMessage(getString(R.string.msg_confirmExit))
            .setPositiveButton(getString(R.string.button_ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
          dialog.dismiss();
          finish();
          System.exit(0);
                }
            })
            .setNegativeButton(getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
          dialog.dismiss();
                }
            })
            .create();
      } else {
        return super.onCreateDialog(id, bundle);
      }
    }
    
    /**
     * ????
     */
    private void showHome() {
    if(curPage != 1) {
      curPage = 1;
      getMobileNews();
    }
    }
    
    /**
     * ?????
     */
    private void showPrevious() {
      if(curPage == 1) {
        return;
      } else if(curPage > 1) {
        --curPage;
      } else {
        curPage = 1;
      }
      getMobileNews();
    }
    
    /**
     * ?????
     */
    private void showNext() {
    ++curPage;
    getMobileNews();
    }
    
    /**
     * ??
     */
    protected void confirmExit() {
      showDialog(DIALOG_ALERT_EXIT, null);
    }
  
  /**
   * ????????????(???)
   */
  private void getMobileNews() {
    Bundle bundle = new Bundle();
    bundle.putString("msg", getString(R.string.msg_loading));
    showDialog(DIALOG_PROGRESS_COMMON, bundle);
    
    //????????????
    setTitle(getString(R.string.app_name) + " #" + curPage);
    //?????????
    mainList.removeAllViewsInLayout();
    //?????
    mainList.scrollTo(0, 0);
    
    try {
      //newsList = client.getNewsList(curPage);
      HtmlRequest htmlRequest = new HtmlRequest(client.getListUrl(curPage),
              new Response.Listener<String>() {
                  @Override
                  public void onResponse(String response) {
                      //Log.d("TAG", response);
                  
                  List<News> newsList = client.getNewsList(response);

                  List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
                  for (News news : newsList) {
                    if(!newsCache.containsKey(news.getId())){
                      newsCache.put(news.getId(), news);
                    }
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("id", news.getId());
                    map.put("title", news.getTitle());
                    resultList.add(map);
                  }
                  
                  SimpleAdapter mainListAdapter = new SimpleAdapter(
                      MainActivity.this, 
                      resultList, 
                      R.layout.layout_main_list, 
                      new String[]{"id", "title"}, 
                      new int[]{R.id.main_list_id, R.id.main_list_title}
                      );
                  mainList.setAdapter(mainListAdapter);
                  
                  closePDialog();
                  }
              },
              new Response.ErrorListener() {  
                  @Override  
                  public void onErrorResponse(VolleyError error) {  
                      //Log.e("TAG", error.getMessage(), error);  
                  }
              });
      mQueue.add(htmlRequest);
    } catch(Exception e) {
      closePDialog();
      showToast(getString(R.string.msg_connectServerFailed));
      return;
    }
  }
}




Java Source Code List

org.foxail.android.common.CommonUtil.java
org.foxail.android.common.http.ConnectionManager.java
org.foxail.android.common.http.HttpConnection.java
org.foxail.android.common.volley.HtmlRequest.java
org.foxail.android.reader.activity.BaseActivity.java
org.foxail.android.reader.activity.FavoritesActivity.java
org.foxail.android.reader.activity.MainActivity.java
org.foxail.android.reader.activity.NewsActivity.java
org.foxail.android.reader.client.ClientFactory.java
org.foxail.android.reader.client.Client.java
org.foxail.android.reader.client.CnBetaClient.java
org.foxail.android.reader.model.News.java