/[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 1066 - (show annotations) (download)
Thu Sep 16 15:32:42 2010 UTC (13 years, 8 months ago) by torben
File size: 6058 byte(s)
Experimental #5, update TrainInfo client to use common data-beans
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;
12 import dk.thoerup.android.traininfo.common.StationBean.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 StationEntry station = StationEntry.fromCSV(stationStr);
69
70 Intent launcher = new Intent( this, DepartureList.class );
71 launcher.putExtra("stationbean", station);
72 startActivityForResult(launcher, SHOW_DEPARTURES);
73 }
74 }
75
76 /**
77 * This function creates a shortcut and returns it to the caller. There are actually two
78 * intents that you will send back.
79 *
80 * The first intent serves as a container for the shortcut and is returned to the launcher by
81 * setResult(). This intent must contain three fields:
82 *
83 * <ul>
84 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
85 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
86 * the shortcut.</li>
87 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
88 * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
89 * a drawable resource.</li>
90 * </ul>
91 *
92 * If you use a simple drawable resource, note that you must wrapper it using
93 * {@link android.content.Intent.ShortcutIconResource}, as shown below. This is required so
94 * that the launcher can access resources that are stored in your application's .apk file. If
95 * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
96 * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
97 *
98 * The shortcut intent can be any intent that you wish the launcher to send, when the user
99 * clicks on the shortcut. Typically this will be {@link android.content.Intent#ACTION_VIEW}
100 * with an appropriate Uri for your content, but any Intent will work here as long as it
101 * triggers the desired action within your Activity.
102 */
103 private void setupShortcut(StationEntry station) {
104 // First, set up the shortcut intent.
105 Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
106 shortcutIntent.setClassName(this, this.getClass().getName());
107 shortcutIntent.putExtra("station", station.toCSV() );
108
109
110 // Then, set up the container intent (the response to the caller)
111 Intent intent = new Intent();
112 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
113 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, station.getName() );
114 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.train);
115 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
116
117
118 // Now, return the result to the launcher
119 setResult(RESULT_OK, intent);
120 }
121
122 @Override
123 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
124 super.onActivityResult(requestCode, resultCode, data);
125
126 switch (requestCode) {
127 case SHOW_DEPARTURES:
128 finish();
129 break;
130
131 case SHOW_STATIONS:
132 if (data != null) {
133 station = (StationEntry) data.getSerializableExtra("station");
134 if (station != null) {
135 okBtn.setEnabled(true);
136
137 TextView tv = (TextView) findViewById(R.id.current_selection);
138 tv.setText( station.getName() );
139
140 }
141 }
142 break;
143 }
144 }
145
146 class StationListener implements OnClickListener{
147 ListType launchType;
148 StationListener(ListType type) {
149 launchType = type;
150 }
151
152 @Override
153 public void onClick(View v) {
154 Intent intent = new Intent(ShortcutActivity.this, StationList.class);
155 intent.putExtra("type", launchType);
156 intent.putExtra("shortcut", true);
157 ShortcutActivity.this.startActivityForResult(intent, SHOW_STATIONS);
158 }
159
160 }
161
162
163 }

  ViewVC Help
Powered by ViewVC 1.1.20