/[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 731 - (show annotations) (download)
Tue May 18 14:02:13 2010 UTC (14 years ago) by torben
File size: 5926 byte(s)
Add support for home/desktop shortcuts

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

  ViewVC Help
Powered by ViewVC 1.1.20