1 /*
2  * Copyright (C) 2023 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.permissioncontroller.role.ui.v35;
18 
19 import android.app.Activity;
20 import android.app.role.RoleManager;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.nfc.cardemulation.CardEmulation;
25 import android.os.Bundle;
26 import android.os.Process;
27 import android.permission.flags.Flags;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 
32 import com.android.modules.utils.build.SdkLevel;
33 import com.android.permissioncontroller.role.ui.DefaultAppActivity;
34 
35 import java.util.List;
36 import java.util.Objects;
37 
38 /**
39  * Activity to handle {@link android.nfc.cardemulation.CardEmulation#ACTION_CHANGE_DEFAULT}.
40  */
41 public class ChangeDefaultCardEmulationActivity extends Activity {
42 
43     @Override
onCreate(@ullable Bundle savedInstanceState)44     protected void onCreate(@Nullable Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46 
47         Intent intent;
48         if (SdkLevel.isAtLeastV() && Flags.walletRoleEnabled()) {
49             intent = DefaultAppActivity.createIntent(RoleManager.ROLE_WALLET,
50                     Process.myUserHandle(), this);
51         } else {
52             intent = getIntent();
53             setDefaultPaymentChangeHandlerDialogComponent(intent);
54         }
55         intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
56         startActivity(intent);
57         finish();
58     }
59 
60     // The only other handler of this intent is in the NFC stack.
setDefaultPaymentChangeHandlerDialogComponent(@onNull Intent intent)61     private void setDefaultPaymentChangeHandlerDialogComponent(@NonNull Intent intent) {
62         Intent queryIntent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
63         PackageManager packageManager = getPackageManager();
64         List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(queryIntent,
65                 PackageManager.MATCH_SYSTEM_ONLY);
66         int resolveInfosSize = resolveInfos.size();
67         for (int i = 0; i < resolveInfosSize; i++) {
68             ResolveInfo resolveInfo = resolveInfos.get(i);
69             String packageName = resolveInfo.activityInfo.packageName;
70             if (!Objects.equals(packageName, getPackageName())) {
71                 intent.setClassName(packageName,
72                         resolveInfo.activityInfo.name);
73                 return;
74             }
75         }
76     }
77 }
78