Friday, 21 November 2014

Using Analyze This' Instant-position analysis from your Android Chess App


ANDROID
You can use the Instant-position analysis feature of Analyze This from your own Chess App! All you need to do is invoke Analyze This and pass additional Intent extras.

1. Instant-position analysis for current position only
You can send the FEN string of the current position for Instant-position analysis. Note that only the FEN string is sent and not the whole PGN. So users would need to return back to your App in case they wish to analyze some other position.

public static final String PKG_ANALYZE_THIS_FREE 
= "com.pereira.analysis";
public static final String PKG_ANALYZE_THIS_PAID 
= "com.pereira.analysis.paid";
public static final String ANALYZE_THIS_ACTIVITY
= "com.pereira.analysis.ui.BoardActivity";
public static final String KEY_FEN = "KEY_FEN";

//Since users may have either the Free or the Paid version of Analyze This, you should check using Package Manager



 //default to free version, 
//but check below if paid version is installed
String analyzeThisPackageName = PKG_ANALYZE_THIS_FREE;
if (isInstalled(this, if (isInstalled(this, PKG_ANALYZE_THIS_PAID)) {
        //paid version is installed, so use it!
        analyzeThisPackageName = PKG_ANALYZE_THIS_PAID;
}

 Intent intent = new Intent();
//send the "fen" string from your program
intent.putExtra(KEY_FEN, fen);
Intent intent = new Intent();
try {
    startActivity(intent);
    } catch (ActivityNotFoundException e) {
    //TODO neither Free nor Paid version of Analyze This is installed, 
so show message to the user to install the App from the Play store
}
//Method to check if the given package is installed on the device
public static boolean isInstalled(Context context, String packageName) {
    boolean installed = true;
    try {
        PackageInfo pinfo = 
context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {
        installed = false;
    }
    return installed;
}

2. Instant-position analysis with whole PGN
You can also send the whole PGN game to Analyze This and have it immediately start analyzing the position at a given ply number: (new addition in Analyze This v3.1.4). The added advantage of sending the whole PGN text is that users can also move back and forth through the game without having to return back to your App.


//complete pgn text
intent.putExtra(android.content.Intent.EXTRA_TEXT, pgn); 
ComponentName name = new ComponentName(analyzeThisPackageName, 
ANALYZE_THIS_ACTIVITY);

//plyNum (half-move) at which to start analyzing
intent.putExtra(KEY_PLY_NUM, plyNum);

intent.setComponent(name);


BONUS!
You can even ask Analyze This to flip the board or choose a different board color which suits your application's board color.

 //Integer value, where 0="Aqua", 1="Blue", 2="Brown", 3="Gray", 
4="Green" , 5="Fritz", 6="Sky Blue"
intent.putExtra("KEY_COLOR", 0);
intent.putExtra("KEY_FLIPPED", true);

 //set the component
ComponentName name = new ComponentName(analyzeThisPackageName, 
ANALYZE_THIS_ACTIVITY);
intent.setComponent(name);

 public static final String KEY_PLY_NUM = "KEY_PLY_NUM";

 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/x-chess-pgn");

 try {
    startActivity(intent);
    } catch (ActivityNotFoundException e) {
    //TODO neither Free nor Paid version of Analyze This is installed,
 so show message to the user to install the App from the Play store
}


iOS

You can send the whole PGN game to Analyze This and have it immediately start analyzing the position at a given ply number. The added advantage of sending the whole PGN text is that users can also move back and forth through the game without having to return back to your App. Your iOS App can interact with Analyze This through custom URL schemes and copy PGN data to clipboard. Analyze This then starts analyzing the pgn copied by your App to the clipboard.

Step 1: Sending whole PGN to Analyze This

You can even ask Analyze This to flip the board or choose a different board color which suits your application’s board color.

static NSString *kPasteboardName = @"pgnPasteBoard";

//MUST – The pgn string that should be sent to Analyze This. 
Currently the App only accepts a single game in pgn string format
static NSString *kPgnKey = @"pgn";

//OPTIONAL - the position at given ply which Analyze This will jump to
static NSString *kPlyKey = @"ply";

//OPTIONAL – if Analyze This should immediately start analyzing the position.
 Default -
static NSString *kAutoEngineKey = @"autoengine";

//OPTIONAL - Integer value where 1=”Aqua”, 2=”Blue”, 3=”Brown”, 
4=”Gray”, 5=”Green”, 6=”Fritz” (starts from 1 not 0)
static NSString *kColorIndex = @"colorIndex";

//OPTIONAL - whether Analyze This should flip the board [true|false]
static NSString *kFlipped = @"boardFlip";

//Open Analyze This app and analyze the pgn
- (void)analyzeTapped {
NSInteger colorIndex = 1; // ex. Aqua board
BOOL isFlip = false;
NSNumber *currentPly;
// get pgn string from your app
NSString* pgnStr;

NSString *customURL = [NSString 
stringWithFormat:@"apxchesspgn://?%@=%@",kPgnKey,
 kPasteboardName]; // configure the url
customURL = [customURL stringByAppendingString:
[NSString stringWithFormat:@"&%@=%@",
 kPlyKey, currentPly]]; 
//plyNum (half-move) at which to start analysing
customURL = [customURL stringByAppendingString:
[NSString stringWithFormat:@"&%@=%@",
 kAutoEngineKey, @(true)]]; 
// start engine when Analyze This loads
customURL = [customURL stringByAppendingString:
[NSString stringWithFormat:@"&%@=%ld",
 kColorIndex, (long)colorIndex]]; 
// colour of your choice
customURL = [customURL stringByAppendingString:
[NSString stringWithFormat:@"&%@=%d",
 kFlipped, isFlip]]; 
// flipped bool
// create a UIPasteboard with name “pgnPasteBoard”
UIPasteboard *pasteBoard = 
[UIPasteboard pasteboardWithName:kPasteboardName 
create:true];

// set pin string to paste Board
[pasteBoard setString:pgnStr];

//if the url opens, it means Analyze This is installed else show the 
//user alert to install Analyze This
if ([[UIApplication sharedApplication] 
canOpenURL:[NSURL URLWithString:customURL]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
} else {
UIAlertView *alert = [[UIAlertView alloc] 
initWithTitle:@"Install Analyze This"
message:@"Analyze this game with a powerful Chess engine! 
Its free. 
Would you like to install it now?"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Install", @"Cancel", nil];
[alert show];
}
}


#pragma mark - UIAlertView Delegate
//Handle button clicks
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:
(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
//install
NSLog(@"Install");
[self installAnalyze];
break;
case 1:
//Do nothing // cancel button
NSLog(@"Do nothing");
break;
}
}


//open the Analyze This store listing in iTunes, so that the user can install the App
- (void)installAnalyze{
// Initialize Product View Controller
SKStoreProductViewController *storeProductViewController = 
[[SKStoreProductViewController alloc] init];
// Configure View Controller
[storeProductViewController setDelegate:self];
[storeProductViewController loadProductWithParameters:
@{SKStoreProductParameterITunesItemIdentifier : @1090863537} 
completionBlock:^(BOOL result, NSError *error) {
if (error) {
NSLog(@"Error %@ with User Info %@.", error, [error userInfo]);
} else {
// Present Store Product View Controller
// [self presentViewController:storeProductViewController 
animated:YES completion:nil];
}
}];
[self presentViewController:storeProductViewController 
animated:YES completion:nil];
}


Step 2: Include URL Scheme in info.plist

Be sure to include Analyze This URL scheme in your application's Info.plist under LSApplicationQueriesSchemes key if you want to query presence of Analyze This on user's iPhone using -[UIApplication canOpenURL:].

Open Info.plist. Add new element (+). Set key as LSApplicationQueriesSchemes. Set values as apxchesspgn