• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.precall.impl;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.support.annotation.MainThread;
22 import android.telecom.PhoneAccount;
23 import com.android.dialer.callintent.CallIntentBuilder;
24 import com.android.dialer.precall.PreCallAction;
25 import com.android.dialer.precall.PreCallCoordinator;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.ImmutableList;
28 
29 /**
30  * Fix common malformed number before it is dialed. Rewrite the number to the first handler that can
31  * handle it
32  */
33 public class MalformedNumberRectifier implements PreCallAction {
34 
35   /** Handler for individual rules. */
36   public interface MalformedNumberHandler {
37 
38     /** @return the number to be corrected to. */
39     @MainThread
handle(Context context, String number)40     Optional<String> handle(Context context, String number);
41   }
42 
43   private final ImmutableList<MalformedNumberHandler> handlers;
44 
MalformedNumberRectifier(ImmutableList<MalformedNumberHandler> handlers)45   MalformedNumberRectifier(ImmutableList<MalformedNumberHandler> handlers) {
46     this.handlers = handlers;
47   }
48 
49   @Override
requiresUi(Context context, CallIntentBuilder builder)50   public boolean requiresUi(Context context, CallIntentBuilder builder) {
51     return false;
52   }
53 
54   @Override
runWithoutUi(Context context, CallIntentBuilder builder)55   public void runWithoutUi(Context context, CallIntentBuilder builder) {
56     if (!PhoneAccount.SCHEME_TEL.equals(builder.getUri().getScheme())) {
57       return;
58     }
59     String number = builder.getUri().getSchemeSpecificPart();
60 
61     for (MalformedNumberHandler handler : handlers) {
62       Optional<String> result = handler.handle(context, number);
63       if (result.isPresent()) {
64         builder.setUri(Uri.fromParts(PhoneAccount.SCHEME_TEL, result.get(), null));
65         return;
66       }
67     }
68   }
69 
70   @Override
runWithUi(PreCallCoordinator coordinator)71   public void runWithUi(PreCallCoordinator coordinator) {
72     runWithoutUi(coordinator.getActivity(), coordinator.getBuilder());
73   }
74 
75   @Override
onDiscard()76   public void onDiscard() {}
77 }
78