From 49c5dcd4e6f09f3931a7df808db5562eb92ed14d Mon Sep 17 00:00:00 2001 From: twyen Date: Fri, 30 Mar 2018 10:56:48 -0700 Subject: Implement new blocking Old blocking is deprecated. Bug: 70989544 Test: TAP PiperOrigin-RevId: 191086875 Change-Id: Icde4963fce603fc4e16e486b78c070bd439e26e4 --- .../dialer/commandline/CommandLineModule.java | 14 ++- .../android/dialer/commandline/impl/Blocking.java | 96 ------------------ .../dialer/commandline/impl/BlockingCommand.java | 111 +++++++++++++++++++++ 3 files changed, 120 insertions(+), 101 deletions(-) delete mode 100644 java/com/android/dialer/commandline/impl/Blocking.java create mode 100644 java/com/android/dialer/commandline/impl/BlockingCommand.java (limited to 'java/com/android/dialer/commandline') diff --git a/java/com/android/dialer/commandline/CommandLineModule.java b/java/com/android/dialer/commandline/CommandLineModule.java index 612155662..915578722 100644 --- a/java/com/android/dialer/commandline/CommandLineModule.java +++ b/java/com/android/dialer/commandline/CommandLineModule.java @@ -16,7 +16,7 @@ package com.android.dialer.commandline; -import com.android.dialer.commandline.impl.Blocking; +import com.android.dialer.commandline.impl.BlockingCommand; import com.android.dialer.commandline.impl.CallCommand; import com.android.dialer.commandline.impl.Echo; import com.android.dialer.commandline.impl.Help; @@ -43,16 +43,20 @@ public abstract class CommandLineModule { private final Help help; private final Version version; private final Echo echo; - private final Blocking blocking; + private final BlockingCommand blockingCommand; private final CallCommand callCommand; @Inject AospCommandInjector( - Help help, Version version, Echo echo, Blocking blocking, CallCommand callCommand) { + Help help, + Version version, + Echo echo, + BlockingCommand blockingCommand, + CallCommand callCommand) { this.help = help; this.version = version; this.echo = echo; - this.blocking = blocking; + this.blockingCommand = blockingCommand; this.callCommand = callCommand; } @@ -60,7 +64,7 @@ public abstract class CommandLineModule { builder.addCommand("help", help); builder.addCommand("version", version); builder.addCommand("echo", echo); - builder.addCommand("blocking", blocking); + builder.addCommand("blocking", blockingCommand); builder.addCommand("call", callCommand); return builder; } diff --git a/java/com/android/dialer/commandline/impl/Blocking.java b/java/com/android/dialer/commandline/impl/Blocking.java deleted file mode 100644 index 2afd16522..000000000 --- a/java/com/android/dialer/commandline/impl/Blocking.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -package com.android.dialer.commandline.impl; - -import android.content.Context; -import android.support.annotation.NonNull; -import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; -import com.android.dialer.commandline.Arguments; -import com.android.dialer.commandline.Command; -import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor; -import com.android.dialer.inject.ApplicationContext; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListeningExecutorService; -import javax.inject.Inject; - -/** Block or unblock a number. */ -public class Blocking implements Command { - - @NonNull - @Override - public String getShortDescription() { - return "block or unblock numbers"; - } - - @NonNull - @Override - public String getUsage() { - return "blocking block|unblock|isblocked number\n\n" + "number should be e.164 formatted"; - } - - private final Context appContext; - private final ListeningExecutorService executorService; - - @Inject - Blocking( - @ApplicationContext Context context, - @BackgroundExecutor ListeningExecutorService executorService) { - this.appContext = context; - this.executorService = executorService; - } - - @Override - public ListenableFuture run(Arguments args) throws IllegalCommandLineArgumentException { - // AsyncQueryHandler must be created on a thread with looper. - // TODO(a bug): Use blocking version - FilteredNumberAsyncQueryHandler asyncQueryHandler = - new FilteredNumberAsyncQueryHandler(appContext); - return executorService.submit(() -> doInBackground(args, asyncQueryHandler)); - } - - private String doInBackground(Arguments args, FilteredNumberAsyncQueryHandler asyncQueryHandler) { - if (args.getPositionals().isEmpty()) { - return getUsage(); - } - - String command = args.getPositionals().get(0); - - if ("block".equals(command)) { - String number = args.getPositionals().get(1); - asyncQueryHandler.blockNumber((unused) -> {}, number, null); - return "blocked " + number; - } - - if ("unblock".equals(command)) { - String number = args.getPositionals().get(1); - Integer id = asyncQueryHandler.getBlockedIdSynchronous(number, null); - if (id == null) { - return number + " is not blocked"; - } - asyncQueryHandler.unblock((unusedRows, unusedValues) -> {}, id); - return "unblocked " + number; - } - - if ("isblocked".equals(command)) { - String number = args.getPositionals().get(1); - Integer id = asyncQueryHandler.getBlockedIdSynchronous(number, null); - return id == null ? "false" : "true"; - } - - return getUsage(); - } -} diff --git a/java/com/android/dialer/commandline/impl/BlockingCommand.java b/java/com/android/dialer/commandline/impl/BlockingCommand.java new file mode 100644 index 000000000..c8f893422 --- /dev/null +++ b/java/com/android/dialer/commandline/impl/BlockingCommand.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.dialer.commandline.impl; + +import android.content.Context; +import android.support.annotation.NonNull; +import com.android.dialer.DialerPhoneNumber; +import com.android.dialer.blocking.Blocking; +import com.android.dialer.commandline.Arguments; +import com.android.dialer.commandline.Command; +import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor; +import com.android.dialer.inject.ApplicationContext; +import com.android.dialer.phonelookup.PhoneLookupComponent; +import com.android.dialer.phonelookup.PhoneLookupInfo; +import com.android.dialer.phonelookup.consolidator.PhoneLookupInfoConsolidator; +import com.android.dialer.phonenumberproto.DialerPhoneNumberUtil; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import javax.inject.Inject; + +/** Block or unblock a number. */ +public class BlockingCommand implements Command { + + @NonNull + @Override + public String getShortDescription() { + return "block or unblock numbers"; + } + + @NonNull + @Override + public String getUsage() { + return "blocking block|unblock|isblocked number\n\n" + "number should be e.164 formatted"; + } + + private final Context appContext; + private final ListeningExecutorService executorService; + + @Inject + BlockingCommand( + @ApplicationContext Context context, + @BackgroundExecutor ListeningExecutorService executorService) { + this.appContext = context; + this.executorService = executorService; + } + + @Override + public ListenableFuture run(Arguments args) throws IllegalCommandLineArgumentException { + if (args.getPositionals().isEmpty()) { + return Futures.immediateFuture(getUsage()); + } + + String command = args.getPositionals().get(0); + + if ("block".equals(command)) { + String number = args.getPositionals().get(1); + return Futures.transform( + Blocking.block(appContext, executorService, number, null), + (unused) -> "blocked " + number, + MoreExecutors.directExecutor()); + } + + if ("unblock".equals(command)) { + String number = args.getPositionals().get(1); + return Futures.transform( + Blocking.unblock(appContext, executorService, number, null), + (unused) -> "unblocked " + number, + MoreExecutors.directExecutor()); + } + + if ("isblocked".equals(command)) { + String number = args.getPositionals().get(1); + ListenableFuture dialerPhoneNumberFuture = + executorService.submit( + () -> new DialerPhoneNumberUtil(PhoneNumberUtil.getInstance()).parse(number, null)); + + ListenableFuture lookupFuture = + Futures.transformAsync( + dialerPhoneNumberFuture, + (dialerPhoneNumber) -> + PhoneLookupComponent.get(appContext) + .compositePhoneLookup() + .lookup(dialerPhoneNumber), + executorService); + + return Futures.transform( + lookupFuture, + (info) -> new PhoneLookupInfoConsolidator(info).isBlocked() ? "true" : "false", + MoreExecutors.directExecutor()); + } + + return Futures.immediateFuture(getUsage()); + } +} -- cgit v1.2.3