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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20