FileDocCategorySizeDatePackage
SampleTrustAgent.javaAPI DocAndroid 5.1 API8167Thu Mar 12 22:22:42 GMT 2015com.android.trustagent.test

SampleTrustAgent

public class SampleTrustAgent extends android.service.trust.TrustAgentService implements SharedPreferences.OnSharedPreferenceChangeListener

Fields Summary
private static final boolean
ALLOW_EXTERNAL_BROADCASTS
If true, allows anyone to control this trust agent, e.g. using adb:
$ adb shell am broadcast -a action.sample_trust_agent.grant_trust\
-e extra.message SampleTrust\
--el extra.duration 1000 --ez extra.init_by_user false
android.support.v4.content.LocalBroadcastManager
mLocalBroadcastManager
private static final String
ACTION_GRANT_TRUST
private static final String
ACTION_REVOKE_TRUST
private static final String
EXTRA_MESSAGE
private static final String
EXTRA_DURATION
private static final String
EXTRA_INITIATED_BY_USER
private static final String
PREFERENCE_REPORT_UNLOCK_ATTEMPTS
private static final String
PREFERENCE_MANAGING_TRUST
private static final String
PREFERENCE_REPORT_DEVICE_LOCKED
private static final String
TAG
private android.content.BroadcastReceiver
mReceiver
Constructors Summary
Methods Summary
public static booleangetIsManagingTrust(android.content.Context context)

        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(PREFERENCE_MANAGING_TRUST, false);
    
public static booleangetReportDeviceLocked(android.content.Context context)

        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(PREFERENCE_REPORT_DEVICE_LOCKED, false);
    
public static booleangetReportUnlockAttempts(android.content.Context context)

        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        return sharedPreferences.getBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, false);
    
private voidlogAndShowToast(java.lang.String text)

        Log.i(TAG, text);
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    
public booleanonConfigure(java.util.List options)

        if (options != null) {
           for (int i = 0; i < options.size(); i++) {
               Log.v(TAG, "Policy options received: " + options.get(i));
           }
        } else {
            Log.w(TAG, "onConfigure() called with no options");
        }
        // TODO: Handle options
        return true; // inform DPM that we support it
    
public voidonCreate()


    
       
        super.onCreate();
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_GRANT_TRUST);
        filter.addAction(ACTION_REVOKE_TRUST);
        mLocalBroadcastManager.registerReceiver(mReceiver, filter);
        if (ALLOW_EXTERNAL_BROADCASTS) {
            registerReceiver(mReceiver, filter);
        }

        setManagingTrust(getIsManagingTrust(this));
        PreferenceManager.getDefaultSharedPreferences(this)
                .registerOnSharedPreferenceChangeListener(this);
    
public voidonDestroy()

        super.onDestroy();
        mLocalBroadcastManager.unregisterReceiver(mReceiver);
        if (ALLOW_EXTERNAL_BROADCASTS) {
            unregisterReceiver(mReceiver);
        }
        PreferenceManager.getDefaultSharedPreferences(this)
                .unregisterOnSharedPreferenceChangeListener(this);
    
public voidonDeviceLocked()

        super.onDeviceLocked();
        if (getReportDeviceLocked(this)) {
            logAndShowToast("onDeviceLocked(): device is now locked");
        }
    
public voidonDeviceUnlocked()

        super.onDeviceUnlocked();
        if (getReportDeviceLocked(this)) {
            logAndShowToast("onDeviceUnlocked(): device is now unlocked");
        }
    
public voidonSharedPreferenceChanged(android.content.SharedPreferences sharedPreferences, java.lang.String key)

        if (PREFERENCE_MANAGING_TRUST.equals(key)) {
            setManagingTrust(getIsManagingTrust(this));
        }
    
public voidonTrustTimeout()

        super.onTrustTimeout();
        logAndShowToast("onTrustTimeout(): timeout expired");
    
public voidonUnlockAttempt(boolean successful)

        if (getReportUnlockAttempts(this)) {
            logAndShowToast("onUnlockAttempt(successful=" + successful + ")");
        }
    
public static voidsendGrantTrust(android.content.Context context, java.lang.String message, long durationMs, boolean initiatedByUser)


        
                  
        Intent intent = new Intent(ACTION_GRANT_TRUST);
        intent.putExtra(EXTRA_MESSAGE, message);
        intent.putExtra(EXTRA_DURATION, durationMs);
        intent.putExtra(EXTRA_INITIATED_BY_USER, initiatedByUser);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    
public static voidsendRevokeTrust(android.content.Context context)

        Intent intent = new Intent(ACTION_REVOKE_TRUST);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    
public static voidsetIsManagingTrust(android.content.Context context, boolean enabled)

        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        sharedPreferences.edit().putBoolean(PREFERENCE_MANAGING_TRUST, enabled).apply();
    
public static voidsetReportDeviceLocked(android.content.Context context, boolean enabled)

        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_DEVICE_LOCKED, enabled).apply();
    
public static voidsetReportUnlockAttempts(android.content.Context context, boolean enabled)

        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, enabled).apply();