From b0ed171b1c9d5b5d5e686b31be623c99ced15cf0 Mon Sep 17 00:00:00 2001 From: twyen Date: Tue, 7 Nov 2017 14:19:23 -0800 Subject: Implement PreCallCoordinator When the user makes a call, the PreCallCoordinator will get a list of actions to perform from PreCallActionProviderComponent and apply them to the pending CallIntentBuilder, before sending the built intent to telecom Bug: 64216442 Test: PreCallExecutorImplTest PiperOrigin-RevId: 174911537 Change-Id: Ide5ec431d4e6e7879fcdfdbbbd7ea5eed9963b2c --- java/com/android/dialer/precall/PreCall.java | 44 ++++++ java/com/android/dialer/precall/PreCallAction.java | 46 +++++++ .../android/dialer/precall/PreCallComponent.java | 37 +++++ .../android/dialer/precall/PreCallCoordinator.java | 68 +++++++++ .../dialer/precall/impl/AndroidManifest.xml | 29 ++++ .../dialer/precall/impl/PreCallActivity.java | 58 ++++++++ .../precall/impl/PreCallCoordinatorImpl.java | 153 +++++++++++++++++++++ .../android/dialer/precall/impl/PreCallImpl.java | 46 +++++++ .../android/dialer/precall/impl/PreCallModule.java | 31 +++++ .../dialer/precall/impl/res/values/styles.xml | 29 ++++ .../dialer/precall/testing/TestPreCallModule.java | 59 ++++++++ 11 files changed, 600 insertions(+) create mode 100644 java/com/android/dialer/precall/PreCall.java create mode 100644 java/com/android/dialer/precall/PreCallAction.java create mode 100644 java/com/android/dialer/precall/PreCallComponent.java create mode 100644 java/com/android/dialer/precall/PreCallCoordinator.java create mode 100644 java/com/android/dialer/precall/impl/AndroidManifest.xml create mode 100644 java/com/android/dialer/precall/impl/PreCallActivity.java create mode 100644 java/com/android/dialer/precall/impl/PreCallCoordinatorImpl.java create mode 100644 java/com/android/dialer/precall/impl/PreCallImpl.java create mode 100644 java/com/android/dialer/precall/impl/PreCallModule.java create mode 100644 java/com/android/dialer/precall/impl/res/values/styles.xml create mode 100644 java/com/android/dialer/precall/testing/TestPreCallModule.java (limited to 'java/com/android/dialer/precall') diff --git a/java/com/android/dialer/precall/PreCall.java b/java/com/android/dialer/precall/PreCall.java new file mode 100644 index 000000000..93fd8e9b6 --- /dev/null +++ b/java/com/android/dialer/precall/PreCall.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2017 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.precall; + +import android.content.Context; +import android.content.Intent; +import android.support.annotation.MainThread; +import android.support.annotation.NonNull; +import com.android.dialer.callintent.CallIntentBuilder; +import com.google.common.collect.ImmutableList; + +/** Interface to prepare a {@link CallIntentBuilder} before placing the call with telecom. */ +public interface PreCall { + + /** + * @return a list of {@link PreCallAction} in execution order for the {@link PreCallCoordinator} + * to run. + */ + @NonNull + ImmutableList getActions(); + + /** + * @return a intent when started as activity, will perform the pre-call actions and then place a + * call. TODO(twyen): if all actions do not require an UI, return a intent that will place the + * call directly instead. + */ + @NonNull + @MainThread + Intent buildIntent(Context context, CallIntentBuilder builder); +} diff --git a/java/com/android/dialer/precall/PreCallAction.java b/java/com/android/dialer/precall/PreCallAction.java new file mode 100644 index 000000000..9434694a4 --- /dev/null +++ b/java/com/android/dialer/precall/PreCallAction.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2017 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.precall; + +import android.support.annotation.MainThread; +import com.android.dialer.callintent.CallIntentBuilder; + +/** + * An action to perform before the call is made. The action should inspect and modify the {@link + * CallIntentBuilder} to generate full information for the call. For example, showing a dialog to + * select the phone account on a multi-SIM device, ask if RTT should be enabled, or rewrite the + * number for roaming calls. + */ +public interface PreCallAction { + + /** + * Runs the action. Should block on the main thread until the action is finished. If the action is + * not instantaneous, {@link PreCallCoordinator#startPendingAction()} should be called to release + * the thread and continue later. + */ + @MainThread + void run(PreCallCoordinator coordinator); + + /** + * Called when the UI is being paused when a {@link PreCallCoordinator.PendingAction} is started, + * and the action is going to be discarded. If the action is showing a dialog the dialog should be + * dismissed. The action should not retain state, a new instance of the action will be re-run when + * the UI is resumed. + */ + @MainThread + void onDiscard(); +} diff --git a/java/com/android/dialer/precall/PreCallComponent.java b/java/com/android/dialer/precall/PreCallComponent.java new file mode 100644 index 000000000..00adde56d --- /dev/null +++ b/java/com/android/dialer/precall/PreCallComponent.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2017 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.precall; + +import android.content.Context; +import com.android.dialer.inject.HasRootComponent; +import dagger.Subcomponent; + +/** Daggaer component for {@link PreCall} */ +@Subcomponent +public abstract class PreCallComponent { + public abstract PreCall getPreCall(); + + public static PreCallComponent get(Context context) { + return ((HasComponent) ((HasRootComponent) context.getApplicationContext()).component()) + .preCallActionsComponent(); + } + + /** Used to refer to the root application component. */ + public interface HasComponent { + PreCallComponent preCallActionsComponent(); + } +} diff --git a/java/com/android/dialer/precall/PreCallCoordinator.java b/java/com/android/dialer/precall/PreCallCoordinator.java new file mode 100644 index 000000000..b5e9e8594 --- /dev/null +++ b/java/com/android/dialer/precall/PreCallCoordinator.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2017 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.precall; + +import android.app.Activity; +import android.support.annotation.MainThread; +import android.support.annotation.NonNull; +import com.android.dialer.callintent.CallIntentBuilder; + +/** + * Runs {@link PreCallAction} one by one to prepare a {@link + * com.android.dialer.callintent.CallIntentBuilder} for a call. + */ +public interface PreCallCoordinator { + + @NonNull + CallIntentBuilder getBuilder(); + + /** + * @return the activity to attach the UI to. Returns {@link null} if the coordinator is running on + * headless mode. TODO(twyen): implement headless mode. + */ + @NonNull + Activity getActivity(); + + /** + * Called by a {@link PreCallAction} to abort the call. For example, the user has dismissed the + * dialog and must start over. + */ + void abortCall(); + + /** + * Callback from a {@link PreCallAction} to signal the action started by {@link + * PreCallCoordinator#startPendingAction()} has finished. + */ + interface PendingAction { + + @MainThread + void finish(); + } + + /** + * Called by the current running {@link PreCallAction} to release the main thread and resume + * pre-call later. + * + * @return a {@link PendingAction} which {@link PendingAction#finish(boolean)} should be called to + * resume pre-call. For example the action shows a dialog to the user, startPendingAction() + * should be called as the action will not be finished immediately. When the dialog is + * completed, {@code finish()} is then called to continue the next step. + */ + @MainThread + @NonNull + PendingAction startPendingAction(); +} diff --git a/java/com/android/dialer/precall/impl/AndroidManifest.xml b/java/com/android/dialer/precall/impl/AndroidManifest.xml new file mode 100644 index 000000000..d3d0f538d --- /dev/null +++ b/java/com/android/dialer/precall/impl/AndroidManifest.xml @@ -0,0 +1,29 @@ + + + + + + + + diff --git a/java/com/android/dialer/precall/impl/PreCallActivity.java b/java/com/android/dialer/precall/impl/PreCallActivity.java new file mode 100644 index 000000000..938d31d5f --- /dev/null +++ b/java/com/android/dialer/precall/impl/PreCallActivity.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 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.precall.impl; + +import android.app.Activity; +import android.os.Bundle; +import android.support.annotation.Nullable; + +/** A transparent activity to host dialogs for {@link PreCallCoordinatorImpl} */ +public class PreCallActivity extends Activity { + + private PreCallCoordinatorImpl preCallCoordinator; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + preCallCoordinator = new PreCallCoordinatorImpl(this); + preCallCoordinator.onCreate(getIntent(), savedInstanceState); + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + preCallCoordinator.onRestoreInstanceState(savedInstanceState); + } + + @Override + public void onResume() { + super.onResume(); + preCallCoordinator.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + preCallCoordinator.onPause(); + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + preCallCoordinator.onSaveInstanceState(outState); + } +} diff --git a/java/com/android/dialer/precall/impl/PreCallCoordinatorImpl.java b/java/com/android/dialer/precall/impl/PreCallCoordinatorImpl.java new file mode 100644 index 000000000..25083ef0b --- /dev/null +++ b/java/com/android/dialer/precall/impl/PreCallCoordinatorImpl.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2017 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.precall.impl; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import com.android.dialer.callintent.CallIntentBuilder; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.android.dialer.precall.PreCallAction; +import com.android.dialer.precall.PreCallComponent; +import com.android.dialer.precall.PreCallCoordinator; +import com.android.dialer.telecom.TelecomUtil; +import com.google.common.collect.ImmutableList; + +/** + * Implements {@link PreCallCoordinator}. Listens to the life cycle of {@link PreCallActivity} to + * save/restore states. + */ +public class PreCallCoordinatorImpl implements PreCallCoordinator { + + private static final String SAVED_STATE_CURRENT_ACTION = "current_action"; + + static final String EXTRA_CALL_INTENT_BUILDER = "extra_call_intent_builder"; + + @NonNull private final Activity activity; + + private CallIntentBuilder builder; + private ImmutableList actions; + private int currentActionIndex = 0; + private PreCallAction currentAction; + private PendingAction pendingAction; + private boolean aborted = false; + + PreCallCoordinatorImpl(@NonNull Activity activity) { + this.activity = Assert.isNotNull(activity); + } + + void onCreate(Intent intent, @Nullable Bundle savedInstanceState) { + LogUtil.enterBlock("PreCallCoordinatorImpl.onCreate"); + if (savedInstanceState != null) { + currentActionIndex = savedInstanceState.getInt(SAVED_STATE_CURRENT_ACTION); + builder = Assert.isNotNull(savedInstanceState.getParcelable(EXTRA_CALL_INTENT_BUILDER)); + } else { + builder = Assert.isNotNull(intent.getParcelableExtra(EXTRA_CALL_INTENT_BUILDER)); + } + actions = PreCallComponent.get(activity).getPreCall().getActions(); + } + + void onRestoreInstanceState(Bundle savedInstanceState) { + currentActionIndex = savedInstanceState.getInt(SAVED_STATE_CURRENT_ACTION); + builder = savedInstanceState.getParcelable(EXTRA_CALL_INTENT_BUILDER); + } + + void onResume() { + runNextAction(); + } + + void onPause() { + if (currentAction != null) { + currentAction.onDiscard(); + } + currentAction = null; + } + + void onSaveInstanceState(Bundle outState) { + outState.putInt(SAVED_STATE_CURRENT_ACTION, currentActionIndex); + outState.putParcelable(EXTRA_CALL_INTENT_BUILDER, builder); + } + + private void runNextAction() { + LogUtil.enterBlock("PreCallCoordinatorImpl.runNextAction"); + Assert.checkArgument(currentAction == null); + if (currentActionIndex >= actions.size()) { + TelecomUtil.placeCall(activity, builder.build()); + activity.finish(); + return; + } + LogUtil.i("PreCallCoordinatorImpl.runNextAction", "running " + actions.get(currentActionIndex)); + currentAction = actions.get(currentActionIndex); + actions.get(currentActionIndex).run(this); + if (pendingAction == null) { + onActionFinished(); + } + } + + private void onActionFinished() { + LogUtil.enterBlock("PreCallCoordinatorImpl.onActionFinished"); + Assert.isNotNull(currentAction); + currentAction = null; + currentActionIndex++; + if (!aborted) { + runNextAction(); + } else { + activity.finish(); + } + } + + @Override + public void abortCall() { + Assert.checkState(currentAction != null); + aborted = true; + } + + @NonNull + @Override + public CallIntentBuilder getBuilder() { + return builder; + } + + @NonNull + @Override + public Activity getActivity() { + return activity; + } + + @Override + @NonNull + public PendingAction startPendingAction() { + Assert.isMainThread(); + Assert.isNotNull(currentAction); + Assert.checkArgument(pendingAction == null); + pendingAction = new PendingActionImpl(); + return pendingAction; + } + + private class PendingActionImpl implements PendingAction { + + @Override + public void finish() { + Assert.checkArgument(pendingAction == this); + pendingAction = null; + onActionFinished(); + } + } +} diff --git a/java/com/android/dialer/precall/impl/PreCallImpl.java b/java/com/android/dialer/precall/impl/PreCallImpl.java new file mode 100644 index 000000000..ac9750ef2 --- /dev/null +++ b/java/com/android/dialer/precall/impl/PreCallImpl.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2017 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.precall.impl; + +import android.content.Context; +import android.content.Intent; +import android.support.annotation.NonNull; +import com.android.dialer.callintent.CallIntentBuilder; +import com.android.dialer.precall.PreCall; +import com.android.dialer.precall.PreCallAction; +import com.google.common.collect.ImmutableList; +import javax.inject.Inject; + +/** Implementation of {@link PreCall} */ +public class PreCallImpl implements PreCall { + + @Inject + PreCallImpl() {} + + @Override + public ImmutableList getActions() { + return ImmutableList.of(); + } + + @NonNull + @Override + public Intent buildIntent(Context context, CallIntentBuilder builder) { + Intent intent = new Intent(context, PreCallActivity.class); + intent.putExtra(PreCallCoordinatorImpl.EXTRA_CALL_INTENT_BUILDER, builder); + return intent; + } +} diff --git a/java/com/android/dialer/precall/impl/PreCallModule.java b/java/com/android/dialer/precall/impl/PreCallModule.java new file mode 100644 index 000000000..608cd5a8f --- /dev/null +++ b/java/com/android/dialer/precall/impl/PreCallModule.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2017 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.precall.impl; + +import com.android.dialer.precall.PreCall; +import dagger.Binds; +import dagger.Module; +import javax.inject.Singleton; + +/** Dagger module for {@link PreCall}. */ +@Module +public abstract class PreCallModule { + + @Binds + @Singleton + public abstract PreCall bindPreCall(PreCallImpl simulator); +} diff --git a/java/com/android/dialer/precall/impl/res/values/styles.xml b/java/com/android/dialer/precall/impl/res/values/styles.xml new file mode 100644 index 000000000..dd41265a4 --- /dev/null +++ b/java/com/android/dialer/precall/impl/res/values/styles.xml @@ -0,0 +1,29 @@ + + + + + + + diff --git a/java/com/android/dialer/precall/testing/TestPreCallModule.java b/java/com/android/dialer/precall/testing/TestPreCallModule.java new file mode 100644 index 000000000..b777de890 --- /dev/null +++ b/java/com/android/dialer/precall/testing/TestPreCallModule.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2017 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.precall.testing; + +import android.content.Context; +import android.content.Intent; +import android.support.annotation.NonNull; +import com.android.dialer.callintent.CallIntentBuilder; +import com.android.dialer.precall.PreCall; +import com.android.dialer.precall.PreCallAction; +import com.google.common.collect.ImmutableList; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +/** Provides test implementation of {@link PreCall} */ +@Module +public class TestPreCallModule { + private static PreCall preCall = new StubPreCall(); + + public static void setPreCall(PreCall preCall) { + TestPreCallModule.preCall = preCall; + } + + @Provides + @Singleton + public static PreCall providePreCall() { + return preCall; + } + + private static class StubPreCall implements PreCall { + + @NonNull + @Override + public ImmutableList getActions() { + return ImmutableList.of(); + } + + @NonNull + @Override + public Intent buildIntent(Context context, CallIntentBuilder builder) { + return builder.build(); + } + } +} -- cgit v1.2.3