1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.emergency.preferences;
18 
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.content.ClipData;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.database.Cursor;
27 import android.graphics.Bitmap;
28 import android.graphics.Bitmap.Config;
29 import android.graphics.BitmapFactory;
30 import android.graphics.Canvas;
31 import android.graphics.Paint;
32 import android.graphics.Rect;
33 import android.graphics.drawable.Drawable;
34 import android.net.Uri;
35 import android.os.AsyncTask;
36 import android.os.UserHandle;
37 import android.os.UserManager;
38 import android.provider.ContactsContract.DisplayPhoto;
39 import android.provider.MediaStore;
40 import android.util.Log;
41 import android.view.Gravity;
42 import android.view.View;
43 import android.view.View.OnClickListener;
44 import android.view.ViewGroup;
45 import android.widget.AdapterView;
46 import android.widget.ArrayAdapter;
47 import android.widget.ImageView;
48 import android.widget.ListPopupWindow;
49 import android.widget.TextView;
50 
51 import androidx.core.content.FileProvider;
52 
53 import com.android.emergency.CircleFramedDrawable;
54 import com.android.emergency.R;
55 import com.android.settingslib.RestrictedLockUtilsInternal;
56 
57 import libcore.io.Streams;
58 
59 import java.io.File;
60 import java.io.FileNotFoundException;
61 import java.io.FileOutputStream;
62 import java.io.IOException;
63 import java.io.InputStream;
64 import java.io.OutputStream;
65 import java.util.ArrayList;
66 import java.util.List;
67 
68 public class EditUserPhotoController {
69     private static final String TAG = "EditUserPhotoController";
70 
71     // It seems that this class generates custom request codes and they may
72     // collide with ours, these values are very unlikely to have a conflict.
73     private static final int REQUEST_CODE_CHOOSE_PHOTO = 10001;
74     private static final int REQUEST_CODE_TAKE_PHOTO = 10002;
75     private static final int REQUEST_CODE_CROP_PHOTO = 10003;
76 
77     private static final String CROP_PICTURE_FILE_NAME = "CropEditUserPhoto.jpg";
78     private static final String TAKE_PICTURE_FILE_NAME = "TakeEditUserPhoto2.jpg";
79     private static final String NEW_USER_PHOTO_FILE_NAME = "NewUserPhoto.png";
80     private static final String ACTION_CROP = "com.android.camera.action.CROP";
81     private static final String FILE_PATH = "com.android.emergency.files";
82 
83     private final int mPhotoSize;
84 
85     private final Context mContext;
86     private final Fragment mFragment;
87     private final ImageView mImageView;
88 
89     private final Uri mCropPictureUri;
90     private final Uri mTakePictureUri;
91 
92     private Bitmap mNewUserPhotoBitmap;
93     private Drawable mNewUserPhotoDrawable;
94 
EditUserPhotoController(Fragment fragment, ImageView view, Bitmap bitmap, Drawable drawable, boolean waiting)95     public EditUserPhotoController(Fragment fragment, ImageView view, Bitmap bitmap,
96             Drawable drawable, boolean waiting) {
97         mContext = view.getContext();
98         mFragment = fragment;
99         mImageView = view;
100         mCropPictureUri = createTempImageUri(mContext, CROP_PICTURE_FILE_NAME, !waiting);
101         mTakePictureUri = createTempImageUri(mContext, TAKE_PICTURE_FILE_NAME, !waiting);
102         mPhotoSize = getPhotoSize(mContext);
103         mImageView.setOnClickListener(new OnClickListener() {
104             @Override
105             public void onClick(View v) {
106                 showUpdatePhotoPopup();
107             }
108         });
109         mNewUserPhotoBitmap = bitmap;
110         mNewUserPhotoDrawable = drawable;
111     }
112 
onActivityResult(int requestCode, int resultCode, Intent data)113     public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
114         if (resultCode != Activity.RESULT_OK) {
115             return false;
116         }
117         final Uri pictureUri = data != null && data.getData() != null
118                 ? data.getData() : mTakePictureUri;
119         switch (requestCode) {
120             case REQUEST_CODE_CROP_PHOTO:
121                 onPhotoCropped(pictureUri, true);
122                 return true;
123             case REQUEST_CODE_TAKE_PHOTO:
124             case REQUEST_CODE_CHOOSE_PHOTO:
125                 if (mTakePictureUri.equals(pictureUri)) {
126                     cropPhoto();
127                 } else {
128                     copyAndCropPhoto(pictureUri);
129                 }
130                 return true;
131         }
132         return false;
133     }
134 
getNewUserPhotoBitmap()135     public Bitmap getNewUserPhotoBitmap() {
136         return mNewUserPhotoBitmap;
137     }
138 
getNewUserPhotoDrawable()139     public Drawable getNewUserPhotoDrawable() {
140         return mNewUserPhotoDrawable;
141     }
142 
showUpdatePhotoPopup()143     private void showUpdatePhotoPopup() {
144         final boolean canTakePhoto = canTakePhoto();
145         final boolean canChoosePhoto = canChoosePhoto();
146 
147         if (!canTakePhoto && !canChoosePhoto) {
148             return;
149         }
150 
151         final Context context = mImageView.getContext();
152         final List<EditUserPhotoController.RestrictedMenuItem> items = new ArrayList<>();
153 
154         if (canTakePhoto) {
155             final String title = context.getString(R.string.user_image_take_photo);
156             final Runnable action = new Runnable() {
157                 @Override
158                 public void run() {
159                     takePhoto();
160                 }
161             };
162             items.add(new RestrictedMenuItem(context, title, UserManager.DISALLOW_SET_USER_ICON,
163                     action));
164         }
165 
166         if (canChoosePhoto) {
167             final String title = context.getString(R.string.user_image_choose_photo);
168             final Runnable action = new Runnable() {
169                 @Override
170                 public void run() {
171                     choosePhoto();
172                 }
173             };
174             items.add(new RestrictedMenuItem(context, title, UserManager.DISALLOW_SET_USER_ICON,
175                     action));
176         }
177 
178         final ListPopupWindow listPopupWindow = new ListPopupWindow(context);
179 
180         listPopupWindow.setAnchorView(mImageView);
181         listPopupWindow.setModal(true);
182         listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
183         listPopupWindow.setAdapter(new RestrictedPopupMenuAdapter(context, items));
184 
185         final int width = Math.max(mImageView.getWidth(), context.getResources()
186                 .getDimensionPixelSize(R.dimen.update_user_photo_popup_min_width));
187         listPopupWindow.setWidth(width);
188         listPopupWindow.setDropDownGravity(Gravity.START);
189 
190         listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
191             @Override
192             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
193                 listPopupWindow.dismiss();
194                 final RestrictedMenuItem item =
195                         (RestrictedMenuItem) parent.getAdapter().getItem(position);
196                 item.doAction();
197             }
198         });
199 
200         listPopupWindow.show();
201     }
202 
canTakePhoto()203     private boolean canTakePhoto() {
204         return mImageView.getContext().getPackageManager().queryIntentActivities(
205                 new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
206                 PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
207     }
208 
canChoosePhoto()209     private boolean canChoosePhoto() {
210         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
211         intent.setType("image/*");
212         return mImageView.getContext().getPackageManager().queryIntentActivities(
213                 intent, 0).size() > 0;
214     }
215 
takePhoto()216     private void takePhoto() {
217         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
218         appendOutputExtra(intent, mTakePictureUri);
219         mFragment.startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
220     }
221 
choosePhoto()222     private void choosePhoto() {
223         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
224         intent.setType("image/*");
225         appendOutputExtra(intent, mTakePictureUri);
226         mFragment.startActivityForResult(intent, REQUEST_CODE_CHOOSE_PHOTO);
227     }
228 
copyAndCropPhoto(final Uri pictureUri)229     private void copyAndCropPhoto(final Uri pictureUri) {
230         new AsyncTask<Void, Void, Void>() {
231             @Override
232             protected Void doInBackground(Void... params) {
233                 final ContentResolver cr = mContext.getContentResolver();
234                 try (InputStream in = cr.openInputStream(pictureUri);
235                         OutputStream out = cr.openOutputStream(mTakePictureUri)) {
236                     Streams.copy(in, out);
237                 } catch (IOException e) {
238                     Log.w(TAG, "Failed to copy photo", e);
239                 }
240                 return null;
241             }
242 
243             @Override
244             protected void onPostExecute(Void result) {
245                 if (!mFragment.isAdded()) return;
246                 cropPhoto();
247             }
248         }.execute();
249     }
250 
cropPhoto()251     private void cropPhoto() {
252         Intent intent = new Intent(ACTION_CROP);
253         intent.setDataAndType(mTakePictureUri, "image/*");
254         appendOutputExtra(intent, mCropPictureUri);
255         appendCropExtras(intent);
256         if (intent.resolveActivity(mContext.getPackageManager()) != null) {
257             mFragment.startActivityForResult(intent, REQUEST_CODE_CROP_PHOTO);
258         } else {
259             onPhotoCropped(mTakePictureUri, false);
260         }
261     }
262 
appendOutputExtra(Intent intent, Uri pictureUri)263     private void appendOutputExtra(Intent intent, Uri pictureUri) {
264         intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
265         intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
266                 | Intent.FLAG_GRANT_READ_URI_PERMISSION);
267         intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, pictureUri));
268     }
269 
appendCropExtras(Intent intent)270     private void appendCropExtras(Intent intent) {
271         intent.putExtra("crop", "true");
272         intent.putExtra("scale", true);
273         intent.putExtra("scaleUpIfNeeded", true);
274         intent.putExtra("aspectX", 1);
275         intent.putExtra("aspectY", 1);
276         intent.putExtra("outputX", mPhotoSize);
277         intent.putExtra("outputY", mPhotoSize);
278     }
279 
onPhotoCropped(final Uri data, final boolean cropped)280     private void onPhotoCropped(final Uri data, final boolean cropped) {
281         new AsyncTask<Void, Void, Bitmap>() {
282             @Override
283             protected Bitmap doInBackground(Void... params) {
284                 if (cropped) {
285                     InputStream imageStream = null;
286                     try {
287                         imageStream = mContext.getContentResolver()
288                                 .openInputStream(data);
289                         return imageStream != null ? BitmapFactory.decodeStream(imageStream) : null;
290                     } catch (FileNotFoundException fe) {
291                         Log.w(TAG, "Cannot find image file", fe);
292                         return null;
293                     } finally {
294                         if (imageStream != null) {
295                             try {
296                                 imageStream.close();
297                             } catch (IOException ioe) {
298                                 Log.w(TAG, "Cannot close image stream", ioe);
299                             }
300                         }
301                     }
302                 } else {
303                     // Scale and crop to a square aspect ratio
304                     Bitmap fullImage = null;
305                     try {
306                         InputStream imageStream = mContext.getContentResolver()
307                                 .openInputStream(data);
308                         fullImage = imageStream != null ? BitmapFactory.decodeStream(imageStream)
309                                 : null;
310                     } catch (FileNotFoundException fe) {
311                         Log.w(TAG, "Cannot find image file", fe);
312                         return null;
313                     }
314                     if (fullImage != null) {
315                         Bitmap croppedImage = Bitmap.createBitmap(mPhotoSize, mPhotoSize,
316                                 Config.ARGB_8888);
317                         Canvas canvas = new Canvas(croppedImage);
318                         final int squareSize = Math.min(fullImage.getWidth(),
319                                 fullImage.getHeight());
320                         final int left = (fullImage.getWidth() - squareSize) / 2;
321                         final int top = (fullImage.getHeight() - squareSize) / 2;
322                         Rect rectSource = new Rect(left, top,
323                                 left + squareSize, top + squareSize);
324                         Rect rectDest = new Rect(0, 0, mPhotoSize, mPhotoSize);
325                         Paint paint = new Paint();
326                         canvas.drawBitmap(fullImage, rectSource, rectDest, paint);
327                         return croppedImage;
328                     } else {
329                         return null;
330                     }
331                 }
332             }
333 
334             @Override
335             protected void onPostExecute(Bitmap bitmap) {
336                 if (bitmap != null) {
337                     mNewUserPhotoBitmap = bitmap;
338                     mNewUserPhotoDrawable = CircleFramedDrawable
339                             .getInstance(mImageView.getContext(), mNewUserPhotoBitmap);
340                     mImageView.setImageDrawable(mNewUserPhotoDrawable);
341                 }
342                 new File(mContext.getCacheDir(), TAKE_PICTURE_FILE_NAME).delete();
343                 new File(mContext.getCacheDir(), CROP_PICTURE_FILE_NAME).delete();
344             }
345         }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
346     }
347 
getPhotoSize(Context context)348     private static int getPhotoSize(Context context) {
349         Cursor cursor = context.getContentResolver().query(
350                 DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
351                 new String[]{DisplayPhoto.DISPLAY_MAX_DIM}, null, null, null);
352         try {
353             cursor.moveToFirst();
354             return cursor.getInt(0);
355         } finally {
356             cursor.close();
357         }
358     }
359 
createTempImageUri(Context context, String fileName, boolean purge)360     private Uri createTempImageUri(Context context, String fileName, boolean purge) {
361         final File folder = context.getCacheDir();
362         folder.mkdirs();
363         final File fullPath = new File(folder, fileName);
364         if (purge) {
365             fullPath.delete();
366         }
367         return FileProvider.getUriForFile(context, FILE_PATH, fullPath);
368     }
369 
saveNewUserPhotoBitmap()370     File saveNewUserPhotoBitmap() {
371         if (mNewUserPhotoBitmap == null) {
372             return null;
373         }
374         try {
375             File file = new File(mContext.getCacheDir(), NEW_USER_PHOTO_FILE_NAME);
376             OutputStream os = new FileOutputStream(file);
377             mNewUserPhotoBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
378             os.flush();
379             os.close();
380             return file;
381         } catch (IOException e) {
382             Log.e(TAG, "Cannot create temp file", e);
383         }
384         return null;
385     }
386 
loadNewUserPhotoBitmap(File file)387     static Bitmap loadNewUserPhotoBitmap(File file) {
388         return BitmapFactory.decodeFile(file.getAbsolutePath());
389     }
390 
removeNewUserPhotoBitmapFile()391     void removeNewUserPhotoBitmapFile() {
392         new File(mContext.getCacheDir(), NEW_USER_PHOTO_FILE_NAME).delete();
393     }
394 
395     private static final class RestrictedMenuItem {
396         private final Context mContext;
397         private final String mTitle;
398         private final Runnable mAction;
399         private final RestrictedLockUtilsInternal.EnforcedAdmin mAdmin;
400         // Restriction may be set by system or something else via UserManager.setUserRestriction().
401         private final boolean mIsRestrictedByBase;
402 
403         /**
404          * The menu item, used for popup menu. Any element of such a menu can be disabled by admin.
405          *
406          * @param context     A context.
407          * @param title       The title of the menu item.
408          * @param restriction The restriction, that if is set, blocks the menu item.
409          * @param action      The action on menu item click.
410          */
RestrictedMenuItem(Context context, String title, String restriction, Runnable action)411         public RestrictedMenuItem(Context context, String title, String restriction,
412                 Runnable action) {
413             mContext = context;
414             mTitle = title;
415             mAction = action;
416 
417             final int myUserId = UserHandle.myUserId();
418             mAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
419                     restriction, myUserId);
420             mIsRestrictedByBase = RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext,
421                     restriction, myUserId);
422         }
423 
424         @Override
toString()425         public String toString() {
426             return mTitle;
427         }
428 
doAction()429         final void doAction() {
430             if (isRestrictedByBase()) {
431                 return;
432             }
433 
434             if (isRestrictedByAdmin()) {
435                 RestrictedLockUtilsInternal.sendShowAdminSupportDetailsIntent(mContext, mAdmin);
436                 return;
437             }
438 
439             mAction.run();
440         }
441 
isRestrictedByAdmin()442         final boolean isRestrictedByAdmin() {
443             return mAdmin != null;
444         }
445 
isRestrictedByBase()446         final boolean isRestrictedByBase() {
447             return mIsRestrictedByBase;
448         }
449     }
450 
451     /**
452      * Provide this adapter to ListPopupWindow.setAdapter() to have a popup window menu, where
453      * any element can be restricted by admin (profile owner or device owner).
454      */
455     private static final class RestrictedPopupMenuAdapter extends ArrayAdapter<RestrictedMenuItem> {
RestrictedPopupMenuAdapter(Context context, List<RestrictedMenuItem> items)456         public RestrictedPopupMenuAdapter(Context context, List<RestrictedMenuItem> items) {
457             super(context, R.layout.restricted_popup_menu_item, R.id.text, items);
458         }
459 
460         @Override
getView(int position, View convertView, ViewGroup parent)461         public View getView(int position, View convertView, ViewGroup parent) {
462             final View view = super.getView(position, convertView, parent);
463             final RestrictedMenuItem item = getItem(position);
464             final TextView text = (TextView) view.findViewById(R.id.text);
465             final ImageView image = (ImageView) view.findViewById(R.id.restricted_icon);
466 
467             text.setEnabled(!item.isRestrictedByAdmin() && !item.isRestrictedByBase());
468             image.setVisibility(item.isRestrictedByAdmin() && !item.isRestrictedByBase() ?
469                     ImageView.VISIBLE : ImageView.GONE);
470 
471             return view;
472         }
473     }
474 }
475