Java tutorial
/*Copyright (C) 2009 Cleriot Simon This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package com.pixellostudio.qqdroid; import greendroid.app.GDActivity; import greendroid.widget.NormalActionBarItem; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.apache.http.util.ByteArrayBuffer; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.Html; import android.text.Spanned; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.View.OnCreateContextMenuListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import com.google.android.apps.analytics.GoogleAnalyticsTracker; public class BaseQuote extends GDActivity { GoogleAnalyticsTracker tracker; ListView view; String url = ""; String adresse = ""; String txt = ""; String name = ""; String title = ""; ArrayAdapter<String> adapter, adapter2; //ArrayAdapter<QuickActionItem> items; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setActionBarContentView(R.layout.show); getActionBar().setTitle(title); getActionBar().addItem(getActionBar().newActionBarItem(NormalActionBarItem.class) .setDrawable(R.drawable.seemore).setContentDescription("List"), R.id.actionbar_seemore); view = (ListView) findViewById(R.id.ListView); view.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(0, 1, 0, R.string.sharequote); } }); adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1); adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1) { @Override public View getView(int position, View convertView, ViewGroup parent) { SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(BaseQuote.this); TextView txt = new TextView(this.getContext()); txt.setTextSize(Float.parseFloat(pref.getString("policesize", "20"))); txt.setText(Html.fromHtml(this.getItem(position))); if (pref.getString("design", "blackonwhite").equals("blackonwhite")) { txt.setTextColor(Color.BLACK); txt.setBackgroundColor(Color.WHITE); txt.setBackgroundDrawable( this.getContext().getResources().getDrawable(R.drawable.quote_gradient_white)); } else if (pref.getString("design", "blackonwhite").equals("whiteonblack")) { txt.setTextColor(Color.WHITE); txt.setBackgroundColor(Color.BLACK); txt.setBackgroundDrawable( this.getContext().getResources().getDrawable(R.drawable.quote_gradient_black)); } return txt; } }; String[] liste = (String[]) getLastNonConfigurationInstance(); if (liste != null && liste.length != 0) { for (int i = 0; i < liste.length; i++) { adapter.add(liste[i]); adapter2.add(liste[i]); } this.setTitle(name); } else { new LoadQuotes().execute(); } view.setAdapter(adapter2); } @Override public Object onRetainNonConfigurationInstance() { String[] liste = new String[adapter2.getCount()]; for (int i = 0; i < adapter2.getCount(); i++) { liste[i] = adapter2.getItem(i); } return liste; } public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); Spanned quote = Html.fromHtml((String) view.getAdapter().getItem(menuInfo.position)); Intent i = new Intent(Intent.ACTION_SEND); i.putExtra(Intent.EXTRA_TEXT, getBaseContext().getText(R.string.quotefrom) + " " + name + " : " + quote); i.setType("text/plain"); startActivity(Intent.createChooser(i, this.getText(R.string.share))); break; default: return super.onContextItemSelected(item); } return true; } public String parseUrl(String url) { return url; } public void ParseText() { } class LoadQuotes extends AsyncTask<Object, Object, Integer> { private final int NETWORK_ERROR = -1, ERROR = 0, SUCCESS = 1; @Override protected void onPreExecute() { getActionBar().setTitle(getBaseContext().getText(R.string.loading) + "..."); setProgressBarIndeterminateVisibility(true); } @Override protected Integer doInBackground(Object... params) { try { URL updateURL = new URL(adresse + parseUrl(url)); URLConnection conn = updateURL.openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ txt = new String(baf.toByteArray()); } catch (IOException e) { return NETWORK_ERROR; } catch (Exception e) { e.printStackTrace(); return ERROR; } ParseText(); return SUCCESS; } @Override protected void onPostExecute(Integer result) { setProgressBarIndeterminateVisibility(false); switch (result) { case NETWORK_ERROR: Log.i("QQDroid", "Network unavailable"); AlertDialog.Builder netEr = new AlertDialog.Builder(BaseQuote.this); netEr.setMessage(R.string.network_error).setCancelable(false) .setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { new LoadQuotes().execute(); } }).setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); netEr.show(); break; case ERROR: AlertDialog.Builder er = new AlertDialog.Builder(BaseQuote.this); er.setMessage(R.string.error).setCancelable(false).setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); er.show(); break; case SUCCESS: adapter2.clear(); for (int i = 0; i < adapter.getCount(); i++) adapter2.add(adapter.getItem(i)); adapter.clear(); view.setAdapter(adapter2); getActionBar().setTitle(title); break; } } } }