/// <summary> /// Load the collection of train trips /// </summary> private static void LoadTrips() { // Load the trips int numberOfTrips = PersistentStorage.GetIntItem(TrainTripsSizeName, 0); trips = new List <TrainTrip>(); for (int tripIndex = 0; tripIndex < numberOfTrips; ++tripIndex) { TrainTrip tripToAdd = new TrainTrip { From = PersistentStorage.GetStringItem(TrainTripFromName + tripIndex, ""), To = PersistentStorage.GetStringItem(TrainTripToName + tripIndex, ""), FromCode = PersistentStorage.GetStringItem(TrainTripFromCodeName + tripIndex, ""), ToCode = PersistentStorage.GetStringItem(TrainTripToCodeName + tripIndex, "") }; trips.Add(tripToAdd); } // Get the current trip selectedTrip = PersistentStorage.GetIntItem(TrainTripSelectedName, 0); // Make sure that the selected trip is valid if (selectedTrip >= trips.Count) { selectedTrip = trips.Count - 1; PersistentStorage.SetIntItem(TrainTripSelectedName, selectedTrip); } }
/// <summary> /// Save the entire station list to storage /// </summary> private static void SaveStations() { PersistentStorage.SetIntItem(RecentStationsSizeName, stations.Count); for (int stationIndex = 0; stationIndex < stations.Count; ++stationIndex) { PersistentStorage.SetStringItem(RecentStationName + stationIndex, stations[stationIndex]); } }
/// <summary> /// This method is called when the BroadcastReceiver has received an Intent /// </summary> /// <param name="context">The Context in which the receiver is running.</param> /// <param name="intent">The Intent being received.</param> public override void OnReceive(Context context, Intent intent) { base.OnReceive(context, intent); // Check if the click is from the details text being clicked if (DetailsClick == intent.Action) { // Open the main QuTi activity context.StartActivity(new Intent(context, typeof(MainActivity))); } // If a new trip has been selected then update all widgets to display it else if (intent.Action == TripUpdated) { UpdateAllWidgets(context); } // If this is an update service state change then action it else if ((intent.Action == StartClick) || (intent.Action == StopClick)) { // Initialise the persistent storage PersistentStorage.StorageMechanism = new StorageMechanism(context); PersistentStorage.UseCache = false; // Store the state of the update service PersistentStorage.SetBoolItem(UpdateServiceRunningName, (intent.Action == StartClick)); // Start or stop the service if (intent.Action == StartClick) { context.StartService(new Intent(context, typeof(UpdateService))); } else { context.StopService(new Intent(context, typeof(UpdateService))); } // Display the updated state UpdateAllWidgets(context); } // If this is a departure time change then update all the widgets else if ((intent.Action == NextDepartureSuspectChange) || (intent.Action == NextDepartureTimeChange)) { UpdateAllWidgets(context); } // If the device has just unlocked then update the widget else if (intent.Action == Intent.ActionUserPresent) { UpdateAllWidgets(context); } else if ((intent.Action == UpdateInProgress) || (intent.Action == UpdateFinished)) { // Store the state of the update operation PersistentStorage.SetBoolItem(UpdateInProgressName, (intent.Action == UpdateInProgress)); UpdateAllWidgets(context); } }
/// <summary> /// Renders the visual contents of a specific widget contents into a RemoteViews object /// </summary> /// <param name="widgetContext">Widget context.</param> /// <param name="widgetId">Widget identifier.</param> private RemoteViews RenderWidgetContents(Context context, int widgetId) { // Initialise the persistent storage PersistentStorage.StorageMechanism = new StorageMechanism(context); PersistentStorage.UseCache = false; // Create a RemoteView for the widget RemoteViews widgetView = new RemoteViews(context.PackageName, Resource.Layout.widget); // Extract the current trip details and display them. // The trip details and selected trip can be changed independently by the main app so a new set of train trip details need to be read TrainTrips.Reset(); TrainTrip selectedTrip = TrainTrips.SelectedTrip; if (selectedTrip != null) { widgetView.SetTextViewText(Resource.Id.widgetTrip, string.Format("{0}:{1}", selectedTrip.FromCode, selectedTrip.ToCode)); } // Extract the next departure time and display it DateTime departureTime = NextDeparture.DepartureTime; widgetView.SetTextViewText(Resource.Id.widgetDeparture, departureTime.ToString("HH:mm")); // Register pending intents for clicking on the displayed fields RegisterClicks(context, widgetView); // Show the correct image for the running state of the update service if (PersistentStorage.GetBoolItem(UpdateInProgressName, false) == true) { // An update is actually in progress, so show the progress indicator and hide // the service status flags widgetView.SetViewVisibility(Resource.Id.layoutProgressBar, Android.Views.ViewStates.Visible); widgetView.SetViewVisibility(Resource.Id.imageStart, Android.Views.ViewStates.Gone); widgetView.SetViewVisibility(Resource.Id.imageStop, Android.Views.ViewStates.Gone); } else { // Hide the progress indicator and show the servide state widgetView.SetViewVisibility(Resource.Id.layoutProgressBar, Android.Views.ViewStates.Gone); if (PersistentStorage.GetBoolItem(UpdateServiceRunningName, false) == true) { widgetView.SetViewVisibility(Resource.Id.imageStart, Android.Views.ViewStates.Gone); widgetView.SetViewVisibility(Resource.Id.imageStop, Android.Views.ViewStates.Visible); } else { widgetView.SetViewVisibility(Resource.Id.imageStart, Android.Views.ViewStates.Visible); widgetView.SetViewVisibility(Resource.Id.imageStop, Android.Views.ViewStates.Gone); } } return(widgetView); }
/// <summary> /// Load upto MaxStations station names from the persistent storage /// </summary> private static void LoadStations() { // Load the stations int numberOfRecentStations = Math.Min(PersistentStorage.GetIntItem(RecentStationsSizeName, 0), MaxStations); stations = new List <string>(); for (int stationIndex = 0; stationIndex < numberOfRecentStations; ++stationIndex) { stations.Add(PersistentStorage.GetStringItem(RecentStationName + stationIndex, "")); } }
/// <summary> /// Save the entire trip list to storage /// </summary> private static void SaveTrips() { PersistentStorage.SetIntItem(TrainTripsSizeName, trips.Count); for (int tripIndex = 0; tripIndex < trips.Count; ++tripIndex) { PersistentStorage.SetStringItem(TrainTripFromName + tripIndex, trips[tripIndex].From); PersistentStorage.SetStringItem(TrainTripToName + tripIndex, trips[tripIndex].To); PersistentStorage.SetStringItem(TrainTripFromCodeName + Trips.Count, trips[tripIndex].FromCode); PersistentStorage.SetStringItem(TrainTripToCodeName + Trips.Count, trips[tripIndex].ToCode); } }
/// <summary> /// Add a new trip to the collection and store. /// </summary> /// <param name="trip"></param> public static void AddTrip(TrainTrip trip) { // Store the trip PersistentStorage.SetStringItem(TrainTripFromName + Trips.Count, trip.From); PersistentStorage.SetStringItem(TrainTripToName + Trips.Count, trip.To); PersistentStorage.SetStringItem(TrainTripFromCodeName + Trips.Count, trip.FromCode); PersistentStorage.SetStringItem(TrainTripToCodeName + Trips.Count, trip.ToCode); // Add to the list Trips.Add(trip); // Update the count PersistentStorage.SetIntItem(TrainTripsSizeName, Trips.Count); }
/// <summary> /// Called when the first widget is displayed. /// </summary> /// <param name="context"></param> public override void OnEnabled(Context context) { // Initialise the persistent storage PersistentStorage.StorageMechanism = new StorageMechanism(context); PersistentStorage.UseCache = false; // Initialise the update service state to not running PersistentStorage.SetBoolItem(UpdateServiceRunningName, false); // Register for when the device wakes up and the keylock is off // Cannot register for this in the manifest // Also cannot use the provided context, must use the application context?? context.ApplicationContext.RegisterReceiver(this, new IntentFilter(Intent.ActionUserPresent)); base.OnEnabled(context); }