Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7a8d9d6f authored by Robert Greenwalt's avatar Robert Greenwalt Committed by Android (Google) Code Review
Browse files

Merge "Add support for batched wifi scans." into klp-dev

parents ba5e533e 0451d59b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -391,6 +391,8 @@ aidl_files := \
	frameworks/base/telephony/java/android/telephony/ServiceState.aidl \
	frameworks/base/telephony/java/com/android/internal/telephony/IPhoneSubInfo.aidl \
	frameworks/base/telephony/java/com/android/internal/telephony/ITelephony.aidl \
	frameworks/base/wifi/java/android/net/wifi/BatchedScanSettings.aidl \
	frameworks/base/wifi/java/android/net/wifi/BatchedScanResult.aidl \

gen := $(TARGET_OUT_COMMON_INTERMEDIATES)/framework.aidl
$(gen): PRIVATE_SRC_FILES := $(aidl_files)
+3 −0
Original line number Diff line number Diff line
@@ -353,6 +353,9 @@
         Default value is 2 minutes. -->
    <integer translatable="false" name="config_wifi_driver_stop_delay">120000</integer>

    <!-- Wifi driver supports batched scan -->
    <bool translatable="false" name="config_wifi_batched_scan_supported">false</bool>

    <!-- Flag indicating whether the we should enable the automatic brightness in Settings.
         Software implementation will be used if config_hardware_auto_brightness_available is not set -->
    <bool name="config_automatic_brightness_available">false</bool>
+2 −1
Original line number Diff line number Diff line
@@ -281,6 +281,7 @@
  <java-symbol type="bool" name="config_speed_up_audio_on_mt_calls" />
  <java-symbol type="bool" name="config_useFixedVolume" />
  <java-symbol type="bool" name="config_forceDefaultOrientation" />
  <java-symbol type="bool" name="config_wifi_batched_scan_supported" />

  <java-symbol type="integer" name="config_cursorWindowSize" />
  <java-symbol type="integer" name="config_extraFreeKbytesAdjust" />
+154 −10
Original line number Diff line number Diff line
@@ -25,18 +25,20 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.DhcpInfo;
import android.net.DhcpResults;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import android.net.RouteInfo;
import android.net.wifi.IWifiManager;
import android.net.wifi.ScanResult;
import android.net.wifi.BatchedScanResult;
import android.net.wifi.BatchedScanSettings;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiStateMachine;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiWatchdogStateMachine;
import android.net.DhcpInfo;
import android.net.DhcpResults;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import android.net.RouteInfo;
import android.os.Binder;
import android.os.Handler;
import android.os.Messenger;
@@ -63,6 +65,7 @@ import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.concurrent.atomic.AtomicBoolean;
@@ -121,6 +124,8 @@ public final class WifiService extends IWifiManager.Stub {
    /* Tracks the persisted states for wi-fi & airplane mode */
    final WifiSettingsStore mSettingsStore;

    final boolean mBatchedScanSupported;

    /**
     * Asynchronous channel to WifiStateMachine
     */
@@ -246,6 +251,9 @@ public final class WifiService extends IWifiManager.Stub {
        mWifiController = new WifiController(mContext, this, wifiThread.getLooper());
        mWifiController.start();

        mBatchedScanSupported = mContext.getResources().getBoolean(
                R.bool.config_wifi_batched_scan_supported);

        registerForScanModeChange();
        mContext.registerReceiver(
                new BroadcastReceiver() {
@@ -314,6 +322,142 @@ public final class WifiService extends IWifiManager.Stub {
        mWifiStateMachine.startScan(Binder.getCallingUid(), workSource);
    }

    private class BatchedScanRequest extends DeathRecipient {
        BatchedScanSettings settings;
        int uid;

        BatchedScanRequest(BatchedScanSettings settings, IBinder binder, int uid) {
            super(0, null, binder, null);
            this.settings = settings;
            this.uid = uid;
        }
        public void binderDied() {
            stopBatchedScan(settings, mBinder);
        }
        public String toString() {
            return "BatchedScanRequest{settings=" + settings + ", binder=" + mBinder + "}";
        }
    }

    private final List<BatchedScanRequest> mBatchedScanners = new ArrayList<BatchedScanRequest>();

    public boolean isBatchedScanSupported() {
        return mBatchedScanSupported;
    }

    /**
     * see {@link android.net.wifi.WifiManager#requestBatchedScan()}
     */
    public boolean requestBatchedScan(BatchedScanSettings requested, IBinder binder) {
        enforceChangePermission();
        if (mBatchedScanSupported == false) return false;
        requested = new BatchedScanSettings(requested);
        if (requested.isInvalid()) return false;
        BatchedScanRequest r = new BatchedScanRequest(requested, binder, Binder.getCallingUid());
        synchronized(mBatchedScanners) {
            mBatchedScanners.add(r);
            resolveBatchedScannersLocked();
        }
        return true;
    }

    public List<BatchedScanResult> getBatchedScanResults(String callingPackage) {
        enforceAccessPermission();
        if (mBatchedScanSupported == false) return new ArrayList<BatchedScanResult>();
        int userId = UserHandle.getCallingUserId();
        int uid = Binder.getCallingUid();
        long ident = Binder.clearCallingIdentity();
        try {
            if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage)
                    != AppOpsManager.MODE_ALLOWED) {
                return new ArrayList<BatchedScanResult>();
            }
            int currentUser = ActivityManager.getCurrentUser();
            if (userId != currentUser) {
                return new ArrayList<BatchedScanResult>();
            } else {
                return mWifiStateMachine.syncGetBatchedScanResultsList();
            }
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }


    public void stopBatchedScan(BatchedScanSettings settings, IBinder binder) {
        enforceChangePermission();
        if (mBatchedScanSupported == false) return;
        synchronized(mBatchedScanners) {
            BatchedScanRequest found = null;
            for (BatchedScanRequest r : mBatchedScanners) {
                if (r.mBinder.equals(binder) && r.settings.equals(settings)) {
                    found = r;
                    break;
                }
            }
            if (found != null) {
                mBatchedScanners.remove(found);
                resolveBatchedScannersLocked();
            }
        }
    }

    private void resolveBatchedScannersLocked() {
        BatchedScanSettings setting = new BatchedScanSettings();
        setting.scanIntervalSec = BatchedScanSettings.DEFAULT_INTERVAL_SEC;
        int responsibleUid = 0;
        setting.channelSet = new ArrayList<String>();

        if (mBatchedScanners.size() == 0) {
            mWifiStateMachine.setBatchedScanSettings(null, 0);
            return;
        }

        for (BatchedScanRequest r : mBatchedScanners) {
            BatchedScanSettings s = r.settings;
            if (s.maxScansPerBatch != BatchedScanSettings.UNSPECIFIED &&
                    s.maxScansPerBatch < setting.maxScansPerBatch) {
                setting.maxScansPerBatch = s.maxScansPerBatch;
                responsibleUid = r.uid;
            }
            if (s.maxApPerScan != BatchedScanSettings.UNSPECIFIED &&
                    s.maxApPerScan > setting.maxApPerScan) {
                setting.maxApPerScan = s.maxApPerScan;
            }
            if (s.scanIntervalSec != BatchedScanSettings.UNSPECIFIED &&
                    s.scanIntervalSec < setting.scanIntervalSec) {
                setting.scanIntervalSec = s.scanIntervalSec;
                responsibleUid = r.uid;
            }
            if (s.maxApForDistance != BatchedScanSettings.UNSPECIFIED &&
                    s.maxApForDistance > setting.maxApForDistance) {
                setting.maxApForDistance = s.maxApForDistance;
            }
            if (s.channelSet != null) {
                for (String i : s.channelSet) {
                    if (setting.channelSet.contains(i) == false) setting.channelSet.add(i);
                }
            }
        }
        if (setting.channelSet.size() == 0) setting.channelSet = null;
        if (setting.scanIntervalSec < BatchedScanSettings.MIN_INTERVAL_SEC) {
            setting.scanIntervalSec = BatchedScanSettings.MIN_INTERVAL_SEC;
        }
        if (setting.maxScansPerBatch == BatchedScanSettings.UNSPECIFIED) {
            setting.maxScansPerBatch = BatchedScanSettings.DEFAULT_SCANS_PER_BATCH;
        }
        if (setting.maxApPerScan == BatchedScanSettings.UNSPECIFIED) {
            setting.maxApPerScan = BatchedScanSettings.DEFAULT_AP_PER_SCAN;
        }
        if (setting.scanIntervalSec == BatchedScanSettings.UNSPECIFIED) {
            setting.scanIntervalSec = BatchedScanSettings.DEFAULT_INTERVAL_SEC;
        }
        if (setting.maxApForDistance == BatchedScanSettings.UNSPECIFIED) {
            setting.maxApForDistance = BatchedScanSettings.DEFAULT_AP_FOR_DISTANCE;
        }
        mWifiStateMachine.setBatchedScanSettings(setting, responsibleUid);
    }

    private void enforceAccessPermission() {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
                                                "WifiService");
@@ -569,11 +713,11 @@ public final class WifiService extends IWifiManager.Stub {
        int userId = UserHandle.getCallingUserId();
        int uid = Binder.getCallingUid();
        long ident = Binder.clearCallingIdentity();
        try {
            if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage)
                    != AppOpsManager.MODE_ALLOWED) {
                return new ArrayList<ScanResult>();
            }
        try {
            int currentUser = ActivityManager.getCurrentUser();
            if (userId != currentUser) {
                return new ArrayList<ScanResult>();
+19 −0
Original line number Diff line number Diff line
/**
 * Copyright (c) 2013, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package android.net.wifi;

parcelable BatchedScanResult;
Loading