package org.imogene.map.app;
import java.util.ArrayList;
import java.util.List;
import org.imogene.map.R;
import org.imogene.map.provider.common.Constants.Intents;
import org.imogene.map.provider.common.Constants.InternalIntents;
import org.imogene.map.provider.common.Constants.Metadata;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class MedesMap extends Activity implements OnItemSelectedListener {
private static final boolean DEBUG = false;
public static final String PREFS_SUPPLIER = "supplier";
private ArrayAdapter<SupplierEntry> mSpinnerAdapter;
private Spinner mSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSpinner = (Spinner) findViewById(R.id.supplier_spinner);
List<SupplierEntry> entries = getSuppliers();
mSpinnerAdapter = new ArrayAdapter<SupplierEntry>(this, android.R.layout.simple_spinner_item, entries);
mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setOnItemSelectedListener(this);
mSpinner.setAdapter(mSpinnerAdapter);
findViewById(R.id.debug).setVisibility(DEBUG ? View.VISIBLE : View.GONE);
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String category = preferences.getString(PREFS_SUPPLIER, null);
if (category != null) {
int position = getCategoryPosition(category);
if (position != -1) {
mSpinner.setSelection(position);
}
}
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(PREFS_SUPPLIER, getSelectedCategory());
editor.commit();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SupplierEntry entry = (SupplierEntry) parent.getAdapter().getItem(position);
findViewById(R.id.navigate_button).setEnabled(true);
findViewById(R.id.download_tiles_button).setEnabled(entry.downloadSupport);
findViewById(R.id.clear_cache_button).setEnabled(entry.downloadSupport);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
findViewById(R.id.navigate_button).setEnabled(false);
findViewById(R.id.download_tiles_button).setEnabled(false);
findViewById(R.id.clear_cache_button).setEnabled(false);
}
public void onNavigate(View v) {
}
public void onDownloadTiles(View v) {
}
public void onClearCache(View v) {
}
private String getSelectedCategory() {
if (mSpinner.getSelectedItemPosition() != Spinner.INVALID_POSITION) {
return ((SupplierEntry) mSpinner.getSelectedItem()).category;
} else {
return null;
}
}
private int getCategoryPosition(String category) {
int count = mSpinnerAdapter.getCount();
for (int i = 0; i < count; i++) {
if (category.equals(mSpinnerAdapter.getItem(i).category)) {
return i;
}
}
return -1;
}
private ArrayList<SupplierEntry> getSuppliers() {
ArrayList<SupplierEntry> result = new ArrayList<SupplierEntry>(2);
Intent i = new Intent(InternalIntents.ACTION_PROVIDER_SHOW_ON_MAP);
List<ResolveInfo> lri = getPackageManager().queryIntentActivities(i, PackageManager.GET_META_DATA);
for (ResolveInfo ri : lri) {
ApplicationInfo ai = ri.activityInfo.applicationInfo;
Bundle bundle = ri.activityInfo.applicationInfo.metaData;
if (bundle != null && bundle.containsKey(Metadata.METADATA_TAG)) {
int id = bundle.getInt(Metadata.METADATA_TAG);
try {
Resources res = getPackageManager().getResourcesForApplication(ai);
String[] array = res.getStringArray(id);
if (array.length == 3) {
SupplierEntry entry = new SupplierEntry();
entry.category = array[0];
entry.description = array[1];
entry.downloadSupport = Boolean.parseBoolean(array[2]);
result.add(entry);
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
return result;
}
private static class SupplierEntry {
private String description;
private String category;
private boolean downloadSupport;
@Override
public String toString() {
return description;
}
}
public void captureGps(View v) {
Intent intent = new Intent(Intents.ACTION_CAPTURE_GPS);
startActivity(intent);
}
public void manageBoxes(View v) {
Intent intent = new Intent(Intents.ACTION_MANAGE_RECT);
startActivity(intent);
}
public void newBox(View v) {
Intent intent = new Intent(Intents.ACTION_NEW_RECT);
startActivity(intent);
}
public void viewPoint(View v) {
Intent intent = new Intent(Intents.ACTION_SHOW_ON_MAP);
startActivity(intent);
}
public void downloadTiles(View v) {
Intent intent = new Intent(Intents.ACTION_DOWNLOAD_TILES);
startActivity(intent);
}
}
|