Android Open Source - AndroidLibraryProject Context Aware Function






From Project

Back to project page AndroidLibraryProject.

License

The source code is released under:

Apache License

If you think the Android project AndroidLibraryProject 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

/*
 * Copyright (c) 2013 by CDAC Chennai /*  w w  w  .  j av a2s. co m*/
 *
 * 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.
 *
 * @File        ContextAwareFunction
 * @Created:    25.11.2013
 * @author:     Prasenjit
 * Last Change: 26.11.2013 by Prasenjit
 */

package com.contextawareframework.contextawarefunctions;

import com.contextawareframework.globalvariable.CAFConfig;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * This class is the controller part. User can register the sensor listener 
 * and can store data in the database by using this class
 */

public class ContextAwareFunction {

  /* To enable / disable Log messages. */
  private static boolean enableDebugging = CAFConfig.isEnableDebugging(); 

  Context localContext;
  private static String TAG = "ContextAwareFuntion";

  private AudioManager mAudioManager ;  

  long eventtime;

  public ContextAwareFunction(Context contextFromMainApp)
  {
    localContext = contextFromMainApp;
  }

  /**
   * Description : Method to increase the volume by getting AudioManager.STREAM_MUSIC
   * service of Android 
   */
  public void volumeIncrease()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"VolumeIncrease Method");
    }  
    try
    {
      mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
      if(mAudioManager!=null)
      {
        int maxVal = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVal = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(curVal <= maxVal)
          curVal = curVal + 1;
        if(curVal == maxVal )
        {
          Toast.makeText(localContext, "Max volume Reached", Toast.LENGTH_SHORT).show();
        }
        // This won't get true at all...
        if(curVal > maxVal)
        {
          curVal = maxVal;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, curVal, 0);
      }

    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Description : To decrease the volume by getting AudioManager.STREAM_MUSIC sevice
   * of Android. 
   */
  public void volumeDecrease()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"VolumeDecrease Method");
    }  
    try{
      mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
      //int maxVal = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      if(mAudioManager!=null)
      {
        int curVal = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(curVal > 0 )
          curVal = curVal - 1;
        if( curVal <= 0 )
        {
          curVal = 0;
          Toast.makeText(localContext,"Min volume Reached", Toast.LENGTH_SHORT).show();
        }

        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, curVal, 0);
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  // Function to play / pause / nextSong / prevSong of a music player
  /**
   * Description : Method to play Song. It uses ACION_MEDIA_BUTTON intent.
   */
  public final void playSong()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"playSong Method");
    }  
    try
    {
      mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
      eventtime = SystemClock.uptimeMillis();

      //mAudioManager.requestAudioFocus(l, streamType, ); here 
      if(mAudioManager!=null)
      {
        if(!mAudioManager.isMusicActive())
        {
          Log.d("PlaySong11","Check");
          Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
          KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MUSIC, 0);
          upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
          localContext.sendOrderedBroadcast(upIntent, null); // Check here if correct
          Log.d("PlaySong22","Check");
        }
        if(mAudioManager.isMusicActive()) //Check Here
        {
          Log.d("PlaySong1","Check");
          Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
          KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
          upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
          localContext.sendOrderedBroadcast(upIntent, null); // Check here if correct
          Log.d("PlaySong2","Check");
        }
      }
      else
      {
        Log.d(TAG,"audioManager is null");
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  /**
   * Description : Method to pause the song. It uses ACTION_MEDIA_BUTTON intent.
   */
  public final void pauseSong()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"pauseSong Method");
    }  
    try
    {
      mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
      eventtime = SystemClock.uptimeMillis();
      if(mAudioManager!=null)
      {
        if(mAudioManager.isMusicActive())
        {
          Log.d("PauseSong","Check1");
          Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
          KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
          downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
          localContext.sendOrderedBroadcast(downIntent, null);
          Log.d("PauseSong","Check2");
        }
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Description : Play the next Song. It uses ACTION_MEDIA_BUTTON intent.
   */
  public final void playNextSong()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"playNextSong Method");
    }  
    try
    {
      if(mAudioManager!=null)
      {
        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
        eventtime = SystemClock.uptimeMillis();
        if(mAudioManager.isMusicActive())
        {
          Intent downIntentnext = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
          KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN,   KeyEvent.KEYCODE_MEDIA_NEXT, 0);
          downIntentnext.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
          localContext.sendOrderedBroadcast(downIntentnext, null);
        }
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Description : Method to play previous song. It uses ACTION_MEDIA_BUTTON intent.
   */
  public final void playPrevSong()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"playPrevSong Method");
    }  
    try
    {
      if(mAudioManager!=null)
      {
        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);
        eventtime = SystemClock.uptimeMillis();
        if(mAudioManager.isMusicActive())
        {
          Intent downIntentprev = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
          KeyEvent downEventprev = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
          downIntentprev.putExtra(Intent.EXTRA_KEY_EVENT, downEventprev);
          localContext.sendOrderedBroadcast(downIntentprev, null);
        }
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Description : Another way to play a song
   */
  public void play1()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"play1 Method");
    }  
    try
    {
      if(mAudioManager!=null)
      {
        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);    
        if (mAudioManager.isMusicActive()) {
          Intent mediaIntent = new Intent("com.android.music.musicservicecommand");
          mediaIntent.putExtra("command", "play");
          localContext.sendBroadcast(mediaIntent);
        }
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  /**
   * Description : Another way to pause the song
   */
  public void pause1()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"pause1 Method");
    }  
    try
    {
      if(mAudioManager!=null)
      {
        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);    
        if (mAudioManager.isMusicActive()) {
          Intent mediaIntent = new Intent("com.android.music.musicservicecommand");
          mediaIntent.putExtra("command", "pause");
          localContext.sendBroadcast(mediaIntent);
        }
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  /**
   * Description : Another way to play next song
   */
  public void next1()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"next1 Method");
    }  
    try
    {
      if(mAudioManager!=null)
      {
        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);    
        if (mAudioManager.isMusicActive()) {
          Intent mediaIntent = new Intent("com.android.music.musicservicecommand");
          mediaIntent.putExtra("command", "next");
          localContext.sendBroadcast(mediaIntent);
        }
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  /**
   * Description : Another way to play previous song
   */
  public void previous1()
  {
    if(CAFConfig.isEnableDebugging())
    {
      Log.d(TAG,"previous1 Method");
    }  
    try
    {
      if(mAudioManager!=null)
      {
        mAudioManager = (AudioManager)localContext.getSystemService(Context.AUDIO_SERVICE);    
        if (mAudioManager.isMusicActive()) {
          Intent mediaIntent = new Intent("com.android.music.musicservicecommand");
          mediaIntent.putExtra("command", "previous");
          localContext.sendBroadcast(mediaIntent);
        }
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  /**
   * Detect Accelerometer Motion, shake in Left for X axis
   */
  public boolean shakeRight(float xAxis)
  {  //boolean status = false;
    if(xAxis < -12)
      return true;
    else
      return false; 
  }
  /**
   * Detect Accelerometer Motion, shake in Right for X axis
   */
  public boolean shakeLeft(float xAxis)
  {
    if(xAxis > 12)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion, detects shake in up for Z axis
   */
  public boolean shakeUp(float zAxis)
  {
    if(zAxis < -15)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion, detects shake in down for Z axis
   */
  public boolean shakeDown(float zAxis)
  {
    if(zAxis > 15)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion, detects shake in front for Y axis
   */
  public boolean shakeFront(float yAxis)
  {
    if(yAxis < -12)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion,detects shake back for Y axis
   */
  public boolean shakeBack(float yAxis)
  {
    if(yAxis > 12)
      return true;
    else
      return false;
  }

  /**
   * Method to give more flexibility to Developers
   */

  /**
   * Detect Accelerometer Motion, shake in Left for X axis
   */
  public boolean shakeRight(float xAxis,int range)
  {  //boolean status = false;
    if(xAxis < -range)
      return true;
    else
      return false; 
  }
  /**
   * Detect Accelerometer Motion, shake in Right for X axis
   */
  public boolean shakeLeft(float xAxis,int range)
  {
    if(xAxis > range)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion, detects shake in up for Z axis
   */
  public boolean shakeUp(float zAxis,int range)
  {
    if(zAxis < -range)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion, detects shake in down for Z axis
   */
  public boolean shakeDown(float zAxis,int range)
  {
    if(zAxis > range)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion, detects shake in front for Y axis
   */
  public boolean shakeFront(float yAxis,int range)
  {
    if(yAxis < -range)
      return true;
    else
      return false;
  }
  /**
   * Detect Accelerometer Motion,detects shake back for Y axis
   */
  public boolean shakeBack(float yAxis,int range)
  {
    if(yAxis > range)
      return true;
    else
      return false;
  }

  /**
   * @return the enableDebugging
   */
  public static final boolean isEnableDebugging() {
    return enableDebugging;
  }

  /**
   * @param enableDebugging the enableDebugging to set
   */
  public static final void setEnableDebugging(boolean enableDebugging) {
    ContextAwareFunction.enableDebugging = enableDebugging;
  }
}

//End of File





Java Source Code List

.BatteryDbHelper.java
.Bluetooth.java
.Camera.java
.Gravity.java
.Humidity.java
.Microphone.java
.NFC.java
.Orientation.java
.Pressure.java
.SDCard.java
.Telephony.java
.Temparature.java
.USB.java
.Wifi.java
com.contextawareframework.backgroundservices.AccelerometerDataListener.java
com.contextawareframework.backgroundservices.BatteryDataService.java
com.contextawareframework.backgroundservices.CAFService.java
com.contextawareframework.backgroundservices.GPSTracker.java
com.contextawareframework.backgroundservices.GyroscopeDataListener.java
com.contextawareframework.backgroundservices.LightDataListener.java
com.contextawareframework.backgroundservices.LocationDataListener.java
com.contextawareframework.backgroundservices.MagnetometerDataListener.java
com.contextawareframework.backgroundservices.ProximityDataListener.java
com.contextawareframework.backgroundservices.SoundRecorder.java
com.contextawareframework.backgroundservices.Text2Speech.java
com.contextawareframework.contextawarefunctions.ContextAwareFunction.java
com.contextawareframework.controller.BatteryController.java
com.contextawareframework.controller.SensorController.java
com.contextawareframework.dataanalysis.AccelerometerDataAnalysis.java
com.contextawareframework.dataanalysis.ApplicationDataAnalysis.java
com.contextawareframework.dataanalysis.LocationDataAnalysis.java
com.contextawareframework.dbmanager.AccelerometerDbHelper.java
com.contextawareframework.dbmanager.ContextAwareSQLiteHelper.java
com.contextawareframework.dbmanager.GyroscopeDbHelper.java
com.contextawareframework.dbmanager.LightDbHelper.java
com.contextawareframework.dbmanager.LocationDbHelper.java
com.contextawareframework.dbmanager.MagnetometerDbHelper.java
com.contextawareframework.dbmanager.ProximityDbHelper.java
com.contextawareframework.dbmanager.UserInfoDbHelper.java
com.contextawareframework.exceptions.AccelerometerSensorException.java
com.contextawareframework.exceptions.BatteryException.java
com.contextawareframework.exceptions.CAFException.java
com.contextawareframework.exceptions.GyrometerSensorException.java
com.contextawareframework.exceptions.LightSensorException.java
com.contextawareframework.exceptions.LocationServiceException.java
com.contextawareframework.exceptions.MagnetometerSensorException.java
com.contextawareframework.exceptions.ProximitySensorException.java
com.contextawareframework.exceptions.SDCardException.java
com.contextawareframework.exceptions.SQLiteQueryException.java
com.contextawareframework.exceptions.SensorException.java
com.contextawareframework.globalvariable.CAFConfig.java
com.contextawareframework.notificationservice.IssueNotification.java
com.contextawareframework.os.Battery.java
com.contextawareframework.os.CAFPowerManager.java
com.contextawareframework.os.UserInfo.java
com.contextawareframework.probe.Probe.java
com.contextawareframework.probe.WfiProbe.java
com.contextawareframework.querymodule.BaseQueryClass.java
com.contextawareframework.querymodule.DeveloperInfo.java
com.contextawareframework.querymodule.UserIdentification.java
com.contextawareframework.security.FileEncryptor.java
com.contextawareframework.security.JavaDESEncryption.java
com.contextawareframework.sensorlistener.BatteryListener.java
com.contextawareframework.sensorlistener.SensorListener.java
com.contextawareframework.sensors.environmentsensors.Light.java
com.contextawareframework.sensors.motionsensors.Accelerometer.java
com.contextawareframework.sensors.motionsensors.Gyrometer.java
com.contextawareframework.sensors.positionsensors.LocationPojo.java
com.contextawareframework.sensors.positionsensors.Magnetometer.java
com.contextawareframework.sensors.positionsensors.Proximity.java
com.contextawareframework.uploadmanager.BackupDb.java
com.contextawareframework.uploadmanager.UploadScheduler.java
com.contextawareframework.uploadmanager.UploadToServer.java
com.contextawareframework.utility.CsvFileWriter.java
com.example.frameworktestcase.FrameworkFunctionalTestActivity.java
com.example.frameworktestcase.MainActivity.java
com.example.frameworktestcase.SecondActivity.java
com.example.frameworktestcase.Singleton.java