• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.settingslib.users;
18 
19 import android.app.KeyguardManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.provider.MediaStore;
24 
25 /**
26  * Utility class that contains helper methods to determine if the current user has permission and
27  * the device is in a proper state to start an activity for a given action.
28  */
29 public class PhotoCapabilityUtils {
30 
31     /**
32      * Check if the current user can perform any activity for
33      * android.media.action.IMAGE_CAPTURE action.
34      */
canTakePhoto(Context context)35     public static boolean canTakePhoto(Context context) {
36         return context.getPackageManager().queryIntentActivities(
37                 new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
38                 PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
39     }
40 
41     /**
42      * Check if the current user can perform any activity for
43      * android.intent.action.GET_CONTENT action for images.
44      * Returns false if the device is currently locked and
45      * requires a PIN, pattern or password to unlock.
46      */
canChoosePhoto(Context context)47     public static boolean canChoosePhoto(Context context) {
48         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
49         intent.setType("image/*");
50         boolean canPerformActivityForGetImage =
51                 context.getPackageManager().queryIntentActivities(intent, 0).size() > 0;
52         // on locked device we can't access the images
53         return canPerformActivityForGetImage && !isDeviceLocked(context);
54     }
55 
56     /**
57      * Check if the current user can perform any activity for
58      * com.android.camera.action.CROP action for images.
59      * Returns false if the device is currently locked and
60      * requires a PIN, pattern or password to unlock.
61      */
canCropPhoto(Context context)62     public static boolean canCropPhoto(Context context) {
63         Intent intent = new Intent("com.android.camera.action.CROP");
64         intent.setType("image/*");
65         boolean canPerformActivityForCropping =
66                 context.getPackageManager().queryIntentActivities(intent, 0).size() > 0;
67         // on locked device we can't start a cropping activity
68         return canPerformActivityForCropping && !isDeviceLocked(context);
69     }
70 
isDeviceLocked(Context context)71     private static boolean isDeviceLocked(Context context) {
72         KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
73         return keyguardManager == null || keyguardManager.isDeviceLocked();
74     }
75 
76 }
77