Android Open Source - garageoPIner-androidApp Communication Service






From Project

Back to project page garageoPIner-androidApp.

License

The source code is released under:

Apache License

If you think the Android project garageoPIner-androidApp 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 com.wirthual.garageopiner.communication;
/* w w w.j  a  v a2 s.  co m*/
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;

import com.wirthual.garageopiner.R;
import com.wirthual.garageopiner.utils.NotificationUpdateRunnable;


public class CommunicationService extends IntentService {
  Handler mHandler;

  public static final String NOTIFICATION = "com.wirthual.garageopiner.communication";
  public static final String MODE = "toggle";
  public static final String ACTION_TOGGLE = "toggle";
  public static final String ACTION_TIMERSTART = "timerstart";
  public static final String ACTION_TIMERSTOPP = "timerstop";
  protected static Thread notificationUpdateThread = null;

  public String time;
  String timestring;
  int timeint;

  NotificationCompat.Builder mBuilder;
  NotificationManager notificationManager;

  public CommunicationService() {
    super("CommunicationService");
    mHandler = new Handler();
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    // TODO Auto-geneated method stub
    String action = intent.getAction();

    SharedPreferences prefs = PreferenceManager
        .getDefaultSharedPreferences(getApplicationContext());

    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    time = prefs.getString("time", "2 Minuten");
    timestring = GetTimeFromPreferenceEntry(time);
    timeint = Integer.valueOf(timestring);

    String username = prefs.getString("username", "webiopi");
    String password = prefs.getString("password", "raspberry");
    String ipaddress = prefs.getString("ipadress", "192.168.2.107");
    int port = Integer.valueOf(prefs.getString("port", "8000"));
    String gpios = prefs.getString("gpio", "4");
    // ToDO: Send GPIO with request

    try {
      // Connect to Raspberry
      GarageoPInerHTTPClient client = new GarageoPInerHTTPClient(ipaddress, port);
      client.setCredentials(username, password);

      if (CommunicationService.ACTION_TOGGLE.equals(action)) {
        client.sendRequest("GET", "/controlRelay/" + gpios);
        checkForRunningThread();
        notificationManager.cancel(0);
      }
      if (CommunicationService.ACTION_TIMERSTART.equals(action)) {
        String request = "/timeControl?seconds=" + timestring + "&pin=" + gpios;
        client.sendRequest("GET",request );
        checkForRunningThread();
        notificationManager.cancel(0);
        makeNotification();
      }
      if (CommunicationService.ACTION_TIMERSTOPP.equals(action)) {
        client.sendRequest("GET", "/cancelTimer");
        checkForRunningThread();
        notificationManager.cancel(0);
      }

    } catch (Exception e) {
      mHandler.post(new DisplayToast(this, this.getString(R.string.connectionerror)));
    }

  }

  private void makeNotification() {
    Intent i = new Intent(this, CommunicationService.class);
    i.setAction(CommunicationService.ACTION_TIMERSTOPP);
    PendingIntent pIntent = PendingIntent.getService(this, 0, i,
        PendingIntent.FLAG_CANCEL_CURRENT);

    Intent i2 = new Intent(this, CommunicationService.class);
    i2.setAction(CommunicationService.ACTION_TOGGLE);
    PendingIntent pIntent2 = PendingIntent.getService(this, 1, i2,
        PendingIntent.FLAG_CANCEL_CURRENT);

    mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle(this.getText(R.string.notificationtitle))
        .setSmallIcon(R.drawable.ic_garageropener)
        .setAutoCancel(false)
        .addAction(android.R.drawable.ic_menu_close_clear_cancel,
            this.getText(R.string.cancel), pIntent)
        .addAction(android.R.drawable.ic_media_play,
            this.getText(R.string.now), pIntent2);

    
    String txt1 = (String)getText(R.string.notificationsubtitle);
    String txt2 = (String) getText(R.string.seconds);
    
    NotificationUpdateRunnable run = new NotificationUpdateRunnable(mBuilder,notificationManager, timeint,txt1,txt2 );

    notificationUpdateThread = new Thread(run);
    notificationUpdateThread.start();
  }

  private String GetTimeFromPreferenceEntry(String time) {
    char i = time.charAt(0);
    String si = "" + i;
    int seconds = Integer.valueOf(si) * 60;
    return String.valueOf(seconds);
  }

  private void checkForRunningThread() {
    if (notificationUpdateThread != null) {
      notificationUpdateThread.interrupt();
      notificationUpdateThread = null;
    }
  }

}




Java Source Code List

com.wirthual.garageopiner.activities.MainActivity.java
com.wirthual.garageopiner.activities.SettingsActivity.java
com.wirthual.garageopiner.activities.TimeTableActivity.java
com.wirthual.garageopiner.communication.CommunicationService.java
com.wirthual.garageopiner.communication.DisplayToast.java
com.wirthual.garageopiner.communication.GarageoPInerHTTPClient.java
com.wirthual.garageopiner.fragments.MainFragment.java
com.wirthual.garageopiner.fragments.TimeTableFragment.java
com.wirthual.garageopiner.utils.ExpandableListAdapter.java
com.wirthual.garageopiner.utils.MyTimeTriggerWidgetProvider.java
com.wirthual.garageopiner.utils.MyTriggerWidgetProvider.java
com.wirthual.garageopiner.utils.NotificationUpdateRunnable.java
com.wirthual.garageopiner.utils.WifiChangedReceiver.java