diff options
author | Srinivas Visvanathan <sriniv@google.com> | 2017-04-13 10:47:27 -0700 |
---|---|---|
committer | Srinivas Visvanathan <sriniv@google.com> | 2017-04-13 14:09:51 -0700 |
commit | a16cb24e5b5690af55597ad5c8540a7c90ae146a (patch) | |
tree | 466845d17185cf6155d37e4ab23c0665fc6a440f | |
parent | 6b0ebb255e2d23c4f95e649a680c892d2d08bed7 (diff) | |
download | Dialer-a16cb24e5b5690af55597ad5c8540a7c90ae146a.tar.gz |
Cleanup and simplification in Dialer
- Removed CallActionReceiver which is not used.
- Removed ClassFactory; TelecomUiManager is now constructed directly.
- Simplified UiBluetoothManager area. No longer have base class, telecom
specific implementation. Telecom bits were merged into main
UiBluetoothMonitor. Also its no longer a singleton. TelecomActivity
constructs it and OngoingCallFragment re-uses that one.
- Simplified logic in OngoingCallFragment to fetch Hfp connected state.
Bug: 37251324
Test: Used dialer on Mojave. Made test calls. Turned off/on BT on phone.
Change-Id: I9b8b5c2bc43a8ef8f77c7f201e2c0059b1735749
10 files changed, 130 insertions, 333 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 65ff7a2d..0d3c89a9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -65,13 +65,5 @@ <action android:name="android.telecom.InCallService"/> </intent-filter> </service> - - <receiver android:name=".bluetooth.BluetoothBroadcastReceiver"> - <intent-filter> - <action android:name="android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED"/> - <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/> - <action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED"/> - </intent-filter> - </receiver> </application> </manifest> diff --git a/src/com/android/car/dialer/CallActionsReceiver.java b/src/com/android/car/dialer/CallActionsReceiver.java deleted file mode 100644 index ea036a63..00000000 --- a/src/com/android/car/dialer/CallActionsReceiver.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2015 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.car.dialer; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.telecom.Call; - -import com.android.car.dialer.telecom.UiCall; -import com.android.car.dialer.telecom.UiCallManager; - -/** - * Performs actions on ongoing calls. - */ -public class CallActionsReceiver extends BroadcastReceiver { - /** - * Answer the incoming call. No-op if there isn't one. - */ - public static final String ACTION_ANSWER_INCOMING_CALL = "action_answer_incoming_call"; - /** - * Reject the incoming call. No-op if there isn't one. - */ - public static final String ACTION_REJECT_INCOMING_CALL = "action_reject_incoming_call"; - /** - * Call a phone number. This will take advantage of any logic in - * {@link UiCallManager#safePlaceCall(String, boolean)}. - * However, it will pass null in for CarBluetoothConnectionManager due to the transient - * lifecycle of broadcast receivers. - */ - public static final String ACTION_CALL_NUMBER = "action_call_number"; - /** - * Extra to store the number to call. - */ - public static final String EXTRA_PHONE_NUMBER = "extra_phone_number"; - - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - if (action == null) { - return; - } - - UiCallManager uiCallManager = UiCallManager.getInstance(context); - - UiCall call; - switch(action) { - case ACTION_ANSWER_INCOMING_CALL: - call = uiCallManager.getCallWithState(Call.STATE_RINGING); - if (call != null) { - uiCallManager.getInstance(context).answerCall(call); - } - break; - case ACTION_REJECT_INCOMING_CALL: - call = uiCallManager.getCallWithState(Call.STATE_RINGING); - if (call != null) { - uiCallManager.getInstance(context).rejectCall(call, false, null); - } - break; - case ACTION_CALL_NUMBER: - uiCallManager.safePlaceCall(intent.getStringExtra(EXTRA_PHONE_NUMBER), false); - break; - } - } -} diff --git a/src/com/android/car/dialer/ClassFactory.java b/src/com/android/car/dialer/ClassFactory.java deleted file mode 100644 index 327da5f9..00000000 --- a/src/com/android/car/dialer/ClassFactory.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2015 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.car.dialer; - -import com.android.car.dialer.bluetooth.UiBluetoothMonitor; -import com.android.car.dialer.bluetooth.embedded.UiBluetoothMonitorImpl; -import com.android.car.dialer.telecom.UiCallManager; -import com.android.car.dialer.telecom.embedded.TelecomUiCallManager; - -/** - * Class factory that should create objects for different Dialer App implementations. - */ -public class ClassFactory { - - public static Factory getFactory() { - return new FactoryForEmbeddedMode(); - } - - public interface Factory { - UiCallManager createCarTelecomManager(); - UiBluetoothMonitor createBluetoothMonitor(); - } - - private static class FactoryForEmbeddedMode implements Factory { - - @Override - public UiCallManager createCarTelecomManager() { - return new TelecomUiCallManager(); - } - - @Override - public UiBluetoothMonitor createBluetoothMonitor() { - return new UiBluetoothMonitorImpl(); - } - } -} diff --git a/src/com/android/car/dialer/OngoingCallFragment.java b/src/com/android/car/dialer/OngoingCallFragment.java index a01b5b1b..9c322412 100644 --- a/src/com/android/car/dialer/OngoingCallFragment.java +++ b/src/com/android/car/dialer/OngoingCallFragment.java @@ -43,7 +43,6 @@ import android.widget.LinearLayout; import android.widget.TextView; import com.android.car.apps.common.CircleBitmapDrawable; import com.android.car.apps.common.FabDrawable; -import com.android.car.dialer.bluetooth.UiBluetoothMonitor; import com.android.car.dialer.telecom.TelecomUtils; import com.android.car.dialer.telecom.UiCall; import com.android.car.dialer.telecom.UiCallManager; @@ -100,18 +99,21 @@ public class OngoingCallFragment extends Fragment { private List<View> mDialpadViews; private String mLoadedNumber; private CharSequence mCallInfoLabel; - private boolean mIsHfpConnected; private UiBluetoothMonitor mUiBluetoothMonitor; private final Interpolator mAccelerateDecelerateInterpolator = new AccelerateDecelerateInterpolator(); private final Interpolator mAccelerateInterpolator = new AccelerateInterpolator(10); + // Should be set soon after construction. + void setUiBluetoothMonitor(UiBluetoothMonitor uiBluetoothMonitor) { + mUiBluetoothMonitor = uiBluetoothMonitor; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUiCallManager = UiCallManager.getInstance(getContext()); - mUiBluetoothMonitor = UiBluetoothMonitor.getInstance(); mHandler = new Handler(); } @@ -271,11 +273,6 @@ public class OngoingCallFragment extends Fragment { mUiCallManager.addListener(mCallListener); - // These must be called after the views are inflated because they have the side affect - // of updating the ui. - mUiBluetoothMonitor.addListener(mBluetoothListener); - mBluetoothListener.onStateChanged(); // Trigger state change to set initial state. - updateCalls(); updateRotaryFocus(); @@ -286,7 +283,6 @@ public class OngoingCallFragment extends Fragment { public void onDestroyView() { super.onDestroyView(); mUiCallManager.removeListener(mCallListener); - mUiBluetoothMonitor.removeListener(mBluetoothListener); } @Override @@ -410,7 +406,7 @@ public class OngoingCallFragment extends Fragment { setStateText(callInfoText); break; case Call.STATE_ACTIVE: - if (mIsHfpConnected) { + if (mUiBluetoothMonitor.isHfpConnected()) { mHandler.post(mUpdateDurationRunnable); } break; @@ -833,9 +829,4 @@ public class OngoingCallFragment extends Fragment { updateCalls(); } }; - - private final UiBluetoothMonitor.Listener mBluetoothListener = () -> { - OngoingCallFragment.this.mIsHfpConnected = - UiBluetoothMonitor.getInstance().isHfpConnected(); - }; } diff --git a/src/com/android/car/dialer/TelecomActivity.java b/src/com/android/car/dialer/TelecomActivity.java index bd6b9215..a23c0631 100644 --- a/src/com/android/car/dialer/TelecomActivity.java +++ b/src/com/android/car/dialer/TelecomActivity.java @@ -29,13 +29,10 @@ import android.telephony.PhoneNumberUtils; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; -import android.view.View; -import android.widget.EditText; import com.android.car.app.CarDrawerActivity; import com.android.car.app.CarDrawerAdapter; import com.android.car.app.DrawerItemViewHolder; -import com.android.car.dialer.bluetooth.UiBluetoothMonitor; import com.android.car.dialer.telecom.PhoneLoader; import com.android.car.dialer.telecom.UiCall; import com.android.car.dialer.telecom.UiCallManager; @@ -92,7 +89,7 @@ public class TelecomActivity extends CarDrawerActivity implements setTitle(getString(R.string.phone_app_name)); mUiCallManager = UiCallManager.getInstance(this); - mUiBluetoothMonitor = UiBluetoothMonitor.getInstance(); + mUiBluetoothMonitor = new UiBluetoothMonitor(this); if (savedInstanceState != null) { mCurrentFragmentName = savedInstanceState.getString(FRAGMENT_CLASS_KEY); @@ -127,6 +124,7 @@ public class TelecomActivity extends CarDrawerActivity implements if (vdebug()) { Log.d(TAG, "onDestroy"); } + mUiBluetoothMonitor.tearDown(); mUiCallManager = null; } @@ -307,7 +305,9 @@ public class TelecomActivity extends CarDrawerActivity implements } if (mOngoingCallFragment == null) { - mOngoingCallFragment = new OngoingCallFragment(); + OngoingCallFragment fragment = new OngoingCallFragment(); + fragment.setUiBluetoothMonitor(mUiBluetoothMonitor); + mOngoingCallFragment = fragment; } setContentFragmentWithFadeAnimation(mOngoingCallFragment); diff --git a/src/com/android/car/dialer/UiBluetoothMonitor.java b/src/com/android/car/dialer/UiBluetoothMonitor.java new file mode 100644 index 00000000..fdc3560c --- /dev/null +++ b/src/com/android/car/dialer/UiBluetoothMonitor.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2016 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.car.dialer; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothHeadsetClient; +import android.bluetooth.BluetoothProfile; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.util.Log; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * Class that responsible for getting status of bluetooth connections. + */ +public class UiBluetoothMonitor { + private static String TAG = "Em.BtMonitor"; + + private List<Listener> mListeners = new CopyOnWriteArrayList<>(); + private final Context mContext; + private final BluetoothAdapter mBluetoothAdapter; + private final BluetoothBroadcastReceiver mBluetoothBroadcastReceiver; + + UiBluetoothMonitor(Context context) { + mContext = context; + mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + mBluetoothBroadcastReceiver = new BluetoothBroadcastReceiver(); + } + + public void tearDown() { + mBluetoothBroadcastReceiver.tearDown(); + } + + public boolean isBluetoothEnabled() { + return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled(); + } + + public boolean isHfpConnected() { + if (mBluetoothAdapter == null) { + return false; + } + return mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET_CLIENT) + == BluetoothProfile.STATE_CONNECTED; + } + public boolean isBluetoothPaired() { + return mBluetoothAdapter != null && mBluetoothAdapter.getBondedDevices().size() > 0; + } + + public void addListener(Listener listener) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "addListener: " + listener); + } + mListeners.add(listener); + } + + protected void notifyListeners() { + for (Listener listener : mListeners) { + listener.onStateChanged(); + } + } + + public void removeListener(Listener listener) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "removeListener: " + listener); + } + mListeners.remove(listener); + } + + public interface Listener { + /** + * Calls when state of Bluetooth was changed, for example when Bluetooth was turned off or + * on, connection state was changed. + */ + void onStateChanged(); + } + + private final class BluetoothBroadcastReceiver extends BroadcastReceiver { + BluetoothBroadcastReceiver() { + IntentFilter filter = new IntentFilter(); + filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); + filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); + filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED); + mContext.registerReceiver(this, filter); + } + + @Override + public void onReceive(Context context, Intent intent) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Bluetooth broadcast intent action received: " + intent.getAction()); + } + notifyListeners(); + } + + void tearDown() { + mContext.unregisterReceiver(this); + } + } +} diff --git a/src/com/android/car/dialer/bluetooth/BluetoothBroadcastReceiver.java b/src/com/android/car/dialer/bluetooth/BluetoothBroadcastReceiver.java deleted file mode 100644 index 90abfdbe..00000000 --- a/src/com/android/car/dialer/bluetooth/BluetoothBroadcastReceiver.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2015 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.car.dialer.bluetooth; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -/** - * A broadcast receiver that listens for bluetooth state changes. - */ -public class BluetoothBroadcastReceiver extends BroadcastReceiver { - private static String TAG = "Em.BtBrdcstRcvr"; - - @Override - public void onReceive(Context context, Intent intent) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Bluetooth broadcast intent action received: " + intent.getAction()); - } - UiBluetoothMonitor.getInstance().notifyListeners(); - } -} diff --git a/src/com/android/car/dialer/bluetooth/UiBluetoothMonitor.java b/src/com/android/car/dialer/bluetooth/UiBluetoothMonitor.java deleted file mode 100644 index 52b101c8..00000000 --- a/src/com/android/car/dialer/bluetooth/UiBluetoothMonitor.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2016 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.car.dialer.bluetooth; - -import com.android.car.dialer.ClassFactory; - -import android.util.Log; - -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * Class that responsible for getting status of bluetooth connections. - */ -public abstract class UiBluetoothMonitor { - private List<Listener> mListeners = new CopyOnWriteArrayList<>(); - - private static String TAG = "Em.BtMonitor"; - - private static UiBluetoothMonitor sInstance; - private static Object sInstanceLock = new Object(); - - public abstract boolean isBluetoothEnabled(); - public abstract boolean isHfpConnected(); - public abstract boolean isBluetoothPaired(); - - public static UiBluetoothMonitor getInstance() { - if (sInstance == null) { - synchronized (sInstanceLock) { - if (sInstance == null) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Creating an instance of UiBluetoothMonitor"); - } - sInstance = ClassFactory.getFactory().createBluetoothMonitor(); - } - } - } - return sInstance; - } - - public void addListener(Listener listener) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "addListener: " + listener); - } - mListeners.add(listener); - } - - protected void notifyListeners() { - for (Listener listener : mListeners) { - listener.onStateChanged(); - } - } - - public void removeListener(Listener listener) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "removeListener: " + listener); - } - mListeners.remove(listener); - } - - public interface Listener { - /** - * Calls when state of Bluetooth was changed, for example when Bluetooth was turned off or - * on, connection state was changed. - */ - void onStateChanged(); - } -} diff --git a/src/com/android/car/dialer/bluetooth/embedded/UiBluetoothMonitorImpl.java b/src/com/android/car/dialer/bluetooth/embedded/UiBluetoothMonitorImpl.java deleted file mode 100644 index 2ab8bbf0..00000000 --- a/src/com/android/car/dialer/bluetooth/embedded/UiBluetoothMonitorImpl.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2016 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.car.dialer.bluetooth.embedded; - -import com.android.car.dialer.bluetooth.UiBluetoothMonitor; - -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothProfile; - -/** - * An implementation of {@link UiBluetoothMonitor} that uses {@link BluetoothAdapter}. - */ -public class UiBluetoothMonitorImpl extends UiBluetoothMonitor { - - @Override - public boolean isBluetoothEnabled() { - BluetoothAdapter adapter = getBluetoothAdapter(); - if (adapter == null) { - return false; - } - return adapter.isEnabled(); - } - - @Override - public boolean isHfpConnected() { - BluetoothAdapter adapter = getBluetoothAdapter(); - if (adapter == null) { - return false; - } - int hfpState = adapter.getProfileConnectionState(BluetoothProfile.HEADSET_CLIENT); - return hfpState == BluetoothProfile.STATE_CONNECTED; - } - - @Override - public boolean isBluetoothPaired() { - BluetoothAdapter adapter = getBluetoothAdapter(); - if (adapter == null) { - return false; - } - return !adapter.getBondedDevices().isEmpty(); - } - - private BluetoothAdapter getBluetoothAdapter() { - return BluetoothAdapter.getDefaultAdapter(); - } -} diff --git a/src/com/android/car/dialer/telecom/UiCallManager.java b/src/com/android/car/dialer/telecom/UiCallManager.java index fc8f9773..8b24f2a2 100644 --- a/src/com/android/car/dialer/telecom/UiCallManager.java +++ b/src/com/android/car/dialer/telecom/UiCallManager.java @@ -23,8 +23,9 @@ import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; -import com.android.car.dialer.ClassFactory; + import com.android.car.dialer.R; +import com.android.car.dialer.telecom.embedded.TelecomUiCallManager; import java.util.ArrayList; import java.util.Calendar; @@ -71,7 +72,7 @@ public abstract class UiCallManager { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Creating an instance of CarTelecomManager"); } - sInstance = ClassFactory.getFactory().createCarTelecomManager(); + sInstance = new TelecomUiCallManager(); sInstance.setUp(context.getApplicationContext()); } } |