VoicemailTosMessageCreator.java revision 5b4becb8ca9cc6249d0cf0a074bf63d6999f3b5e
1/*
2 * Copyright (C) 2017 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
17package com.android.dialer.app.voicemail.error;
18
19import android.app.AlertDialog;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.SharedPreferences;
25import android.preference.PreferenceManager;
26import android.support.annotation.Nullable;
27import android.telecom.PhoneAccountHandle;
28import android.telephony.TelephonyManager;
29import android.view.View;
30import android.view.View.OnClickListener;
31import com.android.contacts.common.compat.TelephonyManagerCompat;
32import com.android.dialer.app.voicemail.error.VoicemailErrorMessage.Action;
33import com.android.dialer.buildtype.BuildType;
34import com.android.dialer.common.LogUtil;
35import com.android.dialer.logging.DialerImpression;
36import com.android.dialer.logging.Logger;
37import com.android.voicemail.VisualVoicemailTypeExtensions;
38import com.android.voicemail.VoicemailClient;
39import com.android.voicemail.VoicemailComponent;
40import java.util.Locale;
41
42/**
43 * Create error message from {@link VoicemailStatus} for voicemail. This is will show different
44 * terms of service for Verizon and for other carriers.
45 */
46public class VoicemailTosMessageCreator {
47  // Flag to check which version of the Verizon ToS that the user has accepted.
48  public static final String VVM3_TOS_VERSION_ACCEPTED_KEY = "vvm3_tos_version_accepted";
49
50  // Flag to check which version of the Google Dialer ToS that the user has accepted.
51  public static final String DIALER_TOS_VERSION_ACCEPTED_KEY = "dialer_tos_version_accepted";
52
53  public static final int CURRENT_VVM3_TOS_VERSION = 2;
54  public static final int CURRENT_DIALER_TOS_VERSION = 1;
55
56  private static final String ISO639_SPANISH = "es";
57
58  private final Context context;
59  private final VoicemailStatus status;
60  private final VoicemailStatusReader statusReader;
61  private final SharedPreferences preferences;
62
63  VoicemailTosMessageCreator(
64      final Context context,
65      final VoicemailStatus status,
66      final VoicemailStatusReader statusReader) {
67    this.context = context;
68    this.status = status;
69    this.statusReader = statusReader;
70    this.preferences = PreferenceManager.getDefaultSharedPreferences(context);
71  }
72
73  @Nullable
74  VoicemailErrorMessage maybeCreateTosMessage() {
75    // TODO(mdooley): add filtering based on carrier
76    if (hasAcceptedTos()) {
77      return null;
78    }
79    // TODO(mdooley): temporarily skip the terms of service for dogfood builds
80    if (BuildType.get() == BuildType.DOGFOOD) {
81      LogUtil.i(
82          "VoicemailTosMessageCreator.maybeCreateTosMessage",
83          "Skipping voicemail ToS for dogfood build");
84      return null;
85    }
86    logTosCreatedImpression();
87
88    return new VoicemailTosMessage(
89            getTosTitle(),
90            getTosMessage(),
91            new Action(
92                getDeclineText(),
93                new OnClickListener() {
94                  @Override
95                  public void onClick(View v) {
96                    LogUtil.i("VoicemailTosMessageCreator.maybeShowTosMessage", "decline clicked");
97                    PhoneAccountHandle handle =
98                        new PhoneAccountHandle(
99                            ComponentName.unflattenFromString(status.phoneAccountComponentName),
100                            status.phoneAccountId);
101                    logTosDeclinedImpression();
102                    showDeclineTosDialog(handle);
103                  }
104                }),
105            new Action(
106                getAcceptText(),
107                new OnClickListener() {
108                  @Override
109                  public void onClick(View v) {
110                    LogUtil.i("VoicemailTosMessageCreator.maybeShowTosMessage", "accept clicked");
111                    recordTosAcceptance();
112                    logTosAcceptedImpression();
113                    statusReader.refresh();
114                  }
115                },
116                true /* raised */))
117        .setModal(true);
118  }
119
120  private void showDeclineTosDialog(final PhoneAccountHandle handle) {
121    if (isVvm3() && Vvm3VoicemailMessageCreator.PIN_NOT_SET == status.configurationState) {
122      LogUtil.i(
123          "VoicemailTosMessageCreator.showDeclineTosDialog", "PIN_NOT_SET, showing set PIN dialog");
124      showSetPinBeforeDeclineDialog();
125      return;
126    }
127    LogUtil.i(
128        "VoicemailTosMessageCreator.showDeclineVerizonTosDialog",
129        "showing decline ToS dialog, status=" + status);
130    final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
131    AlertDialog.Builder builder = new AlertDialog.Builder(context);
132    builder.setMessage(getTosDeclinedDialogMessageId());
133    builder.setPositiveButton(
134        getTosDeclinedDialogDowngradeId(),
135        new DialogInterface.OnClickListener() {
136          @Override
137          public void onClick(DialogInterface dialog, int which) {
138            Logger.get(context).logImpression(DialerImpression.Type.VOICEMAIL_VVM3_TOS_DECLINED);
139            VoicemailClient voicemailClient = VoicemailComponent.get(context).getVoicemailClient();
140            if (voicemailClient.isVoicemailModuleEnabled()) {
141              voicemailClient.setVoicemailEnabled(context, status.getPhoneAccountHandle(), false);
142            } else {
143              TelephonyManagerCompat.setVisualVoicemailEnabled(telephonyManager, handle, false);
144            }
145          }
146        });
147
148    builder.setNegativeButton(
149        android.R.string.cancel,
150        new DialogInterface.OnClickListener() {
151          @Override
152          public void onClick(DialogInterface dialog, int which) {
153            dialog.dismiss();
154          }
155        });
156
157    builder.setCancelable(true);
158    builder.show();
159  }
160
161  private void showSetPinBeforeDeclineDialog() {
162    AlertDialog.Builder builder = new AlertDialog.Builder(context);
163    builder.setMessage(R.string.verizon_terms_and_conditions_decline_set_pin_dialog_message);
164    builder.setPositiveButton(
165        R.string.verizon_terms_and_conditions_decline_set_pin_dialog_set_pin,
166        new DialogInterface.OnClickListener() {
167          @Override
168          public void onClick(DialogInterface dialog, int which) {
169            Logger.get(context)
170                .logImpression(DialerImpression.Type.VOICEMAIL_VVM3_TOS_DECLINE_CHANGE_PIN_SHOWN);
171            Intent intent = new Intent(TelephonyManager.ACTION_CONFIGURE_VOICEMAIL);
172            context.startActivity(intent);
173          }
174        });
175
176    builder.setNegativeButton(
177        android.R.string.cancel,
178        new DialogInterface.OnClickListener() {
179          @Override
180          public void onClick(DialogInterface dialog, int which) {
181            dialog.dismiss();
182          }
183        });
184
185    builder.setCancelable(true);
186    builder.show();
187  }
188
189  private boolean isVvm3() {
190    return VisualVoicemailTypeExtensions.VVM_TYPE_VVM3.equals(status.type);
191  }
192
193  private boolean useSpanish() {
194    return Locale.getDefault().getLanguage().equals(new Locale(ISO639_SPANISH).getLanguage());
195  }
196
197  private boolean hasAcceptedTos() {
198    if (isVvm3()) {
199      return preferences.getInt(VVM3_TOS_VERSION_ACCEPTED_KEY, 0) >= CURRENT_VVM3_TOS_VERSION;
200    } else {
201      return preferences.getInt(DIALER_TOS_VERSION_ACCEPTED_KEY, 0) >= CURRENT_DIALER_TOS_VERSION;
202    }
203  }
204
205  private void recordTosAcceptance() {
206    if (isVvm3()) {
207      preferences.edit().putInt(VVM3_TOS_VERSION_ACCEPTED_KEY, CURRENT_VVM3_TOS_VERSION).apply();
208    } else {
209      preferences
210          .edit()
211          .putInt(DIALER_TOS_VERSION_ACCEPTED_KEY, CURRENT_DIALER_TOS_VERSION)
212          .apply();
213    }
214  }
215
216  private void logTosCreatedImpression() {
217    if (isVvm3()) {
218      Logger.get(context).logImpression(DialerImpression.Type.VOICEMAIL_VVM3_TOS_V2_CREATED);
219    } else {
220      Logger.get(context).logImpression(DialerImpression.Type.VOICEMAIL_DIALER_TOS_CREATED);
221    }
222  }
223
224  private void logTosDeclinedImpression() {
225    if (isVvm3()) {
226      Logger.get(context)
227          .logImpression(DialerImpression.Type.VOICEMAIL_VVM3_TOS_V2_DECLINE_CLICKED);
228    } else {
229      Logger.get(context).logImpression(DialerImpression.Type.VOICEMAIL_DIALER_TOS_DECLINE_CLICKED);
230    }
231  }
232
233  private void logTosAcceptedImpression() {
234    if (isVvm3()) {
235      Logger.get(context).logImpression(DialerImpression.Type.VOICEMAIL_VVM3_TOS_V2_ACCEPTED);
236    } else {
237      Logger.get(context).logImpression(DialerImpression.Type.VOICEMAIL_DIALER_TOS_ACCEPTED);
238    }
239  }
240
241  private CharSequence getVvm3Tos() {
242    return useSpanish()
243        ? context.getString(R.string.verizon_terms_and_conditions_1_1_spanish)
244        : context.getString(R.string.verizon_terms_and_conditions_1_1_english);
245  }
246
247  private CharSequence getDialerTos() {
248    return useSpanish()
249        ? context.getString(R.string.dialer_terms_and_conditions_1_0_spanish)
250        : context.getString(R.string.dialer_terms_and_conditions_1_0_english);
251  }
252
253  private CharSequence getAcceptText() {
254    if (isVvm3()) {
255      return useSpanish()
256          ? context.getString(R.string.verizon_terms_and_conditions_accept_spanish)
257          : context.getString(R.string.verizon_terms_and_conditions_accept_english);
258    } else {
259      return useSpanish()
260          ? context.getString(R.string.dialer_terms_and_conditions_accept_spanish)
261          : context.getString(R.string.dialer_terms_and_conditions_accept_english);
262    }
263  }
264
265  private CharSequence getDeclineText() {
266    if (isVvm3()) {
267      return useSpanish()
268          ? context.getString(R.string.verizon_terms_and_conditions_decline_spanish)
269          : context.getString(R.string.verizon_terms_and_conditions_decline_english);
270    } else {
271      return useSpanish()
272          ? context.getString(R.string.dialer_terms_and_conditions_decline_spanish)
273          : context.getString(R.string.dialer_terms_and_conditions_decline_english);
274    }
275  }
276
277  private String getTosTitle() {
278    return isVvm3()
279        ? context.getString(R.string.verizon_terms_and_conditions_title)
280        : context.getString(R.string.dialer_terms_and_conditions_title);
281  }
282
283  private String getTosMessage() {
284    return isVvm3()
285        ? context.getString(
286            R.string.verizon_terms_and_conditions_message, getDialerTos(), getVvm3Tos())
287        : context.getString(R.string.dialer_terms_and_conditions_message, getDialerTos());
288  }
289
290  private int getTosDeclinedDialogMessageId() {
291    return isVvm3()
292        ? R.string.verizon_terms_and_conditions_decline_dialog_message
293        : R.string.dialer_terms_and_conditions_decline_dialog_message;
294  }
295
296  private int getTosDeclinedDialogDowngradeId() {
297    return isVvm3()
298        ? R.string.verizon_terms_and_conditions_decline_dialog_downgrade
299        : R.string.dialer_terms_and_conditions_decline_dialog_downgrade;
300  }
301}
302