Friday 25 October 2013

Facebook App Development on Android

On Facebook
Firstly, we talk about the tasks and tools available on Facebook developers.

Now, login to your FB account, go to "Manage Apps"

Choose "Edit App" or "Create New App".

Here are the basic settings of the App:

Then, choose "Use Graph API Explorer".
Enter the POST action path: "me/victory-test-now:gin"

victory-test-now is the namespace, gin is the Action. You may have to add the Action in the "Open Graph" section beforehand.


The food url is from "Open Graph->Types":

After clicking submit button in the Graph API Explorer, go to the Activity Log of the user. You can see the Action in the Activity Log.

You can add a place field to the POST Action.

The place id can be obtained using the following method.

Click on the profile picture, right click, choose "Copy link address"

For example, the link address is below.
https://www.facebook.com/photo.php?fbid=10150753663333147&set=a.438257763146.238406.145768288146&type=1&source=11

The place id is "145768288146".

The Action can be seen in the Activity Log again.

On Android
Secondly, we highlight how the Android App is able to access the data in Facebook through the Facebook App.

Preparation steps:

  • Download the Facebook SDK for Android
  • Import the SDK into Eclipse
  • Compile and build it

After that, you can start to create an Android App in Eclipse. The good tutorial from FB is listed below:
https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

After you have created the Android App, we go back to the App Dashboard on Facebook Developers, and register the Android package and activity name in the "Native Android App" settings. As an example, the detail info is shown below.

To get the Key Hash, add the code to the OnCreate() method of the MainActivity class.
    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.facebook.samples.hellofacebook", 
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
Publish to Facebook Feed

In the Facebook tutorial below,
https://developers.facebook.com/docs/android/scrumptious/publish-open-graph-story/
it talks about publishing to Open Graph story.

You could modify the code to make it publish to Facebook Feed. Sounds interesting, isn't it? The essential changes are:

1) populate the Bundle with the description, link, name, caption
2) change the post action path to "me/feed"

The obvious changes are not difficult. However, you may have to retrieve the user info from List<GraphUser>.

                        public String announceString;
                        ...
                        private List<GraphUser> selectedUsers;
                        ...
                     for (GraphUser user : selectedUsers) {
                         announceString += user.getName();
                     }

The changes to AsyncTask:

AsyncTask<Void, Void, Response> task =
    new AsyncTask<Void, Void, Response>() {

    @Override
    protected Response doInBackground(Void... voids) {
        // Create an eat action
        EatAction eatAction =
        GraphObject.Factory.create(EatAction.class);
        // Populate the action with the POST parameters:
        // the meal, friends, and place info
        for (BaseListElement element : listElements) {
            element.populateOGAction(eatAction);
        }
        // Set up a request with the active session, set up
        // an HTTP POST to the eat action endpoint
         // Request request = new Request(Session.getActiveSession(),
         //         POST_ACTION_PATH, null, HttpMethod.POST);
        Bundle postParams = new Bundle();
        postParams.putString("name", "Gin food");
        postParams.putString("caption", "Gin food");
        postParams.putString("description", announceString);
        postParams.putString("link", announceUrl);
        Request request = new Request(Session.getActiveSession(),
        "me/feed", postParams, HttpMethod.POST);
       
        // Add the post parameter, the eat action
        request.setGraphObject(eatAction);
        // Execute the request synchronously in the background
        // and return the response.
        return request.executeAndWait();
    }

    @Override
    protected void onPostExecute(Response response) {
        // When the task completes, process
        // the response on the main thread
        onPostActionResponse(response);
     }
};

Tuesday 22 October 2013

Factory Design Pattern

The factory design pattern is a pattern that has some methods that create objects for you. You don't use new method directly to create the objects. 

When you want to change the types of objects created, you just change the factory. All other codes that use the factory change automatically.

For example, in PHP:

<?php

class ClassModelC {
  private $ModelC;  
  public function __construct($c)
  {
    $this->ModelC = $c;
  }
  public function getModel()
  {  return $this->ModelC; }
}
class ClassModelB {
  private $ModelB;
  public function __construct($b)
  {
    $this->ModelB = $b;
  }  
  public function getModel()
  {  return $this->ModelB; }
}

class ClassModelA {
  private $ModelA;
  public function __construct($b, $c)
  {
    $this->ModelA = $b->getModel().' '.$c->getModel();
  }
  public function getModel()
  {
    return $this->ModelA;
  }
}

class Factory{
    public function build($b, $c)
    {
        $classc = $this->buildC($c);
        $classb = $this->buildB($b);
        return $this->buildA($classb, $classc);
    }

    public function buildA($classb, $classc)
    {
        return new ClassModelA($classb, $classc);
    }

    public function buildB($b)
    {
        return new ClassModelB($b);
    }

    public function buildC($c)
    {
        return new ClassModelC($c);
    }
}

$factory = new Factory;
$obj     = $factory->build('Thunderbird','Jaguar');
print_r($obj->getModel());
print("\n");
?>

If in future, you want to change the object type, you can just change the factory method, as below.

class Factory_New extends Factory{
    public function build($b, $c)
    {
        return DatabaseObject($b, $c);
    }
}

Wednesday 9 October 2013

Getting Started with the Amazon Web Services

The Amazon Web Services (AWS) is a set of virtual computing and storage services. It is scalable, flexible, and has low start-up cost.

AWS application can be programmed through PHP, Python, Java, and Ruby, etc. Mobile apps can also access AWS through SDK for Android and iOS.


Sign up for AWS products
A credit card is required in order to sign up for AWS. You can use EntroPay, a virtual credit card provider, as the credit card.

Get the Security Credentials from your AWS account
Create a group and user in AWS IAM. The purpose is to get the access key ID and the secret key.

Using AWS SDK for PHP
  • install PHP
  in Windows, copy the following dll files to Windows/System32 folder
    libeay32.dll
    ssleay32.dll
  and also, modify php.ini, and enable the curl extension.
  • install the SDK for PHP
git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP
cd ./AWSSDKforPHP
  • configure the security credentials
  copy the config-sample.inc.php file in the SDK folder, and rename it to config.inc.php, and change the access key ID and secret key
  • run the sample php file 
  in the samples folder, run
    php cli-s3_get_urls_for_uploads.php

EXAMPLE
one example of access the bucket using non composer way, it is heavily commented.

<?php

//non-composer way, you need to download aws.phar separately
require 'aws.phar';

use Aws\S3\S3Client;

// Instantiate the S3 client with your AWS credentials and desired AWS region, 
// you must create a user in AWS IAM console
$client = S3Client::factory(array(
    'key'    => 'AKIAILBKCPBE6I2GEKOA',
    'secret' => '2Nbn7LJl/sDz40kviPCFBaOHxZItDelQ1pZzhPyq',
));

//The requested bucket name is not available. 
//The bucket namespace is shared by all users of the system. 
//Please select a different name and try again.
//If you see this error, it means the bucket name is already used by other people. 
//Please choose a bucket name which is not common
$bucket = 'my-bupket1'; 
$array_bucket = array('Bucket' => $bucket);

//check for existence of bucket
if (($client->doesBucketExist($bucket, true, $array_bucket)) == false)
{
  $result = $client->createBucket($array_bucket);

  // Wait until the bucket is created
  $client->waitUntil('BucketExists', array('Bucket' => $bucket));
  echo "bucket created";
}
else
{
  echo "bucket exist";
}

// Key is the identifier of the object
// Body is the content of the object
$client->putObject(array(
    'Bucket' => $bucket,
    'Key'    => 'README.md',
    'Body'   => fopen('README.md', 'r')));

$result = $client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => 'README.md'
));

//the Body value can be cast to a string
echo $result['Body'] . "\n";
?>

Using AWS SDK for Android
  • install Eclipse for Android
  This is necessary to build an Android application.
  • install the SDK for Android
  After that, go to download the SDK from http://aws.amazon.com/developers/getting-started/android/
  • import the sample Android project
   From the samples folder, choose S3_Uploader, change the security credentials, and build the project, and run the application.

Wednesday 2 October 2013

Install Custom ROM on Samsung Galaxy Tab 2

The Samsung Galaxy Tab 2 (7 inch) has the model number of P-3100.


1) Rooting the phone
Reboot galaxy tab 2 to run in "Download Mode". On the Windows PC, start Odin-v1.85, wait for Odin to recognise the galaxy tab 2 in id:com panel. Then, choose CF-Auto-Root-espressorf-espressorfxx-gtp3100 file in the PDA files download section. Then click the start button. The message pane will show the progress messages. After the update is finished, the Galaxy Tab 2 will reboot by itself.

2) Get the custom ROM image
Choose and download the custom ROM image to PC, and copy it to the SD card on the phone.

3) Setup recovery image
In your Galaxy Tab, go to Google Play Store,
install ROM Manager from ClockworkMod,
run the ROM Manager App,
and choose the recovery image

4) Boot to recovery
In PC, run "adb reboot recovery"

or on the phone, press volume up and power button

to go to ClockworkMod Recovery

Important:
Before you install the custom ROM,
Full wipe, and manual format /system

5) Perform the custom ROM image flashing
In ClockworkMod Recovery:
wipe data/factory reset
wipe cache partition
in mounts and storage, format /system

After you have done the full wipe, choose "install zip from sdcard",
Select the Slim Bean or any custom ROM image, then click YES.