Android Open Source - android_monitor My Phone State Listener






From Project

Back to project page android_monitor.

License

The source code is released under:

GNU General Public License

If you think the Android project android_monitor 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) 2014 MUSE team Inria Paris - Rocquencourt
 * /*w  w  w. ja v a 2  s . c  o  m*/
 * This file is part of UCNDataCollector.
 * 
 * UCNDataCollector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * UCNDataCollector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero Public License for more details.
 * 
 * You should have received a copy of the GNU Affero Public License
 * along with UCNDataCollector.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package fr.inria.ucn.listeners;

import org.json.JSONException;
import org.json.JSONObject;

import fr.inria.ucn.Constants;
import fr.inria.ucn.Helpers;
import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

/**
 * Listen for changes in mobile cell and network connectivity.
 * @author apietila
 *
 */
public class MyPhoneStateListener extends PhoneStateListener {

  private Context c = null;
  
  /**
   * 
   * @param c
   */
  public void enable(Context c) {
    TelephonyManager tm = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);  
    if (tm.getPhoneType()!=TelephonyManager.PHONE_TYPE_NONE) {
      tm.listen(this, PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
      this.c = c;
    } // else no telephony
  }
  
  /**
   * 
   * @param c
   */
  public void disable(Context c) {
    TelephonyManager tm = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);  
    if (tm.getPhoneType()!=TelephonyManager.PHONE_TYPE_NONE) {
      tm.listen(this, PhoneStateListener.LISTEN_NONE);    
      this.c = null;
    } // else no telephony
  }

  /**
   * 
   * @return
   */
  public boolean isEnabled() {
    return (this.c!=null);
  }

  /* (non-Javadoc)
   * @see android.telephony.PhoneStateListener#onCellLocationChanged(android.telephony.CellLocation)
   */
  @Override
  public void onCellLocationChanged(CellLocation location) {
    if (location != null) {
      try {
        JSONObject loc = new JSONObject();
        if (location instanceof GsmCellLocation) {
          JSONObject data = new JSONObject();
          data.put("cid", ((GsmCellLocation)location).getCid());
          data.put("lac", ((GsmCellLocation)location).getLac());
          data.put("psc", ((GsmCellLocation)location).getPsc());
          loc.put("gsm", data);
        } else if (location instanceof CdmaCellLocation) {
          JSONObject data = new JSONObject();
          data.put("bs_id", ((CdmaCellLocation)location).getBaseStationId());
          data.put("bs_lat", ((CdmaCellLocation)location).getBaseStationLatitude());
          data.put("bs_lon", ((CdmaCellLocation)location).getBaseStationLongitude());
          data.put("net_id", ((CdmaCellLocation)location).getNetworkId());
          data.put("sys_id", ((CdmaCellLocation)location).getSystemId());
          loc.put("cdma", data);
        }
        Helpers.sendResultObj(this.c,"cell_location",System.currentTimeMillis(),loc);          
      } catch (JSONException jex) {
        Log.w(Constants.LOGTAG, "failed to create json object",jex);
      }
    }
  }

  /* (non-Javadoc)
   * @see android.telephony.PhoneStateListener#onDataConnectionStateChanged(int, int)
   */
  @Override
  public void onDataConnectionStateChanged(int state, int networkType) {
    try {
      JSONObject data = new JSONObject();
      data.put("state", state);
      data.put("network_type", networkType);
      data.put("network_type_str", Helpers.getTelephonyNetworkType(networkType));
      Helpers.sendResultObj(this.c,"data_conn_state",System.currentTimeMillis(),data);
    } catch (JSONException jex) {
      Log.w(Constants.LOGTAG, "failed to create json object",jex);
    }
  }
}




Java Source Code List

fr.inria.ucn.CollectorException.java
fr.inria.ucn.CollectorService.java
fr.inria.ucn.Constants.java
fr.inria.ucn.DataStore.java
fr.inria.ucn.DataUploader.java
fr.inria.ucn.Helpers.java
fr.inria.ucn.OnBootReceiver.java
fr.inria.ucn.Scheduler.java
fr.inria.ucn.collectors.AppDataUsageCollector.java
fr.inria.ucn.collectors.Collector.java
fr.inria.ucn.collectors.DeviceInfoCollector.java
fr.inria.ucn.collectors.LlamaCollector.java
fr.inria.ucn.collectors.NetworkStateCollector.java
fr.inria.ucn.collectors.RunningAppsCollector.java
fr.inria.ucn.collectors.SocketsCollector.java
fr.inria.ucn.collectors.SysStateCollector.java
fr.inria.ucn.listeners.MyPhoneStateListener.java
fr.inria.ucn.listeners.SystemStateListener.java
fr.inria.ucn.ui.SettingsActivity.java
fr.inria.ucn.ui.SettingsFragment.java
fr.inria.ucn.ui.TimePreference.java