Android Open Source - swazam History Activity






From Project

Back to project page swazam.

License

The source code is released under:

MIT License

If you think the Android project swazam 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 at.saws2013.szazam;
/*w  w w .j  av a  2 s.  c o m*/
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;

import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;
import at.saws2013.szazam.entities.IListItem;
import at.saws2013.szazam.store.impl.AuthStore;
import at.saws2013.szazam.store.impl.RequestStore;
import at.saws2013.szazam.store.impl.TransactionStore;
import at.saws2013.szazam.ui.CustomListAdapter;
import at.saws2013.szazam.volley.CustomVolleyStringRequest;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;


/**
 * Activity which displays the transactions or requests
 * history in a list, depending on the intent parameters
 * 
 * @author Ren?
 *
 */
public class HistoryActivity extends BaseActivity {
  
  public static final String HISTORY_TYPE = "history";
  public static final int TRANSACTIONS = 1;
  public static final int REQUESTS = 2;
  private App app;
  private ListView lv_history;
  private CustomListAdapter adapter;
  private List<IListItem> list;  
  private CustomVolleyStringRequest fetchFromServerRequest;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_history);
    // Show the Up button in the action bar.
    setupActionBar();
    app = (App) getApplication();
    initViews();
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    if (fetchFromServerRequest != null){
      setProgressBarIndeterminateVisibility(true);
      app.getVolleyQueue().add(fetchFromServerRequest);
    }
  }
  
  @Override
  protected void onPause() {
    super.onPause();
    app.getVolleyQueue().cancelAll(HistoryActivity.class.getName());
  }
  

  /**
   * Set up the {@link android.app.ActionBar}.
   */
  private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.history, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
      // This ID represents the Home or Up button. In the case of this
      // activity, the Up button is shown. Use NavUtils to allow users
      // to navigate up one level in the application structure. For
      // more details, see the Navigation pattern on Android Design:
      //
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back
      //
      NavUtils.navigateUpFromSameTask(this);
      return true;
    }
    return super.onOptionsItemSelected(item);
  }


  @Override
  void initViews() {
    lv_history = (ListView) findViewById(R.id.lv_history);
    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey(HISTORY_TYPE)){
      switch (extras.getInt(HISTORY_TYPE, REQUESTS)) {
      case TRANSACTIONS:
        getActionBar().setTitle(getString(R.string.transactions));
        TransactionStore transactionStore = TransactionStore.getInstance(getApplicationContext());
        list = transactionStore.getTransactionHistory();
        adapter = new CustomListAdapter(HistoryActivity.this, R.layout.listitem_transaction, list);
        lv_history.setAdapter(adapter);
        initServerRequest(app.getHostFromSettings()+"/transactions?token=" +AuthStore.getInstance(getApplicationContext()).getToken());
        break;
      default:
        getActionBar().setTitle(getString(R.string.requests));
        RequestStore requestStore = RequestStore.getInstance(getApplicationContext());
        list = requestStore.getRequestHistory();
        adapter = new CustomListAdapter(HistoryActivity.this, R.layout.listitem_request, list);
        lv_history.setAdapter(adapter);
        initServerRequest(app.getHostFromSettings()+"/requests?token=" +AuthStore.getInstance(getApplicationContext()).getToken());
        break;
      }
    }
  }
  
  private void initServerRequest(final String url) {
    Map<String, String> params = new HashMap<String, String>(1);
    params.put("token", AuthStore.getInstance(getApplicationContext()).getToken());
    try {
      fetchFromServerRequest = new CustomVolleyStringRequest(Method.GET, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
          Log.d("HistoryActivity", response);
          JSONArray responseJSON;
          try {
            responseJSON = new JSONArray(response);
            if (url.contains("transactions")){
              TransactionStore.getInstance(getApplicationContext()).storeTransactions(responseJSON);
              list = TransactionStore.getInstance(getApplicationContext()).transformToList(responseJSON);
            } else {
              RequestStore.getInstance(getApplicationContext()).storeRequests(responseJSON);
              list = RequestStore.getInstance(getApplicationContext()).transformToList(responseJSON);
            }
            adapter.notifyDataSetChanged();
          } catch (JSONException e) {
            e.printStackTrace();
          }  
          setProgressBarIndeterminateVisibility(false);
        }
      }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
          Log.d("HistoryActivity", "" +error.getMessage());
          setProgressBarIndeterminateVisibility(false);      
        }
      }, params);
      fetchFromServerRequest.setTag(HistoryActivity.class.getName());
    } catch (NullPointerException e) {
      Toast.makeText(getApplicationContext(), "Incorrect host URL", Toast.LENGTH_SHORT).show();
    }
  }

}




Java Source Code List

ac.tuwien.sa13.beans.HibernateAwareObjectMapper.java
ac.tuwien.sa13.controller.HomeController.java
ac.tuwien.sa13.controller.LoginController.java
ac.tuwien.sa13.controller.RequestController.java
ac.tuwien.sa13.controller.TransactionController.java
ac.tuwien.sa13.controller.api.UserRestController.java
ac.tuwien.sa13.dao.HibernateDAO.java
ac.tuwien.sa13.dao.IGenericDAO.java
ac.tuwien.sa13.dao.IRequestDAO.java
ac.tuwien.sa13.dao.ITransactionDAO.java
ac.tuwien.sa13.dao.IUserDAO.java
ac.tuwien.sa13.dao.RequestDAO.java
ac.tuwien.sa13.dao.TransactionDAO.java
ac.tuwien.sa13.dao.UserDAO.java
ac.tuwien.sa13.entity.Request.java
ac.tuwien.sa13.entity.Transaction.java
ac.tuwien.sa13.entity.User.java
ac.tuwien.sa13.service.IGenericService.java
ac.tuwien.sa13.service.IRequestService.java
ac.tuwien.sa13.service.ITransactionService.java
ac.tuwien.sa13.service.IUserService.java
ac.tuwien.sa13.service.PeerManager.java
ac.tuwien.sa13.service.RequestService.java
ac.tuwien.sa13.service.TransactionService.java
ac.tuwien.sa13.service.UserService.java
ac.tuwien.sa13.utilities.SessionIdentifierGenerator.java
ac.tuwien.sa13.utilities.Token.java
ac.tuwien.sa13.validator.LoginValidator.java
at.ac.tuwien.swa13.swazam.NetworkConnection.java
at.ac.tuwien.swa13.swazam.Peer.java
at.ac.tuwien.swa13.swazam.fingerprint.IFingerPrintCreator.java
at.ac.tuwien.swa13.swazam.fingerprint.impl.FingerPrintCreator.java
at.ac.tuwien.swa13.swazam.library.ILibraryManager.java
at.ac.tuwien.swa13.swazam.library.ISong.java
at.ac.tuwien.swa13.swazam.library.impl.LibraryManager.java
at.ac.tuwien.swa13.swazam.library.impl.Song.java
at.ac.tuwien.swa13.swazam.p2p.PeerConnector.java
at.saws2013.szazam.App.java
at.saws2013.szazam.BaseActivity.java
at.saws2013.szazam.HistoryActivity.java
at.saws2013.szazam.LoginActivity.java
at.saws2013.szazam.RecognitionActivity.java
at.saws2013.szazam.SettingsActivity.java
at.saws2013.szazam.entities.IListItem.java
at.saws2013.szazam.entities.ListItemTypes.java
at.saws2013.szazam.entities.Request.java
at.saws2013.szazam.entities.Transaction.java
at.saws2013.szazam.fingerprint.IFingerPrintSystem.java
at.saws2013.szazam.fingerprint.impl.FingerPrintCreator.java
at.saws2013.szazam.fragments.SettingsFragment.java
at.saws2013.szazam.media.IAudioFilePicker.java
at.saws2013.szazam.media.IAudioRecorder.java
at.saws2013.szazam.media.impl.AudioFilePicker.java
at.saws2013.szazam.media.impl.AudioRecorder.java
at.saws2013.szazam.store.IAuthStore.java
at.saws2013.szazam.store.IRequestStore.java
at.saws2013.szazam.store.ITransactionStore.java
at.saws2013.szazam.store.impl.AuthStore.java
at.saws2013.szazam.store.impl.RequestStore.java
at.saws2013.szazam.store.impl.TransactionStore.java
at.saws2013.szazam.ui.CustomListAdapter.java
at.saws2013.szazam.ui.ViewTools.java
at.saws2013.szazam.volley.CustomVolleyStringRequest.java
de.passsy.holocircularprogressbar.HoloCircularProgressBar.java