/[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 1735 - (hide annotations) (download)
Thu Mar 15 19:46:10 2012 UTC (12 years, 2 months ago) by torben
File size: 7015 byte(s)
Make shortcut creation work again
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 (InvalidClassException e) {
50     Log.i("TrainInfo", "invalid class - do a new download of stationlist");
51     }
52     catch (Exception e) {
53     Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
54     Log.e("TrainInfo", "load error", e);
55     }
56    
57    
58 torben 731 Button cancelBtn = (Button) findViewById(R.id.shortcut_cancel);
59     cancelBtn.setOnClickListener( new OnClickListener() {
60    
61     @Override
62     public void onClick(View v) {
63     ShortcutActivity.this.setResult(Activity.RESULT_CANCELED);
64     ShortcutActivity.this.finish();
65     }
66     });
67    
68    
69     okBtn = (Button) findViewById(R.id.shortcut_ok);
70     okBtn.setEnabled(false);
71     okBtn.setOnClickListener( new OnClickListener() {
72     @Override
73     public void onClick(View v) {
74     setupShortcut(station);
75     finish();
76     }
77     });
78     Button btn = (Button)findViewById(R.id.nearest);
79     btn.setOnClickListener(new StationListener(ListType.ListNearest));
80     btn = (Button)findViewById(R.id.search);
81     btn.setOnClickListener(new StationListener(ListType.ListSearch));
82     btn = (Button)findViewById(R.id.favorites);
83     btn.setOnClickListener(new StationListener(ListType.ListFavorites));
84    
85     } else {
86     //launched by the user clicking on the shortcut
87     String stationStr = intent.getStringExtra("station");
88 torben 1151
89     if (stationStr != null) { //haven't reproduced it, but got a stacktrace where stationStr apparently was null
90 torben 1562 StationEntry station = StationEntryCsv.fromCSV(stationStr);
91 torben 731
92 torben 1151 Intent launcher = new Intent( this, DepartureList.class );
93     launcher.putExtra("stationbean", station);
94     startActivityForResult(launcher, SHOW_DEPARTURES);
95     } else {
96     Toast.makeText(this, "Invalid TrainInfo shortcut - please delete and re-create", Toast.LENGTH_LONG).show();
97     }
98 torben 731 }
99     }
100    
101     /**
102     * This function creates a shortcut and returns it to the caller. There are actually two
103     * intents that you will send back.
104     *
105     * The first intent serves as a container for the shortcut and is returned to the launcher by
106     * setResult(). This intent must contain three fields:
107     *
108     * <ul>
109     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
110     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
111     * the shortcut.</li>
112     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
113     * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
114     * a drawable resource.</li>
115     * </ul>
116     *
117     * If you use a simple drawable resource, note that you must wrapper it using
118     * {@link android.content.Intent.ShortcutIconResource}, as shown below. This is required so
119     * that the launcher can access resources that are stored in your application's .apk file. If
120     * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras
121     * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
122     *
123     * The shortcut intent can be any intent that you wish the launcher to send, when the user
124     * clicks on the shortcut. Typically this will be {@link android.content.Intent#ACTION_VIEW}
125     * with an appropriate Uri for your content, but any Intent will work here as long as it
126     * triggers the desired action within your Activity.
127     */
128 torben 1066 private void setupShortcut(StationEntry station) {
129 torben 731 // First, set up the shortcut intent.
130     Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
131     shortcutIntent.setClassName(this, this.getClass().getName());
132 torben 1562 shortcutIntent.putExtra("station", StationEntryCsv.toCSV(station) );
133 torben 731
134    
135     // Then, set up the container intent (the response to the caller)
136     Intent intent = new Intent();
137     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
138     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, station.getName() );
139     Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.train);
140     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
141    
142    
143     // Now, return the result to the launcher
144     setResult(RESULT_OK, intent);
145     }
146    
147     @Override
148     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
149     super.onActivityResult(requestCode, resultCode, data);
150    
151     switch (requestCode) {
152     case SHOW_DEPARTURES:
153     finish();
154     break;
155    
156     case SHOW_STATIONS:
157     if (data != null) {
158 torben 1066 station = (StationEntry) data.getSerializableExtra("station");
159 torben 731 if (station != null) {
160     okBtn.setEnabled(true);
161    
162     TextView tv = (TextView) findViewById(R.id.current_selection);
163     tv.setText( station.getName() );
164    
165     }
166     }
167     break;
168     }
169     }
170    
171     class StationListener implements OnClickListener{
172     ListType launchType;
173     StationListener(ListType type) {
174     launchType = type;
175     }
176    
177     @Override
178     public void onClick(View v) {
179     Intent intent = new Intent(ShortcutActivity.this, StationList.class);
180     intent.putExtra("type", launchType);
181     intent.putExtra("shortcut", true);
182     ShortcutActivity.this.startActivityForResult(intent, SHOW_STATIONS);
183     }
184    
185     }
186    
187    
188     }

  ViewVC Help
Powered by ViewVC 1.1.20