Example usage for android.support.v4.os UserManagerCompat isUserUnlocked

List of usage examples for android.support.v4.os UserManagerCompat isUserUnlocked

Introduction

In this page you can find the example usage for android.support.v4.os UserManagerCompat isUserUnlocked.

Prototype

public static boolean isUserUnlocked(Context context) 

Source Link

Document

Return whether the calling user is running in an "unlocked" state.

Usage

From source file:com.google.android.apps.muzei.settings.Prefs.java

public synchronized static SharedPreferences getSharedPreferences(Context context) {
    Context deviceProtectedContext = ContextCompat.createDeviceProtectedStorageContext(context);
    if (UserManagerCompat.isUserUnlocked(context)) {
        // First migrate the wallpaper settings to their own file
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences wallpaperPreferences = context.getSharedPreferences(WALLPAPER_PREFERENCES_NAME,
                Context.MODE_PRIVATE);
        migratePreferences(defaultSharedPreferences, wallpaperPreferences);

        // Now migrate the file to device protected storage if available
        if (deviceProtectedContext != null) {
            SharedPreferences deviceProtectedPreferences = deviceProtectedContext
                    .getSharedPreferences(WALLPAPER_PREFERENCES_NAME, Context.MODE_PRIVATE);
            migratePreferences(wallpaperPreferences, deviceProtectedPreferences);
        }//from w w w.ja v  a 2 s.com
    }
    // Now open the correct SharedPreferences
    Context contextToUse = deviceProtectedContext != null ? deviceProtectedContext : context;
    return contextToUse.getSharedPreferences(WALLPAPER_PREFERENCES_NAME, Context.MODE_PRIVATE);
}

From source file:com.example.android.directboot.BootBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    boolean bootCompleted;
    String action = intent.getAction();
    Log.i(TAG, "Received action: " + action + ", user unlocked: " + UserManagerCompat.isUserUnlocked(context));
    if (BuildCompat.isAtLeastN()) {
        bootCompleted = Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(action);
    } else {/*from  w w w.  jav  a 2 s. c  o m*/
        bootCompleted = Intent.ACTION_BOOT_COMPLETED.equals(action);
    }
    if (!bootCompleted) {
        return;
    }
    AlarmUtil util = new AlarmUtil(context);
    AlarmStorage alarmStorage = new AlarmStorage(context);
    for (Alarm alarm : alarmStorage.getAlarms()) {
        util.scheduleAlarm(alarm);
    }
}

From source file:com.google.android.apps.muzei.MuzeiWallpaperService.java

@Override
public void onCreate() {
    super.onCreate();
    if (UserManagerCompat.isUserUnlocked(this)) {
        initialize();/*from  ww w . ja va 2  s  .  c  om*/
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        mUnlockReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                initialize();
                unregisterReceiver(this);
            }
        };
        IntentFilter filter = new IntentFilter(Intent.ACTION_USER_UNLOCKED);
        registerReceiver(mUnlockReceiver, filter);
    }
}

From source file:com.android.talkback.labeling.LabelProvider.java

/**
 * Inserts a label in the labels database.
 *
 * @param uri The content URI for labels.
 * @param values The values to insert for the new label.
 * @return The URI of the newly inserted label,
 *         or {@code null} if the insert failed.
 *///from  w w  w  .j  a va 2s  . c  o  m
@Override
public Uri insert(Uri uri, ContentValues values) {
    if (uri == null) {
        LogUtils.log(this, Log.WARN, NULL_URI_FORMAT_STRING);
        return null;
    }

    if (!UserManagerCompat.isUserUnlocked(getContext())) {
        return null;
    }

    switch (sUriMatcher.match(uri)) {
    case LABELS:
        initializeDatabaseIfNull();

        if (values == null) {
            return null;
        }

        if (values.containsKey(LabelsTable.KEY_ID)) {
            LogUtils.log(this, Log.WARN, "Label ID must be assigned by the database.");
            return null;
        }

        long rowId = mDatabase.insert(LabelsTable.TABLE_NAME, null, values);

        if (rowId < 0) {
            LogUtils.log(this, Log.WARN, "Failed to insert label.");
            return null;
        } else {
            return ContentUris.withAppendedId(LABELS_CONTENT_URI, rowId);
        }
    default:
        LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri);
        return null;
    }
}

From source file:com.android.talkback.labeling.LabelProvider.java

/**
 * Queries for a label or multiple labels in the labels database.
 *
 * @param uri The URI representing the type of query to perform:
 *            {@code LABELS_CONTENT_URI} for a subset of all labels,
 *            {@code LABELS_ID_CONTENT_URI} for a specific label, or
 *            {@code PACKAGE_SUMMARY} for a label count per package.
 * @param projection The columns to return.
 * @param selection The WHERE clause for the query.
 * @param selectionArgs The arguments for the WHERE clause of the query.
 * @param sortOrder the ORDER BY clause for the query.
 * @return A cursor representing the data resulting from the query, or]
 *         {@code null} if the query failed to execute.
 *//* w  w  w .  j  a v  a  2 s  . c o m*/
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    if (uri == null) {
        LogUtils.log(this, Log.WARN, NULL_URI_FORMAT_STRING);
        return null;
    }

    if (!UserManagerCompat.isUserUnlocked(getContext())) {
        return null;
    }

    final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(LabelsTable.TABLE_NAME);

    String groupBy = null;

    switch (sUriMatcher.match(uri)) {
    case LABELS:
        if (TextUtils.isEmpty(sortOrder)) {
            sortOrder = LabelsTable.KEY_ID;
        }
        break;
    case LABELS_ID:
        final String labelIdString = uri.getLastPathSegment();
        final int labelId;
        try {
            labelId = Integer.parseInt(labelIdString);
        } catch (NumberFormatException e) {
            LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri);
            return null;
        }

        final String where = String.format(Locale.ROOT, "%s = %d", LabelsTable.KEY_ID, labelId);
        queryBuilder.appendWhere(where);
        break;
    case PACKAGE_SUMMARY:
        projection = new String[] { LabelsTable.KEY_PACKAGE_NAME, "COUNT(*)" };
        groupBy = LabelsTable.KEY_PACKAGE_NAME;
        sortOrder = LabelsTable.KEY_PACKAGE_NAME;
        break;
    default:
        LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri);
        return null;
    }

    initializeDatabaseIfNull();

    return queryBuilder.query(mDatabase, projection, selection, selectionArgs, groupBy, null /* having */,
            sortOrder);
}

From source file:com.android.talkback.labeling.LabelProvider.java

/**
 * Updates a label in the labels database.
 *
 * @param uri The URI matching {code LABELS_ID_CONTENT_URI} that represents
 *            the specific label to update.
 * @param values The values to use to update the label.
 * @param selection The WHERE clause for the query.
 * @param selectionArgs The arguments for the WHERE clause of the query.
 * @return The number of rows affected.// w  w w .  j av  a2 s .c  om
 */
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    if (uri == null) {
        LogUtils.log(this, Log.WARN, NULL_URI_FORMAT_STRING);
        return 0;
    }

    if (!UserManagerCompat.isUserUnlocked(getContext())) {
        return 0;
    }

    switch (sUriMatcher.match(uri)) {
    case LABELS: {
        initializeDatabaseIfNull();

        int result = mDatabase.update(LabelsTable.TABLE_NAME, values, selection, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null /* observer */);
        return result;
    }
    case LABELS_ID: {
        initializeDatabaseIfNull();

        final String labelIdString = uri.getLastPathSegment();
        final int labelId;
        try {
            labelId = Integer.parseInt(labelIdString);
        } catch (NumberFormatException e) {
            LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri);
            return 0;
        }

        final String where = String.format(Locale.ROOT, "%s = %d", LabelsTable.KEY_ID, labelId);
        final int result = mDatabase.update(LabelsTable.TABLE_NAME, values,
                combineSelectionAndWhere(selection, where), selectionArgs);

        getContext().getContentResolver().notifyChange(uri, null /* observer */);

        return result;
    }
    default:
        LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri);
        return 0;
    }
}

From source file:com.android.talkback.labeling.LabelProvider.java

/**
 * Deletes a label in the labels database.
 *
 * @param uri The URI matching {code LABELS_ID_CONTENT_URI} that represents
 *            the specific label to delete.
 * @param selection The WHERE clause for the query.
 * @param selectionArgs The arguments for the WHERE clause of the query.
 * @return The number of rows affected.// w  w w.  j  ava2 s  . co  m
 */
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    if (uri == null) {
        LogUtils.log(this, Log.WARN, NULL_URI_FORMAT_STRING);
        return 0;
    }

    if (!UserManagerCompat.isUserUnlocked(getContext())) {
        return 0;
    }

    switch (sUriMatcher.match(uri)) {
    case LABELS: {
        initializeDatabaseIfNull();

        int result = mDatabase.delete(LabelsTable.TABLE_NAME, selection, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null /* observer */);
        return result;
    }
    case LABELS_ID: {
        initializeDatabaseIfNull();

        final String labelIdString = uri.getLastPathSegment();
        final int labelId;
        try {
            labelId = Integer.parseInt(labelIdString);
        } catch (NumberFormatException e) {
            LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri);
            return 0;
        }

        final String where = String.format(Locale.ROOT, "%s = %d", LabelsTable.KEY_ID, labelId);
        final int result = mDatabase.delete(LabelsTable.TABLE_NAME, combineSelectionAndWhere(selection, where),
                selectionArgs);

        getContext().getContentResolver().notifyChange(uri, null /* observer */);

        return result;
    }
    default:
        LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri);
        return 0;
    }
}

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

@Override
public int delete(@NonNull final Uri uri, final String selection, final String[] selectionArgs) {
    Context context = getContext();
    if (context == null) {
        return 0;
    }//from  w w w .j a v a2  s . co  m
    if (!UserManagerCompat.isUserUnlocked(context)) {
        Log.w(TAG, "Deletes are not supported until the user is unlocked");
        return 0;
    }
    // Only allow Muzei to delete content
    if (!context.getPackageName().equals(getCallingPackage())) {
        throw new UnsupportedOperationException("Deletes are not supported");
    }
    if (MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.ARTWORK
            || MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.ARTWORK_ID) {
        return deleteArtwork(uri, selection, selectionArgs);
    } else if (MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.SOURCES
            || MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.SOURCE_ID) {
        return deleteSource(uri, selection, selectionArgs);
    } else {
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
}

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

@Override
public Uri insert(@NonNull final Uri uri, final ContentValues values) {
    Context context = getContext();
    if (context == null) {
        return null;
    }//from   www.  jav  a2  s.  co m
    if (!UserManagerCompat.isUserUnlocked(context)) {
        Log.w(TAG, "Inserts are not supported until the user is unlocked");
        return null;
    }
    if (MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.ARTWORK) {
        return insertArtwork(uri, values);
    } else if (MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.SOURCES) {
        // Ensure the app inserting the source is Muzei
        if (!context.getPackageName().equals(getCallingPackage())) {
            throw new UnsupportedOperationException("Inserting sources is not supported, use update");
        }
        return insertSource(uri, values);
    } else {
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
}

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

@Override
public Cursor query(@NonNull final Uri uri, final String[] projection, final String selection,
        final String[] selectionArgs, final String sortOrder) {
    if (!UserManagerCompat.isUserUnlocked(getContext())) {
        Log.w(TAG, "Queries are not supported until the user is unlocked");
        return null;
    }/*from  w  w w. j a  v  a2 s . c o m*/
    if (MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.ARTWORK
            || MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.ARTWORK_ID) {
        return queryArtwork(uri, projection, selection, selectionArgs, sortOrder);
    } else if (MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.SOURCES
            || MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.SOURCE_ID) {
        return querySource(uri, projection, selection, selectionArgs, sortOrder);
    } else {
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
}