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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1547 - (hide annotations) (download)
Thu Jul 7 19:07:55 2011 UTC (12 years, 10 months ago) by torben
File size: 5713 byte(s)
First working version with offline stationlist
1 torben 481 package dk.thoerup.traininfo;
2    
3     import android.app.Activity;
4 torben 1547 import android.app.ProgressDialog;
5 torben 481 import android.content.Intent;
6 torben 1547 import android.content.SharedPreferences;
7     import android.content.SharedPreferences.Editor;
8 torben 500 import android.net.Uri;
9 torben 1547 import android.os.AsyncTask;
10 torben 481 import android.os.Bundle;
11 torben 743 import android.os.Handler;
12 torben 1159 import android.util.Log;
13 torben 481 import android.view.View;
14 torben 1547 import android.view.View.OnClickListener;
15 torben 482 import android.view.Window;
16 torben 481 import android.widget.Button;
17 torben 1547 import android.widget.Toast;
18 torben 481
19 torben 720 import com.nullwire.trace.ExceptionHandler;
20    
21 torben 1131 import dk.thoerup.androidutils.CheckUpdates;
22 torben 1547 import dk.thoerup.traininfo.provider.OfflineStationProvider;
23 torben 1159 import dk.thoerup.traininfo.provider.ProviderFactory;
24 torben 1547 import dk.thoerup.traininfo.provider.StationProvider;
25 torben 720
26 torben 481 public class WelcomeScreen extends Activity{
27 torben 1547
28     final static String stationsreload = "stationsreload";
29    
30 torben 481 public enum ListType {
31     ListNearest,
32     ListSearch,
33     ListFavorites
34     }
35    
36 torben 743 Handler handler = new Handler();
37    
38 torben 1547 SharedPreferences prefs;
39    
40 torben 481 @Override
41     public void onCreate(Bundle savedInstanceState) {
42 torben 916
43     super.onCreate(savedInstanceState);
44 torben 1547
45     prefs = getSharedPreferences("TrainStation", 0);
46    
47 torben 482 requestWindowFeature( Window.FEATURE_NO_TITLE );
48 torben 481 setContentView(R.layout.welcome);
49    
50     Button nearestButton = (Button) findViewById(R.id.nearest);
51     nearestButton.setOnClickListener( new StationListListener(ListType.ListNearest));
52    
53     Button searchButton = (Button) findViewById(R.id.search);
54     searchButton.setOnClickListener( new StationListListener(ListType.ListSearch));
55    
56     Button favoritesButton = (Button) findViewById(R.id.favorites);
57     favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
58    
59 torben 1446 Button settingsButton = (Button) findViewById(R.id.settings);
60     settingsButton.setOnClickListener( new SettingsListener() );
61    
62 torben 481 Button aboutButton = (Button) findViewById(R.id.about);
63     aboutButton.setOnClickListener( new AboutListener() );
64 torben 568
65     ExceptionHandler.register(this, "http://t-hoerup.dk/android/trace.php");
66 torben 720
67     CheckUpdates update = new CheckUpdates();
68 torben 911 update.checkForUpdates(this, "http://t-hoerup.dk/android/traininfo/version.txt", "TrainInfo DK", null);
69 torben 846 /*
70 torben 743 Runnable r = new Runnable() {
71     @Override
72     public void run() {
73     View splash = findViewById(R.id.splash);
74     splash.setVisibility(View.GONE);
75     }
76     };
77     handler.postDelayed(r, 1500);
78 torben 846 */
79 torben 1547
80     StationProvider sp = ProviderFactory.getStationProvider();
81    
82     if (sp instanceof OfflineStationProvider ) {
83     OfflineStationProvider osp = (OfflineStationProvider) sp;
84     try {
85    
86     long last = prefs.getLong(stationsreload, 0);
87     long now = System.currentTimeMillis();
88    
89     if ( (now-last) > (14*24*60*60*1000) ) {
90     new StationLoader(osp).execute( (Void)null);
91     } else {
92    
93     boolean didLoad = osp.loadStations(this);
94    
95     if (didLoad == false) {
96     new StationLoader(osp).execute( (Void)null);
97     }
98     }
99    
100     } catch (Exception e) {
101     Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
102     }
103     }
104 torben 481 }
105 torben 1159
106    
107 torben 743
108 torben 1159 @Override
109     protected void onDestroy() {
110     super.onDestroy();
111     ProviderFactory.purgeOldEntries(); //exiting application, do some cleanup
112     }
113    
114    
115    
116 torben 481 class AboutListener implements OnClickListener {
117    
118     @Override
119     public void onClick(View v) {
120 torben 500 /*
121 torben 484 String appName = WelcomeScreen.this.getResources().getString(R.string.app_name);
122     String ver = WelcomeScreen.this.getResources().getString(R.string.app_version);
123    
124 torben 483 StringBuffer message = new StringBuffer();
125 torben 484 message.append(appName);
126     message.append(" v").append(ver).append("\n");
127 torben 483 message.append("By Torben H. Nielsen\n");
128    
129 torben 500 MessageBox.showMessage(WelcomeScreen.this, message.toString());*/
130 torben 1393 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t-hoerup.dk/android/traininfo/"));
131 torben 500 startActivity(browserIntent);
132 torben 481 }
133 torben 483
134 torben 481 }
135 torben 1446
136     class SettingsListener implements OnClickListener{
137    
138     @Override
139     public void onClick(View v) {
140     Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);
141     WelcomeScreen.this.startActivity(intent);
142     }
143     }
144 torben 483
145 torben 481 class StationListListener implements OnClickListener{
146     ListType launchType;
147     StationListListener(ListType type) {
148     launchType = type;
149     }
150    
151     @Override
152     public void onClick(View v) {
153     Intent intent = new Intent(WelcomeScreen.this, StationList.class);
154     intent.putExtra("type", launchType);
155     WelcomeScreen.this.startActivity(intent);
156     }
157    
158     }
159 torben 1547
160     class StationLoader extends AsyncTask<Void,Void,Void> {
161    
162    
163     boolean succeeded;
164     ProgressDialog dlg;
165     OfflineStationProvider osp;
166    
167     public StationLoader(OfflineStationProvider osp) {
168     this.osp = osp;
169     }
170    
171     @Override
172     protected Void doInBackground(Void... params) {
173     try {
174     osp.downloadStations( WelcomeScreen.this );
175     succeeded = true;
176     } catch (Exception e) {
177     succeeded = false;
178     Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show();
179     }
180     return null;
181     }
182    
183     @Override
184     protected void onPreExecute() {
185     super.onPreExecute();
186    
187     dlg = new ProgressDialog(WelcomeScreen.this);
188     dlg.setMessage( "Downloading stations list" );//TODO: translate
189     dlg.setCancelable(true);
190     dlg.show();
191     }
192    
193     @Override
194     protected void onPostExecute(Void result) {
195     super.onPostExecute(result);
196    
197     dlg.dismiss();
198     dlg = null;
199    
200     if (succeeded) {
201     Editor edit = prefs.edit();
202     edit.putLong(stationsreload, System.currentTimeMillis() );
203     edit.commit();
204     }
205     }
206     }
207    
208 torben 481 }

  ViewVC Help
Powered by ViewVC 1.1.20