Home | History | Annotate | Download | only in error
      1 /*
      2  * Copyright (C) 2016 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.dialer.app.voicemail.error;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.provider.Settings;
     22 import android.provider.VoicemailContract;
     23 import android.support.annotation.NonNull;
     24 import android.support.annotation.Nullable;
     25 import android.telecom.PhoneAccountHandle;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import com.android.dialer.common.Assert;
     29 import com.android.dialer.common.PerAccountSharedPreferences;
     30 import com.android.dialer.logging.DialerImpression;
     31 import com.android.dialer.logging.Logger;
     32 import com.android.dialer.util.CallUtil;
     33 import com.android.voicemail.VoicemailClient;
     34 import com.android.voicemail.VoicemailComponent;
     35 import java.util.Arrays;
     36 import java.util.List;
     37 
     38 /**
     39  * Represents an error determined from the current {@link
     40  * android.provider.VoicemailContract.Status}. The message will contain a title, a description, and
     41  * a list of actions that can be performed.
     42  */
     43 public class VoicemailErrorMessage {
     44 
     45   private final CharSequence title;
     46   private final CharSequence description;
     47   private final List<Action> actions;
     48 
     49   private boolean modal;
     50   private Integer imageResourceId;
     51 
     52   /** Something the user can click on to resolve an error, such as retrying or calling voicemail */
     53   public static class Action {
     54 
     55     private final CharSequence text;
     56     private final View.OnClickListener listener;
     57     private final boolean raised;
     58 
     59     public Action(CharSequence text, View.OnClickListener listener) {
     60       this(text, listener, false);
     61     }
     62 
     63     public Action(CharSequence text, View.OnClickListener listener, boolean raised) {
     64       this.text = text;
     65       this.listener = listener;
     66       this.raised = raised;
     67     }
     68 
     69     public CharSequence getText() {
     70       return text;
     71     }
     72 
     73     public View.OnClickListener getListener() {
     74       return listener;
     75     }
     76 
     77     public boolean isRaised() {
     78       return raised;
     79     }
     80   }
     81 
     82   public CharSequence getTitle() {
     83     return title;
     84   }
     85 
     86   public CharSequence getDescription() {
     87     return description;
     88   }
     89 
     90   @Nullable
     91   public List<Action> getActions() {
     92     return actions;
     93   }
     94 
     95   public boolean isModal() {
     96     return modal;
     97   }
     98 
     99   public VoicemailErrorMessage setModal(boolean value) {
    100     modal = value;
    101     return this;
    102   }
    103 
    104   @Nullable
    105   public Integer getImageResourceId() {
    106     return imageResourceId;
    107   }
    108 
    109   public VoicemailErrorMessage setImageResourceId(Integer imageResourceId) {
    110     this.imageResourceId = imageResourceId;
    111     return this;
    112   }
    113 
    114   public VoicemailErrorMessage(CharSequence title, CharSequence description, Action... actions) {
    115     this(title, description, Arrays.asList(actions));
    116   }
    117 
    118   public VoicemailErrorMessage(
    119       CharSequence title, CharSequence description, @Nullable List<Action> actions) {
    120     this.title = title;
    121     this.description = description;
    122     this.actions = actions;
    123   }
    124 
    125   @NonNull
    126   public static Action createChangeAirplaneModeAction(final Context context) {
    127     return new Action(
    128         context.getString(R.string.voicemail_action_turn_off_airplane_mode),
    129         new OnClickListener() {
    130           @Override
    131           public void onClick(View v) {
    132             Logger.get(context)
    133                 .logImpression(DialerImpression.Type.VVM_CHANGE_AIRPLANE_MODE_CLICKED);
    134             Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
    135             context.startActivity(intent);
    136           }
    137         });
    138   }
    139 
    140   @NonNull
    141   public static Action createSetPinAction(
    142       final Context context, PhoneAccountHandle phoneAccountHandle) {
    143     return new Action(
    144         context.getString(R.string.voicemail_action_set_pin),
    145         new OnClickListener() {
    146           @Override
    147           public void onClick(View v) {
    148             Logger.get(context)
    149                 .logImpression(DialerImpression.Type.VOICEMAIL_ALERT_SET_PIN_CLICKED);
    150             context.startActivity(
    151                 VoicemailComponent.get(context)
    152                     .getVoicemailClient()
    153                     .getSetPinIntent(context, phoneAccountHandle));
    154           }
    155         });
    156   }
    157 
    158   @NonNull
    159   public static Action createCallVoicemailAction(final Context context) {
    160     return new Action(
    161         context.getString(R.string.voicemail_action_call_voicemail),
    162         new OnClickListener() {
    163           @Override
    164           public void onClick(View v) {
    165             Logger.get(context).logImpression(DialerImpression.Type.VVM_CALL_VOICEMAIL_CLICKED);
    166             Intent intent = new Intent(Intent.ACTION_CALL, CallUtil.getVoicemailUri());
    167             context.startActivity(intent);
    168           }
    169         });
    170   }
    171 
    172   @NonNull
    173   public static Action createSyncAction(final Context context, final VoicemailStatus status) {
    174     return new Action(
    175         context.getString(R.string.voicemail_action_sync),
    176         new OnClickListener() {
    177           @Override
    178           public void onClick(View v) {
    179             Logger.get(context).logImpression(DialerImpression.Type.VVM_USER_SYNC);
    180             Intent intent = new Intent(VoicemailContract.ACTION_SYNC_VOICEMAIL);
    181             intent.setPackage(status.sourcePackage);
    182             context.sendBroadcast(intent);
    183           }
    184         });
    185   }
    186 
    187   @NonNull
    188   public static Action createRetryAction(final Context context, final VoicemailStatus status) {
    189     return new Action(
    190         context.getString(R.string.voicemail_action_retry),
    191         new OnClickListener() {
    192           @Override
    193           public void onClick(View v) {
    194             Logger.get(context).logImpression(DialerImpression.Type.VVM_USER_RETRY);
    195             Intent intent = new Intent(VoicemailContract.ACTION_SYNC_VOICEMAIL);
    196             intent.setPackage(status.sourcePackage);
    197             context.sendBroadcast(intent);
    198           }
    199         });
    200   }
    201 
    202   @NonNull
    203   public static Action createTurnArchiveOnAction(
    204       final Context context,
    205       DialerImpression.Type impressionToLog,
    206       final VoicemailStatus status,
    207       VoicemailStatusReader statusReader,
    208       VoicemailClient voicemailClient,
    209       PhoneAccountHandle phoneAccountHandle) {
    210     return new Action(
    211         context.getString(R.string.voicemail_action_turn_archive_on),
    212         new OnClickListener() {
    213           @Override
    214           public void onClick(View v) {
    215             Assert.checkArgument(
    216                 VoicemailComponent.get(context)
    217                     .getVoicemailClient()
    218                     .isVoicemailArchiveAvailable(context));
    219             Logger.get(context).logImpression(impressionToLog);
    220             voicemailClient.setVoicemailArchiveEnabled(context, phoneAccountHandle, true);
    221             Intent intent = new Intent(VoicemailContract.ACTION_SYNC_VOICEMAIL);
    222             intent.setPackage(status.sourcePackage);
    223             context.sendBroadcast(intent);
    224             statusReader.refresh();
    225           }
    226         });
    227   }
    228 
    229   @NonNull
    230   public static Action createDismissTurnArchiveOnAction(
    231       final Context context,
    232       DialerImpression.Type impressionToLog,
    233       VoicemailStatusReader statusReader,
    234       PerAccountSharedPreferences sharedPreferenceForAccount,
    235       String preferenceKeyToUpdate) {
    236     return new Action(
    237         context.getString(R.string.voicemail_action_dimiss),
    238         new OnClickListener() {
    239           @Override
    240           public void onClick(View v) {
    241             Assert.checkArgument(
    242                 VoicemailComponent.get(context)
    243                     .getVoicemailClient()
    244                     .isVoicemailArchiveAvailable(context));
    245             Logger.get(context).logImpression(impressionToLog);
    246             sharedPreferenceForAccount.edit().putBoolean(preferenceKeyToUpdate, true).apply();
    247             statusReader.refresh();
    248           }
    249         });
    250   }
    251 }
    252