GitHub PullRequestReviewCommentEvent at 2014-05-20 08:37:52
Repository: crosswalk-android-extensions
Actor: halton
Payload: {"comment":{"url":"https://api.github.com/repos/crosswalk-project/crosswalk-android-extensions/pulls/comments/12832740","id":12832740,"diff_hunk":"@@ -0,0 +1,174 @@\n+// Copyright (c) 2014 Intel Corporation. All rights reserved.\n+// Use of this source code is governed by a BSD-style license that can be\n+// found in the LICENSE file.\n+\n+package org.xwalk.extensions.iap;\n+\n+import android.util.Log;\n+import android.util.Pair;\n+\n+import java.util.ArrayList;\n+import java.util.HashMap;\n+import java.util.List;\n+import java.util.Map;\n+\n+import org.json.JSONArray;\n+import org.json.JSONException;\n+import org.json.JSONObject;\n+\n+import org.xwalk.app.runtime.extension.XWalkExtensionClient;\n+import org.xwalk.app.runtime.extension.XWalkExtensionContextClient;\n+\n+import org.xwalk.extensions.iap.util.IabHelper;\n+import org.xwalk.extensions.iap.util.IabResult;\n+import org.xwalk.extensions.iap.util.Inventory;\n+import org.xwalk.extensions.iap.util.Purchase;\n+import org.xwalk.extensions.iap.util.SkuDetails;\n+\n+public class iap extends XWalkExtensionClient {\n+    final private XWalkExtensionContextClient mExtensionContext;\n+\n+    private static final String TAG = \"IAP\";\n+    private static final String CMD_INIT = \"init\";\n+    private static final String CMD_QUERYPRODUCTS = \"query_products\";\n+\n+    // Request code for the purchase flow\n+    private static final int RC_REQUEST = 10001;\n+\n+    private boolean mInitialized;\n+    private IabHelper mHelper;\n+\n+    // For querying by async methods: cmd => <InstanceID, jsonOutput>\n+    private Map<String, Pair<Integer, JSONObject>> cmdMap =\n+        new HashMap<String, Pair<Integer, JSONObject>>();\n+\n+    public iap(String name, String JsApiContent, XWalkExtensionContextClient context) {\n+        super(name, JsApiContent, context);\n+        mExtensionContext = context;\n+    }\n+\n+    @Override\n+    public void onMessage(int instanceId, String message) {\n+        if (message.isEmpty()) return;\n+        Log.d(TAG, \"Message \" + message + \" received.\");\n+        try {\n+            JSONObject jsonInput = new JSONObject(message);\n+            JSONObject jsonOutput = new JSONObject();\n+            jsonOutput.put(\"_promise_id\", jsonInput.getString(\"_promise_id\"));\n+            String cmd = jsonInput.getString(\"cmd\");\n+            if (cmd.equals(CMD_INIT)) {\n+                init(jsonInput.getString(\"key\"));\n+                Log.d(TAG, \"Init post back: \" + jsonOutput.toString());\n+                postMessage(instanceId, jsonOutput.toString());\n+            } else if (cmd.equals(CMD_QUERYPRODUCTS)) {\n+                cmdMap.put(cmd, new Pair<Integer, JSONObject>(instanceId, jsonOutput));\n+                queryProductDetails(jsonInput.getString(\"ids\"));\n+                return;\n+            } else {\n+                Log.e(TAG, \"Unexpected message received: \" + message);\n+                return;\n+            }\n+        } catch (JSONException e) {\n+            Log.e(TAG, e.toString());\n+        }\n+    }\n+\n+    private void init(String base64EncodedPublicKey) {\n+        if (mInitialized) return;\n+\n+        Log.d(TAG, \"Init starts.\");\n+        mHelper = new IabHelper(mExtensionContext.getActivity(), base64EncodedPublicKey);\n+        mHelper.enableDebugLogging(false);\n+\n+        Log.d(TAG, \"Starting setup.\");\n+        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {\n+            public void onIabSetupFinished(IabResult result) {\n+                Log.d(TAG, \"Setup finished.\");\n+\n+                if (!result.isSuccess()) {\n+                    Log.e(TAG, \"Problem setting up in-app billing: \" + result);\n+                    return;\n+                }\n+\n+                // Have we been disposed of in the meantime? If so, quit.\n+                if (mHelper == null) return;\n+\n+                mInitialized = true;\n+                Log.d(TAG, \"Setup successful.\");\n+            }\n+        });\n+    }\n+\n+    // Callback for when a query for product details is finished\n+    private IabHelper.QueryInventoryFinishedListener mGotDetailsListener =\n+        new IabHelper.QueryInventoryFinishedListener() {\n+            public void onQueryInventoryFinished(IabResult result, Inventory inventory) {\n+                Log.d(TAG, \"Inside mGotDetailsListener\");\n+                if (!ensureResult(result)) return;\n+                Log.d(TAG, \"Query inventory was successful.\");\n+\n+                try {\n+                    List<SkuDetails> skuList = inventory.getAllProducts();\n+                    JSONArray jsonSkuList = new JSONArray();\n+                    for (SkuDetails sku : skuList) {\n+                        Log.d(TAG, \"SKUDetails: Title: \" + sku.getTitle());\n+                        jsonSkuList.put(sku.toJson());\n+                    }\n+                    Pair<Integer, JSONObject> output = cmdMap.get(CMD_QUERYPRODUCTS);\n+                    output.second.put(\"data\", jsonSkuList);\n+                    Log.d(TAG, \"Query post back: \" + output.second.toString());\n+                    postMessage(output.first, output.second.toString());\n+                } catch (JSONException e) {\n+                    Log.e(TAG, e.toString());\n+                }\n+            }\n+        };\n+\n+    // IN: ids - A JSONString format string, e.g. [\"gas\", \"premium\"]\n+    // OUT: result will be posted back in callback method.\n+    private void queryProductDetails(String ids) {\n+        if (!mInitialized){\n+            Log.e(TAG, \"Billing plugin was not initialized\");\n+            return;\n+        }\n+\n+        final List<String> productList = new ArrayList<String>();\n+        try {\n+            final JSONArray jsonIds = new JSONArray(ids);\n+            for (int i=0; i<jsonIds.length(); ++i) {\n+                productList.add(jsonIds.get(i).toString());\n+                Log.d(TAG, \"Added query item: \" + jsonIds.get(i).toString());","path":"IAP/iap.java","position":140,"original_position":140,"commit_id":"6851199632e7863f0488096ab363361294b2aa0d","original_commit_id":"6851199632e7863f0488096ab363361294b2aa0d","user":{"login":"halton","id":433218,"avatar_url":"https://avatars.githubusercontent.com/u/433218?","gravatar_id":"12aa489648dd89d591d6514602d705be","url":"https://api.github.com/users/halton","html_url":"https://github.com/halton","followers_url":"https://api.github.com/users/halton/followers","following_url":"https://api.github.com/users/halton/following{/other_user}","gists_url":"https://api.github.com/users/halton/gists{/gist_id}","starred_url":"https://api.github.com/users/halton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/halton/subscriptions","organizations_url":"https://api.github.com/users/halton/orgs","repos_url":"https://api.github.com/users/halton/repos","events_url":"https://api.github.com/users/halton/events{/privacy}","received_events_url":"https://api.github.com/users/halton/received_events","type":"User","site_admin":false},"body":"ditto.","created_at":"2014-05-20T08:37:52Z","updated_at":"2014-05-20T08:37:52Z","html_url":"https://github.com/crosswalk-project/crosswalk-android-extensions/pull/1#discussion_r12832740","pull_request_url":"https://api.github.com/repos/crosswalk-project/crosswalk-android-extensions/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/crosswalk-project/crosswalk-android-extensions/pulls/comments/12832740"},"html":{"href":"https://github.com/crosswalk-project/crosswalk-android-extensions/pull/1#discussion_r12832740"},"pull_request":{"href":"https://api.github.com/repos/crosswalk-project/crosswalk-android-extensions/pulls/1"}}}}