/// <summary>
 /// Initializes a new instance of the <see cref="PortaPodder.ObscuredSharedPreferences+Editor"/> class.
 /// </summary>
 public EncryptedEditor(EncryptedPreferences parent)
 {
     this.childEditor = parent.child.Edit();
 }
        /// <summary>
        /// Called when the application is starting, before any other application objects have been created.
        /// </summary>
        public override void OnCreate()
        {
            base.OnCreate();

              // attempt to read the user name and password
              prefs = new EncryptedPreferences();
              if(prefs.Contains(EncryptedPreferences.KEY_PASSWORD) && prefs.Contains(EncryptedPreferences.KEY_USERNAME)){
            string username = prefs.GetString(EncryptedPreferences.KEY_USERNAME, string.Empty);
            string password = prefs.GetString(EncryptedPreferences.KEY_PASSWORD, string.Empty);
            MyGPO.ConnectedUser = new User(username, password);
              }

              // the datasource
              datasource = new PortaPodderDataSource();

              // get a list of initialization parameters from the prefernces and database
              DateTime start = DateTime.Now;
              List<Device> storedDevices = datasource.GetAllDevices();
              List<Subscription> subscriptions = datasource.GetSubscriptions();
              List<Episode> episodes = datasource.GetEpisodes();
              PortaPodderApp.LogMessage("It took " + (DateTime.Now - start).TotalSeconds + " seconds to load data");

              string selectedDevice = prefs.GetString(EncryptedPreferences.KEY_SELECTED_DEVICE, string.Empty);
              long lastUpdated = prefs.GetLong(EncryptedPreferences.KEY_LAST_UPDATED, 0);

              // setup the server from the database
              MyGPO.Initialize(storedDevices, selectedDevice, subscriptions, episodes, lastUpdated);

              // add hooks for removing and adding devices
              MyGPO.DeviceAdded += delegate(Device theDevice) {
            datasource.InsertOrUpdate(theDevice);
              };
              MyGPO.DeviceRemoved += delegate(Device theDevice) {
            datasource.DeleteDevice(theDevice);
              };

              // hook for changing the device
              MyGPO.SelectedDeviceChanged += delegate(Device theDevice) {
            ISharedPreferencesEditor editor = prefs.Edit();
            editor.PutString(EncryptedPreferences.KEY_SELECTED_DEVICE, theDevice != null ? theDevice.Id : string.Empty);
            editor.Commit();
              };

              // hooks for episodes
              MyGPO.EpisodeAdded += delegate(List<Episode> addedEpisodes) {
            PortaPodderApp.LogMessage("Adding " + addedEpisodes.Count + " to database");
            datasource.Insert(addedEpisodes);
              };
              MyGPO.EpisodeRemoved += delegate(List<Episode> removedEpisodes) {
            foreach(Episode episode in removedEpisodes){
              datasource.DeleteEpisode(episode);
            }
              };
              Episode.EpisodeUpdates += delegate(Episode episode) {
            datasource.InsertOrUpdate(episode);
              };

              // hooks for subscriptions
              MyGPO.SubscriptionAdded += delegate(Subscription subscription) {
            datasource.InsertOrUpdate(subscription);
              };
              MyGPO.SubscriptionRemoved += delegate(Subscription subscription) {
            datasource.DeleteSubscription(subscription);
              };

              // log the message
              MyGPO.LogMessageEvent += new MyGPO.LogMethod(LogMessage);

              // write the last updated time to the preferences
              MyGPO.UpdatedDateTime += delegate(long updated) {
            ISharedPreferencesEditor editor = prefs.Edit();
            editor.PutLong(EncryptedPreferences.KEY_LAST_UPDATED, updated);
            editor.Commit();
              };
              TelephonyManager tel = (TelephonyManager)GetSystemService(Android.Content.Context.TelephonyService);
              tel.Listen(new IncomingCallReceiver(), PhoneStateListenerFlags.CallState);
        }