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