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.sim.smartForwarding;
18 
19 import static com.android.settings.sim.smartForwarding.EnableSmartForwardingTask.FeatureResult;
20 import static com.android.settings.sim.smartForwarding.SmartForwardingUtils.TAG;
21 import static com.android.settings.sim.smartForwarding.SmartForwardingUtils.backupPrevStatus;
22 import static com.android.settings.sim.smartForwarding.SmartForwardingUtils.clearAllBackupData;
23 import static com.android.settings.sim.smartForwarding.SmartForwardingUtils.getAllSlotCallForwardingStatus;
24 import static com.android.settings.sim.smartForwarding.SmartForwardingUtils.getAllSlotCallWaitingStatus;
25 
26 import android.app.ActionBar;
27 import android.app.AlertDialog;
28 import android.app.ProgressDialog;
29 import android.os.Bundle;
30 import android.telephony.CallForwardingInfo;
31 import android.telephony.SubscriptionManager;
32 import android.telephony.TelephonyManager;
33 import android.util.Log;
34 import android.view.View;
35 import android.widget.Toolbar;
36 
37 import androidx.core.content.ContextCompat;
38 
39 import com.android.settings.R;
40 import com.android.settings.core.SettingsBaseActivity;
41 import com.android.settings.network.SubscriptionUtil;
42 
43 import com.google.common.util.concurrent.FutureCallback;
44 import com.google.common.util.concurrent.Futures;
45 import com.google.common.util.concurrent.ListenableFuture;
46 import com.google.common.util.concurrent.ListeningExecutorService;
47 import com.google.common.util.concurrent.MoreExecutors;
48 
49 import java.util.concurrent.Executors;
50 
51 public class SmartForwardingActivity extends SettingsBaseActivity {
52     static final String LOG_TAG = SmartForwardingActivity.class.toString();
53     final ListeningExecutorService service =
54             MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
55 
56     @Override
onCreate(Bundle savedInstanceState)57     protected void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59 
60         if (!SubscriptionUtil.isSimHardwareVisible(this)) {
61             Log.d(LOG_TAG, "Not support on device without SIM.");
62             finish();
63             return;
64         }
65 
66         final Toolbar toolbar = findViewById(R.id.action_bar);
67         toolbar.setVisibility(View.VISIBLE);
68         setActionBar(toolbar);
69 
70         final ActionBar actionBar = getActionBar();
71         if (actionBar != null) {
72             actionBar.setDisplayHomeAsUpEnabled(true);
73         }
74 
75         getSupportFragmentManager()
76                 .beginTransaction()
77                 .replace(R.id.content_frame, new SmartForwardingFragment())
78                 .commit();
79     }
80 
81     @Override
onDestroy()82     protected void onDestroy() {
83         super.onDestroy();
84     }
85 
enableSmartForwarding(String[] phoneNumber)86     public void enableSmartForwarding(String[] phoneNumber) {
87         // Pop-up ongoing dialog
88         ProgressDialog dialog = new ProgressDialog(this);
89         dialog.setTitle(R.string.smart_forwarding_ongoing_title);
90         dialog.setIndeterminate(true);
91         dialog.setMessage(getText(R.string.smart_forwarding_ongoing_text));
92         dialog.setCancelable(false);
93         dialog.show();
94 
95         // Enable feature
96         ListenableFuture<FeatureResult> enableTask =
97                 service.submit(new EnableSmartForwardingTask(this, phoneNumber));
98         Futures.addCallback(enableTask, new FutureCallback<FeatureResult>() {
99             @Override
100             public void onSuccess(FeatureResult result) {
101                 Log.e(TAG, "Enable Feature result: " + result.getResult());
102                 if (result.getResult()) {
103                     backupPrevStatus(SmartForwardingActivity.this, result.getSlotUTData());
104 
105                     // Turn on switch preference
106                     SmartForwardingFragment fragment =
107                             (SmartForwardingFragment) getSupportFragmentManager()
108                                     .findFragmentById(R.id.content_frame);
109                     if (fragment != null) {
110                         fragment.turnOnSwitchPreference();
111                     }
112                 } else {
113                     onError(result);
114                 }
115                 dialog.dismiss();
116             }
117 
118             @Override
119             public void onFailure(Throwable t) {
120                 Log.e(TAG, "Enable Feature exception", t);
121                 dialog.dismiss();
122 
123                 // Pop-up error dialog
124                 AlertDialog mDialog = new AlertDialog.Builder(SmartForwardingActivity.this)
125                         .setTitle(R.string.smart_forwarding_failed_title)
126                         .setMessage(R.string.smart_forwarding_failed_text)
127                         .setPositiveButton(
128                                 R.string.smart_forwarding_missing_alert_dialog_text,
129                                 (dialog, which) -> { dialog.dismiss(); })
130                         .create();
131                 mDialog.show();
132             }
133         }, ContextCompat.getMainExecutor(this));
134     }
135 
disableSmartForwarding()136     public void disableSmartForwarding() {
137         TelephonyManager tm = getSystemService(TelephonyManager.class);
138         SubscriptionManager sm = getSystemService(SubscriptionManager.class);
139 
140         boolean[] callWaitingStatus = getAllSlotCallWaitingStatus(this, tm);
141         CallForwardingInfo[] callForwardingInfo = getAllSlotCallForwardingStatus(this, sm, tm);
142 
143         // Disable feature
144         ListenableFuture disableTask = service.submit(new DisableSmartForwardingTask(
145                 tm, callWaitingStatus, callForwardingInfo));
146         Futures.addCallback(disableTask, new FutureCallback() {
147             @Override
148             public void onSuccess(Object result) {
149                 clearAllBackupData(SmartForwardingActivity.this, sm, tm);
150             }
151 
152             @Override
153             public void onFailure(Throwable t) {
154                 Log.e(TAG, "Disable Feature exception" + t);
155             }
156         }, ContextCompat.getMainExecutor(this));
157     }
158 
onError(FeatureResult result)159     public void onError(FeatureResult result) {
160         int errorMsg;
161         if (result.getReason() == FeatureResult.FailedReason.SIM_NOT_ACTIVE) {
162             errorMsg = R.string.smart_forwarding_failed_not_activated_text;
163         } else {
164             errorMsg = R.string.smart_forwarding_failed_text;
165         }
166 
167         // Pop-up error dialog
168         AlertDialog mDialog = new AlertDialog.Builder(SmartForwardingActivity.this)
169                 .setTitle(R.string.smart_forwarding_failed_title)
170                 .setMessage(errorMsg)
171                 .setPositiveButton(
172                         R.string.smart_forwarding_missing_alert_dialog_text,
173                         (dialog, which) -> { dialog.dismiss(); })
174                 .create();
175         mDialog.show();
176     }
177 }
178