diff options
Diffstat (limited to 'main/src/cgeo/geocaching/UsefulAppsActivity.java')
| -rw-r--r-- | main/src/cgeo/geocaching/UsefulAppsActivity.java | 124 |
1 files changed, 76 insertions, 48 deletions
diff --git a/main/src/cgeo/geocaching/UsefulAppsActivity.java b/main/src/cgeo/geocaching/UsefulAppsActivity.java index af643b3..d6e0ec8 100644 --- a/main/src/cgeo/geocaching/UsefulAppsActivity.java +++ b/main/src/cgeo/geocaching/UsefulAppsActivity.java @@ -1,79 +1,107 @@ package cgeo.geocaching; +import butterknife.InjectView; +import butterknife.Views; + import cgeo.geocaching.activity.AbstractActivity; +import android.app.Activity; 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; -import java.util.Locale; - public class UsefulAppsActivity extends AbstractActivity { - private LinearLayout parentLayout; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + @InjectView(R.id.apps_list) protected ListView list; - // init - setTheme(); - setContentView(R.layout.useful_apps); - setTitle(res.getString(R.string.helpers)); - parentLayout = (LinearLayout) findViewById(R.id.parent); + 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; - final Locale loc = Locale.getDefault(); - final String language = loc.getLanguage(); + public ViewHolder(View rowView) { + Views.inject(this, rowView); + } + } - final String tutorialUrl; - if ("de".equalsIgnoreCase(language)) { - tutorialUrl = "gnu.android.app.cgeomanual.de"; + private static class HelperApp { + private final int titleId; + private final int descriptionId; + private final int iconId; + private final String packageName; + + public HelperApp(final int title, final int description, final int icon, final String packageName) { + this.titleId = title; + this.descriptionId = description; + this.iconId = icon; + this.packageName = packageName; } - else { - tutorialUrl = "gnu.android.app.cgeomanual.en"; + + private void installFromMarket(Activity activity) { + try { + Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)); + marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); + activity.startActivity(marketIntent); + + } catch (Exception e) { + // market not available in standard emulator + } } - addApp(R.string.helper_manual_title, R.string.helper_manual_description, R.drawable.helper_manual, tutorialUrl); - 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"); } + 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_pocketquery_title, R.string.helper_pocketquery_description, R.drawable.helper_pocketquery, "org.pquery"), + 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 onResume() { - super.onResume(); + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState, R.layout.useful_apps_activity); - } + Views.inject(this); - 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 - } + list.setAdapter(new ArrayAdapter<HelperApp>(this, R.layout.useful_apps_item, HELPER_APPS) { + @Override + 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; + } - finish(); - } + 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)); + } + }); - private void addApp(final int titleId, final int descriptionId, final int imageId, final String marketUrl) { - final LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.useful_apps_item, null); - ((TextView) layout.findViewById(R.id.title)).setText(res.getString(titleId)); - ((ImageView) layout.findViewById(R.id.image)).setImageDrawable(res.getDrawable(imageId)); - ((TextView) layout.findViewById(R.id.description)).setText(res.getString(descriptionId)); - layout.findViewById(R.id.app_layout).setOnClickListener(new OnClickListener() { + list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override - public void onClick(View v) { - installFromMarket(marketUrl); + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + HelperApp helperApp = HELPER_APPS[position]; + helperApp.installFromMarket(UsefulAppsActivity.this); } }); - parentLayout.addView(layout); } - } |
