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