1 /*
2  * Copyright (C) 2022 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.server.credentials;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.credentials.CreateCredentialException;
22 import android.credentials.CreateCredentialResponse;
23 import android.credentials.GetCredentialException;
24 import android.credentials.GetCredentialResponse;
25 import android.credentials.selection.ProviderPendingIntentResponse;
26 import android.service.credentials.BeginGetCredentialResponse;
27 import android.service.credentials.CredentialProviderService;
28 
29 /**
30  * Helper class for setting up pending intent, and extracting objects from it.
31  *
32  * @hide
33  */
34 public class PendingIntentResultHandler {
35     /** Returns true if the result is successful and may contain result extras. */
isValidResponse( ProviderPendingIntentResponse pendingIntentResponse)36     public static boolean isValidResponse(
37             ProviderPendingIntentResponse pendingIntentResponse) {
38         //TODO: Differentiate based on extra_error in the resultData
39         return pendingIntentResponse.getResultCode() == Activity.RESULT_OK;
40     }
41 
42     /** Returns true if the pending intent was cancelled by the user. */
isCancelledResponse( ProviderPendingIntentResponse pendingIntentResponse)43     public static boolean isCancelledResponse(
44             ProviderPendingIntentResponse pendingIntentResponse) {
45         return pendingIntentResponse.getResultCode() == Activity.RESULT_CANCELED;
46     }
47 
48     /** Extracts the {@link BeginGetCredentialResponse} object added to the result data. */
extractResponseContent(Intent resultData)49     public static BeginGetCredentialResponse extractResponseContent(Intent resultData) {
50         if (resultData == null) {
51             return null;
52         }
53         return resultData.getParcelableExtra(
54                 CredentialProviderService.EXTRA_BEGIN_GET_CREDENTIAL_RESPONSE,
55                 BeginGetCredentialResponse.class);
56     }
57 
58     /** Extracts the {@link CreateCredentialResponse} object added to the result data. */
extractCreateCredentialResponse(Intent resultData)59     public static CreateCredentialResponse extractCreateCredentialResponse(Intent resultData) {
60         if (resultData == null) {
61             return null;
62         }
63         return resultData.getParcelableExtra(
64                 CredentialProviderService.EXTRA_CREATE_CREDENTIAL_RESPONSE,
65                 CreateCredentialResponse.class);
66     }
67 
68     /** Extracts the {@link GetCredentialResponse} object added to the result data. */
extractGetCredentialResponse(Intent resultData)69     public static GetCredentialResponse extractGetCredentialResponse(Intent resultData) {
70         if (resultData == null) {
71             return null;
72         }
73         return resultData.getParcelableExtra(
74                 CredentialProviderService.EXTRA_GET_CREDENTIAL_RESPONSE,
75                 GetCredentialResponse.class);
76     }
77 
78     /** Extract the {@link CreateCredentialException} from the
79      * given pending intent . */
extractCreateCredentialException( Intent resultData)80     public static CreateCredentialException extractCreateCredentialException(
81             Intent resultData) {
82         if (resultData == null) {
83             return null;
84         }
85         return resultData.getSerializableExtra(
86                 CredentialProviderService.EXTRA_CREATE_CREDENTIAL_EXCEPTION,
87                 CreateCredentialException.class);
88     }
89 
90     /** Extract the {@link GetCredentialException} from the
91      * given pending intent . */
extractGetCredentialException( Intent resultData)92     public static GetCredentialException extractGetCredentialException(
93             Intent resultData) {
94         if (resultData == null) {
95             return null;
96         }
97         return resultData.getSerializableExtra(
98                 CredentialProviderService.EXTRA_GET_CREDENTIAL_EXCEPTION,
99                 GetCredentialException.class);
100     }
101 }
102