diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-05-09 17:47:50 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-05-09 17:47:50 +0200 |
| commit | 0e50b86d2dff50766d2e0414122f971d814592c5 (patch) | |
| tree | 4d60b9bfb9ab04649e49316a95d796984945d464 /main/src/cgeo/geocaching/UsefulAppsActivity.java | |
| parent | 946c9d95f898893b4c36a427156e72853e2977a9 (diff) | |
| download | cgeo-0e50b86d2dff50766d2e0414122f971d814592c5.zip cgeo-0e50b86d2dff50766d2e0414122f971d814592c5.tar.gz cgeo-0e50b86d2dff50766d2e0414122f971d814592c5.tar.bz2 | |
refactoring: clean up useful apps activity
Diffstat (limited to 'main/src/cgeo/geocaching/UsefulAppsActivity.java')
| -rw-r--r-- | main/src/cgeo/geocaching/UsefulAppsActivity.java | 102 |
1 files changed, 75 insertions, 27 deletions
diff --git a/main/src/cgeo/geocaching/UsefulAppsActivity.java b/main/src/cgeo/geocaching/UsefulAppsActivity.java index b12c5e0..0fddd89 100644 --- a/main/src/cgeo/geocaching/UsefulAppsActivity.java +++ b/main/src/cgeo/geocaching/UsefulAppsActivity.java @@ -1,6 +1,7 @@ package cgeo.geocaching; import butterknife.InjectView; +import butterknife.Views; import cgeo.geocaching.activity.AbstractActivity; @@ -8,49 +9,96 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; -import android.view.View.OnClickListener; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.ImageView; -import android.widget.LinearLayout; +import android.widget.ListView; import android.widget.TextView; public class UsefulAppsActivity extends AbstractActivity { - @InjectView(R.id.parent) protected LinearLayout parentLayout; + @InjectView(R.id.apps_list) protected ListView list; - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState, R.layout.useful_apps); + protected static class ViewHolder { + @InjectView(R.id.title) protected TextView title; + @InjectView(R.id.image) protected ImageView image; + @InjectView(R.id.description) protected TextView description; - addApp(R.string.helper_calendar_title, R.string.helper_calendar_description, R.drawable.cgeo, "cgeo.calendar"); - addApp(R.string.helper_locus_title, R.string.helper_locus_description, R.drawable.helper_locus, "menion.android.locus"); - addApp(R.string.helper_gpsstatus_title, R.string.helper_gpsstatus_description, R.drawable.helper_gpsstatus, "com.eclipsim.gpsstatus2"); - addApp(R.string.helper_bluetoothgps_title, R.string.helper_bluetoothgps_description, R.drawable.helper_bluetoothgps, "googoo.android.btgps"); - addApp(R.string.helper_barcode_title, R.string.helper_barcode_description, R.drawable.helper_barcode, "com.google.zxing.client.android"); + public ViewHolder(View rowView) { + Views.inject(this, rowView); + } } - private void installFromMarket(String marketId) { - try { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + marketId))); - } catch (Exception e) { - // market not available in standard emulator + private static class HelperApp { + private final int titleId; + private final int descriptionId; + private final int iconId; + private final String market; + + public HelperApp(final int title, final int description, final int icon, final String market) { + this.titleId = title; + this.descriptionId = description; + this.iconId = icon; + this.market = market; } - finish(); + private void installFromMarket(UsefulAppsActivity activity) { + try { + activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + market))); + } catch (Exception e) { + // market not available in standard emulator + } + + activity.finish(); + } } - private void addApp(final int titleId, final int descriptionId, final int imageId, final String marketUrl) { - final View appLayout = getLayoutInflater().inflate(R.layout.useful_apps_item, null); - ((TextView) appLayout.findViewById(R.id.title)).setText(res.getString(titleId)); - ((ImageView) appLayout.findViewById(R.id.image)).setImageDrawable(res.getDrawable(imageId)); - ((TextView) appLayout.findViewById(R.id.description)).setText(res.getString(descriptionId)); - appLayout.findViewById(R.id.app_layout).setOnClickListener(new OnClickListener() { + private static final HelperApp[] HELPER_APPS = { + new HelperApp(R.string.helper_calendar_title, R.string.helper_calendar_description, R.drawable.cgeo, "cgeo.calendar"), + new HelperApp(R.string.helper_locus_title, R.string.helper_locus_description, R.drawable.helper_locus, "menion.android.locus"), + new HelperApp(R.string.helper_gpsstatus_title, R.string.helper_gpsstatus_description, R.drawable.helper_gpsstatus, "com.eclipsim.gpsstatus2"), + new HelperApp(R.string.helper_bluetoothgps_title, R.string.helper_bluetoothgps_description, R.drawable.helper_bluetoothgps, "googoo.android.btgps"), + new HelperApp(R.string.helper_barcode_title, R.string.helper_barcode_description, R.drawable.helper_barcode, "com.google.zxing.client.android"), + }; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState, R.layout.useful_apps_activity); + + Views.inject(this); + list.setAdapter(new ArrayAdapter<HelperApp>(this, R.layout.useful_apps_item, HELPER_APPS) { @Override - public void onClick(View v) { - installFromMarket(marketUrl); + public View getView(int position, View convertView, android.view.ViewGroup parent) { + View rowView = convertView; + if (null == rowView) { + rowView = getLayoutInflater().inflate(R.layout.useful_apps_item, null); + } + ViewHolder holder = (ViewHolder) rowView.getTag(); + if (null == holder) { + holder = new ViewHolder(rowView); + rowView.setTag(holder); + } + + final HelperApp app = getItem(position); + fillViewHolder(holder, app); + return rowView; + } + + private void fillViewHolder(ViewHolder holder, HelperApp app) { + holder.title.setText(res.getString(app.titleId)); + holder.image.setImageDrawable(res.getDrawable(app.iconId)); + holder.description.setText(res.getString(app.descriptionId)); } }); - parentLayout.addView(appLayout); - } + list.setOnItemClickListener(new AdapterView.OnItemClickListener() { + + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + HelperApp helperApp = HELPER_APPS[position]; + helperApp.installFromMarket(UsefulAppsActivity.this); + } + }); + } } |
