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