summaryrefslogtreecommitdiff
path: root/packages/SettingsLib/AvatarPicker/src/AvatarPickerActivity.java
blob: de101b1fb2e81ed0b173b07262839669820526c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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);
        }
    }
}