Refactor SettingsLib to remove redundant resources conditionally

Bug: 320878675
Test: manual
Change-Id: I77b3eb4cc2edbbfa6788e53004e370d49da0c0c0
diff --git a/packages/SettingsLib/AvatarPicker/src/AvatarPhotoController.java b/packages/SettingsLib/AvatarPicker/src/AvatarPhotoController.java
new file mode 100644
index 0000000..c20392a
--- /dev/null
+++ b/packages/SettingsLib/AvatarPicker/src/AvatarPhotoController.java
@@ -0,0 +1,400 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.avatarpicker;
+
+import android.app.Activity;
+import android.content.ClipData;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Matrix;
+import android.graphics.Paint;
+import android.graphics.RectF;
+import android.media.ExifInterface;
+import android.net.Uri;
+import android.os.StrictMode;
+import android.provider.MediaStore;
+import android.util.EventLog;
+import android.util.Log;
+
+import androidx.annotation.Nullable;
+import androidx.core.content.FileProvider;
+
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+
+import libcore.io.Streams;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.List;
+
+class AvatarPhotoController {
+
+    interface AvatarUi {
+        boolean isFinishing();
+
+        void returnUriResult(Uri uri);
+
+        void startActivityForResult(Intent intent, int resultCode);
+
+        boolean startSystemActivityForResult(Intent intent, int resultCode);
+
+        int getPhotoSize();
+    }
+
+    interface ContextInjector {
+        File getCacheDir();
+
+        Uri createTempImageUri(File parentDir, String fileName, boolean purge);
+
+        ContentResolver getContentResolver();
+
+        Context getContext();
+    }
+
+    private static final String TAG = "AvatarPhotoController";
+
+    static final int REQUEST_CODE_CHOOSE_PHOTO = 1001;
+    static final int REQUEST_CODE_TAKE_PHOTO = 1002;
+    static final int REQUEST_CODE_CROP_PHOTO = 1003;
+
+    /**
+     * Delay to allow the photo picker exit animation to complete before the crop activity opens.
+     */
+    private static final long DELAY_BEFORE_CROP_MILLIS = 150;
+
+    private static final String IMAGES_DIR = "multi_user";
+    private static final String PRE_CROP_PICTURE_FILE_NAME = "PreCropEditUserPhoto.jpg";
+    private static final String CROP_PICTURE_FILE_NAME = "CropEditUserPhoto.jpg";
+    private static final String TAKE_PICTURE_FILE_NAME = "TakeEditUserPhoto.jpg";
+
+    private final int mPhotoSize;
+
+    private final AvatarUi mAvatarUi;
+    private final ContextInjector mContextInjector;
+
+    private final File mImagesDir;
+    private final Uri mPreCropPictureUri;
+    private final Uri mCropPictureUri;
+    private final Uri mTakePictureUri;
+
+    AvatarPhotoController(AvatarUi avatarUi, ContextInjector contextInjector, boolean waiting) {
+        mAvatarUi = avatarUi;
+        mContextInjector = contextInjector;
+
+        mImagesDir = new File(mContextInjector.getCacheDir(), IMAGES_DIR);
+        mImagesDir.mkdir();
+        mPreCropPictureUri = mContextInjector
+                .createTempImageUri(mImagesDir, PRE_CROP_PICTURE_FILE_NAME, !waiting);
+        mCropPictureUri =
+                mContextInjector.createTempImageUri(mImagesDir, CROP_PICTURE_FILE_NAME, !waiting);
+        mTakePictureUri =
+                mContextInjector.createTempImageUri(mImagesDir, TAKE_PICTURE_FILE_NAME, !waiting);
+        mPhotoSize = mAvatarUi.getPhotoSize();
+    }
+
+    /**
+     * Handles activity result from containing activity/fragment after a take/choose/crop photo
+     * action result is received.
+     */
+    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
+        if (resultCode != Activity.RESULT_OK) {
+            return false;
+        }
+        final Uri pictureUri = data != null && data.getData() != null
+                ? data.getData() : mTakePictureUri;
+
+        // Check if the result is a content uri
+        if (!ContentResolver.SCHEME_CONTENT.equals(pictureUri.getScheme())) {
+            Log.e(TAG, "Invalid pictureUri scheme: " + pictureUri.getScheme());
+            EventLog.writeEvent(0x534e4554, "172939189", -1, pictureUri.getPath());
+            return false;
+        }
+
+        switch (requestCode) {
+            case REQUEST_CODE_CROP_PHOTO:
+                mAvatarUi.returnUriResult(pictureUri);
+                return true;
+            case REQUEST_CODE_TAKE_PHOTO:
+                if (mTakePictureUri.equals(pictureUri)) {
+                    cropPhoto(pictureUri);
+                } else {
+                    copyAndCropPhoto(pictureUri, false);
+                }
+                return true;
+            case REQUEST_CODE_CHOOSE_PHOTO:
+                copyAndCropPhoto(pictureUri, true);
+                return true;
+        }
+        return false;
+    }
+
+    void takePhoto() {
+        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
+        appendOutputExtra(intent, mTakePictureUri);
+        mAvatarUi.startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
+    }
+
+    void choosePhoto() {
+        Intent intent = new Intent(MediaStore.ACTION_PICK_IMAGES, null);
+        intent.setType("image/*");
+        mAvatarUi.startActivityForResult(intent, REQUEST_CODE_CHOOSE_PHOTO);
+    }
+
+    private void copyAndCropPhoto(final Uri pictureUri, boolean delayBeforeCrop) {
+        ListenableFuture<Uri> future = ThreadUtils.getBackgroundExecutor().submit(() -> {
+            final ContentResolver cr = mContextInjector.getContentResolver();
+            try {
+                InputStream in = cr.openInputStream(pictureUri);
+                OutputStream out = cr.openOutputStream(mPreCropPictureUri);
+                Streams.copy(in, out);
+                return mPreCropPictureUri;
+            } catch (IOException e) {
+                Log.w(TAG, "Failed to copy photo", e);
+                return null;
+            }
+        });
+        Futures.addCallback(future, new FutureCallback<>() {
+            @Override
+            public void onSuccess(@Nullable Uri result) {
+                if (result == null) {
+                    return;
+                }
+                Runnable cropRunnable = () -> {
+                    if (!mAvatarUi.isFinishing()) {
+                        cropPhoto(mPreCropPictureUri);
+                    }
+                };
+                if (delayBeforeCrop) {
+                    mContextInjector.getContext().getMainThreadHandler()
+                            .postDelayed(cropRunnable, DELAY_BEFORE_CROP_MILLIS);
+                } else {
+                    cropRunnable.run();
+                }
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
+                Log.e(TAG, "Error performing copy-and-crop", t);
+            }
+        }, mContextInjector.getContext().getMainExecutor());
+    }
+
+    private void cropPhoto(final Uri pictureUri) {
+        // TODO: Use a public intent, when there is one.
+        Intent intent = new Intent("com.android.camera.action.CROP");
+        intent.setDataAndType(pictureUri, "image/*");
+        appendOutputExtra(intent, mCropPictureUri);
+        appendCropExtras(intent);
+        try {
+            StrictMode.disableDeathOnFileUriExposure();
+            if (mAvatarUi.startSystemActivityForResult(intent, REQUEST_CODE_CROP_PHOTO)) {
+                return;
+            }
+        } finally {
+            StrictMode.enableDeathOnFileUriExposure();
+        }
+        onPhotoNotCropped(pictureUri);
+    }
+
+    private void appendOutputExtra(Intent intent, Uri pictureUri) {
+        intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
+        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
+                | Intent.FLAG_GRANT_READ_URI_PERMISSION);
+        intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, pictureUri));
+    }
+
+    private void appendCropExtras(Intent intent) {
+        intent.putExtra("crop", "true");
+        intent.putExtra("scale", true);
+        intent.putExtra("scaleUpIfNeeded", true);
+        intent.putExtra("aspectX", 1);
+        intent.putExtra("aspectY", 1);
+        intent.putExtra("outputX", mPhotoSize);
+        intent.putExtra("outputY", mPhotoSize);
+    }
+
+    private void onPhotoNotCropped(final Uri data) {
+        ListenableFuture<Bitmap> future = ThreadUtils.getBackgroundExecutor().submit(() -> {
+            // Scale and crop to a square aspect ratio
+            Bitmap croppedImage = Bitmap.createBitmap(mPhotoSize, mPhotoSize,
+                    Bitmap.Config.ARGB_8888);
+            Canvas canvas = new Canvas(croppedImage);
+            Bitmap fullImage;
+            try (InputStream imageStream = mContextInjector.getContentResolver()
+                    .openInputStream(data)) {
+                fullImage = BitmapFactory.decodeStream(imageStream);
+            }
+            if (fullImage == null) {
+                Log.e(TAG, "Image data could not be decoded");
+                return null;
+            }
+            int rotation = getRotation(data);
+            final int squareSize = Math.min(fullImage.getWidth(),
+                    fullImage.getHeight());
+            final int left = (fullImage.getWidth() - squareSize) / 2;
+            final int top = (fullImage.getHeight() - squareSize) / 2;
+
+            Matrix matrix = new Matrix();
+            RectF rectSource = new RectF(left, top,
+                    left + squareSize, top + squareSize);
+            RectF rectDest = new RectF(0, 0, mPhotoSize, mPhotoSize);
+            matrix.setRectToRect(rectSource, rectDest, Matrix.ScaleToFit.CENTER);
+            matrix.postRotate(rotation, mPhotoSize / 2f, mPhotoSize / 2f);
+            canvas.drawBitmap(fullImage, matrix, new Paint());
+            saveBitmapToFile(croppedImage, new File(mImagesDir, CROP_PICTURE_FILE_NAME));
+            return croppedImage;
+        });
+        Futures.addCallback(future, new FutureCallback<>() {
+            @Override
+            public void onSuccess(@Nullable Bitmap result) {
+                if (result != null) {
+                    mAvatarUi.returnUriResult(mCropPictureUri);
+                }
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
+                Log.e(TAG, "Error performing internal crop", t);
+            }
+        }, mContextInjector.getContext().getMainExecutor());
+    }
+
+    /**
+     * Reads the image's exif data and determines the rotation degree needed to display the image
+     * in portrait mode.
+     */
+    private int getRotation(Uri selectedImage) {
+        int rotation = -1;
+        try {
+            InputStream imageStream =
+                    mContextInjector.getContentResolver().openInputStream(selectedImage);
+            ExifInterface exif = new ExifInterface(imageStream);
+            rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
+        } catch (IOException exception) {
+            Log.e(TAG, "Error while getting rotation", exception);
+        }
+
+        switch (rotation) {
+            case ExifInterface.ORIENTATION_ROTATE_90:
+                return 90;
+            case ExifInterface.ORIENTATION_ROTATE_180:
+                return 180;
+            case ExifInterface.ORIENTATION_ROTATE_270:
+                return 270;
+            default:
+                return 0;
+        }
+    }
+
+    private void saveBitmapToFile(Bitmap bitmap, File file) {
+        try {
+            OutputStream os = new FileOutputStream(file);
+            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
+            os.flush();
+            os.close();
+        } catch (IOException e) {
+            Log.e(TAG, "Cannot create temp file", e);
+        }
+    }
+
+    static class AvatarUiImpl implements AvatarUi {
+        private final AvatarPickerActivity mActivity;
+
+        AvatarUiImpl(AvatarPickerActivity activity) {
+            mActivity = activity;
+        }
+
+        @Override
+        public boolean isFinishing() {
+            return mActivity.isFinishing() || mActivity.isDestroyed();
+        }
+
+        @Override
+        public void returnUriResult(Uri uri) {
+            mActivity.returnUriResult(uri);
+        }
+
+        @Override
+        public void startActivityForResult(Intent intent, int resultCode) {
+            mActivity.startActivityForResult(intent, resultCode);
+        }
+
+        @Override
+        public boolean startSystemActivityForResult(Intent intent, int code) {
+            List<ResolveInfo> resolveInfos = mActivity.getPackageManager()
+                    .queryIntentActivities(intent, PackageManager.MATCH_SYSTEM_ONLY);
+            if (resolveInfos.isEmpty()) {
+                Log.w(TAG, "No system package activity could be found for code " + code);
+                return false;
+            }
+            intent.setPackage(resolveInfos.get(0).activityInfo.packageName);
+            mActivity.startActivityForResult(intent, code);
+            return true;
+        }
+
+        @Override
+        public int getPhotoSize() {
+            return mActivity.getResources()
+                    .getDimensionPixelSize(com.android.internal.R.dimen.user_icon_size);
+        }
+    }
+
+    static class ContextInjectorImpl implements ContextInjector {
+        private final Context mContext;
+        private final String mFileAuthority;
+
+        ContextInjectorImpl(Context context, String fileAuthority) {
+            mContext = context;
+            mFileAuthority = fileAuthority;
+        }
+
+        @Override
+        public File getCacheDir() {
+            return mContext.getCacheDir();
+        }
+
+        @Override
+        public Uri createTempImageUri(File parentDir, String fileName, boolean purge) {
+            final File fullPath = new File(parentDir, fileName);
+            if (purge) {
+                fullPath.delete();
+            }
+            return FileProvider.getUriForFile(mContext, mFileAuthority, fullPath);
+        }
+
+        @Override
+        public ContentResolver getContentResolver() {
+            return mContext.getContentResolver();
+        }
+
+        @Override
+        public Context getContext() {
+            return mContext;
+        }
+    }
+}
diff --git a/packages/SettingsLib/AvatarPicker/src/AvatarPickerActivity.java b/packages/SettingsLib/AvatarPicker/src/AvatarPickerActivity.java
new file mode 100644
index 0000000..de101b1
--- /dev/null
+++ b/packages/SettingsLib/AvatarPicker/src/AvatarPickerActivity.java
@@ -0,0 +1,391 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.avatarpicker;
+
+import android.app.Activity;
+import android.content.ContentResolver;
+import android.content.Intent;
+import android.content.res.TypedArray;
+import android.graphics.Bitmap;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.net.Uri;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+
+import androidx.annotation.NonNull;
+import androidx.core.graphics.drawable.RoundedBitmapDrawable;
+import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
+import androidx.recyclerview.widget.GridLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+
+import com.android.internal.util.UserIcons;
+
+import com.google.android.setupcompat.template.FooterBarMixin;
+import com.google.android.setupcompat.template.FooterButton;
+import com.google.android.setupdesign.GlifLayout;
+import com.google.android.setupdesign.util.ThemeHelper;
+import com.google.android.setupdesign.util.ThemeResolver;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Activity to allow the user to choose a user profile picture.
+ *
+ * <p>Options are provided to take a photo or choose a photo using the photo picker. In addition,
+ * preselected avatar images may be provided in the resource array {@code avatar_images}. If
+ * provided, every element of that array must be a bitmap drawable.
+ *
+ * <p>If preselected images are not provided, the default avatar will be shown instead, in a range
+ * of colors.
+ *
+ * <p>This activity should be started with startActivityForResult. If a photo or a preselected image
+ * is selected, a Uri will be returned in the data field of the result intent. If a colored default
+ * avatar is selected, the chosen color will be returned as {@code EXTRA_DEFAULT_ICON_TINT_COLOR}
+ * and the data field will be empty.
+ */
+public class AvatarPickerActivity extends Activity {
+
+    static final String EXTRA_FILE_AUTHORITY = "file_authority";
+    static final String EXTRA_DEFAULT_ICON_TINT_COLOR = "default_icon_tint_color";
+
+    private static final String KEY_AWAITING_RESULT = "awaiting_result";
+    private static final String KEY_SELECTED_POSITION = "selected_position";
+
+    private boolean mWaitingForActivityResult;
+
+    private FooterButton mDoneButton;
+    private AvatarAdapter mAdapter;
+
+    private AvatarPhotoController mAvatarPhotoController;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        boolean dayNightEnabled = ThemeHelper.isSetupWizardDayNightEnabled(this);
+        ThemeResolver themeResolver =
+                new ThemeResolver.Builder(ThemeResolver.getDefault())
+                        .setDefaultTheme(ThemeHelper.getSuwDefaultTheme(this))
+                        .setUseDayNight(true)
+                        .build();
+        int themeResId = themeResolver.resolve("", /* suppressDayNight= */ !dayNightEnabled);
+        setTheme(themeResId);
+        ThemeHelper.trySetDynamicColor(this);
+        setContentView(R.layout.avatar_picker);
+        setUpButtons();
+
+        RecyclerView recyclerView = findViewById(R.id.avatar_grid);
+        mAdapter = new AvatarAdapter();
+        recyclerView.setAdapter(mAdapter);
+        recyclerView.setLayoutManager(new GridLayoutManager(this,
+                getResources().getInteger(R.integer.avatar_picker_columns)));
+
+        restoreState(savedInstanceState);
+
+        mAvatarPhotoController = new AvatarPhotoController(
+                new AvatarPhotoController.AvatarUiImpl(this),
+                new AvatarPhotoController.ContextInjectorImpl(this, getFileAuthority()),
+                mWaitingForActivityResult);
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        mAdapter.onAdapterResume();
+    }
+
+    private void setUpButtons() {
+        GlifLayout glifLayout = findViewById(R.id.glif_layout);
+        FooterBarMixin mixin = glifLayout.getMixin(FooterBarMixin.class);
+
+        FooterButton secondaryButton =
+                new FooterButton.Builder(this)
+                        .setText(getString(android.R.string.cancel))
+                        .setListener(view -> cancel())
+                        .build();
+
+        mDoneButton =
+                new FooterButton.Builder(this)
+                        .setText(getString(R.string.done))
+                        .setListener(view -> mAdapter.returnSelectionResult())
+                        .build();
+        mDoneButton.setEnabled(false);
+
+        mixin.setSecondaryButton(secondaryButton);
+        mixin.setPrimaryButton(mDoneButton);
+    }
+
+    private String getFileAuthority() {
+        String authority = getIntent().getStringExtra(EXTRA_FILE_AUTHORITY);
+        if (authority == null) {
+            Log.e(this.getClass().getName(), "File authority must be provided");
+            finish();
+        }
+        return authority;
+    }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        mWaitingForActivityResult = false;
+        mAvatarPhotoController.onActivityResult(requestCode, resultCode, data);
+    }
+
+    @Override
+    protected void onSaveInstanceState(@NonNull Bundle outState) {
+        outState.putBoolean(KEY_AWAITING_RESULT, mWaitingForActivityResult);
+        outState.putInt(KEY_SELECTED_POSITION, mAdapter.mSelectedPosition);
+        super.onSaveInstanceState(outState);
+    }
+
+    private void restoreState(Bundle savedInstanceState) {
+        if (savedInstanceState != null) {
+            mWaitingForActivityResult = savedInstanceState.getBoolean(KEY_AWAITING_RESULT, false);
+            mAdapter.mSelectedPosition =
+                    savedInstanceState.getInt(KEY_SELECTED_POSITION, AvatarAdapter.NONE);
+            mDoneButton.setEnabled(mAdapter.mSelectedPosition != AvatarAdapter.NONE);
+        }
+    }
+
+    @Override
+    public void startActivityForResult(Intent intent, int requestCode) {
+        mWaitingForActivityResult = true;
+        super.startActivityForResult(intent, requestCode);
+    }
+
+    void returnUriResult(Uri uri) {
+        Intent resultData = new Intent();
+        resultData.setData(uri);
+        setResult(RESULT_OK, resultData);
+        finish();
+    }
+
+    void returnColorResult(int color) {
+        Intent resultData = new Intent();
+        resultData.putExtra(EXTRA_DEFAULT_ICON_TINT_COLOR, color);
+        setResult(RESULT_OK, resultData);
+        finish();
+    }
+
+    private void cancel() {
+        setResult(RESULT_CANCELED);
+        finish();
+    }
+
+    private class AvatarAdapter extends RecyclerView.Adapter<AvatarViewHolder> {
+
+        private static final int NONE = -1;
+
+        private final int mTakePhotoPosition;
+        private final int mChoosePhotoPosition;
+        private final int mPreselectedImageStartPosition;
+
+        private final List<Drawable> mImageDrawables;
+        private final List<String> mImageDescriptions;
+        private final TypedArray mPreselectedImages;
+        private final int[] mUserIconColors;
+        private int mSelectedPosition = NONE;
+
+        private int mLastSelectedPosition = NONE;
+
+        AvatarAdapter() {
+            final boolean canTakePhoto =
+                    PhotoCapabilityUtils.canTakePhoto(AvatarPickerActivity.this);
+            final boolean canChoosePhoto =
+                    PhotoCapabilityUtils.canChoosePhoto(AvatarPickerActivity.this);
+            mTakePhotoPosition = (canTakePhoto ? 0 : NONE);
+            mChoosePhotoPosition = (canChoosePhoto ? (canTakePhoto ? 1 : 0) : NONE);
+            mPreselectedImageStartPosition = (canTakePhoto ? 1 : 0) + (canChoosePhoto ? 1 : 0);
+
+            mPreselectedImages = getResources().obtainTypedArray(R.array.avatar_images);
+            mUserIconColors = UserIcons.getUserIconColors(getResources());
+            mImageDrawables = buildDrawableList();
+            mImageDescriptions = buildDescriptionsList();
+        }
+
+        @NonNull
+        @Override
+        public AvatarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
+            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
+            View itemView = layoutInflater.inflate(R.layout.avatar_item, parent, false);
+            return new AvatarViewHolder(itemView);
+        }
+
+        @Override
+        public void onBindViewHolder(@NonNull AvatarViewHolder viewHolder, int position) {
+            if (position == mTakePhotoPosition) {
+                viewHolder.setDrawable(getDrawable(R.drawable.avatar_take_photo_circled));
+                viewHolder.setContentDescription(getString(R.string.user_image_take_photo));
+
+            } else if (position == mChoosePhotoPosition) {
+                viewHolder.setDrawable(getDrawable(R.drawable.avatar_choose_photo_circled));
+                viewHolder.setContentDescription(getString(R.string.user_image_choose_photo));
+
+            } else if (position >= mPreselectedImageStartPosition) {
+                int index = indexFromPosition(position);
+                viewHolder.setSelected(position == mSelectedPosition);
+                viewHolder.setDrawable(mImageDrawables.get(index));
+                if (mImageDescriptions != null && index < mImageDescriptions.size()) {
+                    viewHolder.setContentDescription(mImageDescriptions.get(index));
+                } else {
+                    viewHolder.setContentDescription(getString(
+                            R.string.default_user_icon_description));
+                }
+            }
+            viewHolder.setClickListener(view -> onViewHolderSelected(position));
+        }
+
+        private void onViewHolderSelected(int position) {
+            if ((mTakePhotoPosition == position) && (mLastSelectedPosition != position)) {
+                mAvatarPhotoController.takePhoto();
+            } else if ((mChoosePhotoPosition == position) && (mLastSelectedPosition != position)) {
+                mAvatarPhotoController.choosePhoto();
+            } else {
+                if (mSelectedPosition == position) {
+                    deselect(position);
+                } else {
+                    select(position);
+                }
+            }
+            mLastSelectedPosition = position;
+        }
+
+        public void onAdapterResume() {
+            mLastSelectedPosition = NONE;
+        }
+
+        @Override
+        public int getItemCount() {
+            return mPreselectedImageStartPosition + mImageDrawables.size();
+        }
+
+        private List<Drawable> buildDrawableList() {
+            List<Drawable> result = new ArrayList<>();
+
+            for (int i = 0; i < mPreselectedImages.length(); i++) {
+                Drawable drawable = mPreselectedImages.getDrawable(i);
+                if (drawable instanceof BitmapDrawable) {
+                    result.add(circularDrawableFrom((BitmapDrawable) drawable));
+                } else {
+                    throw new IllegalStateException("Avatar drawables must be bitmaps");
+                }
+            }
+            if (!result.isEmpty()) {
+                return result;
+            }
+
+            // No preselected images. Use tinted default icon.
+            for (int i = 0; i < mUserIconColors.length; i++) {
+                result.add(UserIcons.getDefaultUserIconInColor(getResources(), mUserIconColors[i]));
+            }
+            return result;
+        }
+
+        private List<String> buildDescriptionsList() {
+            if (mPreselectedImages.length() > 0) {
+                return Arrays.asList(
+                        getResources().getStringArray(R.array.avatar_image_descriptions));
+            }
+
+            return null;
+        }
+
+        private Drawable circularDrawableFrom(BitmapDrawable drawable) {
+            Bitmap bitmap = drawable.getBitmap();
+
+            RoundedBitmapDrawable roundedBitmapDrawable =
+                    RoundedBitmapDrawableFactory.create(getResources(), bitmap);
+            roundedBitmapDrawable.setCircular(true);
+
+            return roundedBitmapDrawable;
+        }
+
+        private int indexFromPosition(int position) {
+            return position - mPreselectedImageStartPosition;
+        }
+
+        private void select(int position) {
+            final int oldSelection = mSelectedPosition;
+            mSelectedPosition = position;
+            notifyItemChanged(position);
+            if (oldSelection != NONE) {
+                notifyItemChanged(oldSelection);
+            } else {
+                mDoneButton.setEnabled(true);
+            }
+        }
+
+        private void deselect(int position) {
+            mSelectedPosition = NONE;
+            notifyItemChanged(position);
+            mDoneButton.setEnabled(false);
+        }
+
+        private void returnSelectionResult() {
+            int index = indexFromPosition(mSelectedPosition);
+            if (mPreselectedImages.length() > 0) {
+                int resourceId = mPreselectedImages.getResourceId(index, -1);
+                if (resourceId == -1) {
+                    throw new IllegalStateException("Preselected avatar images must be resources.");
+                }
+                returnUriResult(uriForResourceId(resourceId));
+            } else {
+                returnColorResult(
+                        mUserIconColors[index]);
+            }
+        }
+
+        private Uri uriForResourceId(int resourceId) {
+            return new Uri.Builder()
+                    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
+                    .authority(getResources().getResourcePackageName(resourceId))
+                    .appendPath(getResources().getResourceTypeName(resourceId))
+                    .appendPath(getResources().getResourceEntryName(resourceId))
+                    .build();
+        }
+    }
+
+    private static class AvatarViewHolder extends RecyclerView.ViewHolder {
+        private final ImageView mImageView;
+
+        AvatarViewHolder(View view) {
+            super(view);
+            mImageView = view.findViewById(R.id.avatar_image);
+        }
+
+        public void setDrawable(Drawable drawable) {
+            mImageView.setImageDrawable(drawable);
+        }
+
+        public void setContentDescription(String desc) {
+            mImageView.setContentDescription(desc);
+        }
+
+        public void setClickListener(View.OnClickListener listener) {
+            mImageView.setOnClickListener(listener);
+        }
+
+        public void setSelected(boolean selected) {
+            mImageView.setSelected(selected);
+        }
+    }
+}
diff --git a/packages/SettingsLib/AvatarPicker/src/PhotoCapabilityUtils.java b/packages/SettingsLib/AvatarPicker/src/PhotoCapabilityUtils.java
new file mode 100644
index 0000000..43cb0f5
--- /dev/null
+++ b/packages/SettingsLib/AvatarPicker/src/PhotoCapabilityUtils.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.avatarpicker;
+
+import android.app.KeyguardManager;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.provider.MediaStore;
+
+/**
+ * Utility class that contains helper methods to determine if the current user has permission and
+ * the device is in a proper state to start an activity for a given action.
+ */
+public class PhotoCapabilityUtils {
+
+    /**
+     * Check if the current user can perform any activity for
+     * android.media.action.IMAGE_CAPTURE action.
+     */
+    public static boolean canTakePhoto(Context context) {
+        return context.getPackageManager().queryIntentActivities(
+                new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
+                PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
+    }
+
+    /**
+     * Check if the current user can perform any activity for
+     * ACTION_PICK_IMAGES action for images.
+     * Returns false if the device is currently locked and
+     * requires a PIN, pattern or password to unlock.
+     */
+    public static boolean canChoosePhoto(Context context) {
+        Intent intent = new Intent(MediaStore.ACTION_PICK_IMAGES);
+        intent.setType("image/*");
+        boolean canPerformActivityForGetImage =
+                context.getPackageManager().queryIntentActivities(intent, 0).size() > 0;
+        // on locked device we can't access the images
+        return canPerformActivityForGetImage && !isDeviceLocked(context);
+    }
+
+    /**
+     * Check if the current user can perform any activity for
+     * com.android.camera.action.CROP action for images.
+     * Returns false if the device is currently locked and
+     * requires a PIN, pattern or password to unlock.
+     */
+    public static boolean canCropPhoto(Context context) {
+        Intent intent = new Intent("com.android.camera.action.CROP");
+        intent.setType("image/*");
+        boolean canPerformActivityForCropping =
+                context.getPackageManager().queryIntentActivities(intent, 0).size() > 0;
+        // on locked device we can't start a cropping activity
+        return canPerformActivityForCropping && !isDeviceLocked(context);
+    }
+
+    private static boolean isDeviceLocked(Context context) {
+        KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
+        return keyguardManager == null || keyguardManager.isDeviceLocked();
+    }
+
+}
diff --git a/packages/SettingsLib/AvatarPicker/src/ThreadUtils.java b/packages/SettingsLib/AvatarPicker/src/ThreadUtils.java
new file mode 100644
index 0000000..dc19e66
--- /dev/null
+++ b/packages/SettingsLib/AvatarPicker/src/ThreadUtils.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.avatarpicker;
+
+import android.os.Handler;
+import android.os.Looper;
+
+import androidx.annotation.NonNull;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executors;
+
+// copied from SettinsLib/utils
+public class ThreadUtils {
+
+    private static volatile Thread sMainThread;
+    private static volatile Handler sMainThreadHandler;
+    private static volatile ListeningExecutorService sListeningService;
+
+    /**
+     * Returns true if the current thread is the UI thread.
+     */
+    public static boolean isMainThread() {
+        if (sMainThread == null) {
+            sMainThread = Looper.getMainLooper().getThread();
+        }
+        return Thread.currentThread() == sMainThread;
+    }
+
+    /**
+     * Returns a shared UI thread handler.
+     */
+    @NonNull
+    public static Handler getUiThreadHandler() {
+        if (sMainThreadHandler == null) {
+            sMainThreadHandler = new Handler(Looper.getMainLooper());
+        }
+
+        return sMainThreadHandler;
+    }
+
+    /**
+     * Checks that the current thread is the UI thread. Otherwise throws an exception.
+     */
+    public static void ensureMainThread() {
+        if (!isMainThread()) {
+            throw new RuntimeException("Must be called on the UI thread");
+        }
+    }
+
+    /**
+     * Posts runnable in background using shared background thread pool.
+     *
+     * @return A future of the task that can be monitored for updates or cancelled.
+     */
+    @SuppressWarnings("rawtypes")
+    @NonNull
+    public static ListenableFuture postOnBackgroundThread(@NonNull Runnable runnable) {
+        return getBackgroundExecutor().submit(runnable);
+    }
+
+    /**
+     * Posts callable in background using shared background thread pool.
+     *
+     * @return A future of the task that can be monitored for updates or cancelled.
+     */
+    @NonNull
+    public static <T> ListenableFuture<T> postOnBackgroundThread(@NonNull Callable<T> callable) {
+        return getBackgroundExecutor().submit(callable);
+    }
+
+    /**
+     * Posts the runnable on the main thread.
+     *
+     * @deprecated moving work to the main thread should be done via the main executor provided to
+     * {@link com.google.common.util.concurrent.FutureCallback} via
+     * {@link android.content.Context#getMainExecutor()} or by calling an SDK method such as
+     * {@link android.app.Activity#runOnUiThread(Runnable)} or
+     * {@link android.content.Context#getMainThreadHandler()} where appropriate.
+     */
+    @Deprecated
+    public static void postOnMainThread(@NonNull Runnable runnable) {
+        getUiThreadHandler().post(runnable);
+    }
+
+    /**
+     * Provides a shared {@link ListeningExecutorService} created using a fixed thread pool executor
+     */
+    @NonNull
+    public static synchronized ListeningExecutorService getBackgroundExecutor() {
+        if (sListeningService == null) {
+            sListeningService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(
+                    Runtime.getRuntime().availableProcessors()));
+        }
+        return sListeningService;
+    }
+}