• 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.commandline.impl;
18 
19 import android.content.Context;
20 import android.support.annotation.NonNull;
21 import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
22 import com.android.dialer.commandline.Arguments;
23 import com.android.dialer.commandline.Command;
24 import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
25 import com.android.dialer.inject.ApplicationContext;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.ListeningExecutorService;
28 import javax.inject.Inject;
29 
30 /** Block or unblock a number. */
31 public class Blocking implements Command {
32 
33   @NonNull
34   @Override
getShortDescription()35   public String getShortDescription() {
36     return "block or unblock numbers";
37   }
38 
39   @NonNull
40   @Override
getUsage()41   public String getUsage() {
42     return "blocking block|unblock|isblocked number\n\n" + "number should be e.164 formatted";
43   }
44 
45   private final Context appContext;
46   private final ListeningExecutorService executorService;
47 
48   @Inject
Blocking( @pplicationContext Context context, @BackgroundExecutor ListeningExecutorService executorService)49   Blocking(
50       @ApplicationContext Context context,
51       @BackgroundExecutor ListeningExecutorService executorService) {
52     this.appContext = context;
53     this.executorService = executorService;
54   }
55 
56   @Override
run(Arguments args)57   public ListenableFuture<String> run(Arguments args) throws IllegalCommandLineArgumentException {
58     // AsyncQueryHandler must be created on a thread with looper.
59     // TODO(a bug): Use blocking version
60     FilteredNumberAsyncQueryHandler asyncQueryHandler =
61         new FilteredNumberAsyncQueryHandler(appContext);
62     return executorService.submit(() -> doInBackground(args, asyncQueryHandler));
63   }
64 
doInBackground(Arguments args, FilteredNumberAsyncQueryHandler asyncQueryHandler)65   private String doInBackground(Arguments args, FilteredNumberAsyncQueryHandler asyncQueryHandler) {
66     if (args.getPositionals().isEmpty()) {
67       return getUsage();
68     }
69 
70     String command = args.getPositionals().get(0);
71 
72     if ("block".equals(command)) {
73       String number = args.getPositionals().get(1);
74       asyncQueryHandler.blockNumber((unused) -> {}, number, null);
75       return "blocked " + number;
76     }
77 
78     if ("unblock".equals(command)) {
79       String number = args.getPositionals().get(1);
80       Integer id = asyncQueryHandler.getBlockedIdSynchronous(number, null);
81       if (id == null) {
82         return number + " is not blocked";
83       }
84       asyncQueryHandler.unblock((unusedRows, unusedValues) -> {}, id);
85       return "unblocked " + number;
86     }
87 
88     if ("isblocked".equals(command)) {
89       String number = args.getPositionals().get(1);
90       Integer id = asyncQueryHandler.getBlockedIdSynchronous(number, null);
91       return id == null ? "false" : "true";
92     }
93 
94     return getUsage();
95   }
96 }
97