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

Commit a50f689e authored by Joey's avatar Joey Committed by Han Wang
Browse files

base: add Trust usb restrictor



This is a squash of following commits:

Author: Joey <joey@lineageos.org>
Date:   Wed Jan 9 22:22:46 2019 +0100

    base: add Trust usb restrictor

    Change-Id: Ie2cb8e91d0f13f0d1473a48d22839b378701c756
    Signed-off-by: default avatarJoey <joey@lineageos.org>

Author: Han Wang <416810799@qq.com>
Date:   Fri Jun 7 08:30:54 2019 +0200

    KeyguardStateMonitor: Don't get IUsbRestrict service in constructor

     * If boot up is "too fast" and Trust HAL starts a bit slowly,
       we may end up getting a NULL IUsbRestrict service in
       constructor. This causes a NPE later in onShowingStateChanged().

     * To avoid this, we just try to get IUsbRestrict service in
       onShowingStateChanged(). This adds a little overhead, but prevents
       possible crashes.

    Change-Id: Idc09cc1700aad8753244789a1f14ea1d5267f77c

Change-Id: Idc09cc1700aad8233244789a1f14ea1d5267f223
parent 05bdbc5a
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.policy.keyguard;

import android.app.ActivityManager;
import android.content.Context;
import android.content.ContentResolver;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.security.keystore.IKeystoreService;
@@ -27,7 +28,11 @@ import com.android.internal.policy.IKeyguardService;
import com.android.internal.policy.IKeyguardStateCallback;
import com.android.internal.widget.LockPatternUtils;

import lineageos.providers.LineageSettings;
import vendor.lineage.trust.V1_0.IUsbRestrict;

import java.io.PrintWriter;
import java.util.NoSuchElementException;

/**
 * Maintains a cached copy of Keyguard's state.
@@ -53,12 +58,16 @@ public class KeyguardStateMonitor extends IKeyguardStateCallback.Stub {
    private final LockPatternUtils mLockPatternUtils;
    private final StateCallback mCallback;

    private IUsbRestrict mUsbRestrictor = null;
    private ContentResolver mContentResolver;

    IKeystoreService mKeystoreService;

    public KeyguardStateMonitor(Context context, IKeyguardService service, StateCallback callback) {
        mLockPatternUtils = new LockPatternUtils(context);
        mCurrentUserId = ActivityManager.getCurrentUser();
        mCallback = callback;
        mContentResolver = context.getContentResolver();

        mKeystoreService = IKeystoreService.Stub.asInterface(ServiceManager
                .getService("android.security.keystore"));
@@ -112,6 +121,28 @@ public class KeyguardStateMonitor extends IKeyguardStateCallback.Stub {
                --retry;
            }
        }

        if (mUsbRestrictor == null) {
            try {
                mUsbRestrictor = IUsbRestrict.getService();
                if (mUsbRestrictor == null) {
                    // Ignore, the hal is not available
                    return;
                }
            } catch (NoSuchElementException | RemoteException ignored) {
                return;
            }
        }

        boolean shouldRestrictUsb = LineageSettings.Secure.getInt(mContentResolver,
                LineageSettings.Secure.TRUST_RESTRICT_USB_KEYGUARD, 0) == 1;
        if (shouldRestrictUsb) {
            try {
                mUsbRestrictor.setEnabled(showing);
            } catch (RemoteException ignored) {
                // This feature is not supported
            }
        }
    }

    @Override // Binder interface