package dk.thoerup.traininfo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import dk.thoerup.traininfo.WelcomeScreen.ListType; public class ShortcutActivity extends Activity { final static int SHOW_DEPARTURES = 1000; final static int SHOW_STATIONS = 2000; StationBean station; Button okBtn; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); // Resolve the intent final Intent intent = getIntent(); final String action = intent.getAction(); // If the intent is a request to create a shortcut, we'll do that and exit if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { setContentView(R.layout.shortcut); Button cancelBtn = (Button) findViewById(R.id.shortcut_cancel); cancelBtn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { ShortcutActivity.this.setResult(Activity.RESULT_CANCELED); ShortcutActivity.this.finish(); } }); okBtn = (Button) findViewById(R.id.shortcut_ok); okBtn.setEnabled(false); okBtn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { setupShortcut(station); finish(); } }); Button btn = (Button)findViewById(R.id.nearest); btn.setOnClickListener(new StationListener(ListType.ListNearest)); btn = (Button)findViewById(R.id.search); btn.setOnClickListener(new StationListener(ListType.ListSearch)); btn = (Button)findViewById(R.id.favorites); btn.setOnClickListener(new StationListener(ListType.ListFavorites)); } else { //launched by the user clicking on the shortcut String stationStr = intent.getStringExtra("station"); StationBean station = StationBean.fromCSV(stationStr); Intent launcher = new Intent( this, DepartureList.class ); launcher.putExtra("stationbean", station); startActivityForResult(launcher, SHOW_DEPARTURES); } } /** * This function creates a shortcut and returns it to the caller. There are actually two * intents that you will send back. * * The first intent serves as a container for the shortcut and is returned to the launcher by * setResult(). This intent must contain three fields: * * * * If you use a simple drawable resource, note that you must wrapper it using * {@link android.content.Intent.ShortcutIconResource}, as shown below. This is required so * that the launcher can access resources that are stored in your application's .apk file. If * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}. * * The shortcut intent can be any intent that you wish the launcher to send, when the user * clicks on the shortcut. Typically this will be {@link android.content.Intent#ACTION_VIEW} * with an appropriate Uri for your content, but any Intent will work here as long as it * triggers the desired action within your Activity. */ private void setupShortcut(StationBean station) { // First, set up the shortcut intent. Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setClassName(this, this.getClass().getName()); shortcutIntent.putExtra("station", station.toCSV() ); // Then, set up the container intent (the response to the caller) Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, station.getName() ); Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.train); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); // Now, return the result to the launcher setResult(RESULT_OK, intent); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case SHOW_DEPARTURES: finish(); break; case SHOW_STATIONS: if (data != null) { station = (StationBean) data.getSerializableExtra("station"); if (station != null) { okBtn.setEnabled(true); TextView tv = (TextView) findViewById(R.id.current_selection); tv.setText( station.getName() ); } } break; } } class StationListener implements OnClickListener{ ListType launchType; StationListener(ListType type) { launchType = type; } @Override public void onClick(View v) { Intent intent = new Intent(ShortcutActivity.this, StationList.class); intent.putExtra("type", launchType); intent.putExtra("shortcut", true); ShortcutActivity.this.startActivityForResult(intent, SHOW_STATIONS); } } }