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.settings.network;
18 
19 import android.annotation.IntDef;
20 import android.app.FragmentManager;
21 import android.os.Bundle;
22 import android.util.Log;
23 
24 import com.android.settings.AsyncTaskSidecar;
25 import com.android.settings.SidecarFragment;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 import javax.annotation.Nullable;
31 
32 /** {@link SidecarFragment} to switch SIM slot. */
33 public class SwitchSlotSidecar
34         extends AsyncTaskSidecar<SwitchSlotSidecar.Param, SwitchSlotSidecar.Result> {
35     private static final String TAG = "SwitchSlotSidecar";
36 
37     /** Commands */
38     @Retention(RetentionPolicy.SOURCE)
39     @IntDef({
40         Command.SWITCH_TO_REMOVABLE_SIM,
41     })
42     private @interface Command {
43         int SWITCH_TO_REMOVABLE_SIM = 0;
44     }
45 
46     static class Param {
47         @Command int command;
48         int slotId;
49     }
50 
51     static class Result {
52         Exception exception;
53     }
54 
55     /** Returns a SwitchSlotSidecar sidecar instance. */
get(FragmentManager fm)56     public static SwitchSlotSidecar get(FragmentManager fm) {
57         return SidecarFragment.get(fm, TAG, SwitchSlotSidecar.class, null /* args */);
58     }
59 
60     @Nullable private Exception mException;
61 
62     @Override
onCreate(Bundle savedInstanceState)63     public void onCreate(Bundle savedInstanceState) {
64         super.onCreate(savedInstanceState);
65     }
66 
67     /** Starts switching to the removable slot. */
runSwitchToRemovableSlot(int id)68     public void runSwitchToRemovableSlot(int id) {
69         Param param = new Param();
70         param.command = Command.SWITCH_TO_REMOVABLE_SIM;
71         param.slotId = id;
72         super.run(param);
73     }
74 
75     /**
76      * Returns the exception thrown during the execution of a command. Will be null in any state
77      * other than {@link State#SUCCESS}, and may be null in that state if there was not an error.
78      */
79     @Nullable
getException()80     public Exception getException() {
81         return mException;
82     }
83 
84     @Override
doInBackground(@ullable Param param)85     protected Result doInBackground(@Nullable Param param) {
86         Result result = new Result();
87         if (param == null) {
88             result.exception = new UiccSlotsException("Null param");
89             return result;
90         }
91         try {
92             switch (param.command) {
93                 case Command.SWITCH_TO_REMOVABLE_SIM:
94                     UiccSlotUtil.switchToRemovableSlot(param.slotId, getContext());
95                     break;
96                 default:
97                     Log.e(TAG, "Wrong command.");
98                     break;
99             }
100         } catch (UiccSlotsException e) {
101             result.exception = e;
102         }
103         return result;
104     }
105 
106     @Override
onPostExecute(Result result)107     protected void onPostExecute(Result result) {
108         if (result.exception == null) {
109             setState(State.SUCCESS, Substate.UNUSED);
110         } else {
111             mException = result.exception;
112             setState(State.ERROR, Substate.UNUSED);
113         }
114     }
115 }
116