{"action":"created","comment":{"url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/comments/48399172","id":48399172,"diff_hunk":"@@ -26,32 +26,145 @@\n  */\n package com.lambdacloud.sdk.android;\n \n+import android.content.BroadcastReceiver;\n import android.content.Context;\n+import android.content.Intent;\n+import android.content.IntentFilter;\n import android.content.pm.ApplicationInfo;\n import android.content.pm.PackageInfo;\n import android.graphics.Point;\n+import android.location.Location;\n+import android.location.LocationListener;\n+import android.location.LocationManager;\n import android.net.ConnectivityManager;\n import android.net.NetworkInfo;\n import android.os.Build;\n+import android.os.Bundle;\n+import android.os.CountDownTimer;\n+import android.os.Looper;\n import android.telephony.TelephonyManager;\n import android.view.Display;\n import android.view.WindowManager;\n \n import java.util.List;\n \n-\n public class DeviceInfo {\n     private static Context appContext;\n \n-    public static void init(Context context,String token) {\n+    public static void init(Context context, String token) {\n         appContext = context;\n-        try{\n+        try {\n+            getLocation();\n+            getBatteryPower();\n             String imei = getImei();\n             LogAgent.setToken(token);\n             LogAgent.sendDeviceInfo(imei, null);\n             LogAgent.sendAppList(imei);\n-        }catch (Exception e){\n-            LogUtil.debug(LogSdkConfig.LOG_TAG,\"Failed to send message while starting app.\");\n+        } catch (Exception e) {\n+            LogUtil.debug(LogSdkConfig.LOG_TAG, \"Failed to send message while starting app.\");\n+        }\n+    }\n+\n+    static LocationManager locationManager;\n+    static LocationListener locationListener;\n+    static CountDownTimer cdtForLocation;\n+    static boolean isLocationListener = true;\n+\n+    public static void getLocation() {\n+        try {\n+            //设置倒计时，2min后移除位置监听\n+\n+            cdtForLocation = new CountDownTimer(1000 * 60 * 2, 1000) {\n+                @Override\n+                public void onTick(long l) {\n+                    LogUtil.debug(LogSdkConfig.LOG_TAG, \"count down timer for location:\" + String.valueOf(l));\n+                }\n+\n+                @Override\n+                public void onFinish() {\n+                    LogUtil.debug(LogSdkConfig.LOG_TAG, \"count down finished\");\n+                    if (isLocationListener) {\n+                        locationManager.removeUpdates(locationListener);\n+                    }\n+                }\n+            };\n+            //注册位置监听\n+\n+            locationListener = new LocationListener() {\n+                @Override\n+                public void onLocationChanged(Location location) {\n+                    //当有位置变化时，发送位置日志，并移除监听，停止倒计时\n+                    LogUtil.debug(LogSdkConfig.LOG_TAG, \"latitude:\" + location.getLatitude() + \" longitude:\" + location.getLongitude());\n+                    LogAgent.sendLocationInfo(location);\n+                    if (isLocationListener) {\n+                        locationManager.removeUpdates(locationListener);\n+                        isLocationListener = false;\n+                    }\n+                    cdtForLocation.cancel();\n+                }\n+\n+                @Override\n+                public void onStatusChanged(String s, int i, Bundle bundle) {\n+                }\n+\n+                @Override\n+                public void onProviderEnabled(String s) {\n+                }\n+\n+                @Override\n+                public void onProviderDisabled(String s) {\n+                }\n+            };\n+            locationManager = (LocationManager) appContext.getSystemService(Context.LOCATION_SERVICE);\n+            List<String> providers = locationManager.getProviders(true);\n+            if (providers.contains(LocationManager.GPS_PROVIDER)) {\n+                String locationProvider = LocationManager.GPS_PROVIDER;\n+                try {\n+                    //请求位置更新，启动倒计时\n+                    locationManager.requestLocationUpdates(locationProvider, 60 * 1000, 0, locationListener, Looper.getMainLooper());\n+                    cdtForLocation.start();\n+                } catch (Exception e) {\n+                    LogUtil.debug(LogSdkConfig.LOG_TAG, \"get exception when locationManager requestLocationUpdates, detail is : \" + e.toString());\n+                }\n+            } else {\n+                LogUtil.debug(LogSdkConfig.LOG_TAG, \"GPS is closed\");\n+            }\n+        } catch (Exception e) {\n+            LogUtil.debug(LogSdkConfig.LOG_TAG, \"Failed to get location, detail is:\" + e.toString());\n+        }\n+    }\n+\n+    static BroadcastReceiver broadcastReceiver;\n+    static boolean isBroadcastReceiver = true;\n+\n+    public static void getBatteryPower() {\n+        try {\n+            //当电量变化时获取该电量并发送日志\n+            broadcastReceiver = new BroadcastReceiver() {\n+                @Override\n+                public void onReceive(Context context, Intent intent) {\n+                    if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {","path":"android/logsdk/src/main/java/com/lambdacloud/sdk/android/DeviceInfo.java","position":126,"original_position":126,"commit_id":"c82d2ab409378a5aeec2a700e67c27926e515833","original_commit_id":"c82d2ab409378a5aeec2a700e67c27926e515833","user":{"login":"majun2408","id":15135400,"avatar_url":"https://avatars.githubusercontent.com/u/15135400?v=3","gravatar_id":"","url":"https://api.github.com/users/majun2408","html_url":"https://github.com/majun2408","followers_url":"https://api.github.com/users/majun2408/followers","following_url":"https://api.github.com/users/majun2408/following{/other_user}","gists_url":"https://api.github.com/users/majun2408/gists{/gist_id}","starred_url":"https://api.github.com/users/majun2408/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/majun2408/subscriptions","organizations_url":"https://api.github.com/users/majun2408/orgs","repos_url":"https://api.github.com/users/majun2408/repos","events_url":"https://api.github.com/users/majun2408/events{/privacy}","received_events_url":"https://api.github.com/users/majun2408/received_events","type":"User","site_admin":false},"body":"杨哥这个try catch加在哪？","created_at":"2015-12-24T07:01:49Z","updated_at":"2015-12-24T07:01:49Z","html_url":"https://github.com/lambdacloud/LambdacloudSDK/pull/26#discussion_r48399172","pull_request_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26","_links":{"self":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/comments/48399172"},"html":{"href":"https://github.com/lambdacloud/LambdacloudSDK/pull/26#discussion_r48399172"},"pull_request":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26"}}},"pull_request":{"url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26","id":54230062,"html_url":"https://github.com/lambdacloud/LambdacloudSDK/pull/26","diff_url":"https://github.com/lambdacloud/LambdacloudSDK/pull/26.diff","patch_url":"https://github.com/lambdacloud/LambdacloudSDK/pull/26.patch","issue_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/issues/26","number":26,"state":"open","locked":false,"title":"Android SDK增加获取地理信息以及电量功能","user":{"login":"majun2408","id":15135400,"avatar_url":"https://avatars.githubusercontent.com/u/15135400?v=3","gravatar_id":"","url":"https://api.github.com/users/majun2408","html_url":"https://github.com/majun2408","followers_url":"https://api.github.com/users/majun2408/followers","following_url":"https://api.github.com/users/majun2408/following{/other_user}","gists_url":"https://api.github.com/users/majun2408/gists{/gist_id}","starred_url":"https://api.github.com/users/majun2408/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/majun2408/subscriptions","organizations_url":"https://api.github.com/users/majun2408/orgs","repos_url":"https://api.github.com/users/majun2408/repos","events_url":"https://api.github.com/users/majun2408/events{/privacy}","received_events_url":"https://api.github.com/users/majun2408/received_events","type":"User","site_admin":false},"body":"https://github.com/lambdacloud/lanyun/issues/1285\r\nhttps://github.com/lambdacloud/lanyun/issues/1284\r\nps. LogAgent类中所添加方法不对外提供，仅为内部调用。\r\n\r\n## DeviceInfo:\r\n1. 添加获取地理信息功能（getLocation方法）\r\n  -  在getLocation中添加标志位isLocationListener判断LocationListener是否被移除\r\n2. 获取电量功能（getBatteryPower方法）\r\n  - 在getBatteryPower中添加标志位isBroadcastReceiver判断##BroadcastReceiver是否被移除\r\n  - 在getBatteryPower中修改try {} catch {}为try {} catch {} finally\r\n{}，方便移除BroadcastReceiver\r\n3. 修改代码格式\r\n\r\n## LogAgent:\r\n1. 添加sendLocation方法\r\n  - 参数类型为Location\r\n  - 发送日志格式为”日志类型[ldp_location],时间[ti\r\nmeStamp],用户[userId],来源[客户端],设备平台[ANDROID],ldp_latitude[latitude],ldp_longitude[longitude]”\r\n2. 添加sendBatteryPower方法\r\n  - 参数类型为String\r\n  - 发送日志格式为”日志类型[ldp_battery_powe\r\nr],时间[timeStamp],用户[userId],来源[客户端],设备平台[ANDROID],ldp_battery_power[batteryPower]”\r\n\r\n## DeviceInfoTest:\r\n1. 添加getBatteryPower方法的单元测试\r\n2. 添加getLocation方法的单元测试\r\n\r\n## LogAgentTest:\r\n1. 由于在DeviceInfo.init中添加了sendAppList等方法中含有sendLog因此需要在 LogAgentTest\r\n中添加以下代码来清空队列\r\n\r\n  ```\r\nLogSpout logSpout = LogSpout.getInstance();\r\nlogSpout.queue.clear();\r\n  ```\r\n","created_at":"2015-12-21T08:09:55Z","updated_at":"2015-12-24T07:01:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0c6c73a91865f447cb2a131a30bba1164ef1c54f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26/commits","review_comments_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26/comments","review_comment_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/issues/26/comments","statuses_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/statuses/c82d2ab409378a5aeec2a700e67c27926e515833","head":{"label":"majun2408:master","ref":"master","sha":"c82d2ab409378a5aeec2a700e67c27926e515833","user":{"login":"majun2408","id":15135400,"avatar_url":"https://avatars.githubusercontent.com/u/15135400?v=3","gravatar_id":"","url":"https://api.github.com/users/majun2408","html_url":"https://github.com/majun2408","followers_url":"https://api.github.com/users/majun2408/followers","following_url":"https://api.github.com/users/majun2408/following{/other_user}","gists_url":"https://api.github.com/users/majun2408/gists{/gist_id}","starred_url":"https://api.github.com/users/majun2408/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/majun2408/subscriptions","organizations_url":"https://api.github.com/users/majun2408/orgs","repos_url":"https://api.github.com/users/majun2408/repos","events_url":"https://api.github.com/users/majun2408/events{/privacy}","received_events_url":"https://api.github.com/users/majun2408/received_events","type":"User","site_admin":false},"repo":{"id":44294544,"name":"LambdacloudSDK","full_name":"majun2408/LambdacloudSDK","owner":{"login":"majun2408","id":15135400,"avatar_url":"https://avatars.githubusercontent.com/u/15135400?v=3","gravatar_id":"","url":"https://api.github.com/users/majun2408","html_url":"https://github.com/majun2408","followers_url":"https://api.github.com/users/majun2408/followers","following_url":"https://api.github.com/users/majun2408/following{/other_user}","gists_url":"https://api.github.com/users/majun2408/gists{/gist_id}","starred_url":"https://api.github.com/users/majun2408/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/majun2408/subscriptions","organizations_url":"https://api.github.com/users/majun2408/orgs","repos_url":"https://api.github.com/users/majun2408/repos","events_url":"https://api.github.com/users/majun2408/events{/privacy}","received_events_url":"https://api.github.com/users/majun2408/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/majun2408/LambdacloudSDK","description":"SDK to record client logs","fork":true,"url":"https://api.github.com/repos/majun2408/LambdacloudSDK","forks_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/forks","keys_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/keys{/key_id}","collaborators_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/teams","hooks_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/hooks","issue_events_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/issues/events{/number}","events_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/events","assignees_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/assignees{/user}","branches_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/branches{/branch}","tags_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/tags","blobs_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/git/refs{/sha}","trees_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/git/trees{/sha}","statuses_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/statuses/{sha}","languages_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/languages","stargazers_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/stargazers","contributors_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/contributors","subscribers_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/subscribers","subscription_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/subscription","commits_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/commits{/sha}","git_commits_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/git/commits{/sha}","comments_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/comments{/number}","issue_comment_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/issues/comments{/number}","contents_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/contents/{+path}","compare_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/compare/{base}...{head}","merges_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/merges","archive_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/downloads","issues_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/issues{/number}","pulls_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/pulls{/number}","milestones_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/milestones{/number}","notifications_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/labels{/name}","releases_url":"https://api.github.com/repos/majun2408/LambdacloudSDK/releases{/id}","created_at":"2015-10-15T04:52:38Z","updated_at":"2015-10-15T04:52:39Z","pushed_at":"2015-12-24T06:52:26Z","git_url":"git://github.com/majun2408/LambdacloudSDK.git","ssh_url":"git@github.com:majun2408/LambdacloudSDK.git","clone_url":"https://github.com/majun2408/LambdacloudSDK.git","svn_url":"https://github.com/majun2408/LambdacloudSDK","homepage":null,"size":232,"stargazers_count":0,"watchers_count":0,"language":"Objective-C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"lambdacloud:master","ref":"master","sha":"4110adab88d38bfd3c85a1ef7d4f119bcdba5cc6","user":{"login":"lambdacloud","id":7385683,"avatar_url":"https://avatars.githubusercontent.com/u/7385683?v=3","gravatar_id":"","url":"https://api.github.com/users/lambdacloud","html_url":"https://github.com/lambdacloud","followers_url":"https://api.github.com/users/lambdacloud/followers","following_url":"https://api.github.com/users/lambdacloud/following{/other_user}","gists_url":"https://api.github.com/users/lambdacloud/gists{/gist_id}","starred_url":"https://api.github.com/users/lambdacloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lambdacloud/subscriptions","organizations_url":"https://api.github.com/users/lambdacloud/orgs","repos_url":"https://api.github.com/users/lambdacloud/repos","events_url":"https://api.github.com/users/lambdacloud/events{/privacy}","received_events_url":"https://api.github.com/users/lambdacloud/received_events","type":"Organization","site_admin":false},"repo":{"id":30289128,"name":"LambdacloudSDK","full_name":"lambdacloud/LambdacloudSDK","owner":{"login":"lambdacloud","id":7385683,"avatar_url":"https://avatars.githubusercontent.com/u/7385683?v=3","gravatar_id":"","url":"https://api.github.com/users/lambdacloud","html_url":"https://github.com/lambdacloud","followers_url":"https://api.github.com/users/lambdacloud/followers","following_url":"https://api.github.com/users/lambdacloud/following{/other_user}","gists_url":"https://api.github.com/users/lambdacloud/gists{/gist_id}","starred_url":"https://api.github.com/users/lambdacloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lambdacloud/subscriptions","organizations_url":"https://api.github.com/users/lambdacloud/orgs","repos_url":"https://api.github.com/users/lambdacloud/repos","events_url":"https://api.github.com/users/lambdacloud/events{/privacy}","received_events_url":"https://api.github.com/users/lambdacloud/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lambdacloud/LambdacloudSDK","description":"SDK to record client logs","fork":false,"url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK","forks_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/forks","keys_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/teams","hooks_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/hooks","issue_events_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/issues/events{/number}","events_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/events","assignees_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/assignees{/user}","branches_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/branches{/branch}","tags_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/tags","blobs_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/git/refs{/sha}","trees_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/statuses/{sha}","languages_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/languages","stargazers_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/stargazers","contributors_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/contributors","subscribers_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/subscribers","subscription_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/subscription","commits_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/commits{/sha}","git_commits_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/git/commits{/sha}","comments_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/comments{/number}","issue_comment_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/issues/comments{/number}","contents_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/contents/{+path}","compare_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/merges","archive_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/downloads","issues_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/issues{/number}","pulls_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls{/number}","milestones_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/milestones{/number}","notifications_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/labels{/name}","releases_url":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/releases{/id}","created_at":"2015-02-04T08:32:14Z","updated_at":"2015-10-09T10:44:54Z","pushed_at":"2015-12-24T06:52:27Z","git_url":"git://github.com/lambdacloud/LambdacloudSDK.git","ssh_url":"git@github.com:lambdacloud/LambdacloudSDK.git","clone_url":"https://github.com/lambdacloud/LambdacloudSDK.git","svn_url":"https://github.com/lambdacloud/LambdacloudSDK","homepage":null,"size":231,"stargazers_count":1,"watchers_count":1,"language":"Objective-C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":5,"forks":2,"open_issues":5,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26"},"html":{"href":"https://github.com/lambdacloud/LambdacloudSDK/pull/26"},"issue":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/issues/26"},"comments":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/issues/26/comments"},"review_comments":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26/comments"},"review_comment":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/pulls/26/commits"},"statuses":{"href":"https://api.github.com/repos/lambdacloud/LambdacloudSDK/statuses/c82d2ab409378a5aeec2a700e67c27926e515833"}}}}