Java tutorial
/* 2010 Stephan Reichholf <stephan at reichholf dot net> * * Licensed under the Create-Commons Attribution-Noncommercial-Share Alike 3.0 Unported * http://creativecommons.org/licenses/by-nc-sa/3.0/ */ package net.reichholf.dreamdroid.abstivities; import java.util.ArrayList; import java.util.HashMap; import net.reichholf.dreamdroid.R; import net.reichholf.dreamdroid.activities.MainActivity; import net.reichholf.dreamdroid.helpers.ExtendedHashMap; import net.reichholf.dreamdroid.helpers.SimpleHttpClient; import net.reichholf.dreamdroid.helpers.enigma2.Remote; import net.reichholf.dreamdroid.helpers.enigma2.SimpleResult; import net.reichholf.dreamdroid.helpers.enigma2.requesthandler.SimpleResultRequestHandler; import net.reichholf.dreamdroid.helpers.enigma2.requesthandler.impl.RemoteCommandRequestHandler; import net.reichholf.dreamdroid.helpers.enigma2.requesthandler.impl.ZapRequestHandler; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.*; import android.view.View.OnClickListener; import android.widget.Toast; /** * @author sreichholf * */ public abstract class AbstractHttpActivity extends Activity { public static final int MENU_HOME = 89283794; protected SimpleHttpClient mShc; protected final String sData = "data"; protected SimpleResultTask mSimpleResultTask; protected class SimpleResultTask extends AsyncTask<ArrayList<NameValuePair>, Void, Boolean> { private ExtendedHashMap mResult; private SimpleResultRequestHandler mHandler; public SimpleResultTask(SimpleResultRequestHandler handler) { mHandler = handler; } /* * (non-Javadoc) * * @see android.os.AsyncTask#doInBackground(Params[]) */ @Override protected Boolean doInBackground(ArrayList<NameValuePair>... params) { publishProgress(); String xml = mHandler.get(mShc, params[0]); if (xml != null) { ExtendedHashMap result = mHandler.parseSimpleResult(xml); String stateText = result.getString("statetext"); if (stateText != null) { mResult = result; return true; } } return false; } /* * (non-Javadoc) * * @see android.os.AsyncTask#onProgressUpdate(Progress[]) */ @Override protected void onProgressUpdate(Void... progress) { setProgressBarIndeterminateVisibility(true); } /* * (non-Javadoc) * * @see android.os.AsyncTask#onPostExecute(java.lang.Object) */ protected void onPostExecute(Boolean result) { setProgressBarIndeterminateVisibility(false); if (!result || mResult == null) { mResult = new ExtendedHashMap(); } onSimpleResult(result, mResult); } } /* * (non-Javadoc) * * @see android.app.Activity#onCreate(android.os.Bundle) */ @SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // CustomExceptionHandler.register(this); mShc = null; if (savedInstanceState != null) { Object retained = getLastNonConfigurationInstance(); if (retained instanceof HashMap) { mShc = (SimpleHttpClient) ((HashMap<String, Object>) retained).get("shc"); } } if (mShc == null) { setClient(); } } /* * (non-Javadoc) * * @see android.app.Activity#onRetainNonConfigurationInstance() */ @Override public Object onRetainNonConfigurationInstance() { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("shc", mShc); return map; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } /* * (non-Javadoc) * * @see android.app.Activity#onRestoreInstanceState(android.os.Bundle) */ @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); } /* * (non-Javadoc) * * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) */ @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, MENU_HOME, 99, getText(R.string.home)).setIcon(android.R.drawable.ic_menu_view); return true; } /* * (non-Javadoc) * * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) */ @Override public boolean onOptionsItemSelected(MenuItem item) { return onItemClicked(item.getItemId()); } /** * */ protected void setClient() { mShc = SimpleHttpClient.getInstance(); } /** * Register an <code>OnClickListener</code> for a view and a specific item * ID (<code>ITEM_*</code> statics) * * @param v * The view an OnClickListener should be registered for * @param id * The id used to identify the item clicked (<code>ITEM_*</code> * statics) */ protected void registerOnClickListener(View v, final int id) { v.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onItemClicked(id); } }); } /** * @param id */ protected boolean onItemClicked(int id) { Intent intent; switch (id) { case MENU_HOME: intent = new Intent(this, MainActivity.class); startActivity(intent); return true; default: return false; } } /** * @param progress */ protected void updateProgress(String progress) { setTitle(progress); setProgressBarIndeterminateVisibility(true); } /** * @param success * @param result */ protected void onSimpleResult(boolean success, ExtendedHashMap result) { String toastText = (String) getText(R.string.get_content_error); String stateText = result.getString(SimpleResult.STATE_TEXT); if (stateText != null && !"".equals(stateText)) { toastText = stateText; } else if (mShc.hasError()) { toastText = mShc.getErrorText(); } showToast(toastText); } /** * @param handler * @param params */ @SuppressWarnings("unchecked") public void execSimpleResultTask(SimpleResultRequestHandler handler, ArrayList<NameValuePair> params) { if (mSimpleResultTask != null) { mSimpleResultTask.cancel(true); } mSimpleResultTask = new SimpleResultTask(handler); mSimpleResultTask.execute(params); } /** * @param ref * The ServiceReference to zap to */ public void zapTo(String ref) { ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("sRef", ref)); execSimpleResultTask(new ZapRequestHandler(), params); } /** * @param title */ protected void finishProgress(String title) { setTitle(title); setProgressBarIndeterminateVisibility(false); } /** * @param toastText */ protected void showToast(String toastText) { Toast toast = Toast.makeText(this, toastText, Toast.LENGTH_LONG); toast.show(); } /** * @param toastText */ protected void showToast(CharSequence toastText) { Toast toast = Toast.makeText(this, toastText, Toast.LENGTH_LONG); toast.show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: onButtonClicked(Remote.KEY_VOLP, false); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: onButtonClicked(Remote.KEY_VOLM, false); return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || super.onKeyUp(keyCode, event); } /** * Called after a Button has been clicked * * @param id * The id of the item * @param longClick * If true the item has been long-clicked */ private void onButtonClicked(int id, boolean longClick) { ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("command", new Integer(id).toString())); if (longClick) { params.add(new BasicNameValuePair("type", Remote.CLICK_TYPE_LONG)); } execSimpleResultTask(new RemoteCommandRequestHandler(), params); } }