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

Commit 27019c7d authored by Joy Babafemi's avatar Joy Babafemi Committed by Roshan Pius
Browse files

DO NOT MERGE: Disable UWB in airplane mode.

(Cherry-picked from sc-qpr branch)

Bug: 199933856
Bug: 212462949
Test: Manual
Change-Id: I13c5986e28e7a1a9277518ca96da9a22c5e1c21e
parent b8b20bd4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ public class UwbServiceImplTest {
        when(mUwbInjector.getVendorService()).thenReturn(mVendorService);
        when(mUwbInjector.checkUwbRangingPermissionForDataDelivery(any(), any())).thenReturn(true);
        when(mUwbInjector.isPersistedUwbStateEnabled()).thenReturn(true);
        when(mUwbInjector.isAirplaneModeOn()).thenReturn(false);
        when(mVendorService.asBinder()).thenReturn(mVendorServiceBinder);
        mUwbServiceImpl = new UwbServiceImpl(mContext, mUwbInjector);
    }
+6 −0
Original line number Diff line number Diff line
@@ -96,4 +96,10 @@ public class UwbInjector {
            return true;
        }
    }

    /** Returns true if airplane mode is turned on. */
    public boolean isAirplaneModeOn() {
        return Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
    }
}
+27 −2
Original line number Diff line number Diff line
@@ -18,8 +18,11 @@ package com.android.server.uwb;

import android.annotation.NonNull;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.os.PersistableBundle;
@@ -238,7 +241,7 @@ public class UwbServiceImpl extends IUwbAdapter.Stub implements IBinder.DeathRec
        Log.i(TAG, "Retrieved vendor service");
        long token = Binder.clearCallingIdentity();
        try {
        mVendorUwbAdapter.setEnabled(mUwbInjector.isPersistedUwbStateEnabled());
            mVendorUwbAdapter.setEnabled(isEnabled());
        } finally {
          Binder.restoreCallingIdentity(token);
        }
@@ -249,6 +252,7 @@ public class UwbServiceImpl extends IUwbAdapter.Stub implements IBinder.DeathRec
    UwbServiceImpl(@NonNull Context context, @NonNull UwbInjector uwbInjector) {
        mContext = context;
        mUwbInjector = uwbInjector;
        registerAirplaneModeReceiver();
    }

    private void enforceUwbPrivilegedPermission() {
@@ -331,7 +335,7 @@ public class UwbServiceImpl extends IUwbAdapter.Stub implements IBinder.DeathRec
    @Override
    public synchronized void setEnabled(boolean enabled) throws RemoteException {
        persistUwbState(enabled);
        getVendorUwbAdapter().setEnabled(enabled);
        getVendorUwbAdapter().setEnabled(isEnabled());
    }

    private void persistUwbState(boolean enabled) {
@@ -339,4 +343,25 @@ public class UwbServiceImpl extends IUwbAdapter.Stub implements IBinder.DeathRec
        int state = enabled ? AdapterState.STATE_ENABLED_ACTIVE : AdapterState.STATE_DISABLED;
        Settings.Global.putInt(cr, Settings.Global.UWB_ENABLED, state);
    }

    private void registerAirplaneModeReceiver() {
        mContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                handleAirplaneModeEvent();
            }
        }, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
    }

    private void handleAirplaneModeEvent() {
        try {
            getVendorUwbAdapter().setEnabled(isEnabled());
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to set UWB Adapter state.", e);
        }
    }

    private boolean isEnabled() {
        return mUwbInjector.isPersistedUwbStateEnabled() && !mUwbInjector.isAirplaneModeOn();
    }
}