FileDocCategorySizeDatePackage
TrustAgentWrapper.javaAPI DocAndroid 5.1 API17816Thu Mar 12 22:22:42 GMT 2015com.android.server.trust

TrustAgentWrapper

public class TrustAgentWrapper extends Object
A wrapper around a TrustAgentService interface. Coordinates communication between TrustManager and the actual TrustAgent.

Fields Summary
private static final String
EXTRA_COMPONENT_NAME
private static final String
TRUST_EXPIRED_ACTION
private static final String
PERMISSION
private static final boolean
DEBUG
private static final String
TAG
private static final int
MSG_GRANT_TRUST
private static final int
MSG_REVOKE_TRUST
private static final int
MSG_TRUST_TIMEOUT
private static final int
MSG_RESTART_TIMEOUT
private static final int
MSG_SET_TRUST_AGENT_FEATURES_COMPLETED
private static final int
MSG_MANAGING_TRUST
private static final long
RESTART_TIMEOUT_MILLIS
Time in uptime millis that we wait for the service connection, both when starting and when the service disconnects.
private static final String
DATA_DURATION
Long extra for {@link #MSG_GRANT_TRUST}
private final TrustManagerService
mTrustManagerService
private final int
mUserId
private final android.content.Context
mContext
private final android.content.ComponentName
mName
private android.service.trust.ITrustAgentService
mTrustAgentService
private boolean
mBound
private long
mScheduledRestartUptimeMillis
private long
mMaximumTimeToLock
private boolean
mTrusted
private CharSequence
mMessage
private boolean
mTrustDisabledByDpm
private boolean
mManagingTrust
private android.os.IBinder
mSetTrustAgentFeaturesToken
private android.app.AlarmManager
mAlarmManager
private final android.content.Intent
mAlarmIntent
private android.app.PendingIntent
mAlarmPendingIntent
private final android.content.BroadcastReceiver
mBroadcastReceiver
private final android.os.Handler
mHandler
private android.service.trust.ITrustAgentServiceCallback
mCallback
private final android.content.ServiceConnection
mConnection
Constructors Summary
public TrustAgentWrapper(android.content.Context context, TrustManagerService trustManagerService, android.content.Intent intent, android.os.UserHandle user)


        
                
        mContext = context;
        mTrustManagerService = trustManagerService;
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        mUserId = user.getIdentifier();
        mName = intent.getComponent();

        mAlarmIntent = new Intent(TRUST_EXPIRED_ACTION).putExtra(EXTRA_COMPONENT_NAME, mName);
        mAlarmIntent.setData(Uri.parse(mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME)));
        mAlarmIntent.setPackage(context.getPackageName());

        final IntentFilter alarmFilter = new IntentFilter(TRUST_EXPIRED_ACTION);
        alarmFilter.addDataScheme(mAlarmIntent.getScheme());
        final String pathUri = mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME);
        alarmFilter.addDataPath(pathUri, PatternMatcher.PATTERN_LITERAL);
        mContext.registerReceiver(mBroadcastReceiver, alarmFilter, PERMISSION, null);

        // Schedules a restart for when connecting times out. If the connection succeeds,
        // the restart is canceled in mCallback's onConnected.
        scheduleRestart();
        mBound = context.bindServiceAsUser(intent, mConnection, Context.BIND_AUTO_CREATE, user);
        if (!mBound) {
            Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString());
        }
    
Methods Summary
public voiddestroy()

        mHandler.removeMessages(MSG_RESTART_TIMEOUT);

        if (!mBound) {
            return;
        }
        if (DEBUG) Log.v(TAG, "TrustAgent unbound : " + mName.flattenToShortString());
        mTrustManagerService.mArchive.logAgentStopped(mUserId, mName);
        mContext.unbindService(mConnection);
        mBound = false;
        mTrustAgentService = null;
        mSetTrustAgentFeaturesToken = null;
        mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
    
public java.lang.CharSequencegetMessage()

        return mMessage;
    
public longgetScheduledRestartUptimeMillis()
If not connected, returns the time at which the agent is restarted.

return
restart time in uptime millis.

        return mScheduledRestartUptimeMillis;
    
public booleanisBound()

        return mBound;
    
public booleanisConnected()

        return mTrustAgentService != null;
    
public booleanisManagingTrust()

        return mManagingTrust && !mTrustDisabledByDpm;
    
public booleanisTrusted()

        return mTrusted && mManagingTrust && !mTrustDisabledByDpm;
    
public voidonDeviceLocked()

see
android.service.trust.TrustAgentService#onDeviceLocked()

        try {
            if (mTrustAgentService != null) mTrustAgentService.onDeviceLocked();
        } catch (RemoteException e) {
            onError(e);
        }
    
public voidonDeviceUnlocked()

see
android.service.trust.TrustAgentService#onDeviceUnlocked()

        try {
            if (mTrustAgentService != null) mTrustAgentService.onDeviceUnlocked();
        } catch (RemoteException e) {
            onError(e);
        }
    
private voidonError(java.lang.Exception e)

        Slog.w(TAG , "Remote Exception", e);
    
private voidonTrustTimeout()

        try {
            if (mTrustAgentService != null) mTrustAgentService.onTrustTimeout();
        } catch (RemoteException e) {
            onError(e);
        }
    
public voidonUnlockAttempt(boolean successful)

see
android.service.trust.TrustAgentService#onUnlockAttempt(boolean)

        try {
            if (mTrustAgentService != null) mTrustAgentService.onUnlockAttempt(successful);
        } catch (RemoteException e) {
            onError(e);
        }
    
private voidscheduleRestart()

        mHandler.removeMessages(MSG_RESTART_TIMEOUT);
        mScheduledRestartUptimeMillis = SystemClock.uptimeMillis() + RESTART_TIMEOUT_MILLIS;
        mHandler.sendEmptyMessageAtTime(MSG_RESTART_TIMEOUT, mScheduledRestartUptimeMillis);
    
private voidsetCallback(android.service.trust.ITrustAgentServiceCallback callback)

        try {
            if (mTrustAgentService != null) {
                mTrustAgentService.setCallback(callback);
            }
        } catch (RemoteException e) {
            onError(e);
        }
    
booleanupdateDevicePolicyFeatures()

        boolean trustDisabled = false;
        if (DEBUG) Slog.v(TAG, "updateDevicePolicyFeatures(" + mName + ")");
        try {
            if (mTrustAgentService != null) {
                DevicePolicyManager dpm =
                    (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);

                if ((dpm.getKeyguardDisabledFeatures(null, mUserId)
                        & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0) {
                    List<PersistableBundle> config = dpm.getTrustAgentConfiguration(
                            null, mName, mUserId);
                    trustDisabled = true;
                    if (DEBUG) Slog.v(TAG, "Detected trust agents disabled. Config = " + config);
                    if (config != null && config.size() > 0) {
                        if (DEBUG) {
                            Slog.v(TAG, "TrustAgent " + mName.flattenToShortString()
                                    + " disabled until it acknowledges "+ config);
                        }
                        mSetTrustAgentFeaturesToken = new Binder();
                        mTrustAgentService.onConfigure(config, mSetTrustAgentFeaturesToken);
                    }
                }
                final long maxTimeToLock = dpm.getMaximumTimeToLock(null);
                if (maxTimeToLock != mMaximumTimeToLock) {
                    // If the timeout changes, cancel the alarm and send a timeout event to have
                    // the agent re-evaluate trust.
                    mMaximumTimeToLock = maxTimeToLock;
                    if (mAlarmPendingIntent != null) {
                        mAlarmManager.cancel(mAlarmPendingIntent);
                        mAlarmPendingIntent = null;
                        mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT);
                    }
                }
            }
        } catch (RemoteException e) {
            onError(e);
        }
        if (mTrustDisabledByDpm != trustDisabled) {
            mTrustDisabledByDpm = trustDisabled;
            mTrustManagerService.updateTrust(mUserId, false);
        }
        return trustDisabled;