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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1547 by torben, Thu Jul 7 19:07:55 2011 UTC revision 1555 by torben, Fri Jul 8 12:24:48 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    
4    
5    import java.io.InvalidClassException;
6    
7  import android.app.Activity;  import android.app.Activity;
8  import android.app.ProgressDialog;  import android.app.ProgressDialog;
9  import android.content.Intent;  import android.content.Intent;
# Line 10  import android.os.AsyncTask; Line 14  import android.os.AsyncTask;
14  import android.os.Bundle;  import android.os.Bundle;
15  import android.os.Handler;  import android.os.Handler;
16  import android.util.Log;  import android.util.Log;
17    import android.view.Menu;
18    import android.view.MenuItem;
19  import android.view.View;  import android.view.View;
20  import android.view.View.OnClickListener;  import android.view.View.OnClickListener;
21  import android.view.Window;  import android.view.Window;
# Line 23  import dk.thoerup.traininfo.provider.Off Line 29  import dk.thoerup.traininfo.provider.Off
29  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
30  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
31    
32    
33  public class WelcomeScreen extends Activity{  public class WelcomeScreen extends Activity{
34                    
35          final static String stationsreload = "stationsreload";          final static String stationsreload = "stationsreload";
36            final static int MENU_SETTINGS = 1;
37            final static int MENU_RELOAD = 2;
38            
39                    
40          public enum ListType {          public enum ListType {
41                  ListNearest,                  ListNearest,
# Line 56  public class WelcomeScreen extends Activ Line 66  public class WelcomeScreen extends Activ
66                  Button favoritesButton = (Button) findViewById(R.id.favorites);                  Button favoritesButton = (Button) findViewById(R.id.favorites);
67                  favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));                  favoritesButton.setOnClickListener( new StationListListener(ListType.ListFavorites));
68                                    
                 Button settingsButton = (Button) findViewById(R.id.settings);  
                 settingsButton.setOnClickListener( new SettingsListener() );  
                   
69                  Button aboutButton = (Button) findViewById(R.id.about);                  Button aboutButton = (Button) findViewById(R.id.about);
70                  aboutButton.setOnClickListener( new AboutListener() );                  aboutButton.setOnClickListener( new AboutListener() );
71                                    
# Line 81  public class WelcomeScreen extends Activ Line 88  public class WelcomeScreen extends Activ
88                                    
89                  if (sp instanceof OfflineStationProvider ) {                  if (sp instanceof OfflineStationProvider ) {
90                          OfflineStationProvider osp = (OfflineStationProvider) sp;                          OfflineStationProvider osp = (OfflineStationProvider) sp;
91                          try {                          long last = prefs.getLong(stationsreload, 0);
92                            long now = System.currentTimeMillis();
93                            Log.i("TrainInfo", "Last Load: " + last);
94                            
95                            if ( (now-last) > (14*24*60*60*1000) ) {
96                                    new StationLoader(osp).execute( (Void)null);
97                            } else {
98                            
99                                    boolean didLoad = false;
100                                                                    
101                                  long last = prefs.getLong(stationsreload, 0);                                  try {
102                                  long now = System.currentTimeMillis();                                          didLoad = osp.loadStations(this);
103                                    }
104                                    catch (InvalidClassException e) {
105                                            Log.i("TrainInfo", "invalid class - do a new download of stationlist");
106                                    }
107                                    catch (Exception e) {
108                                            Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
109                                            Log.e("TrainInfo", "load error", e);
110                                    }
111                                                                    
112                                  if ( (now-last) > (14*24*60*60*1000) ) {                                  if (didLoad == false) {                                
113                                          new StationLoader(osp).execute( (Void)null);                                          new StationLoader(osp).execute( (Void)null);
                                 } else {  
                                   
                                         boolean didLoad = osp.loadStations(this);  
                                           
                                         if (didLoad == false) {                                  
                                                 new StationLoader(osp).execute( (Void)null);  
                                         }  
114                                  }                                  }
                                   
                         } catch (Exception e) {  
                                 Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();  
115                          }                          }
116    
117                  }                  }
118          }          }
119                    
# Line 112  public class WelcomeScreen extends Activ Line 126  public class WelcomeScreen extends Activ
126          }          }
127    
128    
129            @Override
130            public boolean onCreateOptionsMenu(Menu menu) {
131                    MenuItem item;
132                    
133                    item = menu.add(0, MENU_SETTINGS, 0, getString(R.string.welcome_settings) );
134                    item.setIcon(android.R.drawable.ic_menu_preferences);
135                    
136                    item = menu.add(0, MENU_RELOAD, 0, getString(R.string.welcome_reloadstations));
137                    item.setIcon(android.R.drawable.ic_menu_rotate);
138                    
139                    return true;
140            }
141            
142            @Override
143            public boolean onOptionsItemSelected(MenuItem item) {
144                    boolean retval = true;
145                    
146                    switch (item.getItemId()) {
147                    case MENU_SETTINGS:
148                            Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);
149                            WelcomeScreen.this.startActivity(intent);
150                            break;
151                            
152                    case MENU_RELOAD:
153                            OfflineStationProvider osp = (OfflineStationProvider) ProviderFactory.getStationProvider();
154                            new StationLoader(osp).execute( (Void)null);                    
155                            break;
156                            
157                    default:
158                            retval = super.onOptionsItemSelected(item);
159                    }
160                    
161                    return retval;
162            }
163            
164    
165          class AboutListener implements OnClickListener {          class AboutListener implements OnClickListener {
166    
# Line 133  public class WelcomeScreen extends Activ Line 182  public class WelcomeScreen extends Activ
182    
183          }          }
184                    
         class SettingsListener implements OnClickListener{  
           
                 @Override  
                 public void onClick(View v) {  
                         Intent intent = new Intent(WelcomeScreen.this, SettingsScreen.class);  
                         WelcomeScreen.this.startActivity(intent);  
                 }                
         }  
185    
186          class StationListListener implements OnClickListener{          class StationListListener implements OnClickListener{
187                  ListType launchType;                  ListType launchType;
# Line 163  public class WelcomeScreen extends Activ Line 204  public class WelcomeScreen extends Activ
204                  boolean succeeded;                  boolean succeeded;
205                  ProgressDialog dlg;                  ProgressDialog dlg;
206                  OfflineStationProvider osp;                  OfflineStationProvider osp;
207                    String exMsg;
208                                    
209                  public StationLoader(OfflineStationProvider osp) {                  public StationLoader(OfflineStationProvider osp) {
210                          this.osp = osp;                          this.osp = osp;
# Line 175  public class WelcomeScreen extends Activ Line 217  public class WelcomeScreen extends Activ
217                                  succeeded = true;                                  succeeded = true;
218                          } catch (Exception e) {                          } catch (Exception e) {
219                                  succeeded = false;                                  succeeded = false;
220                                  Toast.makeText(WelcomeScreen.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show();                                  exMsg = e.getMessage();                        
221                          }                          }
222                          return null;                          return null;
223                  }                  }
# Line 201  public class WelcomeScreen extends Activ Line 243  public class WelcomeScreen extends Activ
243                                  Editor edit = prefs.edit();                                  Editor edit = prefs.edit();
244                                  edit.putLong(stationsreload, System.currentTimeMillis() );                                  edit.putLong(stationsreload, System.currentTimeMillis() );
245                                  edit.commit();                                  edit.commit();
246                            } else {
247                                    Toast.makeText(WelcomeScreen.this, "Error " + exMsg, Toast.LENGTH_LONG).show();
248                          }                          }
249                  }                        }      
250          }          }

Legend:
Removed from v.1547  
changed lines
  Added in v.1555

  ViewVC Help
Powered by ViewVC 1.1.20