Android Open Source - Fall_Prevention_2013 Main Screen






From Project

Back to project page Fall_Prevention_2013.

License

The source code is released under:

Apache License

If you think the Android project Fall_Prevention_2013 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Licensed to UbiCollab.org under one or more contributor
 * license agreements.  See the NOTICE file distributed 
 * with this work for additional information regarding
 * copyright ownership. UbiCollab.org licenses this file
 * to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * //  www.  j  a  v  a  2  s.  co  m
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 ******************************************************************************/
package ntnu.stud.valens.demonstration.activity;

import ntnu.stud.valens.demonstration.R;
import ntnu.stud.valens.demonstration.connectivity.AlarmManagerBroadcastReceiver;
import ntnu.stud.valens.demonstration.connectivity.ContentProviderHelper;
import ntnu.stud.valens.demonstration.connectivity.DatabaseHelper;
import ntnu.stud.valens.demonstration.datastructures.RiskStatus;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * Handles the main screen interactions.
 * 
 * @author Elias
 */
public class MainScreen extends Activity {
  RiskStatus status;
  Thread notificationThread;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainscreen);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
    }

    updateVisible();

    // Change image depending on information from the database
    Drawable drawable;

    status = new ContentProviderHelper(getApplicationContext())
        .getRiskValue();
    if (status == RiskStatus.BAD_JOB) {
      drawable = getResources().getDrawable(R.drawable.bad_job);
    } else if (status == RiskStatus.NOT_SO_OK_JOB) {
      drawable = getResources().getDrawable(R.drawable.not_so_ok_job);
    } else if (status == RiskStatus.OK_JOB) {
      drawable = getResources().getDrawable(R.drawable.ok_job);
    } else if (status == RiskStatus.GOOD_JOB) {
      drawable = getResources().getDrawable(R.drawable.good_job);
    } else if (status == RiskStatus.VERY_GOOD_JOB) {
      drawable = getResources().getDrawable(R.drawable.very_good_job);
    } else {
      // Problem
      drawable = null;
    }
    checkForPush();

    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    Drawable d = new BitmapDrawable(getResources(),
        Bitmap.createScaledBitmap(bitmap, 200, 200, true));
    ImageButton imageButton = (ImageButton) findViewById(R.id.mainScreenSmileyImage);
    imageButton.setBackgroundDrawable(d);
  }

  /**
   * This is just an OnCreate method for displaying the Menu when you choose
   * to activate it
   */
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_mainscreen, menu);
    return true;
  }

  @Override
  protected void onResume() {
    super.onResume();
    checkForPush();
    updateVisible();
  }

  /**
   * Updates the mainscreen when a new notification is available, will also
   * tell when you dont have any new notifications. Gets the predefined name
   * if the user if there were any.
   */
  private void updateVisible() {
    // Find name from shared prefences file
    SharedPreferences sp = PreferenceManager
        .getDefaultSharedPreferences(this);
    String name = sp.getString("name", "");
    String displayString = getString(R.string.greeting) + ", " + name + "!";
    TextView txtGreetingName = (TextView) findViewById(R.id.widget_notification_text);
    txtGreetingName.setText(displayString);

    // Display a message if there are new messages
    TextView txtSubGreeting = (TextView) findViewById(R.id.mainScreenSubText);
    DatabaseHelper dbHelper = new DatabaseHelper(this);
    if (dbHelper.dbHaveEvents()) {
      String message = getString(R.string.main_got_new_events);
      txtSubGreeting.setText(message);
    } else {
      txtSubGreeting.setVisibility(View.GONE);
    }
  }

  /**
   * this is basically a listener for the menu buttons
   */
  public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
    case R.id.menu_statistics:
      intent = new Intent(this, Statistics.class);
      startActivity(intent);
      break;
    case R.id.menu_settings:
      intent = new Intent(this, Settings.class);
      startActivity(intent);
      break;
    // case R.id.menu_related:
    // intent = new Intent(this, Related.class);
    // startActivity(intent);
    // break;
    }
    return false;
  }

  /**
   * Checks if it is time to push the daily notification to the database.
   */
  @SuppressLint("NewApi")
  private void checkForPush() {
    Log.v("Main Screen", "Checking for push time");
    AlarmManagerBroadcastReceiver alarm= new AlarmManagerBroadcastReceiver();
    alarm.SetAlarm(getApplicationContext());
    // Fetch current time and time stored in file
//    Calendar current = Calendar.getInstance();
//    Calendar last = Calendar.getInstance();
//
//    SharedPreferences sp = PreferenceManager
//        .getDefaultSharedPreferences(this);
//    Long lastTimestamp = sp.getLong("lastPushed", 0l);
//    last.setTimeInMillis(lastTimestamp);
//
//    if (DateUtils.DAY_IN_MILLIS < (current.getTimeInMillis() - last
//        .getTimeInMillis())) {
//      // More than a day has passed since the last message, so we'll make
//      // a new one. To do so, we need to know the step count yesterday and
//      // the day before that. So we first need to establish the
//      // time stamps to find step count between..
//      Calendar thisMorningC = Calendar.getInstance();
//      thisMorningC.set(Calendar.HOUR, 5);
//      thisMorningC.set(Calendar.SECOND, 0);
//      thisMorningC.set(Calendar.MINUTE, 0);
//      long thisMorning = thisMorningC.getTimeInMillis();
//      long yesterday = thisMorning - DateUtils.DAY_IN_MILLIS;
//      long dayBeforeYesterday = yesterday - DateUtils.DAY_IN_MILLIS;
//      Timestamp yesterdayTs = new Timestamp(yesterday);
//      Timestamp todayTs = new Timestamp(thisMorning);
//      Timestamp dayBeforeTs = new Timestamp(dayBeforeYesterday);
      
    // Get step counts 
//      ContentProviderHelper cph = new ContentProviderHelper(getApplicationContext());
//      int yesterdaySteps = cph.getStepCount(yesterdayTs, todayTs);
//      int dayBeforeSteps = cph.getStepCount(dayBeforeTs, todayTs);
//      // Finally, generate a message to be stored in the local DB
//      DatabaseHelper dbh = new DatabaseHelper(getApplicationContext());
//      if ((double)yesterdaySteps / (double)dayBeforeSteps < Constants.BAD_STEP_CHANGE) {
//        dbh.dbAddEvent(1, yesterdaySteps, dayBeforeSteps);
//      } else if ((double)yesterdaySteps / (double)dayBeforeSteps > Constants.GOOD_STEP_CHANGE) {
//        dbh.dbAddEvent(0, yesterdaySteps, dayBeforeSteps);
//      } else {
//        dbh.dbAddEvent(2, yesterdaySteps, dayBeforeSteps);
//      }
      // Record that the message that was created corresponds to the last
      // 24 hours before 5 this morning.
//      SharedPreferences.Editor editor = sp.edit();
//      editor.putLong("lastPushed", thisMorning);
//      editor.commit();
//    }
  }
  
  /**
   * A listener for a button that sends you to the Eventlist screen
   * 
   * @param view
   */
  public void fireEvent(View view) {
    Intent intent = new Intent(this, EventList.class);
    startActivity(intent);
  }

}




Java Source Code List

ntnu.stud.valens.contentprovider.CPValensDB.java
ntnu.stud.valens.contentprovider.DBSchema.java
ntnu.stud.valens.contentprovider.Main.java
ntnu.stud.valens.contentprovider.ValensDataProvider.java
ntnu.stud.valens.contentprovider.calculations.AlarmManagerBroadcastReceiver.java
ntnu.stud.valens.contentprovider.calculations.ContentProviderHelper.java
ntnu.stud.valens.contentprovider.calculations.ManipulationStarter.java
ntnu.stud.valens.contentprovider.calculations.ManipulatorHelper.java
ntnu.stud.valens.demonstration.Constants.java
ntnu.stud.valens.demonstration.NotificationBroadcastReciever.java
ntnu.stud.valens.demonstration.activity.ClearHistory.java
ntnu.stud.valens.demonstration.activity.ContactPerson.java
ntnu.stud.valens.demonstration.activity.CreateContact.java
ntnu.stud.valens.demonstration.activity.EventDetail.java
ntnu.stud.valens.demonstration.activity.EventList.java
ntnu.stud.valens.demonstration.activity.LaunchActivity.java
ntnu.stud.valens.demonstration.activity.MainScreen.java
ntnu.stud.valens.demonstration.activity.NewContact.java
ntnu.stud.valens.demonstration.activity.Related.java
ntnu.stud.valens.demonstration.activity.Settings.java
ntnu.stud.valens.demonstration.activity.Statistics.java
ntnu.stud.valens.demonstration.activity.WriteName.java
ntnu.stud.valens.demonstration.connectivity.AlarmManagerBroadcastReceiver.java
ntnu.stud.valens.demonstration.connectivity.ContentProviderHelper.java
ntnu.stud.valens.demonstration.connectivity.DatabaseContract.java
ntnu.stud.valens.demonstration.connectivity.DatabaseHelper.java
ntnu.stud.valens.demonstration.datastructures.Contact.java
ntnu.stud.valens.demonstration.datastructures.Event.java
ntnu.stud.valens.demonstration.datastructures.RiskStatus.java
ntnu.stud.valens.demonstration.listadapters.EditContactAdapter.java
ntnu.stud.valens.demonstration.listadapters.EventListAdapter.java
ntnu.stud.valens.demonstration.widget.WidgetProvider.java
ntnu.stud.valens.stepdetector.DetectStepsThread.java
ntnu.stud.valens.stepdetector.LaunchActivity.java
ntnu.stud.valens.stepdetector.Methods.java
ntnu.stud.valens.stepdetector.StepMainServiceStarter.java
ntnu.stud.valens.stepdetector.StepMainService.java
ntnu.stud.valens.stepdetector.Values.java
ntnu.stud.valens.stepdetector.calibration.CalibrationActivity.java
ntnu.stud.valens.stepdetector.calibration.CalibrationStartTask.java
ntnu.stud.valens.stepdetector.calibration.CalibrationThread.java