1 /*
2  * Copyright (C) 2021 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.settings.sim;
18 
19 import android.os.Bundle;
20 import android.telephony.SubscriptionInfo;
21 import android.util.Log;
22 
23 import com.android.settings.R;
24 import com.android.settings.SidecarFragment;
25 import com.android.settings.network.SwitchToEuiccSubscriptionSidecar;
26 import com.android.settings.network.telephony.AlertDialogFragment;
27 import com.android.settings.network.telephony.ConfirmDialogFragment;
28 import com.android.settings.network.telephony.SubscriptionActionDialogActivity;
29 
30 /**
31  * Starts a confirm dialog asking the user to switch to the eSIM slot/subscription. The caller needs
32  * to pass in the current enabled eSIM subscription, which is also the subscription to switch to.
33  */
34 public class SwitchToEsimConfirmDialogActivity extends SubscriptionActionDialogActivity
35         implements SidecarFragment.Listener, ConfirmDialogFragment.OnConfirmListener {
36 
37     public static final String KEY_SUB_TO_ENABLE = "sub_to_enable";
38 
39     private static final String TAG = "SwitchToEsimConfirmDialogActivity";
40     private static final int TAG_CONFIRM = 1;
41 
42     private SubscriptionInfo mSubToEnabled = null;
43     private SwitchToEuiccSubscriptionSidecar mSwitchToEuiccSubscriptionSidecar;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48 
49         mSubToEnabled = getIntent().getParcelableExtra(KEY_SUB_TO_ENABLE);
50         mSwitchToEuiccSubscriptionSidecar =
51                 SwitchToEuiccSubscriptionSidecar.get(getFragmentManager());
52 
53         if (mSubToEnabled == null) {
54             Log.e(TAG, "Cannot find SIM to enable.");
55             finish();
56             return;
57         }
58 
59         if (savedInstanceState == null) {
60             ConfirmDialogFragment.show(
61                     this,
62                     ConfirmDialogFragment.OnConfirmListener.class,
63                     TAG_CONFIRM,
64                     getString(R.string.switch_sim_dialog_title, mSubToEnabled.getDisplayName()),
65                     getString(R.string.switch_sim_dialog_text, mSubToEnabled.getDisplayName()),
66                     getString(R.string.okay),
67                     getString(R.string.cancel));
68         }
69     }
70 
71     @Override
onResume()72     public void onResume() {
73         super.onResume();
74         mSwitchToEuiccSubscriptionSidecar.addListener(this);
75     }
76 
77     @Override
onPause()78     public void onPause() {
79         mSwitchToEuiccSubscriptionSidecar.removeListener(this);
80         super.onPause();
81     }
82 
83     @Override
onStateChange(SidecarFragment fragment)84     public void onStateChange(SidecarFragment fragment) {
85         if (fragment == mSwitchToEuiccSubscriptionSidecar) {
86             switch (mSwitchToEuiccSubscriptionSidecar.getState()) {
87                 case SidecarFragment.State.SUCCESS:
88                     mSwitchToEuiccSubscriptionSidecar.reset();
89                     Log.i(TAG, "Successfully switched to eSIM slot.");
90                     dismissProgressDialog();
91                     finish();
92                     break;
93                 case SidecarFragment.State.ERROR:
94                     mSwitchToEuiccSubscriptionSidecar.reset();
95                     Log.e(TAG, "Failed switching to eSIM slot.");
96                     dismissProgressDialog();
97                     finish();
98                     break;
99             }
100         }
101     }
102 
103     @Override
onConfirm(int tag, boolean confirmed)104     public void onConfirm(int tag, boolean confirmed) {
105         if (!confirmed) {
106             AlertDialogFragment.show(
107                     this,
108                     getString(R.string.switch_sim_dialog_no_switch_title),
109                     getString(R.string.switch_sim_dialog_no_switch_text));
110             return;
111         }
112         Log.i(TAG, "User confirmed to switch to embedded slot.");
113         mSwitchToEuiccSubscriptionSidecar.run(mSubToEnabled.getSubscriptionId());
114         showProgressDialog(
115                 getString(
116                         R.string.sim_action_switch_sub_dialog_progress,
117                         mSubToEnabled.getDisplayName()));
118     }
119 }
120