{"id":1844,"date":"2023-03-29T21:56:14","date_gmt":"2023-03-29T13:56:14","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1844"},"modified":"2023-04-22T09:11:23","modified_gmt":"2023-04-22T01:11:23","slug":"restrictions-of-android-o-wifi-on-application-front-end-and-back-end-scanning","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/29\/restrictions-of-android-o-wifi-on-application-front-end-and-back-end-scanning\/","title":{"rendered":"Android O WiFi\u5bf9\u5e94\u7528\u524d\u540e\u53f0\u626b\u63cf\u7684\u9650\u5236"},"content":{"rendered":"<h2>\u6d41\u7a0b\u68b3\u7406<\/h2>\n<ul>\n<li><code>ScanRequestProxy<\/code><\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">    \/\/ Check and throttle scan request unless,\n    \/\/ a) App has either NETWORK_SETTINGS or NETWORK_SETUP_WIZARD permission.\n    \/\/ b) Throttling has been disabled by user.\n    if (!fromSettingsOrSetupWizard &amp;&amp; mThrottleEnabledSettingObserver.isEnabled()\n        &amp;&amp; shouldScanRequestBeThrottledForApp(callingUid, packageName)) {\n        Log.i(TAG, &quot;Scan request from &quot; + packageName + &quot; throttled&quot;);\n        sendScanResultFailureBroadcastToPackage(packageName);\n        return false;\n    }\n}\n\n\/**\n * Checks if the scan request from the app (specified by callingUid &amp; packageName) needs\n * to be throttled.\n *\/\nprivate boolean shouldScanRequestBeThrottledForApp(int callingUid, String packageName) {\n    boolean isThrottled;\n    if (isRequestFromBackground(callingUid, packageName)) {\n        isThrottled = shouldScanRequestBeThrottledForBackgroundApp();\n        if (isThrottled) {\n            if (mVerboseLoggingEnabled) {\n                Log.v(TAG, &quot;Background scan app request [&quot; + callingUid + &quot;, &quot;\n                        + packageName + &quot;]&quot;);\n            }\n            mWifiMetrics.incrementExternalBackgroundAppOneshotScanRequestsThrottledCount();\n        }\n    } else {\n        isThrottled = shouldScanRequestBeThrottledForForegroundApp(callingUid, packageName);\n        if (isThrottled) {\n            if (mVerboseLoggingEnabled) {\n                Log.v(TAG, &quot;Foreground scan app request [&quot; + callingUid + &quot;, &quot;\n                        + packageName + &quot;]&quot;);\n            }\n            mWifiMetrics.incrementExternalForegroundAppOneshotScanRequestsThrottledCount();\n        }\n    }\n    mWifiMetrics.incrementExternalAppOneshotScanRequestsCount();\n    return isThrottled;\n}<\/code><\/pre>\n<h3>\u540e\u53f0\u626b\u63cf\u9650\u5236<\/h3>\n<pre><code class=\"language-java\">\/**\n * Checks if the scan request from a background app needs to be throttled.\n *\/\nprivate boolean shouldScanRequestBeThrottledForBackgroundApp() {\n    long lastScanMs = mLastScanTimestampForBgApps;\n    long elapsedRealtime = mClock.getElapsedSinceBootMillis();\n    if (lastScanMs != 0\n            &amp;&amp; (elapsedRealtime - lastScanMs) &lt; SCAN_REQUEST_THROTTLE_INTERVAL_BG_APPS_MS) {\n        return true;\n    }\n    \/\/ Proceed with the scan request and record the time.\n    mLastScanTimestampForBgApps = elapsedRealtime;\n    return false;\n}\n\npublic static final int SCAN_REQUEST_THROTTLE_INTERVAL_BG_APPS_MS = 30 * 60 * 1000;<\/code><\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\u6240\u6709\u540e\u53f0\u5e94\u752830min\u5185\u53ea\u80fd\u53d1\u8d77\u4e00\u6b21\u626b\u63cf<\/p>\n<h3>\u524d\u53f0\u626b\u63cf\u9650\u5236<\/h3>\n<pre><code class=\"language-java\">\/**\n * Checks if the scan request from the app (specified by packageName) needs\n * to be throttled.\n * The throttle limit allows a max of {@link #SCAN_REQUEST_THROTTLE_MAX_IN_TIME_WINDOW_FG_APPS}\n * in {@link #SCAN_REQUEST_THROTTLE_TIME_WINDOW_FG_APPS_MS} window.\n *\/\nprivate boolean shouldScanRequestBeThrottledForForegroundApp(\n        int callingUid, String packageName) {\n    LinkedList&lt;Long&gt; scanRequestTimestamps =\n            getOrCreateScanRequestTimestampsForForegroundApp(callingUid, packageName);\n    long currentTimeMillis = mClock.getElapsedSinceBootMillis();\n    \/\/ First evict old entries from the list.\n    trimPastScanRequestTimesForForegroundApp(scanRequestTimestamps, currentTimeMillis);\n    if (scanRequestTimestamps.size() &gt;= SCAN_REQUEST_THROTTLE_MAX_IN_TIME_WINDOW_FG_APPS) {\n        return true;\n    }\n    \/\/ Proceed with the scan request and record the time.\n    scanRequestTimestamps.addLast(currentTimeMillis);\n    return false;\n}\n\n@VisibleForTesting\npublic static final int SCAN_REQUEST_THROTTLE_TIME_WINDOW_FG_APPS_MS = 120 * 1000;\n@VisibleForTesting\npublic static final int SCAN_REQUEST_THROTTLE_MAX_IN_TIME_WINDOW_FG_APPS = 4;\n\nprivate void trimPastScanRequestTimesForForegroundApp(\n        List&lt;Long&gt; scanRequestTimestamps, long currentTimeMillis) {\n    Iterator&lt;Long&gt; timestampsIter = scanRequestTimestamps.iterator();\n    while (timestampsIter.hasNext()) {\n        Long scanRequestTimeMillis = timestampsIter.next();\n        if ((currentTimeMillis - scanRequestTimeMillis)\n                &gt; SCAN_REQUEST_THROTTLE_TIME_WINDOW_FG_APPS_MS) {\n            timestampsIter.remove();\n        } else {\n            \/\/ This list is sorted by timestamps, so we can skip any more checks\n            break;\n        }\n    }\n}<\/code><\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\u6bcf\u4e2a\u5e94\u75282\u5206\u949f\u5185\u53ea\u80fd\u53d1\u8d77\u56db\u6b21\u626b\u63cf<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<pre><code> * d) Throttle scan requests from non-setting apps:\n *  a) Each foreground app can request a max of\n *   {@link #SCAN_REQUEST_THROTTLE_MAX_IN_TIME_WINDOW_FG_APPS} scan every\n *   {@link #SCAN_REQUEST_THROTTLE_TIME_WINDOW_FG_APPS_MS}.\n *  b) Background apps combined can request 1 scan every\n *   {@link #SCAN_REQUEST_THROTTLE_INTERVAL_BG_APPS_MS}.<\/code><\/pre>\n<p><code>ScanRequestProxy<\/code>\u7684\u6ce8\u91ca\u8bb2\u7684\u5f88\u6e05\u695a<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u524d\u53f0\u5e94\u75282\u5206\u949f\u5185\u53ea\u80fd\u53d1\u8d77\u56db\u6b21\u626b\u63cf<\/li>\n<li>\u6240\u6709\u7684\u540e\u53f0\u5e94\u7528\u52a0\u8d77\u676530\u5206\u949f\u5185\u53ea\u80fd\u53d1\u8d77\u4e00\u6b21\u626b\u63cf<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6d41\u7a0b\u68b3\u7406 ScanRequestProxy \/\/ Check and throttle scan reques [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[177],"class_list":["post-1844","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-wifi"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1844"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1844\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}