public override void OnReceive(Context context, Intent intent) { PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService); PowerManager.WakeLock w1 = pm.NewWakeLock (WakeLockFlags.Full | WakeLockFlags.AcquireCausesWakeup | WakeLockFlags.OnAfterRelease, "NotificationReceiver"); w1.Acquire (); //Toast.MakeText (context, "Received intent!", ToastLength.Short).Show (); var nMgr = (NotificationManager)context.GetSystemService (Context.NotificationService); //var notification = new Notification (Resource.Drawable.Icon, context.Resources.GetString(Resource.String.ReminderTitle)); var notification = new Notification (Resource.Drawable.Icon, context.Resources.GetString(Resource.String.ReminderTitle)); //Clicking the pending intent does not go to the Home Activity Screen, but to the last activity that was active before leaving the app var pendingIntent = PendingIntent.GetActivity (context, 0, new Intent (context, typeof(Home)), PendingIntentFlags.UpdateCurrent); //Notification should be language specific notification.SetLatestEventInfo (context, context.Resources.GetString(Resource.String.ReminderTitle), context.Resources.GetString(Resource.String.InvalidationText), pendingIntent); notification.Flags |= NotificationFlags.AutoCancel; nMgr.Notify (0, notification); // Vibrator vibrator = (Vibrator) context.GetSystemService(Context.VibratorService); // if (vibrator != null) // vibrator.Vibrate(400); //change shared preferences such that the questionnaire button can change its availability ISharedPreferences sharedPref = context.GetSharedPreferences("com.FSoft.are_u_ok.PREFERENCES",FileCreationMode.Private); ISharedPreferencesEditor editor = sharedPref.Edit(); editor.PutBoolean("QuestionnaireActive", false ); editor.Commit (); //insert a line of -1 into some DB values to indicate that the questions have not been answered at the scheduled time MoodDatabase dbMood = new MoodDatabase(context); ContentValues insertValues = new ContentValues(); insertValues.Put("date", DateTime.Now.ToString("dd.MM.yy")); insertValues.Put("time", DateTime.Now.ToString("HH:mm")); insertValues.Put("mood", -1); insertValues.Put("people", -1); insertValues.Put("what", -1); insertValues.Put("location", -1); //use the old value of questionFlags Android.Database.ICursor cursor; cursor = dbMood.ReadableDatabase.RawQuery("SELECT date, QuestionFlags FROM MoodData WHERE date = '" + DateTime.Now.ToString("dd.MM.yy") + "'", null); // cursor query int alreadyAsked = 0; //default value: no questions have been asked yet if (cursor.Count > 0) { //data was already saved today and questions have been asked, so retrieve which ones have been asked cursor.MoveToLast (); //take the last entry of today alreadyAsked = cursor.GetInt(cursor.GetColumnIndex("QuestionFlags")); //retrieve value from last entry in db column QuestionFlags } insertValues.Put("QuestionFlags", alreadyAsked); dbMood.WritableDatabase.Insert ("MoodData", null, insertValues); //set the new alarm AlarmReceiverQuestionnaire temp = new AlarmReceiverQuestionnaire(); temp.SetAlarm(context); w1.Release (); //check these pages for really waking up the device // http://stackoverflow.com/questions/6864712/android-alarmmanager-not-waking-phone-up // https://forums.xamarin.com/discussion/7490/alarm-manager //it's good to use the alarm manager for tasks that should last even days: // http://stackoverflow.com/questions/14376470/scheduling-recurring-task-in-android/ }
protected override void OnCreate(Bundle bundle) { base.OnCreate (bundle); // Create your application here SetContentView (Resource.Layout.Settings); Button BackHome = FindViewById<Button> (Resource.Id.button1); BackHome.Click += delegate { //create an intent to go back to home Intent intent = new Intent(this, typeof(Home)); intent.SetFlags(ActivityFlags.ClearTop); //remove the history and go back to home screen StartActivity(intent); }; //save name in preferences //first check if there is already one saved and if yes put it in the field //http://developer.android.com/training/basics/data-storage/shared-preferences.html ISharedPreferences sharedPref = GetSharedPreferences("com.FSoft.are_u_ok.PREFERENCES",FileCreationMode.Private); String savedName = sharedPref.GetString ("Name", ""); EditText NameEdit = FindViewById<EditText> (Resource.Id.editText1); if (savedName.Length > 0) NameEdit.Text = savedName; NameEdit.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => { //if text is entered, save the name in the preferences ISharedPreferencesEditor editor = sharedPref.Edit(); editor.PutString("Name", e.Text.ToString() ); editor.Commit (); }; //determine current language and set radio buttons accordingly RadioButton RadioGerman = FindViewById<RadioButton> (Resource.Id.radioButton1); RadioButton RadioEnglish = FindViewById<RadioButton> (Resource.Id.radioButton2); Android.Content.Res.Configuration conf = this.Resources.Configuration; //Debug output // Console.WriteLine("Language: {0}", conf.Locale.Language.ToString()); if (conf.Locale.Language == "de") { RadioGerman.Checked = true; } else { RadioEnglish.Checked = true; } //change language with radio buttons RadioGerman.Click += delegate { //try the concept of a Toast Toast toast = Toast.MakeText(this, "Switching to German", ToastLength.Short); //Toast toast = Toast.MakeText(this, Resources.GetText (Resource.String.Standard_Name), ToastLength.Short); toast.SetGravity(GravityFlags.Center,0,0); toast.Show(); //set language to German and restart activity to see the effect conf.Locale = new Java.Util.Locale("de"); Android.Util.DisplayMetrics dm = this.Resources.DisplayMetrics; this.Resources.UpdateConfiguration (conf, dm); //save language settings ISharedPreferencesEditor editor = sharedPref.Edit(); editor.PutString("Language", "de" ); editor.Commit (); Intent intent = new Intent(this, typeof(Settings)); intent.SetFlags(ActivityFlags.ClearTop); //remove the history StartActivity(intent); }; RadioEnglish.Click += delegate { Toast toast = Toast.MakeText(this, "Wechseln nach Englisch", ToastLength.Short); toast.SetGravity(GravityFlags.Center,0,0); toast.Show(); //set language to German and restart activity to see the effect conf.Locale = new Java.Util.Locale("en"); //conf.SetLocale(Java.Util.Locale.English); //only supported from API 17 on Android.Util.DisplayMetrics dm = this.Resources.DisplayMetrics; this.Resources.UpdateConfiguration (conf, dm); //save language settings ISharedPreferencesEditor editor = sharedPref.Edit(); editor.PutString("Language", "en" ); editor.Commit (); Intent intent = new Intent(this, typeof(Settings)); intent.SetFlags(ActivityFlags.ClearTop); //remove the history StartActivity(intent); }; //start sending reminders after the button was pressed Button ReminderButton = FindViewById<Button> (Resource.Id.button2); ReminderButton.Click += delegate { //Call setAlarm in the Receiver class AlarmReceiverQuestionnaire temp = new AlarmReceiverQuestionnaire(); temp.SetAlarm(this); //Feedback message Toast toast = Toast.MakeText (this, GetString (Resource.String.Done), ToastLength.Short); toast.SetGravity (GravityFlags.Center, 0, 0); toast.Show (); }; //cancel all alarms Button CancelAlarmsButton = FindViewById<Button> (Resource.Id.button3); CancelAlarmsButton.Click += delegate { AlarmManager alarmMgr = (AlarmManager)GetSystemService(Context.AlarmService); //First cancel the Invalid Alarm Intent intentTemp = new Intent(this, typeof(AlarmReceiverInvalid)); PendingIntent alarmIntent = PendingIntent.GetBroadcast(this, 0, intentTemp, 0); alarmMgr.Cancel(alarmIntent); //now the other alarm intentTemp = new Intent(this, typeof(AlarmReceiverQuestionnaire)); alarmIntent = PendingIntent.GetBroadcast(this, 0, intentTemp, 0); alarmMgr.Cancel(alarmIntent); //also reset the state of the questionnaire avaliabilty ISharedPreferencesEditor editor = sharedPref.Edit(); editor.PutBoolean("QuestionnaireActive", false ); editor.Commit (); //Feedback message Toast toast = Toast.MakeText (this, GetString (Resource.String.Done), ToastLength.Short); toast.SetGravity (GravityFlags.Center, 0, 0); toast.Show (); }; //show an example questionnaire to demonstrate the app Button ExampleButton = FindViewById<Button> (Resource.Id.button4); ExampleButton.Click += delegate { Intent intent = new Intent(this, typeof(MoodAssessmentExample)); StartActivity(intent); }; }
//use a scrollview: http://stackoverflow.com/questions/4055537/how-do-you-make-a-linearlayout-scrollable protected override void OnCreate(Bundle bundle) { base.OnCreate (bundle); //CHECK HOW OFTEN WE ALREADY TESTED TODAY dbMood = new MoodDatabase(this); cursor = dbMood.ReadableDatabase.RawQuery("SELECT date, QuestionFlags FROM MoodData WHERE date = '" + DateTime.Now.ToString("dd.MM.yy") + "'", null); // cursor query if (cursor.Count >= 5) { //IF ALREADY 5 OR MORE RETURN TO HOME Toast toast = Toast.MakeText (this, GetString (Resource.String.Already5Times), ToastLength.Long); toast.SetGravity (GravityFlags.Center, 0, 0); toast.Show (); //create an intent to go back to the home screen Intent intent = new Intent (this, typeof(Home)); intent.SetFlags (ActivityFlags.ClearTop); //remove the history and go back to home screen StartActivity (intent); } else { //OTHERWISE LOAD THE SCREEN AND START // Create your application here SetContentView (Resource.Layout.MoodAssessment); //select random background picture //find view with background LinearLayout LayoutMood = FindViewById<LinearLayout>(Resource.Id.LinearLayoutMood); rnd = new Random(); //generator is seeded each time it is initialized switch (rnd.Next (7)) { case 0: LayoutMood.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.clouds_low)); break; case 1: LayoutMood.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.beach_gauss)); break; case 2: LayoutMood.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.dawn_low)); break; case 3: LayoutMood.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.flower_pink_low)); break; case 4: LayoutMood.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.flower_red_low)); break; case 5: LayoutMood.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.forest_low)); break; case 6: LayoutMood.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.sea_low)); break; } //CHOOSE QUESTIONS TO ASK //rnd = new Random(); //generator is seeded each time it is initialized //take latest db entry from today and read out which questions have already been asked. //if there is no enry yet, set already asked to 0 int alreadyAsked = 0; //default value: no questions have been asked yet if (cursor.Count > 0) { //data was already saved today and questions have been asked, so retrieve which ones have been asked cursor.MoveToLast (); //take the last entry of today alreadyAsked = cursor.GetInt(cursor.GetColumnIndex("QuestionFlags")); //retrieve value from last entry in db column QuestionFlags } //This was for testing purposes //int alreadyAsked = 1 + 4 + 8 + 32 + 64 + 128 ; //only 1 and 4 are still valid options for pos and 3 & 4 for neg int posQuestion = ChoosePositiveQuestion (alreadyAsked); int negQuestion = ChooseNegativeQuestion (alreadyAsked); //update alreadyAsked with the newly chosen questions, only write to db after the save button has been pressed alreadyAsked += (int)Math.Pow(2,posQuestion) + (int)Math.Pow(2,negQuestion+5); //Toast.MakeText(this, "New Asked: " + alreadyAsked.ToString(), ToastLength.Short).Show(); //Provide the chosen questions with seekbars and smileys. Make sure to pick good smileys for the negative affect questions //CODE FOR POSITIVE AND NEGATIVE SEEKBAR QUESTIONS TextView posAffectText = FindViewById<TextView>(Resource.Id.textViewPosAffect); TextView negAffectText = FindViewById<TextView>(Resource.Id.textViewNegAffect); switch (posQuestion) { case 0: posAffectText.Text = GetString(Resource.String.PosAffect1); break; case 1: posAffectText.Text = GetString(Resource.String.PosAffect2); break; case 2: posAffectText.Text = GetString(Resource.String.PosAffect3); break; case 3: posAffectText.Text = GetString(Resource.String.PosAffect4); break; case 4: posAffectText.Text = GetString(Resource.String.PosAffect5); break; } _seekBarPosAffect = FindViewById<SeekBar>(Resource.Id.seekBarPosAffect); _LeftPosAffect = FindViewById<ImageView> (Resource.Id.imageViewLeftPosAffect); _RightPosAffect = FindViewById<ImageView> (Resource.Id.imageViewRightPosAffect); nProgressPosAffect = _seekBarPosAffect.Progress; _seekBarPosAffect.SetOnSeekBarChangeListener(this); _seekBarNegAffect = FindViewById<SeekBar>(Resource.Id.seekBarNegAffect); _LeftNegAffect = FindViewById<ImageView> (Resource.Id.imageViewLeftNegAffect); _RightNegAffect = FindViewById<ImageView> (Resource.Id.imageViewRightNegAffect); nProgressNegAffect = _seekBarNegAffect.Progress; _seekBarNegAffect.SetOnSeekBarChangeListener(this); switch (negQuestion) { case 0: negAffectText.Text = GetString(Resource.String.NegAffect1); break; case 1: negAffectText.Text = GetString(Resource.String.NegAffect2); //change sad smiley to afraid _RightNegAffect.SetImageResource(Resource.Drawable.afraid); break; case 2: negAffectText.Text = GetString(Resource.String.NegAffect3); break; case 3: negAffectText.Text = GetString(Resource.String.NegAffect4); //change sad smiley to agitated _RightNegAffect.SetImageResource(Resource.Drawable.Agitated); break; case 4: negAffectText.Text = GetString(Resource.String.NegAffect5); //change sad smiley to angry _RightNegAffect.SetImageResource(Resource.Drawable.angry); break; } //SEEKBAR CODE FOR HOW ARE YOU _seekBarHowAreYou = FindViewById<SeekBar>(Resource.Id.seekBar1); _sadHowAreYou = FindViewById<ImageView> (Resource.Id.imageViewSadHowAreYou); _happyHowAreYou = FindViewById<ImageView> (Resource.Id.imageViewHappyHowAreYou); nProgressHowAreYou = _seekBarHowAreYou.Progress; // Assign this class as a listener for the SeekBar events // In order to be able to do so I have to put the ChangeListener into the definition of the class: extend Activity as well as the Listener // and implement all functions of the Listener, even if they do nothing _seekBarHowAreYou.SetOnSeekBarChangeListener(this); //PEOPLE AROUND CODE nPeopleAround = -1; Button ButtonNone = FindViewById<Button> (Resource.Id.buttonNone); Button ButtonOne = FindViewById<Button> (Resource.Id.buttonOne); Button ButtonMany = FindViewById<Button> (Resource.Id.buttonMany); ButtonNone.Click += delegate { ButtonOne.Background.ClearColorFilter(); ButtonMany.Background.ClearColorFilter(); ButtonNone.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); nPeopleAround = 0; }; ButtonOne.Click += delegate { ButtonNone.Background.ClearColorFilter(); ButtonMany.Background.ClearColorFilter(); ButtonOne.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); nPeopleAround = 1; }; ButtonMany.Click += delegate { ButtonMany.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); ButtonOne.Background.ClearColorFilter(); ButtonNone.Background.ClearColorFilter(); nPeopleAround = 2; }; //WHAT ARE YOU DOING? CODE nWhat = -1; Button ButtonLeisure = FindViewById<Button> (Resource.Id.buttonLeisure); Button ButtonEating = FindViewById<Button> (Resource.Id.buttonEating); Button ButtonWork = FindViewById<Button> (Resource.Id.buttonWorking); ButtonLeisure.Click += delegate { ButtonEating.Background.ClearColorFilter(); ButtonWork.Background.ClearColorFilter(); ButtonLeisure.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); nWhat = 0; }; ButtonEating.Click += delegate { ButtonLeisure.Background.ClearColorFilter(); ButtonWork.Background.ClearColorFilter(); ButtonEating.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); nWhat = 1; }; ButtonWork.Click += delegate { ButtonEating.Background.ClearColorFilter(); ButtonLeisure.Background.ClearColorFilter(); ButtonWork.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); nWhat = 2; }; nWhere = -1; Button ButtonAway = FindViewById<Button> (Resource.Id.buttonAway); Button ButtonHome = FindViewById<Button> (Resource.Id.buttonHome); ButtonAway.Click += delegate { ButtonHome.Background.ClearColorFilter(); ButtonAway.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); nWhere = 0; }; ButtonHome.Click += delegate { ButtonAway.Background.ClearColorFilter(); ButtonHome.Background.SetColorFilter(Android.Graphics.Color.Green, Android.Graphics.PorterDuff.Mode.Darken); nWhere = 1; }; //CONTINUE BUTTON CODE Button ContinueHome = FindViewById<Button> (Resource.Id.buttonContinue); ContinueHome.Click += delegate { //only possible if data has been correctly selected if ( (nPeopleAround == -1) || (nWhere == -1) || (nWhat == -1) ) { Toast toast = Toast.MakeText(this, Resource.String.AnswerQuestions, ToastLength.Short); toast.SetGravity(GravityFlags.Center,0,0); toast.Show(); } else { //update database ContentValues insertValues = new ContentValues(); insertValues.Put("date", DateTime.Now.ToString("dd.MM.yy")); insertValues.Put("time", DateTime.Now.ToString("HH:mm")); insertValues.Put("mood", nProgressHowAreYou); insertValues.Put("people", nPeopleAround); insertValues.Put("what", nWhat); insertValues.Put("location", nWhere); insertValues.Put("QuestionFlags", alreadyAsked); //score affect questions between 1 and 9 instead of 0 and 8, because in the database question that have not been asked are stored as zeros. insertValues.Put("pos" + (posQuestion+1).ToString(), nProgressPosAffect+1); insertValues.Put("neg" + (negQuestion+1).ToString(), nProgressNegAffect+1); dbMood.WritableDatabase.Insert ("MoodData", null, insertValues); //TEST CODE FOR QUERYING THE DB //cursor = dbMood.ReadableDatabase.RawQuery("SELECT date, mood FROM MoodData ORDER BY _id DESC LIMIT 2", null); // cursor query //select only these entries where mood = 4; later change to where date = today //cursor = dbMood.ReadableDatabase.RawQuery("SELECT date, mood FROM MoodData WHERE mood = 0 ORDER BY _id DESC LIMIT 3", null); // cursor query //cursor = dbMood.ReadableDatabase.RawQuery("SELECT date, mood FROM MoodData WHERE date = '24.09.15' ORDER BY _id DESC LIMIT 3", null); // cursor query //Select only entries from today //cursor = dbMood.ReadableDatabase.RawQuery("SELECT date, mood FROM MoodData WHERE date = '" + DateTime.Now.ToString("dd.MM.yy") + "' ORDER BY _id DESC", null); // cursor query //Select only entries for which the question pos2 has been answered // cursor = dbMood.ReadableDatabase.RawQuery("SELECT date, mood FROM MoodData WHERE pos2 IS NOT NULL ORDER BY _id DESC LIMIT 3", null); // cursor query // cursor.MoveToLast(); // Toast.MakeText(this, cursor.Count.ToString(), ToastLength.Short).Show(); //Tutorial on SELECT: http://zetcode.com/db/sqlite/select/ //After the questions have been answered we disable the questionnaire button again and start a new alarm ISharedPreferences sharedPref = GetSharedPreferences("com.FSoft.are_u_ok.PREFERENCES",FileCreationMode.Private); ISharedPreferencesEditor editor = sharedPref.Edit(); editor.PutBoolean("QuestionnaireActive", false ); editor.Commit (); //cancel the invalidation alarm // http://stackoverflow.com/questions/14485368/delete-alarm-from-alarmmanager-using-cancel-android AlarmManager alarmMgr = (AlarmManager)GetSystemService(Context.AlarmService); Intent intentTemp = new Intent(this, typeof(AlarmReceiverInvalid)); PendingIntent alarmIntent = PendingIntent.GetBroadcast(this, 0, intentTemp, 0); alarmMgr.Cancel(alarmIntent); //set the new alarm AlarmReceiverQuestionnaire temp = new AlarmReceiverQuestionnaire(); temp.SetAlarm(this); //create an intent to go to the next screen Intent intent = new Intent(this, typeof(Home)); intent.SetFlags(ActivityFlags.ClearTop); //remove the history and go back to home screen StartActivity(intent); } }; } }