Java tutorial
/* * This file is part of Droidpile: a Mailpile client for Android. * * You should have recieved a copy of the LICENSE file along with Droidpile. * If not: see <https://github.com/maikelwever/droidpile/blob/master/LICENSE> */ package org.maikelwever.droidpile; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.util.Log; import android.view.View; import android.widget.ExpandableListView; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import org.json.JSONException; import org.maikelwever.droidpile.adapters.MessageExpandableListAdapter; import org.maikelwever.droidpile.backend.ApiConnecter; import org.maikelwever.droidpile.backend.ApiConnecterFactory; import org.maikelwever.droidpile.models.MailpileMessage; import org.maikelwever.droidpile.models.ParsedMailpileMessage; import java.util.ArrayDeque; /** */ public class ViewMessageActivity extends SherlockActivity { private MailpileMessage mailpileMessage; private ParsedMailpileMessage parsedMessage; ProgressDialog progressDialog; ProgressDialog attachmentProgressDialog; MessageExpandableListAdapter adapter; ArrayDeque<String> messagesToDownload; Menu menu; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.messagesToDownload = new ArrayDeque<String>(); this.mailpileMessage = new MailpileMessage(getIntent().getExtras()); adapter = new MessageExpandableListAdapter(ViewMessageActivity.this, getFragmentManager()); getSupportActionBar().setTitle(this.mailpileMessage.title); View vi = getLayoutInflater().inflate(R.layout.activity_view_message, null); ExpandableListView listView = (ExpandableListView) vi.findViewById(R.id.view_message_listview); listView.setAdapter(adapter); setContentView(vi); if (this.parsedMessage == null) { new FetchMessageTask().execute(mailpileMessage.messageId); progressDialog = ProgressDialog.show(ViewMessageActivity.this, "", getString(R.string.download_please_wait), true, true); } } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); if (this.mailpileMessage != null) { savedInstanceState.putAll(this.mailpileMessage.toBundle()); } } public void renderMessage(ParsedMailpileMessage message) { if (message == null) { return; } adapter.addItem(message); adapter.notifyDataSetChanged(); if (this.parsedMessage == null) { this.parsedMessage = message; ExpandableListView listView = (ExpandableListView) findViewById(R.id.view_message_listview); listView.expandGroup(0); for (int x = 0; x < message.threadMessageIds.length(); x++) { try { String messageId = message.threadMessageIds.getString(x); this.messagesToDownload.addLast(messageId); } catch (JSONException e) { Log.d("droidpile", "Errourr", e); } } messagesToDownload.getFirst(); messagesToDownload.removeFirst(); } if (messagesToDownload.size() > 0) { new FetchMessageTask().execute(messagesToDownload.getFirst()); messagesToDownload.removeFirst(); } if (progressDialog != null) { progressDialog.dismiss(); progressDialog = null; } } public void displayFullMessage(ParsedMailpileMessage result) { this.renderMessage(result); } @Override public boolean onCreateOptionsMenu(Menu menu) { this.menu = menu; // Inflate the menu; this adds items to the action bar if it is present. getSupportMenuInflater().inflate(R.menu.view_message_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_reply_to_all) { new ReplyToAllTask().execute(mailpileMessage.messageId); } else if (id == R.id.action_reply) { new ReplyTask().execute(mailpileMessage.messageId); } else if (id == R.id.action_forward) { new ForwardTask().execute(mailpileMessage.messageId); } else if (id == android.R.id.home) { NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } private class FetchMessageTask extends AsyncTask<String, Integer, ParsedMailpileMessage> { @Override protected ParsedMailpileMessage doInBackground(String... params) { String json = ""; try { ApiConnecter api = ApiConnecterFactory.getApiConnecter(getApplicationContext()); return api.getMessage(params[0]); } catch (Exception e) { e.printStackTrace(); Log.d("droidpile", json); return null; } } @Override protected void onPostExecute(ParsedMailpileMessage result) { displayFullMessage(result); } } private abstract class BasicNewDraftTask extends AsyncTask<String, Integer, String> { ProgressDialog pDialog = null; @Override protected void onPreExecute() { pDialog = ProgressDialog.show(ViewMessageActivity.this, "", getString(R.string.additional_please_wait), true, true); } @Override protected String doInBackground(String... params) { ApiConnecter api = ApiConnecterFactory.getApiConnecter(getApplicationContext()); return invokeApiMethod(api, params[0]); } @Override protected void onPostExecute(String result) { Bundle bundle = new Bundle(); bundle.putString("draft-mid", result); Intent intent = new Intent(getApplicationContext(), SendMailActivity.class); intent.putExtras(bundle); if (pDialog != null) { pDialog.dismiss(); pDialog = null; } startActivity(intent); } public abstract String invokeApiMethod(ApiConnecter api, String messageId); } private class ForwardTask extends BasicNewDraftTask { @Override public String invokeApiMethod(ApiConnecter api, String messageId) { return api.forwardMessage(messageId); } } private class ReplyTask extends BasicNewDraftTask { @Override public String invokeApiMethod(ApiConnecter api, String messageId) { return api.replyMessage(false, messageId); } } private class ReplyToAllTask extends BasicNewDraftTask { @Override public String invokeApiMethod(ApiConnecter api, String messageId) { return api.replyMessage(true, messageId); } } }