Android Open Source - airprobe Facebook Manager






From Project

Back to project page airprobe.

License

The source code is released under:

GNU General Public License

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

/**
 * AirProbe/*  w ww  . j a  v  a2  s  .c  om*/
 * Air quality application for Android, developed as part of 
 * EveryAware project (<http://www.everyaware.eu>).
 *
 * Copyright (C) 2014 CSP Innovazione nelle ICT. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For any inquiry please write to <devel@csp.it>
 * 
 * CONTRIBUTORS
 * 
 * This program was made with the contribution of:
 *   Fabio Saracino <fabio.saracino@csp.it>
 *   Patrick Facco <patrick.facco@csp.it>
 * 
 * 
 * SOURCE CODE
 * 
 *  The source code of this program is available at
 *  <https://github.com/CSPICT/airprobe>
 */

package org.csp.everyaware.internet;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.csp.everyaware.Constants;
import org.csp.everyaware.db.Record;
import org.csp.everyaware.facebooksdk.AsyncFacebookRunner;
import org.csp.everyaware.facebooksdk.AsyncFacebookRunner.RequestListener;
import org.csp.everyaware.facebooksdk.DialogError;
import org.csp.everyaware.facebooksdk.Facebook;
import org.csp.everyaware.facebooksdk.Facebook.DialogListener;
import org.csp.everyaware.facebooksdk.FacebookError;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class FacebookManager 
{
  private static FacebookManager mFbMan;
  private Activity mActivity;
    private Facebook mFacebook;
    private Handler mFacebookHandler; //to send msg to Share activity
    
    //msg posted on facebook wall
    private String mMessage;
    
   // Your Facebook Application ID must be set before running this example
    // See http://www.facebook.com/developers/createapp.php
    public static final String APP_ID = "140238296120716"; //mia signature
    
  public static FacebookManager getInstance(Activity activity, Handler fbHandler)
  {
    if(mFbMan == null)
      mFbMan = new FacebookManager(activity, fbHandler);
    return mFbMan;
  }
  
  private FacebookManager(Activity activity, Handler fbHandler)
  {
    mActivity = activity;
    initFacebook();
    mFacebookHandler = fbHandler;
  }
  
  //call this only if internet connection is available
  private void initFacebook()
  {
    mFacebook = new Facebook(APP_ID);
  }
  
  //send everyaware message on user's facebook wall
  public void postMessageOnWall(Record record)
  {      
      composeMessage(record);
    
    if(mFacebook.isSessionValid())
    {
      Bundle parameters = new Bundle();
      
      String link = "http://www.everyaware.eu/";
      
      parameters.putString(Facebook.TOKEN, mFacebook.getAccessToken());  
      
      parameters.putString("link", link); //link box
      parameters.putString("message", mMessage); //message above link
      parameters.putString("description", mMessage); //message IN link
      
      //works
      //parameters.putString("picture", "http://lib.store.yahoo.net/lib/yhst-17155638221985/img-twitter.gif"); //used for testing
      
      //doesn't work
      //parameters.putString("icon", "http://photos-c.ak.fbcdn.net/photos-ak-snc1/v27562/74/174829003346/app_2_174829003346_2760.gif"); //used for testing

      Log.d("FacebookManager", "postMessageOnWall()--> starting message upload");
      
      try
      {
        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
        mAsyncRunner.request("me/feed", parameters, "POST", new MyRequestListener(), null);        
      }
      catch(Exception e)
      {
        e.printStackTrace();
        Log.d("FacebookManager", "postMessageOnWall()--> Error during upload");
      }
    }
    else
      authorizeFbUser();
  }
  
  //do logout of user from facebook
  public void clearCredentials()
  {
    try
    {
      String res = mFacebook.logout(mActivity);
      
      Log.d("FacebookManager", "result: "+res);
      
          Message msg = mFacebookHandler.obtainMessage(Constants.LOGIN_CLOSED);
          mFacebookHandler.sendMessage(msg);
    }
    catch(MalformedURLException e)
    {
      e.printStackTrace();
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  }  
  
  //compose string message that will be posted on wall
  private void composeMessage(Record record)
  {    
    //add user annotation to msg
    mMessage = "\"" +record.mUserData1+ "\"\n\n";
    
    //average pollution
    mMessage += "Average pollution: " +record.calcAvgPoll()+ "\n";
    
    //add gps coordinates to msg
    if(record.mValues[0] != 0)
      mMessage += "GPS Coordinates: lat " +record.mValues[0]+"?, lon "+record.mValues[1]+"?\n";
        
    //format timestamp into date and add to msg
    String dateString = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(record.mSysTimestamp));  
    mMessage += "Time: " +dateString+ "\n";
    
    mMessage += "\n\n";
    
    mMessage += "Sent with AirProbe for Android - http://www.everyaware.eu/\n";
  }
  
  //user login on facebook
  public void authorizeFbUser()
  {
    if(!mFacebook.isSessionValid())
    {
      Log.d("FacebookManager", "authorizeFbUser()--> session not valid yet");
      mFacebook.authorize(mActivity, new String[] {"email", "read_stream", "publish_stream"}, 
          new MyDialogListener());
    }
    else
    {
      Log.d("FacebookManager", "authorizeFbUser()--> session still valid");
          Message msg = mFacebookHandler.obtainMessage(Constants.LOGIN_COMPLETED);
          mFacebookHandler.sendMessage(msg);
    }
  }
  
  //is user login session valid?
  public boolean isSessionValid()
  {
    if((mFacebook!=null)&&(mFacebook.isSessionValid()))
    {
      return true;
    }
    return false;
  }
  
  /**************************** FACEBOOK LISTENERS **********************************************/
  
  //events from sending message to facebook server
  private class MyRequestListener implements RequestListener
  {
    @Override
    public void onComplete(String response, Object state) 
    {
      Log.d("RequestListener", "onComplete()--> response: " +response);    
    }

    @Override
    public void onIOException(IOException e, Object state) 
    {
      Log.d("RequestListener", "onIOException()");      
    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e, Object state) 
    {
      Log.d("RequestListener", "onFileNotFoundException()");    
    }

    @Override
    public void onMalformedURLException(MalformedURLException e, Object state) 
    {
      Log.d("RequestListener", "onMalformedURLException()");        
    }

    @Override
    public void onFacebookError(FacebookError e, Object state) 
    {
      Log.d("RequestListener", "onFacebookError() " +e.getLocalizedMessage());  
    }    
  }
  
  //events from login phase
  private class MyDialogListener implements DialogListener
  {
    @Override
    public void onComplete(Bundle values)
    {
      Log.d("DialogListener()", "onComplete()");
      
          Message msg = mFacebookHandler.obtainMessage(Constants.LOGIN_COMPLETED);
          mFacebookHandler.sendMessage(msg);
    }

    @Override
    public void onFacebookError(FacebookError e) 
    {
      Log.d("DialogListener()", "onFacebookError() " +e.getLocalizedMessage());    
      
          Message msg = mFacebookHandler.obtainMessage(Constants.LOGIN_FACEBOOK_ERROR);
          mFacebookHandler.sendMessage(msg);
    }

    @Override
    public void onError(DialogError e) 
    {
      Log.d("DialogListener()", "onError()");      
      
          Message msg = mFacebookHandler.obtainMessage(Constants.LOGIN_ERROR);
          mFacebookHandler.sendMessage(msg);
    }

    @Override
    public void onCancel() 
    {
      Log.d("DialogListener()", "onCancel()");  
      
          Message msg = mFacebookHandler.obtainMessage(Constants.LOGIN_CANCEL);
          mFacebookHandler.sendMessage(msg);
    }    
  }
}




Java Source Code List

android.UnusedStub.java
org.csp.everyaware.ColorHelper.java
org.csp.everyaware.Constants.java
org.csp.everyaware.Credits.java
org.csp.everyaware.ExtendedLatLng.java
org.csp.everyaware.Installation.java
org.csp.everyaware.KmlParser.java
org.csp.everyaware.ManageAccount.java
org.csp.everyaware.Options.java
org.csp.everyaware.Start.java
org.csp.everyaware.Utils.java
org.csp.everyaware.bluetooth.BluetoothBroadcastReceiver.java
org.csp.everyaware.bluetooth.BluetoothHistoryManager.java
org.csp.everyaware.bluetooth.BluetoothManager.java
org.csp.everyaware.bluetooth.BluetoothObject.java
org.csp.everyaware.db.AnnotatedRecord.java
org.csp.everyaware.db.DbManager.java
org.csp.everyaware.db.MapCluster.java
org.csp.everyaware.db.MarkerRecord.java
org.csp.everyaware.db.Record.java
org.csp.everyaware.db.SemanticSessionDetails.java
org.csp.everyaware.db.Track.java
org.csp.everyaware.facebooksdk.AsyncFacebookRunner.java
org.csp.everyaware.facebooksdk.DialogError.java
org.csp.everyaware.facebooksdk.FacebookError.java
org.csp.everyaware.facebooksdk.Facebook.java
org.csp.everyaware.facebooksdk.FbDialog.java
org.csp.everyaware.facebooksdk.Util.java
org.csp.everyaware.fragments.FragmentWizardStep0.java
org.csp.everyaware.fragments.FragmentWizardStep1.java
org.csp.everyaware.fragments.FragmentWizardStep2.java
org.csp.everyaware.gps.GpsTrackingService.java
org.csp.everyaware.internet.FacebookManager.java
org.csp.everyaware.internet.StoreAndForwardService.java
org.csp.everyaware.internet.TwitterLogin.java
org.csp.everyaware.internet.TwitterManager.java
org.csp.everyaware.offline.Graph.java
org.csp.everyaware.offline.Map.java
org.csp.everyaware.offline.MyTracks.java
org.csp.everyaware.offline.Tabs.java
org.csp.everyaware.tabactivities.Graph.java
org.csp.everyaware.tabactivities.Map.java
org.csp.everyaware.tabactivities.Monitor.java
org.csp.everyaware.tabactivities.Tabs.java