Search in sources :

Example 1 with UserIconDrawable

use of com.android.settingslib.drawable.UserIconDrawable in project android_frameworks_base by crdroidandroid.

the class UserInfoController method queryForUserInformation.

private void queryForUserInformation() {
    Context currentUserContext;
    UserInfo userInfo;
    try {
        userInfo = ActivityManagerNative.getDefault().getCurrentUser();
        currentUserContext = mContext.createPackageContextAsUser("android", 0, new UserHandle(userInfo.id));
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Couldn't create user context", e);
        throw new RuntimeException(e);
    } catch (RemoteException e) {
        Log.e(TAG, "Couldn't get user info", e);
        throw new RuntimeException(e);
    }
    final int userId = userInfo.id;
    final boolean isGuest = userInfo.isGuest();
    final String userName = userInfo.name;
    final Resources res = mContext.getResources();
    final int avatarSize = Math.max(res.getDimensionPixelSize(R.dimen.multi_user_avatar_expanded_size), res.getDimensionPixelSize(R.dimen.multi_user_avatar_keyguard_size));
    final Context context = currentUserContext;
    mUserInfoTask = new AsyncTask<Void, Void, Pair<String, Drawable>>() {

        @Override
        protected Pair<String, Drawable> doInBackground(Void... params) {
            final UserManager um = UserManager.get(mContext);
            // Fall back to the UserManager nickname if we can't read the name from the local
            // profile below.
            String name = userName;
            Drawable avatar = null;
            Bitmap rawAvatar = um.getUserIcon(userId);
            if (rawAvatar != null) {
                avatar = new UserIconDrawable(avatarSize).setIcon(rawAvatar).setBadgeIfManagedUser(mContext, userId).bake();
            } else {
                avatar = UserIcons.getDefaultUserIcon(isGuest ? UserHandle.USER_NULL : userId, /* light= */
                true);
            }
            // usually valid
            if (um.getUsers().size() <= 1) {
                // Try and read the display name from the local profile
                final Cursor cursor = context.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, null, null, null);
                if (cursor != null) {
                    try {
                        if (cursor.moveToFirst()) {
                            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        }
                    } finally {
                        cursor.close();
                    }
                }
            }
            return new Pair<String, Drawable>(name, avatar);
        }

        @Override
        protected void onPostExecute(Pair<String, Drawable> result) {
            mUserName = result.first;
            mUserDrawable = result.second;
            mUserInfoTask = null;
            notifyChanged();
        }
    };
    mUserInfoTask.execute();
}
Also used : Context(android.content.Context) Drawable(android.graphics.drawable.Drawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) UserInfo(android.content.pm.UserInfo) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) Cursor(android.database.Cursor) Bitmap(android.graphics.Bitmap) PackageManager(android.content.pm.PackageManager) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle) Resources(android.content.res.Resources) RemoteException(android.os.RemoteException) Pair(android.util.Pair)

Example 2 with UserIconDrawable

use of com.android.settingslib.drawable.UserIconDrawable in project android_packages_apps_Settings by LineageOS.

the class SecondaryUserControllerTest method setIcon_doesntNpeOnNullPreference.

@Test
public void setIcon_doesntNpeOnNullPreference() throws Exception {
    SparseArray<Drawable> icons = new SparseArray<>();
    Bitmap userBitmap = BitmapFactory.decodeResource(RuntimeEnvironment.application.getResources(), R.drawable.home);
    UserIconDrawable drawable = new UserIconDrawable(100).setIcon(userBitmap).bake();
    icons.put(10, drawable);
    mPrimaryUser.name = TEST_NAME;
    mPrimaryUser.id = 10;
    mController.handleUserIcons(icons);
// Doesn't crash
}
Also used : SparseArray(android.util.SparseArray) Bitmap(android.graphics.Bitmap) Drawable(android.graphics.drawable.Drawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) Test(org.junit.Test)

Example 3 with UserIconDrawable

use of com.android.settingslib.drawable.UserIconDrawable in project android_packages_apps_Settings by LineageOS.

the class UserProfileControllerTest method iconCallbackChangesPreferenceIcon.

@Test
public void iconCallbackChangesPreferenceIcon() throws Exception {
    SparseArray<Drawable> icons = new SparseArray<>();
    Bitmap userBitmap = BitmapFactory.decodeResource(RuntimeEnvironment.application.getResources(), R.drawable.home);
    UserIconDrawable drawable = new UserIconDrawable(100).setIcon(userBitmap).bake();
    icons.put(10, drawable);
    mController.handleUserIcons(icons);
    final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
    verify(mScreen).addPreference(argumentCaptor.capture());
    Preference preference = argumentCaptor.getValue();
    assertThat(preference.getIcon()).isEqualTo(drawable);
}
Also used : SparseArray(android.util.SparseArray) Bitmap(android.graphics.Bitmap) Preference(android.support.v7.preference.Preference) Drawable(android.graphics.drawable.Drawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) Test(org.junit.Test)

Example 4 with UserIconDrawable

use of com.android.settingslib.drawable.UserIconDrawable in project android_packages_apps_Settings by crdroidandroid.

the class SecondaryUserControllerTest method setIcon_doesntNpeOnNullPreference.

@Test
public void setIcon_doesntNpeOnNullPreference() throws Exception {
    SparseArray<Drawable> icons = new SparseArray<>();
    Bitmap userBitmap = BitmapFactory.decodeResource(RuntimeEnvironment.application.getResources(), R.drawable.home);
    UserIconDrawable drawable = new UserIconDrawable(100).setIcon(userBitmap).bake();
    icons.put(10, drawable);
    mPrimaryUser.name = TEST_NAME;
    mPrimaryUser.id = 10;
    mController.handleUserIcons(icons);
// Doesn't crash
}
Also used : SparseArray(android.util.SparseArray) Bitmap(android.graphics.Bitmap) Drawable(android.graphics.drawable.Drawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) Test(org.junit.Test)

Example 5 with UserIconDrawable

use of com.android.settingslib.drawable.UserIconDrawable in project android_packages_apps_Settings by SudaMod.

the class SecondaryUserControllerTest method iconCallbackChangesPreferenceIcon.

@Test
public void iconCallbackChangesPreferenceIcon() throws Exception {
    SparseArray<Drawable> icons = new SparseArray<>();
    Bitmap userBitmap = BitmapFactory.decodeResource(RuntimeEnvironment.application.getResources(), R.drawable.home);
    UserIconDrawable drawable = new UserIconDrawable(100).setIcon(userBitmap).bake();
    icons.put(10, drawable);
    mPrimaryUser.name = TEST_NAME;
    mPrimaryUser.id = 10;
    mController.displayPreference(mScreen);
    mController.handleUserIcons(icons);
    final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
    verify(mGroup).addPreference(argumentCaptor.capture());
    Preference preference = argumentCaptor.getValue();
    assertThat(preference.getIcon()).isEqualTo(drawable);
}
Also used : SparseArray(android.util.SparseArray) Bitmap(android.graphics.Bitmap) Preference(android.support.v7.preference.Preference) Drawable(android.graphics.drawable.Drawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) UserIconDrawable(com.android.settingslib.drawable.UserIconDrawable) Test(org.junit.Test)

Aggregations

Drawable (android.graphics.drawable.Drawable)26 UserIconDrawable (com.android.settingslib.drawable.UserIconDrawable)26 Bitmap (android.graphics.Bitmap)21 SparseArray (android.util.SparseArray)21 Test (org.junit.Test)21 Preference (android.support.v7.preference.Preference)11 Context (android.content.Context)5 PackageManager (android.content.pm.PackageManager)5 UserInfo (android.content.pm.UserInfo)5 Resources (android.content.res.Resources)5 Cursor (android.database.Cursor)5 RemoteException (android.os.RemoteException)5 UserHandle (android.os.UserHandle)5 UserManager (android.os.UserManager)5 Pair (android.util.Pair)5 Preference (androidx.preference.Preference)3