{"action":"created","comment":{"url":"https://api.github.com/repos/OneEducation/xo-robot/pulls/comments/23223687","id":23223687,"diff_hunk":"@@ -0,0 +1,157 @@\n+package org.olpc.xorobothash;\n+\n+import android.content.Context;\n+import android.graphics.Bitmap;\n+import android.graphics.BitmapFactory;\n+import android.net.ConnectivityManager;\n+import android.net.NetworkInfo;\n+import android.os.AsyncTask;\n+import android.support.v7.app.ActionBarActivity;\n+import android.os.Bundle;\n+import android.view.Menu;\n+import android.view.MenuItem;\n+import android.view.View;\n+import android.widget.ImageView;\n+import android.util.Log;\n+import android.widget.TextView;\n+\n+import java.io.BufferedInputStream;\n+import java.io.File;\n+import java.io.FileOutputStream;\n+import java.io.IOException;\n+import java.io.InputStream;\n+import java.io.OutputStream;\n+import java.net.URL;\n+import java.net.HttpURLConnection;\n+\n+public class MainActivity extends ActionBarActivity {\n+\n+    private ImageView robotImageView;\n+\n+    //For debugging\n+    private static final String TAG = \"XORobotHash\";\n+\n+    @Override\n+    protected void onCreate(Bundle savedInstanceState) {\n+        super.onCreate(savedInstanceState);\n+        setContentView(R.layout.activity_main);\n+        robotImageView = (ImageView) findViewById(R.id.robot_hash_image);\n+        getRobot();\n+    }\n+\n+    private void getRobot() {\n+        boolean isConnected = checkNetworkConnectivity();\n+        boolean done = updateView(isConnected);\n+        if (!done) {\n+           if(isConnected) {\n+               // Will call updateView again when done\n+               new DownloadRobotImage().execute(uniqueRoboHashURL(), robotImagePath());\n+           }\n+           // Nothing else to do, updateView already showed the right error message\n+        }\n+    }\n+\n+    private boolean updateView(boolean isConnected) {\n+        Log.i(TAG, \"In updateView()\");\n+        boolean robotVisible = false;\n+        File file = new File(robotImagePath());\n+        // Check if we have ever successfully downloaded the XO's robot image\n+        if (file.exists()) {\n+            Log.i(TAG, \"Robohash image file exists.\");\n+            robotVisible = showRobot(robotImagePath());\n+        } else if (!isConnected) {\n+            Log.i(TAG, \"Robohash image file does not exist, network is not connected.\");\n+            showDisconnected();\n+        }\n+        return robotVisible;\n+    }\n+\n+    private boolean showRobot(String imagePath) {\n+        Log.i(TAG, \"In showRobot()\");\n+        boolean robotDrawn;\n+        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);\n+        robotImageView.setImageBitmap(bitmap);\n+        // Check if the robot image is visible\n+        robotDrawn = robotImageView.getDrawable() == null ? false : true;\n+        return robotDrawn;\n+    }\n+\n+    private void showDisconnected() {\n+        //Hide the robot text\n+        TextView robotText = (TextView) findViewById(R.id.robot_text_message);\n+        robotText.setVisibility(View.GONE);\n+        //Hide the robot image\n+        ImageView robotImage = (ImageView) findViewById(R.id.robot_hash_image);\n+        robotImage.setVisibility(View.GONE);\n+        //Show the disconnected error message\n+        TextView errorMessage = (TextView) findViewById(R.id.error_message);\n+        errorMessage.setText(getString(R.string.no_network_connection));\n+    }\n+\n+    private String robotImagePath() {\n+        return this.getFilesDir() + File.pathSeparator + \"this-xos-robot.png\";\n+    }\n+\n+    private String uniqueRoboHashURL() {\n+        //TODO test on a real android device\n+        String serialNumber = android.os.Build.SERIAL;  // if SERIAL doesn't work, try Settings.Secure.ANDROID_ID; ?\n+        return \"http://robohash.org/\" + serialNumber + \".png?size=500x500\";\n+    }\n+\n+    private boolean checkNetworkConnectivity() {\n+        boolean connected;\n+        ConnectivityManager connMgr = (ConnectivityManager)\n+                getSystemService(Context.CONNECTIVITY_SERVICE);\n+        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();\n+        if (networkInfo != null && networkInfo.isConnected()) {\n+            Log.i(TAG, \"Network is connected.\");\n+            connected = true;\n+        } else {\n+            Log.e(TAG, \"Network is NOT connected.\");\n+            connected = false;\n+        }\n+        return connected;\n+    }\n+\n+    private class DownloadRobotImage extends AsyncTask<String, Void, Boolean> {\n+\n+        @Override\n+        protected void onPreExecute() {\n+            // TODO Display a spinner or loading message\n+        }\n+\n+        @Override\n+        protected Boolean doInBackground(String... params) {\n+            Log.i(TAG, \"In DownloadRobotImage() doInBackground()\");\n+            HttpURLConnection connection = null;\n+            Boolean downloadSuccess = true;\n+            try {\n+                URL url = new URL(params[0]);\n+                connection = (HttpURLConnection) url.openConnection();\n+\n+                InputStream in = new BufferedInputStream(connection.getInputStream());\n+                OutputStream out = new FileOutputStream(params[1]);\n+\n+                int data = in.read();\n+                while (data != -1) {\n+                    out.write(data);\n+                    data = in.read();\n+                }\n+            } catch (IOException e) {\n+                Log.e(TAG + \" TASK\", \"error retrieving robohash.org image\", e);\n+                downloadSuccess = false;\n+            } finally {\n+                if (connection != null) {\n+                    connection.disconnect();\n+                }\n+            }\n+            return downloadSuccess;\n+        }\n+\n+        protected void onPostExecute(Boolean downloadSuccess) {\n+            Log.i(TAG, \"In onPostExecute()\");\n+            updateView(downloadSuccess);","path":"app/src/main/java/org/olpc/xorobothash/MainActivity.java","position":153,"original_position":153,"commit_id":"c22af44cd86219e262b97c34d09c5a9a8968b896","original_commit_id":"c22af44cd86219e262b97c34d09c5a9a8968b896","user":{"login":"andymurph","id":1820919,"avatar_url":"https://avatars.githubusercontent.com/u/1820919?v=3","gravatar_id":"","url":"https://api.github.com/users/andymurph","html_url":"https://github.com/andymurph","followers_url":"https://api.github.com/users/andymurph/followers","following_url":"https://api.github.com/users/andymurph/following{/other_user}","gists_url":"https://api.github.com/users/andymurph/gists{/gist_id}","starred_url":"https://api.github.com/users/andymurph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andymurph/subscriptions","organizations_url":"https://api.github.com/users/andymurph/orgs","repos_url":"https://api.github.com/users/andymurph/repos","events_url":"https://api.github.com/users/andymurph/events{/privacy}","received_events_url":"https://api.github.com/users/andymurph/received_events","type":"User","site_admin":false},"body":"Could it be as simple as\r\n```\r\nif (!downloadSuccess) { \r\n     // TODO Delete file at robotImagePath()\r\n     showDisconnected();    \r\n} else {    \r\n     updateView(downloadSuccess);\r\n}\r\n```\r\n?\r\n\r\n\r\n","created_at":"2015-01-20T14:09:45Z","updated_at":"2015-01-20T14:09:45Z","html_url":"https://github.com/OneEducation/xo-robot/pull/1#discussion_r23223687","pull_request_url":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/OneEducation/xo-robot/pulls/comments/23223687"},"html":{"href":"https://github.com/OneEducation/xo-robot/pull/1#discussion_r23223687"},"pull_request":{"href":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1","id":27588988,"html_url":"https://github.com/OneEducation/xo-robot/pull/1","diff_url":"https://github.com/OneEducation/xo-robot/pull/1.diff","patch_url":"https://github.com/OneEducation/xo-robot/pull/1.patch","issue_url":"https://api.github.com/repos/OneEducation/xo-robot/issues/1","number":1,"state":"open","locked":false,"title":"@anorth Work in progress - application shell and remotely fetching robot image","user":{"login":"andymurph","id":1820919,"avatar_url":"https://avatars.githubusercontent.com/u/1820919?v=3","gravatar_id":"","url":"https://api.github.com/users/andymurph","html_url":"https://github.com/andymurph","followers_url":"https://api.github.com/users/andymurph/followers","following_url":"https://api.github.com/users/andymurph/following{/other_user}","gists_url":"https://api.github.com/users/andymurph/gists{/gist_id}","starred_url":"https://api.github.com/users/andymurph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andymurph/subscriptions","organizations_url":"https://api.github.com/users/andymurph/orgs","repos_url":"https://api.github.com/users/andymurph/repos","events_url":"https://api.github.com/users/andymurph/events{/privacy}","received_events_url":"https://api.github.com/users/andymurph/received_events","type":"User","site_admin":false},"body":"@anorth \r\nLayout is not complete.\r\nRobot image only needs to be remotely fetched once; after that it should store the image internally and not require network access.\r\n","created_at":"2015-01-19T07:28:32Z","updated_at":"2015-01-20T14:09:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"487af208da0ee5437492b9aba70206cdc8705905","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1/commits","review_comments_url":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1/comments","review_comment_url":"https://api.github.com/repos/OneEducation/xo-robot/pulls/comments/{number}","comments_url":"https://api.github.com/repos/OneEducation/xo-robot/issues/1/comments","statuses_url":"https://api.github.com/repos/OneEducation/xo-robot/statuses/c22af44cd86219e262b97c34d09c5a9a8968b896","head":{"label":"OneEducation:layout-and-ux","ref":"layout-and-ux","sha":"c22af44cd86219e262b97c34d09c5a9a8968b896","user":{"login":"OneEducation","id":8618371,"avatar_url":"https://avatars.githubusercontent.com/u/8618371?v=3","gravatar_id":"","url":"https://api.github.com/users/OneEducation","html_url":"https://github.com/OneEducation","followers_url":"https://api.github.com/users/OneEducation/followers","following_url":"https://api.github.com/users/OneEducation/following{/other_user}","gists_url":"https://api.github.com/users/OneEducation/gists{/gist_id}","starred_url":"https://api.github.com/users/OneEducation/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OneEducation/subscriptions","organizations_url":"https://api.github.com/users/OneEducation/orgs","repos_url":"https://api.github.com/users/OneEducation/repos","events_url":"https://api.github.com/users/OneEducation/events{/privacy}","received_events_url":"https://api.github.com/users/OneEducation/received_events","type":"Organization","site_admin":false},"repo":{"id":29116839,"name":"xo-robot","full_name":"OneEducation/xo-robot","owner":{"login":"OneEducation","id":8618371,"avatar_url":"https://avatars.githubusercontent.com/u/8618371?v=3","gravatar_id":"","url":"https://api.github.com/users/OneEducation","html_url":"https://github.com/OneEducation","followers_url":"https://api.github.com/users/OneEducation/followers","following_url":"https://api.github.com/users/OneEducation/following{/other_user}","gists_url":"https://api.github.com/users/OneEducation/gists{/gist_id}","starred_url":"https://api.github.com/users/OneEducation/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OneEducation/subscriptions","organizations_url":"https://api.github.com/users/OneEducation/orgs","repos_url":"https://api.github.com/users/OneEducation/repos","events_url":"https://api.github.com/users/OneEducation/events{/privacy}","received_events_url":"https://api.github.com/users/OneEducation/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/OneEducation/xo-robot","description":"","fork":false,"url":"https://api.github.com/repos/OneEducation/xo-robot","forks_url":"https://api.github.com/repos/OneEducation/xo-robot/forks","keys_url":"https://api.github.com/repos/OneEducation/xo-robot/keys{/key_id}","collaborators_url":"https://api.github.com/repos/OneEducation/xo-robot/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/OneEducation/xo-robot/teams","hooks_url":"https://api.github.com/repos/OneEducation/xo-robot/hooks","issue_events_url":"https://api.github.com/repos/OneEducation/xo-robot/issues/events{/number}","events_url":"https://api.github.com/repos/OneEducation/xo-robot/events","assignees_url":"https://api.github.com/repos/OneEducation/xo-robot/assignees{/user}","branches_url":"https://api.github.com/repos/OneEducation/xo-robot/branches{/branch}","tags_url":"https://api.github.com/repos/OneEducation/xo-robot/tags","blobs_url":"https://api.github.com/repos/OneEducation/xo-robot/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/OneEducation/xo-robot/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/OneEducation/xo-robot/git/refs{/sha}","trees_url":"https://api.github.com/repos/OneEducation/xo-robot/git/trees{/sha}","statuses_url":"https://api.github.com/repos/OneEducation/xo-robot/statuses/{sha}","languages_url":"https://api.github.com/repos/OneEducation/xo-robot/languages","stargazers_url":"https://api.github.com/repos/OneEducation/xo-robot/stargazers","contributors_url":"https://api.github.com/repos/OneEducation/xo-robot/contributors","subscribers_url":"https://api.github.com/repos/OneEducation/xo-robot/subscribers","subscription_url":"https://api.github.com/repos/OneEducation/xo-robot/subscription","commits_url":"https://api.github.com/repos/OneEducation/xo-robot/commits{/sha}","git_commits_url":"https://api.github.com/repos/OneEducation/xo-robot/git/commits{/sha}","comments_url":"https://api.github.com/repos/OneEducation/xo-robot/comments{/number}","issue_comment_url":"https://api.github.com/repos/OneEducation/xo-robot/issues/comments/{number}","contents_url":"https://api.github.com/repos/OneEducation/xo-robot/contents/{+path}","compare_url":"https://api.github.com/repos/OneEducation/xo-robot/compare/{base}...{head}","merges_url":"https://api.github.com/repos/OneEducation/xo-robot/merges","archive_url":"https://api.github.com/repos/OneEducation/xo-robot/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/OneEducation/xo-robot/downloads","issues_url":"https://api.github.com/repos/OneEducation/xo-robot/issues{/number}","pulls_url":"https://api.github.com/repos/OneEducation/xo-robot/pulls{/number}","milestones_url":"https://api.github.com/repos/OneEducation/xo-robot/milestones{/number}","notifications_url":"https://api.github.com/repos/OneEducation/xo-robot/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/OneEducation/xo-robot/labels{/name}","releases_url":"https://api.github.com/repos/OneEducation/xo-robot/releases{/id}","created_at":"2015-01-12T03:25:49Z","updated_at":"2015-01-19T07:19:27Z","pushed_at":"2015-01-20T13:42:42Z","git_url":"git://github.com/OneEducation/xo-robot.git","ssh_url":"git@github.com:OneEducation/xo-robot.git","clone_url":"https://github.com/OneEducation/xo-robot.git","svn_url":"https://github.com/OneEducation/xo-robot","homepage":null,"size":200,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"OneEducation:master","ref":"master","sha":"884e40d76c84971585cf4cc4011a5d19c7df5309","user":{"login":"OneEducation","id":8618371,"avatar_url":"https://avatars.githubusercontent.com/u/8618371?v=3","gravatar_id":"","url":"https://api.github.com/users/OneEducation","html_url":"https://github.com/OneEducation","followers_url":"https://api.github.com/users/OneEducation/followers","following_url":"https://api.github.com/users/OneEducation/following{/other_user}","gists_url":"https://api.github.com/users/OneEducation/gists{/gist_id}","starred_url":"https://api.github.com/users/OneEducation/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OneEducation/subscriptions","organizations_url":"https://api.github.com/users/OneEducation/orgs","repos_url":"https://api.github.com/users/OneEducation/repos","events_url":"https://api.github.com/users/OneEducation/events{/privacy}","received_events_url":"https://api.github.com/users/OneEducation/received_events","type":"Organization","site_admin":false},"repo":{"id":29116839,"name":"xo-robot","full_name":"OneEducation/xo-robot","owner":{"login":"OneEducation","id":8618371,"avatar_url":"https://avatars.githubusercontent.com/u/8618371?v=3","gravatar_id":"","url":"https://api.github.com/users/OneEducation","html_url":"https://github.com/OneEducation","followers_url":"https://api.github.com/users/OneEducation/followers","following_url":"https://api.github.com/users/OneEducation/following{/other_user}","gists_url":"https://api.github.com/users/OneEducation/gists{/gist_id}","starred_url":"https://api.github.com/users/OneEducation/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OneEducation/subscriptions","organizations_url":"https://api.github.com/users/OneEducation/orgs","repos_url":"https://api.github.com/users/OneEducation/repos","events_url":"https://api.github.com/users/OneEducation/events{/privacy}","received_events_url":"https://api.github.com/users/OneEducation/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/OneEducation/xo-robot","description":"","fork":false,"url":"https://api.github.com/repos/OneEducation/xo-robot","forks_url":"https://api.github.com/repos/OneEducation/xo-robot/forks","keys_url":"https://api.github.com/repos/OneEducation/xo-robot/keys{/key_id}","collaborators_url":"https://api.github.com/repos/OneEducation/xo-robot/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/OneEducation/xo-robot/teams","hooks_url":"https://api.github.com/repos/OneEducation/xo-robot/hooks","issue_events_url":"https://api.github.com/repos/OneEducation/xo-robot/issues/events{/number}","events_url":"https://api.github.com/repos/OneEducation/xo-robot/events","assignees_url":"https://api.github.com/repos/OneEducation/xo-robot/assignees{/user}","branches_url":"https://api.github.com/repos/OneEducation/xo-robot/branches{/branch}","tags_url":"https://api.github.com/repos/OneEducation/xo-robot/tags","blobs_url":"https://api.github.com/repos/OneEducation/xo-robot/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/OneEducation/xo-robot/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/OneEducation/xo-robot/git/refs{/sha}","trees_url":"https://api.github.com/repos/OneEducation/xo-robot/git/trees{/sha}","statuses_url":"https://api.github.com/repos/OneEducation/xo-robot/statuses/{sha}","languages_url":"https://api.github.com/repos/OneEducation/xo-robot/languages","stargazers_url":"https://api.github.com/repos/OneEducation/xo-robot/stargazers","contributors_url":"https://api.github.com/repos/OneEducation/xo-robot/contributors","subscribers_url":"https://api.github.com/repos/OneEducation/xo-robot/subscribers","subscription_url":"https://api.github.com/repos/OneEducation/xo-robot/subscription","commits_url":"https://api.github.com/repos/OneEducation/xo-robot/commits{/sha}","git_commits_url":"https://api.github.com/repos/OneEducation/xo-robot/git/commits{/sha}","comments_url":"https://api.github.com/repos/OneEducation/xo-robot/comments{/number}","issue_comment_url":"https://api.github.com/repos/OneEducation/xo-robot/issues/comments/{number}","contents_url":"https://api.github.com/repos/OneEducation/xo-robot/contents/{+path}","compare_url":"https://api.github.com/repos/OneEducation/xo-robot/compare/{base}...{head}","merges_url":"https://api.github.com/repos/OneEducation/xo-robot/merges","archive_url":"https://api.github.com/repos/OneEducation/xo-robot/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/OneEducation/xo-robot/downloads","issues_url":"https://api.github.com/repos/OneEducation/xo-robot/issues{/number}","pulls_url":"https://api.github.com/repos/OneEducation/xo-robot/pulls{/number}","milestones_url":"https://api.github.com/repos/OneEducation/xo-robot/milestones{/number}","notifications_url":"https://api.github.com/repos/OneEducation/xo-robot/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/OneEducation/xo-robot/labels{/name}","releases_url":"https://api.github.com/repos/OneEducation/xo-robot/releases{/id}","created_at":"2015-01-12T03:25:49Z","updated_at":"2015-01-19T07:19:27Z","pushed_at":"2015-01-20T13:42:42Z","git_url":"git://github.com/OneEducation/xo-robot.git","ssh_url":"git@github.com:OneEducation/xo-robot.git","clone_url":"https://github.com/OneEducation/xo-robot.git","svn_url":"https://github.com/OneEducation/xo-robot","homepage":null,"size":200,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1"},"html":{"href":"https://github.com/OneEducation/xo-robot/pull/1"},"issue":{"href":"https://api.github.com/repos/OneEducation/xo-robot/issues/1"},"comments":{"href":"https://api.github.com/repos/OneEducation/xo-robot/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/OneEducation/xo-robot/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/OneEducation/xo-robot/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/OneEducation/xo-robot/statuses/c22af44cd86219e262b97c34d09c5a9a8968b896"}}}}