1 /*
2  * Copyright (C) 2012 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.cellbroadcastreceiver;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 /**
29  * Container activity for CMAS opt-in/opt-out alert dialog.
30  */
31 public class CellBroadcastOptOutActivity extends Activity {
32     private static final String TAG = "CellBroadcastOptOutActivity";
33 
34     @VisibleForTesting
35     public static TestableShowAlertDialog sShowDialog = AlertDialog::show;
36 
37     @VisibleForTesting
38     public AlertDialog mAlertDialog;
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         mAlertDialog = showOptOutDialog(this);
44     }
45 
46     /**
47      * Show the opt-out dialog. Uses the CellBroadcastAlertDialog activity unless the device is
48      * in restricted keyguard mode, in which case we create a new CellBroadcastOptOutActivity
49      * so that the dialog appears underneath the lock screen. The user must unlock the device
50      * to configure the settings, so we don't want to show the opt-in dialog before then.
51      */
52     @VisibleForTesting
showOptOutDialog(final Activity activity)53     public static AlertDialog showOptOutDialog(final Activity activity) {
54         AlertDialog.Builder builder = new AlertDialog.Builder(activity);
55         AlertDialog alertdialog = builder.setMessage(R.string.cmas_opt_out_dialog_text)
56                 .setPositiveButton(R.string.cmas_opt_out_button_yes,
57                         new DialogInterface.OnClickListener() {
58                             @Override
59                             public void onClick(DialogInterface dialog, int which) {
60                                 Log.d(TAG, "User clicked Yes");
61                                 activity.finish();
62                             }
63                         })
64                 .setNegativeButton(R.string.cmas_opt_out_button_no,
65                         new DialogInterface.OnClickListener() {
66                             @Override
67                             public void onClick(DialogInterface dialog, int which) {
68                                 Log.d(TAG, "User clicked No");
69                                 Intent intent = new Intent(activity, CellBroadcastSettings.class);
70                                 activity.startActivity(intent);
71                                 activity.finish();
72                             }
73                         })
74                 .setOnCancelListener(
75                         new DialogInterface.OnCancelListener() {
76                             @Override
77                             public void onCancel(DialogInterface dialog) {
78                                 Log.d(TAG, "User cancelled");
79                                 // If the user presses the back button, make sure we dismiss the
80                                 // emergency alert's dialog as well.
81                                 activity.finish();
82                             }
83                         })
84                 .create();
85         sShowDialog.show(alertdialog);
86 
87         return alertdialog;
88     }
89 
90     /**
91      * An interface that defines the action of showing a AlertDialog, which can be replaced with
92      * mock in testing so that it can be monitored.
93      */
94     @VisibleForTesting
95     public interface TestableShowAlertDialog {
96         /**
97          * Method to show the alert dialog. In production should be simply AlertDialog::show.
98          * @param alertDialog
99          */
show(AlertDialog alertDialog)100         void show(AlertDialog alertDialog);
101     }
102 }
103