• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
51   /** Something the user can click on to resolve an error, such as retrying or calling voicemail */
52   public static class Action {
53 
54     private final CharSequence text;
55     private final View.OnClickListener listener;
56     private final boolean raised;
57 
Action(CharSequence text, View.OnClickListener listener)58     public Action(CharSequence text, View.OnClickListener listener) {
59       this(text, listener, false);
60     }
61 
Action(CharSequence text, View.OnClickListener listener, boolean raised)62     public Action(CharSequence text, View.OnClickListener listener, boolean raised) {
63       this.text = text;
64       this.listener = listener;
65       this.raised = raised;
66     }
67 
getText()68     public CharSequence getText() {
69       return text;
70     }
71 
getListener()72     public View.OnClickListener getListener() {
73       return listener;
74     }
75 
isRaised()76     public boolean isRaised() {
77       return raised;
78     }
79   }
80 
getTitle()81   public CharSequence getTitle() {
82     return title;
83   }
84 
getDescription()85   public CharSequence getDescription() {
86     return description;
87   }
88 
89   @Nullable
getActions()90   public List<Action> getActions() {
91     return actions;
92   }
93 
isModal()94   public boolean isModal() {
95     return modal;
96   }
97 
setModal(boolean value)98   public VoicemailErrorMessage setModal(boolean value) {
99     modal = value;
100     return this;
101   }
102 
VoicemailErrorMessage(CharSequence title, CharSequence description, Action... actions)103   public VoicemailErrorMessage(CharSequence title, CharSequence description, Action... actions) {
104     this(title, description, Arrays.asList(actions));
105   }
106 
VoicemailErrorMessage( CharSequence title, CharSequence description, @Nullable List<Action> actions)107   public VoicemailErrorMessage(
108       CharSequence title, CharSequence description, @Nullable List<Action> actions) {
109     this.title = title;
110     this.description = description;
111     this.actions = actions;
112   }
113 
114   @NonNull
createChangeAirplaneModeAction(final Context context)115   public static Action createChangeAirplaneModeAction(final Context context) {
116     return new Action(
117         context.getString(R.string.voicemail_action_turn_off_airplane_mode),
118         new OnClickListener() {
119           @Override
120           public void onClick(View v) {
121             Logger.get(context)
122                 .logImpression(DialerImpression.Type.VVM_CHANGE_AIRPLANE_MODE_CLICKED);
123             Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
124             context.startActivity(intent);
125           }
126         });
127   }
128 
129   @NonNull
130   public static Action createSetPinAction(
131       final Context context, PhoneAccountHandle phoneAccountHandle) {
132     return new Action(
133         context.getString(R.string.voicemail_action_set_pin),
134         new OnClickListener() {
135           @Override
136           public void onClick(View v) {
137             Logger.get(context)
138                 .logImpression(DialerImpression.Type.VOICEMAIL_ALERT_SET_PIN_CLICKED);
139             context.startActivity(
140                 VoicemailComponent.get(context)
141                     .getVoicemailClient()
142                     .getSetPinIntent(context, phoneAccountHandle));
143           }
144         });
145   }
146 
147   @NonNull
148   public static Action createCallVoicemailAction(final Context context) {
149     return new Action(
150         context.getString(R.string.voicemail_action_call_voicemail),
151         new OnClickListener() {
152           @Override
153           public void onClick(View v) {
154             Logger.get(context).logImpression(DialerImpression.Type.VVM_CALL_VOICEMAIL_CLICKED);
155             Intent intent = new Intent(Intent.ACTION_CALL, CallUtil.getVoicemailUri());
156             context.startActivity(intent);
157           }
158         });
159   }
160 
161   @NonNull
162   public static Action createSyncAction(final Context context, final VoicemailStatus status) {
163     return new Action(
164         context.getString(R.string.voicemail_action_sync),
165         new OnClickListener() {
166           @Override
167           public void onClick(View v) {
168             Logger.get(context).logImpression(DialerImpression.Type.VVM_USER_SYNC);
169             Intent intent = new Intent(VoicemailContract.ACTION_SYNC_VOICEMAIL);
170             intent.setPackage(status.sourcePackage);
171             context.sendBroadcast(intent);
172           }
173         });
174   }
175 
176   @NonNull
177   public static Action createRetryAction(final Context context, final VoicemailStatus status) {
178     return new Action(
179         context.getString(R.string.voicemail_action_retry),
180         new OnClickListener() {
181           @Override
182           public void onClick(View v) {
183             Logger.get(context).logImpression(DialerImpression.Type.VVM_USER_RETRY);
184             Intent intent = new Intent(VoicemailContract.ACTION_SYNC_VOICEMAIL);
185             intent.setPackage(status.sourcePackage);
186             context.sendBroadcast(intent);
187           }
188         });
189   }
190 
191   @NonNull
192   public static Action createTurnArchiveOnAction(
193       final Context context,
194       DialerImpression.Type impressionToLog,
195       final VoicemailStatus status,
196       VoicemailStatusReader statusReader,
197       VoicemailClient voicemailClient,
198       PhoneAccountHandle phoneAccountHandle) {
199     return new Action(
200         context.getString(R.string.voicemail_action_turn_archive_on),
201         new OnClickListener() {
202           @Override
203           public void onClick(View v) {
204             Assert.checkArgument(
205                 VoicemailComponent.get(context)
206                     .getVoicemailClient()
207                     .isVoicemailArchiveAvailable(context));
208             Logger.get(context).logImpression(impressionToLog);
209             voicemailClient.setVoicemailArchiveEnabled(context, phoneAccountHandle, true);
210             Intent intent = new Intent(VoicemailContract.ACTION_SYNC_VOICEMAIL);
211             intent.setPackage(status.sourcePackage);
212             context.sendBroadcast(intent);
213             statusReader.refresh();
214           }
215         });
216   }
217 
218   @NonNull
219   public static Action createDismissTurnArchiveOnAction(
220       final Context context,
221       DialerImpression.Type impressionToLog,
222       VoicemailStatusReader statusReader,
223       PerAccountSharedPreferences sharedPreferenceForAccount,
224       String preferenceKeyToUpdate) {
225     return new Action(
226         context.getString(R.string.voicemail_action_dimiss),
227         new OnClickListener() {
228           @Override
229           public void onClick(View v) {
230             Assert.checkArgument(
231                 VoicemailComponent.get(context)
232                     .getVoicemailClient()
233                     .isVoicemailArchiveAvailable(context));
234             Logger.get(context).logImpression(impressionToLog);
235             sharedPreferenceForAccount.edit().putBoolean(preferenceKeyToUpdate, true).apply();
236             statusReader.refresh();
237           }
238         });
239   }
240 }
241