Android Open Source - datapulse Schedule Alarm Receiver






From Project

Back to project page datapulse.

License

The source code is released under:

GNU General Public License

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

package ar.idack.datapulse.activity;
//from   w  ww.  j a  v a 2  s.  c  o m
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.SystemClock;
import android.util.Log;

public class ScheduleAlarmReceiver extends BroadcastReceiver {

  public static final String WIFI = "ar.idack.datapulse.wifi";
  public static final String DATA = "ar.idack.datapulse.data";
  public static final String DURATION = "ar.idack.datapulse.duration";
  public static final String ENABLE = "ar.idack.datapulse.enable";
  public static final int REQUEST_ON = 0;
  public static final int REQUEST_OFF = 1;

  private static final String TAG = "DataPulse-Scheduler";
  
  @Override
  public void onReceive(Context context, Intent intent) {
    boolean wifi = intent.getBooleanExtra(WIFI, false);
    boolean data = intent.getBooleanExtra(DATA, false);
    boolean enable = intent.getBooleanExtra(ENABLE, true);

    if (data) {
      Log.i(TAG, ((enable) ? "Enabling" : "Disabling") + " mobile data schedule.");
      changeMobileData(context, enable);
    }
    if (wifi) {
      Log.i(TAG, ((enable) ? "Enabling" : "Disabling") + " wifi schedule.");
      changeWifi(context, enable);
    }
    if (enable) {
      long duration = intent.getLongExtra(DURATION, 5);
      
      Intent endIntent = new Intent(context, ScheduleAlarmReceiver.class);
      endIntent.putExtra(ScheduleAlarmReceiver.WIFI, wifi);
      endIntent.putExtra(ScheduleAlarmReceiver.DATA, data);
      endIntent.putExtra(ScheduleAlarmReceiver.ENABLE, false);
      
      PendingIntent endConnectionIntent = PendingIntent.getBroadcast(
          context, ScheduleAlarmReceiver.REQUEST_OFF, 
          endIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      
      AlarmManager alarmManager = 
          (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
          SystemClock.elapsedRealtime() + duration * 60 * 1000, 
          endConnectionIntent);
      Log.i(TAG, "Disabling connection in " + duration + " minutes.");
    }
  }

  private void changeMobileData(Context context, boolean enable) {
    // Data handling
    ConnectivityManager connectivityManager = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo dataInfo = connectivityManager
        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (dataInfo.isConnectedOrConnecting() == enable) {
      Log.i(TAG, "Mobile data was already " + ((enable) ? "enabled." : "disabled."));
    } else {
      // Dirty code!!
      try {
        Method dataMtd = ConnectivityManager.class
            .getDeclaredMethod("setMobileDataEnabled",
                boolean.class);
        dataMtd.setAccessible(true);
        dataMtd.invoke(connectivityManager, enable);
      } catch (NoSuchMethodException e) {
        Log.d(TAG, "Method 'setMobileDataEnabled' not found.");
        Log.e(TAG, "Unable to turn on mobile data");
      } catch (IllegalArgumentException e) {
        Log.d(TAG, "Illegal argument to 'setMobileDataEnabled' .");
        Log.e(TAG, "Unable to turn on mobile data");
      } catch (IllegalAccessException e) {
        Log.d(TAG, "Illegal access to 'setMobileDataEnabled'.");
        Log.e(TAG, "Unable to turn on mobile data");
      } catch (InvocationTargetException e) {
        Log.d(TAG, "Invocation target exception in 'setMobileDataEnabled'.");
        Log.e(TAG, "Unable to turn on mobile data");
      }
      Log.i(TAG, "Mobile data " + ((enable) ? "enabled." : "disabled."));
    }
  }

  private void changeWifi(Context context, boolean enable) {
    // Wifi handling
    WifiManager wifiManager = (WifiManager) context
        .getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null && wifiManager.isWifiEnabled() != enable) {
      wifiManager.setWifiEnabled(enable);
      Log.i(TAG, "Wifi " + ((enable) ? "enabled." : "disabled."));
    } else {
      Log.i(TAG, "Wifi was already " + ((enable) ? "enabled." : "disabled."));
    }
  }

}




Java Source Code List

ar.idack.datapulse.activity.ScheduleAlarmReceiver.java
ar.idack.datapulse.activity.ScheduleList.java
ar.idack.datapulse.activity.Schedule.java
ar.idack.datapulse.model.Pulse.java