es.uva.tel.gte.cardiomanager.main.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for es.uva.tel.gte.cardiomanager.main.MainActivity.java

Source

package es.uva.tel.gte.cardiomanager.main;

/**
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed 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
 *
 *     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.
 */

import java.util.Calendar;

import es.uva.tel.gte.cardiomanager.R;
import es.uva.tel.gte.cardiomanager.databasehelper.DBAdapter;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

/**
 * This Activity contains the main page of the application and 
 * another fragment that allows to call the emergency contact.
 * The fragments are accessible by scrolling.
 */
public class MainActivity extends FragmentActivity {
    public final static String EXTRA_MESSAGE = "es.uva.tel.gte.cardiomanager.MESSAGE";
    private static final int NUM_PAGES = 2;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    FragmentManager fManager;

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

        // Comprobamos la alarma que nos indica si se ha cerrado o no la aplicacin
        DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
        dbAdapter.open();

        boolean alarmUp = isMyServiceRunning(AppService.class);

        if (alarmUp) {
            // No se ha cerrado la aplicacin
            Log.println(Log.ASSERT, "Main Activity", "La alarma estaba activa");
        } else {
            Log.println(Log.ASSERT, "Main Activity", "Activamos alarma");
            // Se ha cerrado la aplicacion
            // Volvemos a poner la alarma
            Intent intent = new Intent(getApplicationContext(), CheckAlarmsService.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123456789, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.YEAR, 1); // Alarma a 1 ao, tiempo suficiente 
            Log.println(Log.ASSERT, "Alarm Created", "Alarm Created");
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60,
                    pendingIntent);
            // Comprobamos ahora cuantos medicamentos no se han tomado
            startService(intent);

        }

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);

        fManager = getSupportFragmentManager();

        Cursor cursor = dbAdapter.fetchAllRows(DBAdapter.TABLE_NAME_POLITICS_ACCEPTED);
        if (cursor.moveToFirst()) {
            if (cursor.getInt(1) == 1 && cursor.getInt(2) == 1) {
                // Accepted
            } else {
                // Mostrar dilogo
                PoliticDialog newFragment = new PoliticDialog();
                newFragment.show(fManager, "Tag");
            }
        } else {
            // Mostrar dilogo
            PoliticDialog newFragment = new PoliticDialog();
            newFragment.show(fManager, "Tag");
        }
        dbAdapter.close();

    }

    /**
     * A simple pager adapter that represents 2 {@link ScreenSlidePageFragment} objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            return MainFragment.create(position, getApplicationContext());
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        Intent intent = null;
        switch (item.getItemId()) {
        case R.id.reg:
            intent = new Intent(this, es.uva.tel.gte.cardiomanager.patient.Login.class);
            break;
        case R.id.help:
            intent = new Intent(this, es.uva.tel.gte.cardiomanager.help.HelpActivity.class);
            break;
        case R.id.surveyMenu:
            intent = new Intent(this, es.uva.tel.gte.cardiomanager.survey.Survey.class);
            break;
        }
        startActivity(intent);
        return super.onMenuItemSelected(featureId, item);

    }

    /** Called when the user clicks the New Activity button */

    public void clickNewRegister(View view) {
        Intent intent = new Intent(this, es.uva.tel.gte.cardiomanager.activity.NewActivity.class);
        startActivity(intent);
    }

    /** Called when the user clicks the Activity Record button */
    public void clickActivityRegister(View view) {
        Intent intent = new Intent(this, es.uva.tel.gte.cardiomanager.activityrecord.ActivityRecordMain.class);
        startActivity(intent);
    }

    /** Called when the user clicks the New Medicine button */
    public void regMedicine(View v) {
        Intent intent = new Intent(this, es.uva.tel.gte.cardiomanager.medicinenew.MedicineMain.class);
        startActivity(intent);
    }

    /** Called when the user clicks the Medicine Record button */
    public void showMedicinesInfo(View v) {
        Intent intent = new Intent(this, es.uva.tel.gte.cardiomanager.medicinerecord.MedicineRecordMain.class);
        startActivity(intent);
    }

    /** Called when the user clicks the Info button */
    public void clickInfo(View view) {
        Intent intent = new Intent(this, es.uva.tel.gte.cardiomanager.info.Information.class);
        intent.putExtra(EXTRA_MESSAGE, "info");
        startActivity(intent);
    }

    /** Called when the user clicks the Education button */
    public void clickEdu(View view) {
        Intent intent = new Intent(this, es.uva.tel.gte.cardiomanager.guide.Guidelines.class);
        intent.putExtra(EXTRA_MESSAGE, "education");
        startActivity(intent);
    }

    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        Log.println(Log.ASSERT, "DEBUG", "ConfigChangdeddd");
    }

}