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