/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/ShortcutActivity.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/ShortcutActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1078 - (show annotations) (download)
Sat Sep 18 07:18:59 2010 UTC (13 years, 8 months ago) by torben
File size: 6001 byte(s)
Cleanup
1 package dk.thoerup.traininfo;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.os.Parcelable;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
10 import android.widget.TextView;
11 import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
12 import dk.thoerup.traininfo.WelcomeScreen.ListType;
13
14 public class ShortcutActivity extends Activity {
15
16 final static int SHOW_DEPARTURES = 1000;
17 final static int SHOW_STATIONS = 2000;
18
19
20 StationEntry station;
21 Button okBtn;
22
23 @Override
24 public void onCreate(Bundle icicle) {
25 super.onCreate(icicle);
26
27 // Resolve the intent
28
29 final Intent intent = getIntent();
30 final String action = intent.getAction();
31
32 // If the intent is a request to create a shortcut, we'll do that and exit
33
34 if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
35 setContentView(R.layout.shortcut);
36
37 Button cancelBtn = (Button) findViewById(R.id.shortcut_cancel);
38 cancelBtn.setOnClickListener( new OnClickListener() {
39
40 @Override
41 public void onClick(View v) {
42 ShortcutActivity.this.setResult(Activity.RESULT_CANCELED);
43 ShortcutActivity.this.finish();
44 }
45 });
46
47
48 okBtn = (Button) findViewById(R.id.shortcut_ok);
49 okBtn.setEnabled(false);
50 okBtn.setOnClickListener( new OnClickListener() {
51 @Override
52 public void onClick(View v) {
53 setupShortcut(station);
54 finish();
55 }
56 });
57 Button btn = (Button)findViewById(R.id.nearest);
58 btn.setOnClickListener(new StationListener(ListType.ListNearest));
59 btn = (Button)findViewById(R.id.search);
60 btn.setOnClickListener(new StationListener(ListType.ListSearch));
61 btn = (Button)findViewById(R.id.favorites);
62 btn.setOnClickListener(new StationListener(ListType.ListFavorites));
63
64 } else {
65 //launched by the user clicking on the shortcut
66 String stationStr = intent.getStringExtra("station");
67 StationEntry station = StationEntry.fromCSV(stationStr);
68
69 Intent launcher = new Intent( this, DepartureList.class );
70 launcher.putExtra("stationbean", station);
71 startActivityForResult(launcher, SHOW_DEPARTURES);
72 }
73 }
74
75 /**
76 * This function creates a shortcut and returns it to the caller. There are actually two
77 * intents that you will send back.
78 *
79 * The first intent serves as a container for the shortcut and is returned to the launcher by
80 * setResult(). This intent must contain three fields:
81 *
82 * <ul>
83 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
84 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
85 * the shortcut.</li>
86 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
87 * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
88 * a drawable resource.</li>
89 * </ul>
90 *
91 * If you use a simple drawable resource, note that you must wrapper it using
92 * {@link android.content.Intent.ShortcutIconResource}, as shown below. This is required so
93 * that the launcher can access resources that are stored in your application's .apk file. If
94 * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
95 * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
96 *
97 * The shortcut intent can be any intent that you wish the launcher to send, when the user
98 * clicks on the shortcut. Typically this will be {@link android.content.Intent#ACTION_VIEW}
99 * with an appropriate Uri for your content, but any Intent will work here as long as it
100 * triggers the desired action within your Activity.
101 */
102 private void setupShortcut(StationEntry station) {
103 // First, set up the shortcut intent.
104 Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
105 shortcutIntent.setClassName(this, this.getClass().getName());
106 shortcutIntent.putExtra("station", station.toCSV() );
107
108
109 // Then, set up the container intent (the response to the caller)
110 Intent intent = new Intent();
111 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
112 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, station.getName() );
113 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.train);
114 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
115
116
117 // Now, return the result to the launcher
118 setResult(RESULT_OK, intent);
119 }
120
121 @Override
122 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
123 super.onActivityResult(requestCode, resultCode, data);
124
125 switch (requestCode) {
126 case SHOW_DEPARTURES:
127 finish();
128 break;
129
130 case SHOW_STATIONS:
131 if (data != null) {
132 station = (StationEntry) data.getSerializableExtra("station");
133 if (station != null) {
134 okBtn.setEnabled(true);
135
136 TextView tv = (TextView) findViewById(R.id.current_selection);
137 tv.setText( station.getName() );
138
139 }
140 }
141 break;
142 }
143 }
144
145 class StationListener implements OnClickListener{
146 ListType launchType;
147 StationListener(ListType type) {
148 launchType = type;
149 }
150
151 @Override
152 public void onClick(View v) {
153 Intent intent = new Intent(ShortcutActivity.this, StationList.class);
154 intent.putExtra("type", launchType);
155 intent.putExtra("shortcut", true);
156 ShortcutActivity.this.startActivityForResult(intent, SHOW_STATIONS);
157 }
158
159 }
160
161
162 }

  ViewVC Help
Powered by ViewVC 1.1.20